From 4ab165b70a76eecce94249fbcbd0017239a14002 Mon Sep 17 00:00:00 2001 From: Leonardo Venturini Date: Mon, 27 May 2024 08:46:23 -0400 Subject: [PATCH] fix declaration detection --- lib/import-export-visitor.js | 11 ++++++++++- node/version.js | 2 +- package.json | 3 +++ test/tla/exported-declaration.js | 5 +++++ test/tla/non-exported-declaration.js | 7 +++++++ test/top-level-await-tests.js | 12 +++++++++++- 6 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 test/tla/exported-declaration.js create mode 100644 test/tla/non-exported-declaration.js diff --git a/lib/import-export-visitor.js b/lib/import-export-visitor.js index 8e2a2e98..6622ced9 100644 --- a/lib/import-export-visitor.js +++ b/lib/import-export-visitor.js @@ -388,7 +388,16 @@ class ImportExportVisitor extends Visitor { visitAwaitExpression(path) { if (!this.hasTopLevelAwait) { - let parent = path.getParentNode(1); + const parent = path.getParentNode(1); + + if ( + parent.type === 'VariableDeclarator' && + (path.getParentNode(3).type === 'Program' || + path.getParentNode(4).type === 'Program') + ) { + this.hasTopLevelAwait = true; + } + if ( parent.type === 'ExpressionStatement' && path.getParentNode(2).type === 'Program' diff --git a/node/version.js b/node/version.js index 255187cd..f3dd3623 100644 --- a/node/version.js +++ b/node/version.js @@ -5,6 +5,6 @@ const path = require("path"); const pkgPath = path.join(__dirname, "../package.json"); const SemVer = require("semver"); -module.exports = new SemVer( +module.exports = SemVer.parse( process.env.REIFY_VERSION || fs.readJSON(pkgPath).version ); diff --git a/package.json b/package.json index bf92c263..eae5a026 100644 --- a/package.json +++ b/package.json @@ -44,5 +44,8 @@ "lodash": "4.17.21", "mocha": "9.1.1", "recast": "0.20.5" + }, + "volta": { + "node": "14.21.3" } } diff --git a/test/tla/exported-declaration.js b/test/tla/exported-declaration.js new file mode 100644 index 00000000..de734b63 --- /dev/null +++ b/test/tla/exported-declaration.js @@ -0,0 +1,5 @@ +export const test = await new Promise((resolve) => { + setTimeout(() => { + resolve('value'); + }, 1) +}) \ No newline at end of file diff --git a/test/tla/non-exported-declaration.js b/test/tla/non-exported-declaration.js new file mode 100644 index 00000000..8b6874e7 --- /dev/null +++ b/test/tla/non-exported-declaration.js @@ -0,0 +1,7 @@ +const test = await new Promise((resolve) => { + setTimeout(() => { + resolve('value'); + }, 1) +}) + +export const redirected = test \ No newline at end of file diff --git a/test/top-level-await-tests.js b/test/top-level-await-tests.js index 2d1aa4d8..558fcf32 100644 --- a/test/top-level-await-tests.js +++ b/test/top-level-await-tests.js @@ -3,7 +3,7 @@ const assert = require('assert'); const reify = require('../lib/runtime/index'); import { importSync, importAsync, importAsyncEvaluated } from './tla/nested/parent.js'; -(topLevelAwaitEnabled ? describe : describe.skip) ('top level await', () => { +(topLevelAwaitEnabled ? describe.only : describe.skip) ('top level await', () => { describe('evaluation order', () => { let logs = []; @@ -85,6 +85,16 @@ import { importSync, importAsync, importAsyncEvaluated } from './tla/nested/pare assert(exports.a === 1); }); + it('should detect exported declarations', async () => { + const exports = await require('./tla/exported-declaration.js'); + assert(exports.test === 'value'); + }) + + it('should detect non exported declarations', async () => { + const exports = await require('./tla/non-exported-declaration.js'); + assert(exports.redirected === 'value'); + }) + describe('errors', () => { it('should synchronously throw error for sync module', () => { try {