Skip to content

Commit

Permalink
fix declaration detection
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardoventurini committed May 27, 2024
1 parent 444ecd8 commit 4ab165b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 3 deletions.
11 changes: 10 additions & 1 deletion lib/import-export-visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion node/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,8 @@
"lodash": "4.17.21",
"mocha": "9.1.1",
"recast": "0.20.5"
},
"volta": {
"node": "14.21.3"
}
}
5 changes: 5 additions & 0 deletions test/tla/exported-declaration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const test = await new Promise((resolve) => {
setTimeout(() => {
resolve('value');
}, 1)
})
7 changes: 7 additions & 0 deletions test/tla/non-exported-declaration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const test = await new Promise((resolve) => {
setTimeout(() => {
resolve('value');
}, 1)
})

export const redirected = test
12 changes: 11 additions & 1 deletion test/top-level-await-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4ab165b

Please sign in to comment.