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

Chore: Add rel/abs path tests in no-restricted-{imports/modules} rules #14910

Merged
Merged
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
79 changes: 79 additions & 0 deletions tests/lib/rules/no-restricted-imports.js
Expand Up @@ -29,6 +29,19 @@ ruleTester.run("no-restricted-imports", rule, {
{ code: "import \"foo/bar\";", options: ["foo"] },
{ code: "import withPaths from \"foo/bar\";", options: [{ paths: ["foo", "bar"] }] },
{ 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"] }] },
"import absolute from '/foo';",
{ code: "import absolute from '/foo';", options: ["/notFoo"] },
{ code: "import absoluteWithPaths from '/foo';", options: [{ paths: ["/notFoo"] }] },
{ code: "import absoluteWithPatterns from '/foo';", options: [{ patterns: ["notFoo"] }] },
{
code: "import withPatternsAndPaths from \"foo/bar\";",
options: [{ paths: ["foo"], patterns: ["foo/c*"] }]
Expand Down Expand Up @@ -815,6 +828,72 @@ ruleTester.run("no-restricted-imports", rule, {
column: 15,
endColumn: 29
}]
},
{
code: "import relative from '../foo';",
options: ["../foo"],
errors: [{
message: "'../foo' import is restricted from being used.",
type: "ImportDeclaration",
line: 1,
column: 1,
endColumn: 31
}]
},
{
code: "import relativeWithPaths from '../foo';",
options: [{ paths: ["../foo"] }],
errors: [{
message: "'../foo' import is restricted from being used.",
type: "ImportDeclaration",
line: 1,
column: 1,
endColumn: 40
}]
},
{
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: 43
}]
},
{
code: "import absolute from '/foo';",
options: ["/foo"],
errors: [{
message: "'/foo' import is restricted from being used.",
type: "ImportDeclaration",
line: 1,
column: 1,
endColumn: 29
}]
},
{
code: "import absoluteWithPaths from '/foo';",
options: [{ paths: ["/foo"] }],
errors: [{
message: "'/foo' import is restricted from being used.",
type: "ImportDeclaration",
line: 1,
column: 1,
endColumn: 38
}]
},
{
code: "import absoluteWithPatterns from '/foo';",
options: [{ patterns: ["foo"] }],
errors: [{
message: "'/foo' import is restricted from being used by a pattern.",
type: "ImportDeclaration",
line: 1,
column: 1,
endColumn: 41
}]
}
]
});
81 changes: 80 additions & 1 deletion tests/lib/rules/no-restricted-modules.js
Expand Up @@ -32,7 +32,20 @@ ruleTester.run("no-restricted-modules", rule, {
{ code: "var withPatternsAndPaths = require(\"foo/bar\");", options: [{ paths: ["foo"], patterns: ["foo/c*"] }] },
{ code: "var withGitignores = require(\"foo/bar\");", options: [{ paths: ["foo"], patterns: ["foo/*", "!foo/bar"] }] },
{ code: "require(`fs`)", options: ["crypto"], parserOptions: { ecmaVersion: 6 } },
{ code: "require(`foo${bar}`)", options: ["foo"], parserOptions: { ecmaVersion: 6 } }
{ 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"] }] },
"var absolute = require('/foo');",
{ code: "var absolute = require('/foo');", options: ["/notFoo"] },
{ code: "var absoluteWithPaths = require('/foo');", options: [{ paths: ["/notFoo"] }] },
{ code: "var absoluteWithPatterns = require('/foo');", options: [{ patterns: ["notFoo"] }] }
],
invalid: [{
code: "require(\"fs\")",
Expand Down Expand Up @@ -111,5 +124,71 @@ ruleTester.run("no-restricted-modules", rule, {
options: ["crypto"],
parserOptions: { ecmaVersion: 6 },
errors: [{ messageId: "defaultMessage", data: { name: "crypto" }, type: "CallExpression" }]
},
{
code: "var relative = require('../foo');",
options: ["../foo"],
errors: [{
message: "'../foo' module is restricted from being used.",
type: "CallExpression",
line: 1,
column: 16,
endColumn: 33
}]
},
{
code: "var relativeWithPaths = require('../foo');",
options: [{ paths: ["../foo"] }],
errors: [{
message: "'../foo' module is restricted from being used.",
type: "CallExpression",
line: 1,
column: 25,
endColumn: 42
}]
},
{
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: 45
}]
},
{
code: "var absolute = require('/foo');",
options: ["/foo"],
errors: [{
message: "'/foo' module is restricted from being used.",
type: "CallExpression",
line: 1,
column: 16,
endColumn: 31
}]
},
{
code: "var absoluteWithPaths = require('/foo');",
options: [{ paths: ["/foo"] }],
errors: [{
message: "'/foo' module is restricted from being used.",
type: "CallExpression",
line: 1,
column: 25,
endColumn: 40
}]
},
{
code: "var absoluteWithPatterns = require('/foo');",
options: [{ patterns: ["foo"] }],
errors: [{
message: "'/foo' module is restricted from being used by a pattern.",
type: "CallExpression",
line: 1,
column: 28,
endColumn: 43
}]
}]
});