Skip to content

Commit

Permalink
improve nested checks
Browse files Browse the repository at this point in the history
  • Loading branch information
planttheidea committed Jun 5, 2019
1 parent 06fc4e4 commit c74f9f7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"no-param-reassign": "off",
"no-plusplus": "off",
"no-shadow": "off",
"no-underscore-dangle": "off",
"no-use-before-define": "off",

"import/order": "off"
Expand Down
33 changes: 14 additions & 19 deletions inline-loops.macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,10 @@ function inlineLoops({ references, babel }) {
return allMethods.push(...incrementingCalls, ...decrementingCalls, ...objectCalls);
});

allMethods.forEach((method) => {
method.node.__inlineLoopsMacro = true;
});

const handlers = {
every: handleEvery,
filter: handleFilter,
Expand All @@ -971,33 +975,24 @@ function inlineLoops({ references, babel }) {
const { callee } = path.parentPath.parent;

if (callee) {
let ancestorPath = path.parentPath;

while (ancestorPath) {
if (ancestorPath.node && ancestorPath.node.body) {
break;
}

const ancestry = path.parentPath.getAncestry();

for (let index = 0; index < ancestry.length; index++) {
const ancestorPath = ancestry[index];

if (t.isCallExpression(ancestorPath)) {
const { expression } = ancestorPath.parent;
const caller = expression
? expression.callee
: ancestorPath.parent.callee;

if (
allMethods.find(
({ node }) => node === caller && node !== path.node,
)
) {
const callee = ancestorPath.node
? ancestorPath.node.callee
: ancestorPath.parent.expression.callee;

if (callee !== path.node && callee.__inlineLoopsMacro) {
throw new MacroError(
`You cannot nest looper methods. You should store the results of ${name} to a variable, and then call ${
path.parentPath.parent.callee.name
} with it.`,
);
}
}

ancestorPath = ancestorPath.parentPath;
}
}

Expand Down

0 comments on commit c74f9f7

Please sign in to comment.