-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add rule 'no-self-import' #727
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Forbid a module from importing itself | ||
|
||
Forbid a module from importing itself. This can sometimes happen during refactoring. | ||
|
||
## Rule Details | ||
|
||
### Fail | ||
|
||
```js | ||
// foo.js | ||
import foo from './foo'; | ||
|
||
const foo = require('./foo'); | ||
``` | ||
|
||
```js | ||
// index.js | ||
import index from '.'; | ||
|
||
const index = require('.'); | ||
``` | ||
|
||
### Pass | ||
|
||
```js | ||
// foo.js | ||
import bar from './bar'; | ||
|
||
const bar = require('./bar'); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* @fileOverview Forbids a module from importing itself | ||
* @author Gio d'Amelio | ||
*/ | ||
|
||
import resolve from 'eslint-module-utils/resolve' | ||
import isStaticRequire from '../core/staticRequire' | ||
|
||
function isImportingSelf(context, node, requireName) { | ||
const filePath = context.getFilename() | ||
|
||
// If the input is from stdin, this test can't fail | ||
if (filePath !== '<text>' && filePath === resolve(requireName, context)) { | ||
context.report({ | ||
node, | ||
message: 'Module imports itself.', | ||
}) | ||
} | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
doc: { | ||
description: 'Forbid a module from importing itself', | ||
recommended: true, | ||
}, | ||
schema: [], | ||
}, | ||
create: function (context) { | ||
return { | ||
ImportDeclaration(node) { | ||
isImportingSelf(context, node, node.source.value) | ||
}, | ||
CallExpression(node) { | ||
if (isStaticRequire(node)) { | ||
isImportingSelf(context, node, node.arguments[0].value) | ||
} | ||
}, | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// Used in `no-self-import` tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// Used in `no-self-import` tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// Used in `no-self-import` tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { test, testFilePath } from '../utils' | ||
|
||
import { RuleTester } from 'eslint' | ||
|
||
const ruleTester = new RuleTester() | ||
, rule = require('rules/no-self-import') | ||
|
||
const error = { | ||
ruleId: 'no-self-import', | ||
message: 'Module imports itself.', | ||
} | ||
|
||
ruleTester.run('no-self-import', rule, { | ||
valid: [ | ||
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 "./no-self-import"', | ||
errors: [error], | ||
filename: testFilePath('./no-self-import.js'), | ||
}), | ||
test({ | ||
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('./no-self-import-folder/index.js'), | ||
}), | ||
], | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what happens when the input is from stdin, and thus there's no filename?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! When that happens,
filePath
will equal'<text>
. In some other plugin, we've handled it like: https://github.com/sindresorhus/eslint-plugin-unicorn/blob/master/rules/filename-case.js#L71-L73There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems totally reasonable - it might also be useful to create a generic way to tag rules as "does not apply when the file is from stdin"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have actually never used the
--stdin
option and don't know the uses for it 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't even know that was a thing you could do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I use it all the time when providing reproduction steps for eslint bugs :-p
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
last I knew, Sublime uses
--stdin
, but with--stdin-filename
(or something like that) to fake in the filename