Skip to content

Commit

Permalink
Merge pull request #12 from perbergland/main
Browse files Browse the repository at this point in the history
Add anticheck for not detecting TLA in injected code
  • Loading branch information
leonardoventurini authored Oct 23, 2024
2 parents 8f49ab0 + a4e689c commit f59788d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
9 changes: 8 additions & 1 deletion lib/import-export-visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,19 @@ class ImportExportVisitor extends Visitor {
visitAwaitExpression(path) {
if (!this.hasTopLevelAwait) {
const parent = path.getParentNode(1);

const node = path.getNode();
if (
parent.type === 'ExpressionStatement' &&
path.getParentNode(2).type === 'Program'
) {
this.hasTopLevelAwait = true;
} else if (node.argument.type === "CallExpression" &&
node.argument.callee.type === "Identifier" &&
node.argument.callee.name === "__reifyWaitForDeps__"
) {
// Do not treat the emitted "await __reifyWaitForDeps__()" as a TLA trigger
// This happens e.g. for the package refapp:meteor-typescript which subclasses
// BabelCompiler
} else {
this.hasTopLevelAwait = isTopLevelAwait(path.stack);
}
Expand Down
40 changes: 40 additions & 0 deletions test/babel-plugin-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,46 @@ describe("reify/plugins/babel", () => {
);
});


(topLevelAwaitEnabled ? describe : describe.skip)("top level await via babel plugin", ()=>{
it('doesn’t detect TLA for simple content', () => {
// Depends on the special __reifyWaitForDeps__ detection in visitAwaitExpression
const code = `
import { Meteor } from "meteor/meteor";
export const isServer = Meteor.isServer;
`;
const ast = parse(code);
delete ast.tokens;
const result = transformFromAst(ast, code, {
plugins: [[reifyPlugin, {
dynamicImport: true
}]]
});
assert.match(
result.code,
/async: false\n/
);
});

it('detects TLA for simple content', () => {
const code = `
import { Meteor } from "meteor/meteor";
export const isServer = await Meteor.isServer;
`;
const ast = parse(code);
delete ast.tokens;
const result = transformFromAst(ast, code, {
plugins: [[reifyPlugin, {
dynamicImport: true
}]]
});
assert.match(
result.code,
/async: true\n/
);
});
});

function check(code, options) {
const ast = parse(code);
delete ast.tokens;
Expand Down
9 changes: 6 additions & 3 deletions test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ TEST_DIR=$(pwd)
export NODE_PENDING_DEPRECATION=1
export NODE_OPTIONS="--trace-warnings"

cd "$TEST_DIR"
MOCHA_GREP=${MOCHA_GREP:-""}

cd "$TEST_DIR"
parsers=("babel" "acorn")
tlaModes=('false' 'true')

Expand All @@ -21,17 +22,19 @@ for parser in ${parsers[@]}; do
export REIFY_PARSER="$parser"
export REIFY_TLA="$tla"

mocha \
npx mocha \
--require "../node" \
--reporter spec \
--full-trace \
--grep "$MOCHA_GREP" \
run.js
done
done

# Run tests again using test/.cache.
mocha \
npx mocha \
--require "../node" \
--reporter spec \
--full-trace \
--grep "$MOCHA_GREP" \
run.js

0 comments on commit f59788d

Please sign in to comment.