This repository has been archived by the owner on Apr 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
esm: utility method for detecting ES module syntax
- Loading branch information
1 parent
030fa2e
commit 45954e1
Showing
15 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const { strictEqual, fail } = require('assert'); | ||
const { readFileSync } = require('fs'); | ||
|
||
const { containsModuleSyntax } = require('module'); | ||
|
||
expect('esm-with-import-statement.js', 'module'); | ||
expect('esm-with-export-statement.js', 'module'); | ||
expect('esm-with-import-expression.js', 'module'); | ||
expect('esm-with-indented-import-statement.js', 'module'); | ||
|
||
expect('cjs-with-require.js', 'commonjs'); | ||
expect('cjs-with-import-expression.js', 'commonjs'); | ||
expect('cjs-with-property-named-import.js', 'commonjs'); | ||
expect('cjs-with-property-named-export.js', 'commonjs'); | ||
expect('cjs-with-string-containing-import.js', 'commonjs'); | ||
|
||
expect('print-version.js', 'commonjs'); | ||
expect('ambiguous-with-import-expression.js', 'commonjs'); | ||
|
||
expect('syntax-error.js', 'Invalid or unexpected token', true); | ||
|
||
function expect(file, want, wantsError = false) { | ||
const source = readFileSync( | ||
require.resolve(`../fixtures/es-modules/detect/${file}`), | ||
'utf8'); | ||
let isModule; | ||
try { | ||
isModule = containsModuleSyntax(source); | ||
} catch (err) { | ||
if (wantsError) { | ||
return strictEqual(err.message, want); | ||
} else { | ||
return fail( | ||
`Expected ${file} to throw '${want}'; received '${err.message}'`); | ||
} | ||
} | ||
if (wantsError) | ||
return fail(`Expected ${file} to throw '${want}'; no error was thrown`); | ||
else | ||
return strictEqual((isModule ? 'module' : 'commonjs'), want); | ||
} |
3 changes: 3 additions & 0 deletions
3
test/fixtures/es-modules/detect/ambiguous-with-import-expression.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
(async () => { | ||
await import('./print-version.js'); | ||
})(); |
5 changes: 5 additions & 0 deletions
5
test/fixtures/es-modules/detect/cjs-with-import-expression.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const { version } = require('process'); | ||
|
||
(async () => { | ||
await import('./print-version.js'); | ||
})(); |
11 changes: 11 additions & 0 deletions
11
test/fixtures/es-modules/detect/cjs-with-property-named-export.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// See ./cjs-with-property-named-import.js | ||
|
||
global.export = 3; | ||
|
||
global['export'] = 3; | ||
|
||
const obj = { | ||
export: 3 // Specifically at column 0, to try to trick the detector | ||
} | ||
|
||
console.log(require('process').version); |
14 changes: 14 additions & 0 deletions
14
test/fixtures/es-modules/detect/cjs-with-property-named-import.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// In JavaScript, reserved words cannot be identifiers (the `foo` in `var foo`) | ||
// but they can be properties (`obj.foo`). This file checks that the `import` | ||
// reserved word isn't incorrectly detected as a keyword. For more info see: | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Reserved_word_usage | ||
|
||
global.import = 3; | ||
|
||
global['import'] = 3; | ||
|
||
const obj = { | ||
import: 3 // Specifically at column 0, to try to trick the detector | ||
} | ||
|
||
console.log(require('process').version); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const { version } = require('process'); | ||
|
||
console.log(version); |
7 changes: 7 additions & 0 deletions
7
test/fixtures/es-modules/detect/cjs-with-string-containing-import.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const { version } = require('process'); | ||
|
||
const sneakyString = ` | ||
import { version } from 'process'; | ||
`; | ||
|
||
console.log(version); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const version = process.version; | ||
|
||
export default version; | ||
|
||
console.log(version); | ||
|
5 changes: 5 additions & 0 deletions
5
test/fixtures/es-modules/detect/esm-with-import-expression.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { version } from 'process'; | ||
|
||
(async () => { | ||
await import('./print-version.js'); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { version } from 'process'; | ||
console.log(version); |
2 changes: 2 additions & 0 deletions
2
test/fixtures/es-modules/detect/esm-with-indented-import-statement.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { version } from 'process'; | ||
console.log(version); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log(process.version); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const str = 'import | ||
var foo = 3; |