Skip to content

Commit

Permalink
fix: improve regexp for multiple imports on same line (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Mar 24, 2022
1 parent dca8094 commit bc64246
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/analyze.ts
Expand Up @@ -52,12 +52,12 @@ export interface DefaultExport extends ESMExport {
type: 'default'
}

export const ESM_STATIC_IMPORT_RE = /^(?<=\s*)import\s*(["'\s]*(?<imports>[\w*${}\n\r\t, /]+)from\s*)?["']\s*(?<specifier>.*[@\w_-]+)\s*["'][^\n]*$/gm
export const ESM_STATIC_IMPORT_RE = /(?<=\s*|^|;)import\s*(["'\s]*(?<imports>[\w*${}\n\r\t, /]+)from\s*)?["']\s*(?<specifier>(?<="\s*)[^"]*[^"\s](?=\s*")|(?<='\s*)[^']*[^'\s](?=\s*'))\s*["'][\s;]*/gm
export const DYNAMIC_IMPORT_RE = /import\s*\((?<expression>(?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*)\)/gm

export const EXPORT_DECAL_RE = /\bexport\s+(?<declaration>(async function|function|let|const|var|class))\s+(?<name>[\w$_]+)/g
const EXPORT_NAMED_RE = /\bexport\s+{(?<exports>[^}]+)}(\s*from\s*["']\s*(?<specifier>.*[@\w_-]+)\s*["'][^\n]*)?/g
const EXPORT_STAR_RE = /\bexport\s*(\*)\s*(\s*from\s*["']\s*(?<specifier>.*[@\w_-]+)\s*["'][^\n]*)?/g
const EXPORT_NAMED_RE = /\bexport\s+{(?<exports>[^}]+)}(\s*from\s*["']\s*(?<specifier>(?<="\s*)[^"]*[^"\s](?=\s*")|(?<='\s*)[^']*[^'\s](?=\s*'))\s*["'][^\n]*)?/g
const EXPORT_STAR_RE = /\bexport\s*(\*)\s*(\s*from\s*["']\s*(?<specifier>(?<="\s*)[^"]*[^"\s](?=\s*")|(?<='\s*)[^']*[^'\s](?=\s*'))\s*["'][^\n]*)?/g
const EXPORT_DEFAULT_RE = /\bexport\s+default\s+/g

export function findStaticImports (code: string): StaticImport[] {
Expand Down
38 changes: 22 additions & 16 deletions test/imports.test.mjs
Expand Up @@ -45,7 +45,11 @@ const staticTests = {
},
'import "module-name";': {
specifier: 'module-name'
}
},
'import { thing } from "module-name";import { other } from "other-module"': [{
specifier: 'module-name',
namedImports: { thing: 'thing' }
}]
}

staticTests[`import {
Expand Down Expand Up @@ -94,25 +98,27 @@ const dynamicTests = {
}

describe('findStaticImports', () => {
for (const [input, test] of Object.entries(staticTests)) {
for (const [input, _results] of Object.entries(staticTests)) {
it(input.replace(/\n/g, '\\n'), () => {
const matches = findStaticImports(input)
expect(matches.length).to.equal(1)
const results = Array.isArray(_results) ? _results : [_results]
for (let i = 0; i < results.length; i++) {
const test = results[i]
const match = matches[i]
expect(match.type).to.equal('static')

const match = matches[0]
expect(match.type).to.equal('static')

expect(match.specifier).to.equal(test.specifier)
expect(match.specifier).to.equal(test.specifier)

const parsed = parseStaticImport(match)
if (test.defaultImport) {
expect(parsed.defaultImport).to.equals(test.defaultImport)
}
if (test.namedImports) {
expect(parsed.namedImports).to.eql(test.namedImports)
}
if (test.namespacedImport) {
expect(parsed.namespacedImport).to.eql(test.namespacedImport)
const parsed = parseStaticImport(match)
if (test.defaultImport) {
expect(parsed.defaultImport).to.equals(test.defaultImport)
}
if (test.namedImports) {
expect(parsed.namedImports).to.eql(test.namedImports)
}
if (test.namespacedImport) {
expect(parsed.namespacedImport).to.eql(test.namespacedImport)
}
}
})
}
Expand Down

0 comments on commit bc64246

Please sign in to comment.