diff --git a/tests/lib/rules/no-restricted-imports.js b/tests/lib/rules/no-restricted-imports.js index f54d4df0354..c86e67ecf65 100644 --- a/tests/lib/rules/no-restricted-imports.js +++ b/tests/lib/rules/no-restricted-imports.js @@ -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 + }] } ] }); diff --git a/tests/lib/rules/no-restricted-modules.js b/tests/lib/rules/no-restricted-modules.js index 08cc7c092f5..5e89a6a7329 100644 --- a/tests/lib/rules/no-restricted-modules.js +++ b/tests/lib/rules/no-restricted-modules.js @@ -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 + }] }] });