From 5e133c0b54aaa5fb20d1dee9f8c67aa40406f6e4 Mon Sep 17 00:00:00 2001 From: Gio d'Amelio Date: Sun, 22 Jan 2017 15:59:55 -0800 Subject: [PATCH] Add requested changes --- src/rules/no-self-import.js | 10 +- tests/files/index.js | 0 tests/files/no-self-import-folder/index.js | 0 tests/files/no-self-import.js | 0 tests/src/rules/no-self-import.js | 117 +++++++++++++++++---- 5 files changed, 105 insertions(+), 22 deletions(-) create mode 100644 tests/files/index.js create mode 100644 tests/files/no-self-import-folder/index.js create mode 100644 tests/files/no-self-import.js diff --git a/src/rules/no-self-import.js b/src/rules/no-self-import.js index 16c08addf5..de655886ba 100644 --- a/src/rules/no-self-import.js +++ b/src/rules/no-self-import.js @@ -9,11 +9,16 @@ import isStaticRequire from '../core/staticRequire' function isImportingSelf(context, node, requireName) { const filePath = context.getFilename() const resolvedPath = resolve(requireName, context) + + // If the input is from stdin, this test can't fail + if (filePath === '') { + return + } + if (filePath === resolvedPath) { context.report({ node, - // TODO: check in about message, should it print the name of the module itself - message: 'Module imports itself', + message: 'Module imports itself.', }) } } @@ -24,7 +29,6 @@ module.exports = { description: 'Prohibits a file from importing itself', recommended: true, }, - fixable: 'code', schema: [], }, create: function (context) { diff --git a/tests/files/index.js b/tests/files/index.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/files/no-self-import-folder/index.js b/tests/files/no-self-import-folder/index.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/files/no-self-import.js b/tests/files/no-self-import.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/src/rules/no-self-import.js b/tests/src/rules/no-self-import.js index d879a10845..f8549b49ed 100644 --- a/tests/src/rules/no-self-import.js +++ b/tests/src/rules/no-self-import.js @@ -7,36 +7,115 @@ const ruleTester = new RuleTester() const error = { ruleId: 'no-self-import', - message: 'Module imports itself', + message: 'Module imports itself.', } ruleTester.run('no-self-import', rule, { valid: [ - test({ code: 'import _ from "lodash"'}), - test({ code: 'import find from "lodash.find"'}), - test({ code: 'import foo from "./foo"'}), - test({ code: 'import foo from "../foo"'}), - test({ code: 'import foo from "foo"'}), - test({ code: 'import foo from "./"'}), - test({ code: 'import foo from "@scope/foo"'}), - test({ code: 'var _ = require("lodash")'}), - test({ code: 'var find = require("lodash.find")'}), - test({ code: 'var foo = require("./foo")'}), - test({ code: 'var foo = require("../foo")'}), - test({ code: 'var foo = require("foo")'}), - test({ code: 'var foo = require("./")'}), - test({ code: 'var foo = require("@scope/foo")'}), + test({ + code: 'import _ from "lodash"', + filename: testFilePath('./no-self-import.js'), + }), + test({ + code: 'import find from "lodash.find"', + filename: testFilePath('./no-self-import.js'), + }), + test({ + code: 'import foo from "./foo"', + filename: testFilePath('./no-self-import.js'), + }), + test({ + code: 'import foo from "../foo"', + filename: testFilePath('./no-self-import.js'), + }), + test({ + code: 'import foo from "foo"', + filename: testFilePath('./no-self-import.js'), + }), + test({ + code: 'import foo from "./"', + filename: testFilePath('./no-self-import.js'), + }), + test({ + code: 'import foo from "@scope/foo"', + filename: testFilePath('./no-self-import.js'), + }), + test({ + code: 'var _ = require("lodash")', + filename: testFilePath('./no-self-import.js'), + }), + test({ + code: 'var find = require("lodash.find")', + filename: testFilePath('./no-self-import.js'), + }), + test({ + code: 'var foo = require("./foo")', + filename: testFilePath('./no-self-import.js'), + }), + test({ + code: 'var foo = require("../foo")', + filename: testFilePath('./no-self-import.js'), + }), + test({ + code: 'var foo = require("foo")', + filename: testFilePath('./no-self-import.js'), + }), + test({ + code: 'var foo = require("./")', + filename: testFilePath('./no-self-import.js'), + }), + test({ + code: 'var foo = require("@scope/foo")', + filename: testFilePath('./no-self-import.js'), + }), + test({ + code: 'var bar = require("./bar/index")', + filename: testFilePath('./no-self-import.js'), + }), + test({ + code: 'var bar = require("./bar")', + filename: testFilePath('./bar/index.js'), + }), + test({ + code: 'var bar = require("./bar")', + filename: '', + }), ], invalid: [ test({ - code: 'import bar from "./bar"', + code: 'import bar from "./no-self-import"', errors: [error], - filename: testFilePath('./bar.js'), + filename: testFilePath('./no-self-import.js'), }), test({ - code: 'var bar = require("./bar")', + code: 'var bar = require("./no-self-import")', + errors: [error], + filename: testFilePath('./no-self-import.js'), + }), + test({ + code: 'var bar = require("./no-self-import.js")', + errors: [error], + filename: testFilePath('./no-self-import.js'), + }), + test({ + code: 'var bar = require(".")', + errors: [error], + filename: testFilePath('./index.js'), + }), + test({ + code: 'var bar = require("./")', + errors: [error], + filename: testFilePath('./index.js'), + }), + test({ + code: 'var bar = require("././././")', + errors: [error], + filename: testFilePath('./index.js'), + }), + test({ + code: 'var bar = require("../no-self-import-folder")', errors: [error], - filename: testFilePath('./bar.js'), + filename: testFilePath('./no-self-import-folder/index.js'), }), ], })