Skip to content

Commit

Permalink
fix: ignore dynamic import for has ESM syntax (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Nov 12, 2023
1 parent b2969ed commit 2eeee5b
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 4 deletions.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -176,6 +176,15 @@ const [,,, hasModuleSyntax] = parse(`
hasModuleSyntax === true;
```

Dynamic imports are ignored since they can be used in Non-ESM files.

```js
const [,,, hasModuleSyntax] = parse(`
import('./foo.js')
`);
hasModuleSyntax === false;
```

### Environment Support

Node.js 10+, and [all browsers with Web Assembly support](https://caniuse.com/#feat=wasm).
Expand Down
2 changes: 1 addition & 1 deletion lib/lexer.asm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/lexer.emcc.asm.js

Large diffs are not rendered by default.

Binary file modified lib/lexer.wasm
Binary file not shown.
3 changes: 2 additions & 1 deletion src/lexer.h
Expand Up @@ -123,7 +123,8 @@ void addImport (const char16_t* statement_start, const char16_t* start, const ch
import->dynamic = dynamic;
import->safe = dynamic == STANDARD_IMPORT;
import->next = NULL;
hasModuleSyntax = true;
if (dynamic == IMPORT_META || dynamic == STANDARD_IMPORT)
hasModuleSyntax = true;
}

void addExport (const char16_t* start, const char16_t* end, const char16_t* local_start, const char16_t* local_end) {
Expand Down
3 changes: 2 additions & 1 deletion test/_unit.cjs
Expand Up @@ -1399,7 +1399,8 @@ function x() {
})
test('hasModuleSyntax import3', () => {
const [,,, hasModuleSyntax] = parse('import("./foo")')
assert.strictEqual(hasModuleSyntax, true)
// dynamic imports can be used in non-ESM files as well
assert.strictEqual(hasModuleSyntax, false)
})
test('hasModuleSyntax import4', () => {
const [,,, hasModuleSyntax] = parse('import.meta.url')
Expand Down

0 comments on commit 2eeee5b

Please sign in to comment.