Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler: allow function calls on lazy state #835

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions integration-tests/execute/ava.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default {
extensions: {
ts: 'module',
},

environmentVariables: {
TS_NODE_TRANSPILE_ONLY: 'true',
},

nodeArguments: ['--loader=ts-node/esm', '--no-warnings', '--experimental-vm-modules'],

files: ['test/**/*test.ts'],
};
51 changes: 51 additions & 0 deletions integration-tests/execute/test/lazy-state.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import test from 'ava';
import execute from '../src/execute';

test.serial(
'use lazy-state in template literal & property access',
async (t) => {
const state = {
keyName: 'someKey',
firstName: 'John',
lastName: 'Doe',
};
const job = `
fnIf(\`$\{$.keyName\}\` === 'someKey', state => {
state.literal = true;
return state;
})

fnIf($.firstName + $['lastName'] === 'JohnDoe', state=> {
state.concat = true;
return state;
})`;

const result = await execute(job, state);
t.is(result.literal, true);
t.is(result.concat, true);
}
);

test.serial('state function called with lazy-state', async (t) => {
const state = { data: {} };

const job = `
fn((state) => {
state.callMeMaybe = (value) => {
state.data.greetings = "Hello " + value
return state;
}
return state
});

fn(state => {
state.data.name = "John"
return state;
})

fn($.callMeMaybe($.data.name))`;

const result = await execute(job, state);

t.deepEqual(result, { data: { name: 'John', greetings: 'Hello John' } });
});
10 changes: 9 additions & 1 deletion packages/compiler/src/transforms/lazy-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ const ensureParentArrow = (path: NodePath<n.MemberExpression>) => {
}

if (root && n.CallExpression.check(root.node)) {
const arg = last as NodePath;
let arg = last as NodePath;
if (
arg.parentPath &&
n.CallExpression.check(arg.parentPath.node) &&
n.MemberExpression.check(arg.parentPath.node.callee) &&
arg.parentPath.node.callee.object.name === 'state'
) {
arg = arg.parentPath;
}

if (!isOpenFunction(arg)) {
const params = b.identifier('state');
Expand Down
18 changes: 18 additions & 0 deletions packages/compiler/test/transforms/lazy-state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,21 @@ test('convert logical not', (t) => {

t.is(code, 'get(state => !state.data.x)');
});

test('convert function call on state', (t) => {
const ast = parse('fn($.callMeMaybe())');

const transformed = transform(ast, [visitors]);
const { code } = print(transformed);

t.is(code, 'fn(state => state.callMeMaybe())');
});

test('convert function call on state with state argument', (t) => {
const ast = parse('fn($.callMeMaybe($.age))');

const transformed = transform(ast, [visitors]);
const { code } = print(transformed);

t.is(code, 'fn(state => state.callMeMaybe(state.age))');
});