Skip to content

Commit

Permalink
Filter scoped modules correctly
Browse files Browse the repository at this point in the history
Fixes microsoft#25

A valid node module name is either `\w+` or `@\w+/\w+` never `@\w+`.

This is enough of a hairy internal implementation detail that filter
should probably not be exposed even as an (undocumented) option. But
leaving alone for now as changing the pattern has been a useful
workaround.
  • Loading branch information
bz2 committed Apr 4, 2021
1 parent c88d90f commit 575de08
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/optionsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const optionsSchema = object({
});

const defaultOptions = {
filter: /(^.*[/\\]node_modules[/\\]((?:@[^/\\]+[/\\])?(?:[^/\\]+)))/,
filter: /(^.*[/\\]node_modules[/\\]((?:@[^/\\]+[/\\])?(?:[^@/\\][^/\\]*)))/,
allow: "(Apache-2.0 OR BSD-2-Clause OR BSD-3-Clause OR MIT)",
ignore: [],
override: {},
Expand Down
39 changes: 39 additions & 0 deletions src/optionsUtils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,42 @@ describe("getOptions", () => {
);
});
});

describe("defaults behaviour", () => {
const { filter } = getOptions();

test.each([
// Modules are things under node_modules
["/base/node_modules/a-proj/dist/index.js", ["/base/node_modules/a-proj", "a-proj"]],
["/base/node_modules/a-proj/dist", ["/base/node_modules/a-proj", "a-proj"]],
["/base/node_modules/a-proj", ["/base/node_modules/a-proj", "a-proj"]],
["/base/node_modules", null],
["/base", null],
["/", null],
// Scoped names have two parts
["/base/node_modules/@babel/runtime/", ["/base/node_modules/@babel/runtime", "@babel/runtime"]],
["/base/node_modules/@babel", null],
// Modules can be nested and deepest is retrieved
[
"/base/node_modules/a-proj/node_modules/b-proj/index.js",
["/base/node_modules/a-proj/node_modules/b-proj", "b-proj"]
],
[
"/base/node_modules/@a/proj/node_modules/b-proj/index.js",
["/base/node_modules/@a/proj/node_modules/b-proj", "b-proj"]
],
[
"/base/node_modules/@a/proj/node_modules/@b/proj/src/index.mjs",
["/base/node_modules/@a/proj/node_modules/@b/proj", "@b/proj"]
],
[
"/base/node_modules/a-proj/node_modules/@b/proj/src/index.mjs",
["/base/node_modules/a-proj/node_modules/@b/proj", "@b/proj"]
],
// Backslashes are allowed as separators
["C:\\No\\node_modules\\a-proj\\dist\\index.js", ["C:\\No\\node_modules\\a-proj", "a-proj"]]
])("filter %#", (path, expected) => {
const result = filter.exec(path);
expect(result ? result.slice(1) : result).toEqual(expected);
});
});

0 comments on commit 575de08

Please sign in to comment.