Skip to content

Commit

Permalink
Add requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
giodamelio committed Jan 22, 2017
1 parent 411af62 commit 5e133c0
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 22 deletions.
10 changes: 7 additions & 3 deletions src/rules/no-self-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 === '<text>') {
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.',
})
}
}
Expand All @@ -24,7 +29,6 @@ module.exports = {
description: 'Prohibits a file from importing itself',
recommended: true,
},
fixable: 'code',
schema: [],
},
create: function (context) {
Expand Down
Empty file added tests/files/index.js
Empty file.
Empty file.
Empty file added tests/files/no-self-import.js
Empty file.
117 changes: 98 additions & 19 deletions tests/src/rules/no-self-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<text>',
}),
],
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'),
}),
],
})

0 comments on commit 5e133c0

Please sign in to comment.