-
-
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
No relative parent imports rule #1093
Merged
benmosher
merged 11 commits into
import-js:master
from
chrislloyd:no-relative-parent-imports
May 17, 2018
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6278039
No relative parent imports rule
37a24fa
Fix test
5bc2ae4
Add tests for require()
d4e896f
Add support for dynamic imports
2305d32
Typo
8fb588f
Removes `src/core/staticImport` and adds support for dynamic imports …
df6540f
Make the error messages more actionable.
07f3707
Add a lot more detail around how to fix errors in the docs.
4b3bbca
docs grammar
dd0520e
docs grammar
2e05c3f
Merge branch 'master' into no-relative-parent-imports
benmosher 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,55 @@ | ||
# no-relative-parent-imports | ||
|
||
Use this rule to prevent imports to folds in relative parent paths. | ||
|
||
It's useful for large codebases codebases to enforce directed-acyclic-graph like folder structures. | ||
|
||
|
||
### Examples | ||
|
||
Given the following folder structure: | ||
|
||
``` | ||
my-project | ||
├── lib | ||
│ ├── a.js | ||
│ └── b.js | ||
└── main.js | ||
``` | ||
|
||
And the .eslintrc file: | ||
``` | ||
{ | ||
... | ||
"rules": { | ||
"import/no-relative-parent-imports": "error" | ||
} | ||
} | ||
``` | ||
|
||
The following patterns are considered problems: | ||
|
||
```js | ||
/** | ||
* in my-project/lib/a.js | ||
*/ | ||
|
||
import bar from '../main'; // Import parent file using a relative path | ||
``` | ||
|
||
The following patterns are NOT considered problems: | ||
|
||
```js | ||
/** | ||
* in my-project/main.js | ||
*/ | ||
|
||
import foo from 'foo'; // Import package using module path | ||
import a from './lib/a'; // Import child file using relative path | ||
|
||
/** | ||
* in my-project/lib/a.js | ||
*/ | ||
|
||
import b from './b'; // Import sibling file using relative path | ||
``` |
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,36 @@ | ||
import docsUrl from '../docsUrl' | ||
|
||
import importType from '../core/importType' | ||
import isStaticRequire from '../core/staticRequire' | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
url: docsUrl('no-relative-parent-imports'), | ||
}, | ||
}, | ||
|
||
create: function noRelativePackages(context) { | ||
|
||
function checkImportForRelativeParentPath(importPath, node) { | ||
if (importType(importPath, context) === 'parent') { | ||
context.report({ | ||
node, | ||
message: 'Relative imports from parent directories are not allowed.', | ||
}) | ||
} | ||
} | ||
|
||
return { | ||
ImportDeclaration(node) { | ||
checkImportForRelativeParentPath(node.source.value, node.source) | ||
}, | ||
CallExpression(node) { | ||
if (isStaticRequire(node)) { | ||
const [ firstArgument ] = node.arguments | ||
checkImportForRelativeParentPath(firstArgument.value, firstArgument) | ||
} | ||
}, | ||
} | ||
}, | ||
} |
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,35 @@ | ||
import { RuleTester } from 'eslint' | ||
import rule from 'rules/no-relative-parent-imports' | ||
|
||
import { test, testFilePath } from '../utils' | ||
|
||
const ruleTester = new RuleTester() | ||
|
||
ruleTester.run('no-relative-parent-imports', rule, { | ||
valid: [ | ||
test({ | ||
code: 'import foo from "./internal.js"', | ||
filename: testFilePath('./internal-modules/plugins/plugin2/index.js'), | ||
}), | ||
test({ | ||
code: 'import foo from "./app/index.js"', | ||
filename: testFilePath('./internal-modules/plugins/plugin2/index.js'), | ||
}), | ||
test({ | ||
code: 'import foo from "package"', | ||
filename: testFilePath('./internal-modules/plugins/plugin2/index.js'), | ||
}), | ||
], | ||
|
||
invalid: [ | ||
test({ | ||
code: 'import foo from "../plugin.js"', | ||
filename: testFilePath('./internal-modules/plugins/plugin2/index.js'), | ||
errors: [ { | ||
message: 'Relative imports from parent directories are not allowed.', | ||
line: 1, | ||
column: 17, | ||
} ], | ||
}), | ||
], | ||
}) |
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 about
import()
calls?