diff --git a/lib/import-export-visitor.js b/lib/import-export-visitor.js index 04589bc..95272b1 100644 --- a/lib/import-export-visitor.js +++ b/lib/import-export-visitor.js @@ -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); } diff --git a/test/babel-plugin-tests.js b/test/babel-plugin-tests.js index 61a68e0..b1f01b7 100644 --- a/test/babel-plugin-tests.js +++ b/test/babel-plugin-tests.js @@ -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; diff --git a/test/run.sh b/test/run.sh index 31f4f8f..ac235b0 100755 --- a/test/run.sh +++ b/test/run.sh @@ -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') @@ -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