Skip to content

Commit 1c4c2bc

Browse files
authored
🐛 Allow RegExp escape characters in options (#158)
1 parent c59bd0a commit 1c4c2bc

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/index.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,24 @@ export default function(
4242
let knownFramework = options.knownFramework || []
4343
let knownFirstparty = options.knownFirstparty || []
4444

45+
/**
46+
* Transforms the option string to JSON and back again, so that
47+
* RegExp escapes are maintained.
48+
*/
49+
function moduleMatchesOption(module, option) {
50+
let [base] = module.split(path.sep)
51+
return [option].some((opt: string) => {
52+
let pattern = JSON.parse(`{"regex": ${JSON.stringify(opt)}}`)
53+
return RegExp(`${pattern}$`).test(base)
54+
})
55+
}
56+
4557
function isFrameworkModule(imported: IImport) {
46-
let [base] = imported.moduleName.split(path.sep)
47-
return knownFramework.some((prefix: string) =>
48-
RegExp(`${prefix}$`).test(base),
49-
)
58+
return moduleMatchesOption(imported.moduleName, knownFramework)
5059
}
5160

5261
function isFirstPartyModule(imported: IImport) {
53-
let [base] = imported.moduleName.split(path.sep)
54-
return knownFirstparty.some((prefix: string) =>
55-
RegExp(`${prefix}$`).test(base),
56-
)
62+
return moduleMatchesOption(imported.moduleName, knownFirstparty)
5763
}
5864

5965
return [

0 commit comments

Comments
 (0)