-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b5a962f
commit 0ea3d80
Showing
9 changed files
with
203 additions
and
0 deletions.
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'), | ||
}), | ||
], | ||
}) |