From e1f47842e57be7af67f57a027cf84704b46d98a0 Mon Sep 17 00:00:00 2001 From: Joe Clark Date: Thu, 25 Jul 2024 16:55:56 +0100 Subject: [PATCH 1/6] compiler: allow method calls eg http.get to be Operations --- .../src/transforms/top-level-operations.ts | 5 ++-- packages/compiler/test/compile.test.ts | 7 +++++ .../transforms/top-level-operations.test.ts | 27 ++++++++++++------- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/packages/compiler/src/transforms/top-level-operations.ts b/packages/compiler/src/transforms/top-level-operations.ts index b6a68282e..50d7980a9 100644 --- a/packages/compiler/src/transforms/top-level-operations.ts +++ b/packages/compiler/src/transforms/top-level-operations.ts @@ -21,8 +21,9 @@ function visitor(path: NodePath) { // ie, the parent must be an ExpressionStatement, and the statement's parent must be a Program n.Program.check(root) && n.Statement.check(path.parent.node) && - // If it's an Operation call (ie, fn(() => {})), the callee will be an IdentifierExpression - n.Identifier.check(path.node.callee) + // An Operation could be an Identifier (fn()) or Member Expression (http.get()) + (n.Identifier.check(path.node.callee) || + n.MemberExpression.check(path.node.callee)) ) { // Now Find the top level exports array const target = root.body.at(-1); diff --git a/packages/compiler/test/compile.test.ts b/packages/compiler/test/compile.test.ts index 376902f64..956f5037e 100644 --- a/packages/compiler/test/compile.test.ts +++ b/packages/compiler/test/compile.test.ts @@ -24,6 +24,13 @@ test('compile a single operation', (t) => { t.is(result, expected); }); +test('compile a single namespaced operation', (t) => { + const source = 'http.get();'; + const expected = 'export default [http.get()];'; + const result = compile(source); + t.is(result, expected); +}); + test('compile a single operation without being fussy about semicolons', (t) => { const source = 'fn()'; const expected = 'export default [fn()];'; diff --git a/packages/compiler/test/transforms/top-level-operations.test.ts b/packages/compiler/test/transforms/top-level-operations.test.ts index 75cdf58c9..0f0b3fb58 100644 --- a/packages/compiler/test/transforms/top-level-operations.test.ts +++ b/packages/compiler/test/transforms/top-level-operations.test.ts @@ -108,7 +108,7 @@ test('does not move a nested operation into the exports array', (t) => { t.assert(call.callee.name === 'fn'); }); -test('does not move method call into the exports array', (t) => { +test('moves a method call into the exports array', (t) => { const ast = createProgramWithExports([ b.expressionStatement( b.callExpression( @@ -119,17 +119,24 @@ test('does not move method call into the exports array', (t) => { ]); const { body } = transform(ast, [visitors]); - // should be two top level children - t.assert(body.length === 2); - // Those children should still be an expression and export statement - const [stmt, ex] = body; - t.assert(n.ExpressionStatement.check(stmt)); - t.assert(n.ExportDefaultDeclaration.check(ex)); + // should only be ony top level child + t.assert(body.length === 1); + + // That child should be a default declaration + t.assert(n.ExportDefaultDeclaration.check(body[0])); + + // The declaration should be an array of 1 + t.assert(n.ArrayExpression.check(body[0].declaration)); + t.assert(body[0].declaration.elements.length == 1); + + // And the one element should be a call to http.get + const call = body[0].declaration.elements[0]; + t.assert(n.CallExpression.check(call)); + t.assert(n.MemberExpression.check(call.callee)); - // The declaration should be an array of 0 - t.assert(n.ArrayExpression.check(ex.declaration)); - t.assert(ex.declaration.elements.length == 0); + t.is(call.callee.object.name, 'a'); + t.is(call.callee.property.name, 'b'); }); test("does nothing if there's no export statement", (t) => { From 4751c90b7aa42caad8811c14c9e51b0520f6e171 Mon Sep 17 00:00:00 2001 From: Joe Clark Date: Thu, 25 Jul 2024 16:56:41 +0100 Subject: [PATCH 2/6] changeset --- .changeset/wise-deers-speak.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/wise-deers-speak.md diff --git a/.changeset/wise-deers-speak.md b/.changeset/wise-deers-speak.md new file mode 100644 index 000000000..60f057aee --- /dev/null +++ b/.changeset/wise-deers-speak.md @@ -0,0 +1,5 @@ +--- +'@openfn/compiler': minor +--- + +Treat method calls like http.get like operations From 2f5dc51108007dbb29692f569d5515073892a9d4 Mon Sep 17 00:00:00 2001 From: Joe Clark Date: Thu, 25 Jul 2024 17:35:58 +0100 Subject: [PATCH 3/6] changeset --- .changeset/cool-windows-hide.md | 6 ++++++ packages/compiler/src/transforms/add-imports.ts | 3 +++ packages/compiler/src/util.ts | 1 + 3 files changed, 10 insertions(+) create mode 100644 .changeset/cool-windows-hide.md diff --git a/.changeset/cool-windows-hide.md b/.changeset/cool-windows-hide.md new file mode 100644 index 000000000..515131e55 --- /dev/null +++ b/.changeset/cool-windows-hide.md @@ -0,0 +1,6 @@ +--- +'@openfn/cli': patch +'@openfn/ws-worker': patch +--- + +Update the compiler to treat method calls (http.get()) like operations diff --git a/packages/compiler/src/transforms/add-imports.ts b/packages/compiler/src/transforms/add-imports.ts index d6c9ae36c..16dd3d45e 100644 --- a/packages/compiler/src/transforms/add-imports.ts +++ b/packages/compiler/src/transforms/add-imports.ts @@ -108,6 +108,7 @@ export function findAllDanglingIdentifiers(ast: ASTNode) { const result: IdentifierList = {}; visit(ast, { visitIdentifier: function (path) { + console.log(path.node.name); // If this is part of an import statement, do nothing if (n.ImportSpecifier.check(path.parent.node)) { return false; @@ -167,6 +168,8 @@ function visitor(path: NodePath, logger: Logger, options: AddImportsOptions) { if (name) { const identifiers = findAllDanglingIdentifiers(path.node); + console.log(identifiers); + console.log(exports); const usedExports = exports && exports.length ? // If we have exports for this adaptor, import any dangling variables from the export list diff --git a/packages/compiler/src/util.ts b/packages/compiler/src/util.ts index 0f70df991..5ce700f32 100644 --- a/packages/compiler/src/util.ts +++ b/packages/compiler/src/util.ts @@ -87,6 +87,7 @@ const findExports = async ( const dtsFiles = files.filter((f) => f.endsWith('.d.ts')); const result = []; for (const f of dtsFiles) { + console.log(f); const relPath = `${typesRoot}/${f}`; const contents = await readFile(`${moduleRoot}/${relPath}`, 'utf8'); project.createFile(contents, relPath); From 3fd0e03dfcc9e6ef22faee5461f6f74b3f546904 Mon Sep 17 00:00:00 2001 From: Joe Clark Date: Thu, 25 Jul 2024 17:36:38 +0100 Subject: [PATCH 4/6] versions: cli@1.7.1 worker@1.4.1 --- .changeset/cool-windows-hide.md | 6 ------ .changeset/wise-deers-speak.md | 5 ----- integration-tests/execute/CHANGELOG.md | 7 +++++++ integration-tests/execute/package.json | 2 +- integration-tests/worker/CHANGELOG.md | 9 +++++++++ integration-tests/worker/package.json | 2 +- packages/cli/CHANGELOG.md | 8 ++++++++ packages/cli/package.json | 2 +- packages/compiler/CHANGELOG.md | 6 ++++++ packages/compiler/package.json | 2 +- packages/engine-multi/CHANGELOG.md | 7 +++++++ packages/engine-multi/package.json | 2 +- packages/lightning-mock/CHANGELOG.md | 6 ++++++ packages/lightning-mock/package.json | 2 +- packages/ws-worker/CHANGELOG.md | 7 +++++++ packages/ws-worker/package.json | 2 +- 16 files changed, 57 insertions(+), 18 deletions(-) delete mode 100644 .changeset/cool-windows-hide.md delete mode 100644 .changeset/wise-deers-speak.md diff --git a/.changeset/cool-windows-hide.md b/.changeset/cool-windows-hide.md deleted file mode 100644 index 515131e55..000000000 --- a/.changeset/cool-windows-hide.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -'@openfn/cli': patch -'@openfn/ws-worker': patch ---- - -Update the compiler to treat method calls (http.get()) like operations diff --git a/.changeset/wise-deers-speak.md b/.changeset/wise-deers-speak.md deleted file mode 100644 index 60f057aee..000000000 --- a/.changeset/wise-deers-speak.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@openfn/compiler': minor ---- - -Treat method calls like http.get like operations diff --git a/integration-tests/execute/CHANGELOG.md b/integration-tests/execute/CHANGELOG.md index 25a096473..a7d3f14d0 100644 --- a/integration-tests/execute/CHANGELOG.md +++ b/integration-tests/execute/CHANGELOG.md @@ -1,5 +1,12 @@ # @openfn/integration-tests-execute +## 1.0.2 + +### Patch Changes + +- Updated dependencies [4751c90] + - @openfn/compiler@0.3.0 + ## 1.0.1 ### Patch Changes diff --git a/integration-tests/execute/package.json b/integration-tests/execute/package.json index ed4bf550a..da148d707 100644 --- a/integration-tests/execute/package.json +++ b/integration-tests/execute/package.json @@ -1,7 +1,7 @@ { "name": "@openfn/integration-tests-execute", "private": true, - "version": "1.0.1", + "version": "1.0.2", "description": "Job execution tests", "author": "Open Function Group ", "license": "ISC", diff --git a/integration-tests/worker/CHANGELOG.md b/integration-tests/worker/CHANGELOG.md index bcfc5a864..ef7e3d44e 100644 --- a/integration-tests/worker/CHANGELOG.md +++ b/integration-tests/worker/CHANGELOG.md @@ -1,5 +1,14 @@ # @openfn/integration-tests-worker +## 1.0.52 + +### Patch Changes + +- Updated dependencies [2f5dc51] + - @openfn/ws-worker@1.4.1 + - @openfn/engine-multi@1.2.1 + - @openfn/lightning-mock@2.0.15 + ## 1.0.51 ### Patch Changes diff --git a/integration-tests/worker/package.json b/integration-tests/worker/package.json index 78e871557..e0ab1c019 100644 --- a/integration-tests/worker/package.json +++ b/integration-tests/worker/package.json @@ -1,7 +1,7 @@ { "name": "@openfn/integration-tests-worker", "private": true, - "version": "1.0.51", + "version": "1.0.52", "description": "Lightning WOrker integration tests", "author": "Open Function Group ", "license": "ISC", diff --git a/packages/cli/CHANGELOG.md b/packages/cli/CHANGELOG.md index 5f725a5fc..a50bb8c03 100644 --- a/packages/cli/CHANGELOG.md +++ b/packages/cli/CHANGELOG.md @@ -1,5 +1,13 @@ # @openfn/cli +## 1.7.1 + +### Patch Changes + +- 2f5dc51: Update the compiler to treat method calls (http.get()) like operations +- Updated dependencies [4751c90] + - @openfn/compiler@0.3.0 + ## 1.7.0 ### Minor Changes diff --git a/packages/cli/package.json b/packages/cli/package.json index 748b95268..a160d256e 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -1,6 +1,6 @@ { "name": "@openfn/cli", - "version": "1.7.0", + "version": "1.7.1", "description": "CLI devtools for the openfn toolchain.", "engines": { "node": ">=18", diff --git a/packages/compiler/CHANGELOG.md b/packages/compiler/CHANGELOG.md index fbe2f4d99..78e08e40a 100644 --- a/packages/compiler/CHANGELOG.md +++ b/packages/compiler/CHANGELOG.md @@ -1,5 +1,11 @@ # @openfn/compiler +## 0.3.0 + +### Minor Changes + +- 4751c90: Treat method calls like http.get like operations + ## 0.2.0 ### Minor Changes diff --git a/packages/compiler/package.json b/packages/compiler/package.json index 9bca4763f..a0229ce97 100644 --- a/packages/compiler/package.json +++ b/packages/compiler/package.json @@ -1,6 +1,6 @@ { "name": "@openfn/compiler", - "version": "0.2.0", + "version": "0.3.0", "description": "Compiler and language tooling for openfn jobs.", "author": "Open Function Group ", "license": "ISC", diff --git a/packages/engine-multi/CHANGELOG.md b/packages/engine-multi/CHANGELOG.md index 98fd0c5b3..394e3f69f 100644 --- a/packages/engine-multi/CHANGELOG.md +++ b/packages/engine-multi/CHANGELOG.md @@ -1,5 +1,12 @@ # engine-multi +## 1.2.1 + +### Patch Changes + +- Updated dependencies [4751c90] + - @openfn/compiler@0.3.0 + ## 1.2.0 ### Minor Changes diff --git a/packages/engine-multi/package.json b/packages/engine-multi/package.json index d4b6045ff..6755ad6f4 100644 --- a/packages/engine-multi/package.json +++ b/packages/engine-multi/package.json @@ -1,6 +1,6 @@ { "name": "@openfn/engine-multi", - "version": "1.2.0", + "version": "1.2.1", "description": "Multi-process runtime engine", "main": "dist/index.js", "type": "module", diff --git a/packages/lightning-mock/CHANGELOG.md b/packages/lightning-mock/CHANGELOG.md index 4d97a24f4..71d217307 100644 --- a/packages/lightning-mock/CHANGELOG.md +++ b/packages/lightning-mock/CHANGELOG.md @@ -1,5 +1,11 @@ # @openfn/lightning-mock +## 2.0.15 + +### Patch Changes + +- @openfn/engine-multi@1.2.1 + ## 2.0.14 ### Patch Changes diff --git a/packages/lightning-mock/package.json b/packages/lightning-mock/package.json index 2de385a0e..ec5564fac 100644 --- a/packages/lightning-mock/package.json +++ b/packages/lightning-mock/package.json @@ -1,6 +1,6 @@ { "name": "@openfn/lightning-mock", - "version": "2.0.14", + "version": "2.0.15", "private": true, "description": "A mock Lightning server", "main": "dist/index.js", diff --git a/packages/ws-worker/CHANGELOG.md b/packages/ws-worker/CHANGELOG.md index b660de64a..117e0a1ad 100644 --- a/packages/ws-worker/CHANGELOG.md +++ b/packages/ws-worker/CHANGELOG.md @@ -1,5 +1,12 @@ # ws-worker +## 1.4.1 + +### Patch Changes + +- 2f5dc51: Update the compiler to treat method calls (http.get()) like operations + - @openfn/engine-multi@1.2.1 + ## 1.4.0 ### Minor Changes diff --git a/packages/ws-worker/package.json b/packages/ws-worker/package.json index 33135a3b7..45bcd17f3 100644 --- a/packages/ws-worker/package.json +++ b/packages/ws-worker/package.json @@ -1,6 +1,6 @@ { "name": "@openfn/ws-worker", - "version": "1.4.0", + "version": "1.4.1", "description": "A Websocket Worker to connect Lightning to a Runtime Engine", "main": "dist/index.js", "type": "module", From 5a7d21f4d69b28e80cf2527b52137efdaa923076 Mon Sep 17 00:00:00 2001 From: Joe Clark Date: Fri, 26 Jul 2024 14:12:33 +0100 Subject: [PATCH 5/6] compiler: remove logging --- packages/compiler/src/transforms/add-imports.ts | 3 --- packages/compiler/src/util.ts | 1 - 2 files changed, 4 deletions(-) diff --git a/packages/compiler/src/transforms/add-imports.ts b/packages/compiler/src/transforms/add-imports.ts index 16dd3d45e..d6c9ae36c 100644 --- a/packages/compiler/src/transforms/add-imports.ts +++ b/packages/compiler/src/transforms/add-imports.ts @@ -108,7 +108,6 @@ export function findAllDanglingIdentifiers(ast: ASTNode) { const result: IdentifierList = {}; visit(ast, { visitIdentifier: function (path) { - console.log(path.node.name); // If this is part of an import statement, do nothing if (n.ImportSpecifier.check(path.parent.node)) { return false; @@ -168,8 +167,6 @@ function visitor(path: NodePath, logger: Logger, options: AddImportsOptions) { if (name) { const identifiers = findAllDanglingIdentifiers(path.node); - console.log(identifiers); - console.log(exports); const usedExports = exports && exports.length ? // If we have exports for this adaptor, import any dangling variables from the export list diff --git a/packages/compiler/src/util.ts b/packages/compiler/src/util.ts index 5ce700f32..0f70df991 100644 --- a/packages/compiler/src/util.ts +++ b/packages/compiler/src/util.ts @@ -87,7 +87,6 @@ const findExports = async ( const dtsFiles = files.filter((f) => f.endsWith('.d.ts')); const result = []; for (const f of dtsFiles) { - console.log(f); const relPath = `${typesRoot}/${f}`; const contents = await readFile(`${moduleRoot}/${relPath}`, 'utf8'); project.createFile(contents, relPath); From c55c9aa2fb17144d73c454bddc5ec635e03824d9 Mon Sep 17 00:00:00 2001 From: Joe Clark Date: Fri, 26 Jul 2024 14:29:25 +0100 Subject: [PATCH 6/6] compiler: extra tests --- packages/compiler/test/compile.test.ts | 8 ++++++ .../transforms/top-level-operations.test.ts | 26 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/packages/compiler/test/compile.test.ts b/packages/compiler/test/compile.test.ts index 956f5037e..9eca1efea 100644 --- a/packages/compiler/test/compile.test.ts +++ b/packages/compiler/test/compile.test.ts @@ -31,6 +31,14 @@ test('compile a single namespaced operation', (t) => { t.is(result, expected); }); +test('compile a const assignment with single method call', (t) => { + const source = 'const x = dateFns.parse()'; + const expected = `const x = dateFns.parse() +export default [];`; + const result = compile(source); + t.is(result, expected); +}); + test('compile a single operation without being fussy about semicolons', (t) => { const source = 'fn()'; const expected = 'export default [fn()];'; diff --git a/packages/compiler/test/transforms/top-level-operations.test.ts b/packages/compiler/test/transforms/top-level-operations.test.ts index 0f0b3fb58..720c59791 100644 --- a/packages/compiler/test/transforms/top-level-operations.test.ts +++ b/packages/compiler/test/transforms/top-level-operations.test.ts @@ -139,6 +139,32 @@ test('moves a method call into the exports array', (t) => { t.is(call.callee.property.name, 'b'); }); +test('does not move a method call inside an asisignment', (t) => { + const ast = createProgramWithExports([ + b.variableDeclaration('const', [ + b.variableDeclarator( + b.identifier('x'), + b.callExpression( + b.memberExpression(b.identifier('a'), b.identifier('b')), + [] + ) + ), + ]), + ]); + + const { body } = transform(ast, [visitors]); + console.log(body); + + // should add the export + t.is(body.length, 2); + + // The first child should be an variable declaration + t.true(n.VariableDeclaration.check(body[0])); + + // the exported array should be empty + t.is(body[1].declaration.elements.length, 0); +}); + test("does nothing if there's no export statement", (t) => { const ast = b.program([createOperationStatement('fn')]);