Skip to content

Commit

Permalink
Merge pull request #738 from OpenFn/support-method-operations
Browse files Browse the repository at this point in the history
compiler: support method operations
  • Loading branch information
josephjclark authored Jul 29, 2024
2 parents f52073a + c55c9aa commit 5c72cb6
Show file tree
Hide file tree
Showing 17 changed files with 118 additions and 19 deletions.
7 changes: 7 additions & 0 deletions integration-tests/execute/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/execute/package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"license": "ISC",
Expand Down
9 changes: 9 additions & 0 deletions integration-tests/worker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/worker/package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"license": "ISC",
Expand Down
8 changes: 8 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 6 additions & 0 deletions packages/compiler/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"license": "ISC",
Expand Down
5 changes: 3 additions & 2 deletions packages/compiler/src/transforms/top-level-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ function visitor(path: NodePath<namedTypes.CallExpression>) {
// 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);
Expand Down
15 changes: 15 additions & 0 deletions packages/compiler/test/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ 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 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()];';
Expand Down
53 changes: 43 additions & 10 deletions packages/compiler/test/transforms/top-level-operations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -119,17 +119,50 @@ 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));

t.is(call.callee.object.name, 'a');
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 declaration should be an array of 0
t.assert(n.ArrayExpression.check(ex.declaration));
t.assert(ex.declaration.elements.length == 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) => {
Expand Down
7 changes: 7 additions & 0 deletions packages/engine-multi/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# engine-multi

## 1.2.1

### Patch Changes

- Updated dependencies [4751c90]
- @openfn/compiler@0.3.0

## 1.2.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/engine-multi/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 6 additions & 0 deletions packages/lightning-mock/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @openfn/lightning-mock

## 2.0.15

### Patch Changes

- @openfn/engine-multi@1.2.1

## 2.0.14

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/lightning-mock/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
7 changes: 7 additions & 0 deletions packages/ws-worker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/ws-worker/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 5c72cb6

Please sign in to comment.