Skip to content

Commit 05b4d8f

Browse files
committed
feat(syntax): detect dynamic imports in commonjs
- https://nodejs.org/api/esm.html#import-expressions Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
1 parent d2027f8 commit 05b4d8f

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/internal/constants.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @const {RegExp} CJS_SYNTAX_REGEX
1212
*/
1313
export const CJS_SYNTAX_REGEX: RegExp =
14-
/(?<!(?:\/\/|\*).*)((?:module\.)?exports(?:\.\w+|(?<!\s+=))|require(?=\(.)|require\.\w+|__dirname|__filename)/gm
14+
/(?<!(?:\/\/|\*).*)((?:module\.)?exports(?:\.\w+|(?<!\s+=))|require(?=\(.)|require\.\w+|__dirname|__filename|await import)/gm
1515

1616
/**
1717
* Dynamic import statement regex.

src/lib/__tests__/has-cjs-syntax.spec.ts

+23
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,29 @@ describe('unit:lib/hasCJSSyntax', () => {
6464
})
6565
})
6666

67+
describe('await import', () => {
68+
it('should detect await import', () => {
69+
expect(testSubject('await import("read-pkg")')).to.be.true
70+
})
71+
72+
it('should ignore await import in multi-line comment', () => {
73+
// Arrange
74+
const code = dedent`
75+
/**
76+
* @example
77+
* await import('read-pkg')
78+
*/
79+
`
80+
81+
// Act + Expect
82+
expect(testSubject(code)).to.be.false
83+
})
84+
85+
it('should ignore await import in single-line comment', () => {
86+
expect(testSubject('// await import("read-pkg")')).to.be.false
87+
})
88+
})
89+
6790
describe('exports', () => {
6891
it('should detect default export', () => {
6992
expect(testSubject('exports = {}')).to.be.true

0 commit comments

Comments
 (0)