Skip to content

Commit f325c04

Browse files
authored
🎁 Allow RegEx patterns in options (#155)
Strings in `knownFramework` and `knownFirstparty` option arrays can now include regular expression patterns. 💣 Options are now matched against only the first part of the module path (i.e. up to the first `/`). This means something like `myapp` in `knownFirstparty` will match `'myapp/subdir/foo'` but not `'myapp-addon/lib'`, where previously it would have.
1 parent 29a62f9 commit f325c04

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import path from 'path'
2+
13
import { IStyleAPI, IStyleItem } from 'import-sort-style'
24

35
declare type ImportType = 'import' | 'require' | 'import-equals' | 'import-type'
@@ -41,14 +43,16 @@ export default function(
4143
let knownFirstparty = options.knownFirstparty || []
4244

4345
function isFrameworkModule(imported: IImport) {
44-
return knownFramework.some((prefix) =>
45-
imported.moduleName.startsWith(prefix),
46+
let [base] = imported.moduleName.split(path.sep)
47+
return knownFramework.some((prefix: string) =>
48+
RegExp(`${prefix}$`).test(base),
4649
)
4750
}
4851

4952
function isFirstPartyModule(imported: IImport) {
50-
return knownFirstparty.some((prefix) =>
51-
imported.moduleName.startsWith(prefix),
53+
let [base] = imported.moduleName.split(path.sep)
54+
return knownFirstparty.some((prefix: string) =>
55+
RegExp(`${prefix}$`).test(base),
5256
)
5357
}
5458

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"compilerOptions": {
55
"declaration": true,
66

7+
"esModuleInterop": true,
78
"lib": ["es2017"],
89
"module": "commonjs",
910
"moduleResolution": "node",

0 commit comments

Comments
 (0)