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

Add anticheck for not detecting TLA in injected code #12

Merged
merged 7 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading