Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaking: Update ignore to v5 #14889

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/rules/no-restricted-imports.js
Expand Up @@ -147,6 +147,10 @@ module.exports = {
? [{ matcher: ignore().add(restrictedPatterns) }]
: restrictedPatterns.map(({ group, message }) => ({ matcher: ignore().add(group), customMessage: message }));

if (restrictedPatterns && restrictedPatterns.some(restrictedPattern => /^(\.|\/)/u.test(restrictedPattern))) {
throw new Error("`patterns` passed to `no-restricted-modules` should not begin with `./` or `../`.");
}

// if no imports are restricted we don"t need to check
if (Object.keys(restrictedPaths).length === 0 && restrictedPatternGroups.length === 0) {
return {};
Expand Down Expand Up @@ -242,6 +246,10 @@ module.exports = {
* @private
*/
function isRestrictedPattern(importSource, group) {
while (/^(\.|\/)/u.test(importSource)) {
// ignore v5 doesn't like relative or absolute paths so remove such prefixes from the path.
importSource = importSource.replace(/^(\.|\/)/u, '');
}
return group.matcher.ignores(importSource);
}

Expand Down
21 changes: 20 additions & 1 deletion lib/rules/no-restricted-modules.js
Expand Up @@ -97,6 +97,10 @@ module.exports = {
return memo;
}, {});

if (restrictedPatterns && restrictedPatterns.some(restrictedPattern => /^(\.|\/)/u.test(restrictedPattern))) {
throw new Error("`patterns` passed to `no-restricted-modules` should not begin with `./` or `../`.");
}

// if no imports are restricted we don"t need to check
if (Object.keys(restrictedPaths).length === 0 && restrictedPatterns.length === 0) {
return {};
Expand Down Expand Up @@ -182,6 +186,21 @@ module.exports = {
return Object.prototype.hasOwnProperty.call(restrictedPathMessages, name);
}

/**
* Check if the given importSource is restricted by a pattern.
* @param {string} importSource path of the import
* @param {Object} ig ignore matcher
* @returns {boolean} whether the variable is a restricted pattern or not
* @private
*/
function isRestrictedPattern(importSource, ig) {
while (/^(\.|\/)/u.test(importSource)) {
// ignore v5 doesn't like relative or absolute paths so remove such prefixes from the path.
importSource = importSource.replace(/^(\.|\/)/u, '');
}
return ig.ignores(importSource);
}

return {
CallExpression(node) {
if (isRequireCall(node)) {
Expand All @@ -198,7 +217,7 @@ module.exports = {
reportPath(node, name);
}

if (restrictedPatterns.length > 0 && ig.ignores(name)) {
if (restrictedPatterns.length > 0 && isRestrictedPattern(name, ig)) {
context.report({
node,
messageId: "patternMessage",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -68,7 +68,7 @@
"functional-red-black-tree": "^1.0.1",
"glob-parent": "^6.0.1",
"globals": "^13.6.0",
"ignore": "^4.0.6",
"ignore": "^5.1.8",
"import-fresh": "^3.0.0",
"imurmurhash": "^0.1.4",
"is-glob": "^4.0.0",
Expand Down
15 changes: 13 additions & 2 deletions tests/lib/rules/no-restricted-imports.js
Expand Up @@ -31,13 +31,13 @@ ruleTester.run("no-restricted-imports", rule, {
{ code: "import withPatterns from \"foo/bar\";", options: [{ patterns: ["foo/c*"] }] },
{ code: "import foo from 'foo';", options: ["../foo"] },
{ code: "import foo from 'foo';", options: [{ paths: ["../foo"] }] },
{ code: "import foo from 'foo';", options: [{ patterns: ["../foo"] }] },
{ code: "import foo from 'foo';", options: ["/foo"] },
{ code: "import foo from 'foo';", options: [{ paths: ["/foo"] }] },
"import relative from '../foo';",
{ code: "import relative from '../foo';", options: ["../notFoo"] },
{ code: "import relativeWithPaths from '../foo';", options: [{ paths: ["../notFoo"] }] },
{ code: "import relativeWithPatterns from '../foo';", options: [{ patterns: ["notFoo"] }] },
{ code: "import relativeWithPatterns from '../../foo';", options: [{ patterns: ["notFoo"] }] },
"import absolute from '/foo';",
{ code: "import absolute from '/foo';", options: ["/notFoo"] },
{ code: "import absoluteWithPaths from '/foo';", options: [{ paths: ["/notFoo"] }] },
Expand Down Expand Up @@ -853,7 +853,7 @@ ruleTester.run("no-restricted-imports", rule, {
},
{
code: "import relativeWithPatterns from '../foo';",
options: [{ patterns: ["../foo"] }],
options: [{ patterns: ["foo"] }],
errors: [{
message: "'../foo' import is restricted from being used by a pattern.",
type: "ImportDeclaration",
Expand All @@ -862,6 +862,17 @@ ruleTester.run("no-restricted-imports", rule, {
endColumn: 43
}]
},
{
code: "import relativeWithPatterns from '../../foo';",
options: [{ patterns: ["foo"] }],
errors: [{
message: "'../../foo' import is restricted from being used by a pattern.",
type: "ImportDeclaration",
line: 1,
column: 1,
endColumn: 46
}]
},
{
code: "import absolute from '/foo';",
options: ["/foo"],
Expand Down
15 changes: 13 additions & 2 deletions tests/lib/rules/no-restricted-modules.js
Expand Up @@ -35,13 +35,13 @@ ruleTester.run("no-restricted-modules", rule, {
{ code: "require(`foo${bar}`)", options: ["foo"], parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = require('foo');", options: ["../foo"] },
{ code: "var foo = require('foo');", options: [{ paths: ["../foo"] }] },
{ code: "var foo = require('foo');", options: [{ patterns: ["../foo"] }] },
{ code: "var foo = require('foo');", options: ["/foo"] },
{ code: "var foo = require('foo');", options: [{ paths: ["/foo"] }] },
"var relative = require('../foo');",
{ code: "var relative = require('../foo');", options: ["../notFoo"] },
{ code: "var relativeWithPaths = require('../foo');", options: [{ paths: ["../notFoo"] }] },
{ code: "var relativeWithPatterns = require('../foo');", options: [{ patterns: ["notFoo"] }] },
{ code: "var relativeWithPatterns = require('../../foo');", options: [{ patterns: ["notFoo"] }] },
"var absolute = require('/foo');",
{ code: "var absolute = require('/foo');", options: ["/notFoo"] },
{ code: "var absoluteWithPaths = require('/foo');", options: [{ paths: ["/notFoo"] }] },
Expand Down Expand Up @@ -149,7 +149,7 @@ ruleTester.run("no-restricted-modules", rule, {
},
{
code: "var relativeWithPatterns = require('../foo');",
options: [{ patterns: ["../foo"] }],
options: [{ patterns: ["foo"] }],
errors: [{
message: "'../foo' module is restricted from being used by a pattern.",
type: "CallExpression",
Expand All @@ -158,6 +158,17 @@ ruleTester.run("no-restricted-modules", rule, {
endColumn: 45
}]
},
{
code: "var relativeWithPatterns = require('../../foo');",
options: [{ patterns: ["foo"] }],
errors: [{
message: "'../../foo' module is restricted from being used by a pattern.",
type: "CallExpression",
line: 1,
column: 28,
endColumn: 48
}]
},
{
code: "var absolute = require('/foo');",
options: ["/foo"],
Expand Down