Skip to content

Commit 3d47c8b

Browse files
authored
Merge pull request #1064 from OpenFn/ignore-rules
feat: ignore top objects in several transforms
2 parents cb97744 + 4c6c86d commit 3d47c8b

3 files changed

Lines changed: 30 additions & 10 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { namedTypes as n } from 'ast-types';
2+
import { NodePath } from 'ast-types/lib/node-path';
3+
4+
const IgnoreRules = (path: NodePath<n.Expression>) => {
5+
return {
6+
check: n.ObjectExpression.check(path.node),
7+
shouldSkip: () => {
8+
if (
9+
n.VariableDeclarator.check(path.parentPath.node) &&
10+
n.VariableDeclaration.check(path.parentPath.parentPath.node) &&
11+
n.Program.check(path.parentPath.parentPath.parentPath.parentPath.node)
12+
) {
13+
return true;
14+
}
15+
return false;
16+
},
17+
};
18+
};
19+
20+
export default IgnoreRules;

packages/compiler/src/transforms/lazy-state.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import { builders as b, namedTypes as n } from 'ast-types';
1010
import type { NodePath } from 'ast-types/lib/node-path';
1111
import type { Transformer } from '../transform';
12+
import IgnoreRules from '../transform-ignore';
1213

1314
// Walk up the AST and work out where the parent arrow function should go
1415
const ensureParentArrow = (path: NodePath<n.MemberExpression>) => {
@@ -80,15 +81,9 @@ const isOpenFunction = (path: NodePath) => {
8081

8182
function visitor(path: NodePath<n.MemberExpression>) {
8283
// if it was called for an ObjectExpression
83-
if (n.ObjectExpression.check(path.node)) {
84-
if (
85-
n.VariableDeclarator.check(path.parentPath.node) &&
86-
n.VariableDeclaration.check(path.parentPath.parentPath.node) &&
87-
n.Program.check(path.parentPath.parentPath.parentPath.parentPath.node)
88-
) {
89-
return true;
90-
}
91-
return false;
84+
const ignoreRule = IgnoreRules(path);
85+
if (ignoreRule.check) {
86+
return ignoreRule.shouldSkip();
9287
}
9388
let first = path.node.object;
9489
while (first.hasOwnProperty('object')) {

packages/compiler/src/transforms/promises.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { namedTypes as n, builders as b } from 'ast-types';
22

33
import type { NodePath } from 'ast-types/lib/node-path';
4+
import IgnoreRules from '../transform-ignore';
45

56
const NO_DEFER_DECLARATION_ERROR = 'No defer declaration found';
67

@@ -146,6 +147,10 @@ const isTopScope = (path: NodePath<any>) => {
146147
};
147148

148149
const visitor = (path: NodePath<n.CallExpression>) => {
150+
const ignoreRule = IgnoreRules(path);
151+
if (ignoreRule.check) {
152+
return ignoreRule.shouldSkip();
153+
}
149154
let root: NodePath<any> = path;
150155
while (!n.Program.check(root.node)) {
151156
root = root.parent;
@@ -166,7 +171,7 @@ const visitor = (path: NodePath<n.CallExpression>) => {
166171

167172
export default {
168173
id: 'promises',
169-
types: ['CallExpression'],
174+
types: ['CallExpression', 'ObjectExpression'],
170175
visitor,
171176
// this should run before top-level operations are moved into the exports array
172177
order: 0,

0 commit comments

Comments
 (0)