@openfn/compiler
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -36,4 +36,2 @@ // src/compile.ts | ||
var globals = [ | ||
"\\$", | ||
// TMP hack to fix a problem with lazy-state (needs double escaping to work) | ||
"AggregateError", | ||
@@ -216,3 +214,38 @@ "Array", | ||
// src/transforms/lazy-state.ts | ||
import { builders as b3 } from "ast-types"; | ||
import { builders as b3, namedTypes as n2 } from "ast-types"; | ||
var ensureParentArrow = (path2) => { | ||
let root = path2; | ||
let last; | ||
while (root && !n2.CallExpression.check(root.node)) { | ||
last = root; | ||
root = root.parent; | ||
if (n2.Statement.check(root.node) || n2.Declaration.check(root.node)) { | ||
throw new Error(`invalid state operator: must be inside an expression`); | ||
} | ||
} | ||
if (root && n2.CallExpression.check(root.node)) { | ||
const arg = last; | ||
if (!isOpenFunction(arg)) { | ||
const params = b3.identifier("state"); | ||
const arrow = b3.arrowFunctionExpression([params], arg.node); | ||
arg.replace(arrow); | ||
} | ||
} else { | ||
throw new Error(`invalid state operator: must be be passed as an argument to an operator`); | ||
} | ||
}; | ||
var isOpenFunction = (path2) => { | ||
if (n2.ArrowFunctionExpression.check(path2.node)) { | ||
const arrow = path2.node; | ||
if (arrow.params.length == 1) { | ||
const name = arrow.params[0].name; | ||
if (name === "state") { | ||
return true; | ||
} | ||
throw new Error(`invalid state operator: parameter "${name}" should be called "state"`); | ||
} | ||
throw new Error("invalid state operator: parent has wrong arity"); | ||
} | ||
return false; | ||
}; | ||
function visitor3(path2) { | ||
@@ -233,8 +266,3 @@ let first = path2.node.object; | ||
firstIdentifer.name = "state"; | ||
const params = b3.identifier("state"); | ||
const arrow = b3.arrowFunctionExpression( | ||
[params], | ||
path2.node | ||
); | ||
path2.replace(arrow); | ||
ensureParentArrow(path2); | ||
} | ||
@@ -246,7 +274,9 @@ return false; | ||
types: ["MemberExpression"], | ||
visitor: visitor3 | ||
visitor: visitor3, | ||
// It's important that $ symbols are escaped before any other transformations can run | ||
order: 0 | ||
}; | ||
// src/transforms/top-level-operations.ts | ||
import { namedTypes as n2 } from "ast-types"; | ||
import { namedTypes as n3 } from "ast-types"; | ||
function visitor4(path2) { | ||
@@ -257,7 +287,7 @@ const root = path2.parent.parent.node; | ||
// ie, the parent must be an ExpressionStatement, and the statement's parent must be a Program | ||
n2.Program.check(root) && n2.Statement.check(path2.parent.node) && // If it's an Operation call (ie, fn(() => {})), the callee will be an IdentifierExpression | ||
n2.Identifier.check(path2.node.callee) | ||
n3.Program.check(root) && n3.Statement.check(path2.parent.node) && // If it's an Operation call (ie, fn(() => {})), the callee will be an IdentifierExpression | ||
n3.Identifier.check(path2.node.callee) | ||
) { | ||
const target = root.body.at(-1); | ||
if (n2.ExportDefaultDeclaration.check(target) && n2.ArrayExpression.check(target.declaration)) { | ||
if (n3.ExportDefaultDeclaration.check(target) && n3.ArrayExpression.check(target.declaration)) { | ||
const statement = path2.parent; | ||
@@ -280,33 +310,21 @@ target.declaration.elements.push(path2.node); | ||
if (!transformers) { | ||
transformers = [lazy_state_default, ensure_exports_default, top_level_operations_default, add_imports_default]; | ||
transformers = [ | ||
lazy_state_default, | ||
ensure_exports_default, | ||
top_level_operations_default, | ||
add_imports_default | ||
]; | ||
} | ||
const logger = options.logger || defaultLogger; | ||
const transformerIndex = indexTransformers(transformers, options); | ||
const v = buildVisitors(transformerIndex, logger, options); | ||
visit2(ast, v); | ||
return ast; | ||
} | ||
function indexTransformers(transformers, options = {}) { | ||
const index = {}; | ||
for (const t of transformers) { | ||
const { types, id } = t; | ||
if (options[id] !== false) { | ||
for (const type of types) { | ||
const name = `visit${type}`; | ||
if (!index[name]) { | ||
index[name] = []; | ||
} | ||
index[name].push(t); | ||
} | ||
} | ||
} | ||
return index; | ||
} | ||
function buildVisitors(transformerIndex, logger, options = {}) { | ||
const visitors = {}; | ||
for (const k in transformerIndex) { | ||
const astType = k; | ||
visitors[astType] = function(path2) { | ||
const transformers = transformerIndex[astType]; | ||
for (const { id, visitor: visitor5 } of transformers) { | ||
transformers.filter(({ id }) => options[id] ?? true).map((t) => ({ ...t, order: t.order ?? 1 })).sort((a, b4) => { | ||
if (a.order > b4.order) | ||
return 1; | ||
if (a.order < b4.order) | ||
return -1; | ||
return 0; | ||
}).forEach(({ id, types, visitor: visitor5 }) => { | ||
const astTypes = {}; | ||
for (const type of types) { | ||
const name = `visit${type}`; | ||
astTypes[name] = function(path2) { | ||
const opts = options[id] || {}; | ||
@@ -317,7 +335,8 @@ const abort = visitor5(path2, logger, opts); | ||
} | ||
} | ||
this.traverse(path2); | ||
}; | ||
} | ||
return visitors; | ||
this.traverse(path2); | ||
}; | ||
} | ||
visit2(ast, astTypes); | ||
}); | ||
return ast; | ||
} | ||
@@ -324,0 +343,0 @@ |
{ | ||
"name": "@openfn/compiler", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Compiler and language tooling for openfn jobs.", | ||
@@ -37,3 +37,3 @@ "author": "Open Function Group <admin@openfn.org>", | ||
"recast": "^0.21.5", | ||
"@openfn/describe-package": "0.0.18", | ||
"@openfn/describe-package": "0.0.19", | ||
"@openfn/logger": "1.0.1" | ||
@@ -40,0 +40,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15845
452
+ Added@openfn/describe-package@0.0.19(transitive)
+ Addedtypescript@5.6.3(transitive)
- Removed@openfn/describe-package@0.0.18(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedimurmurhash@0.1.4(transitive)
- Removednode-localstorage@2.2.1(transitive)
- Removedslide@1.1.6(transitive)
- Removedtypescript@4.9.5(transitive)
- Removedwrite-file-atomic@1.3.4(transitive)