Skip to content
Permalink
Browse files
Chore: Add rel/abs path tests in no-restricted-{imports/modules} ru…
…les (#14910)
  • Loading branch information
bmish committed Aug 11, 2021
1 parent 62c6fe7 commit 3b6cd8934b3640ffb6fa49b471babf07f0ad769a
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 1 deletion.
@@ -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*"] }]
@@ -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
}]
}
]
});
@@ -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\")",
@@ -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
}]
}]
});

0 comments on commit 3b6cd89

Please sign in to comment.