Skip to content

Commit

Permalink
feat(isValidNodeImport): mark unknown /es/ path as invalid without …
Browse files Browse the repository at this point in the history
…depending on syntax detection (resolves #23)
  • Loading branch information
pi0 committed Sep 20, 2022
1 parent 6b5df10 commit 70e2141
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/syntax.ts
Expand Up @@ -77,7 +77,7 @@ export async function isValidNodeImport (id: string, _opts: ValidNodeImportOptio
const pkg = await readPackageJSON(resolvedPath).catch(() => null)
if (pkg?.type === 'module') { return true }

if (resolvedPath.match(/\.(\w+-)?esm?(-\w+)?\.js$/)) {
if (resolvedPath.match(/\.(\w+-)?esm?(-\w+)?\.js$|\/(esm?)\//)) {
return false
}

Expand Down
2 changes: 2 additions & 0 deletions test/fixture/imports/js-esm/es/index.js
@@ -0,0 +1,2 @@
// @ts-ignore
console.log(await Promise.resolve('foo'))
2 changes: 2 additions & 0 deletions test/fixture/imports/js-esm/es/index.mjs
@@ -0,0 +1,2 @@
// @ts-ignore
console.log(await Promise.resolve('foo'))
9 changes: 7 additions & 2 deletions test/syntax.test.ts
Expand Up @@ -55,15 +55,20 @@ const nodeImportTests = {
[join(import.meta.url, '../fixture/imports/esm')]: true,
[join(import.meta.url, '../fixture/imports/esm-module')]: true,
[join(import.meta.url, '../fixture/imports/js-cjs')]: true,
[join(import.meta.url, '../fixture/imports/js-esm')]: false
[join(import.meta.url, '../fixture/imports/js-esm')]: false,
[join(import.meta.url, '../fixture/imports/js-esm/es/index.mjs')]: true,
[join(import.meta.url, '../fixture/imports/js-esm/es/index.js')]: false
}

describe('isValidNodeImport', () => {
for (const [input, result] of Object.entries(nodeImportTests)) {
it(input, async () => {
try {
expect(await isValidNodeImport(input)).to.equal(result)
} catch (e) {
} catch (err) {
if (result !== 'error') {
throw err
}
expect(result).to.equal('error')
}
})
Expand Down

0 comments on commit 70e2141

Please sign in to comment.