-
-
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
Implement new rule: no-internal-modules #485
Merged
benmosher
merged 9 commits into
import-js:master
from
spalger:implement/rule/no-reaching-in
Sep 15, 2016
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f072e8a
Implement new rule: no-reaching-inside
a2dd4fd
normalize path segments all path segments to '/'
4530ae6
point to minimatch/glob docs to define glob syntax
f872281
refactor no-reaching-inside, allow now points to importable modules/f…
d0ce3a8
add test to verify that "no-reaching/allow" can limit the depth of ma…
0509d70
code style improvements
3d00542
Merge branch 'master' of github.com:benmosher/eslint-plugin-import in…
152dd37
rename to no-internal-modules
b158a32
remove extra test executor
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# no-reaching-inside | ||
|
||
Use this rule to prevent importing the submodules of other modules. | ||
|
||
## Rule Details | ||
|
||
This rule has one option, `allow` which is an array of minimatch patterns to identify directories whose children can be imported explicitly. | ||
|
||
### Examples | ||
|
||
Given the following folder structure: | ||
|
||
``` | ||
my-project | ||
├── actions | ||
│ └── getUser.js | ||
│ └── updateUser.js | ||
├── reducer | ||
│ └── index.js | ||
│ └── user.js | ||
├── redux | ||
│ └── index.js | ||
│ └── configureStore.js | ||
└── app | ||
│ └── index.js | ||
│ └── settings.js | ||
└── entry.js | ||
``` | ||
|
||
And the .eslintrc file: | ||
``` | ||
{ | ||
... | ||
"rules": { | ||
"import/no-reaching-inside": [ "error", { | ||
"allow": [ "**/actions", "source-map-support/*" ] | ||
} ] | ||
} | ||
} | ||
``` | ||
|
||
The following patterns are considered problems: | ||
|
||
```js | ||
/** | ||
* in my-project/entry.jz | ||
*/ | ||
|
||
import { settings } from './app/index'; // Reaching into "./app" is not allowed | ||
import userReducer from './reducer/user'; // Reaching into "./reducer" is not allowed | ||
import configureStore from './redux/configureStore'; // Reaching into "./redux" is not allowed | ||
``` | ||
|
||
The following patterns are NOT considered problems: | ||
|
||
```js | ||
/** | ||
* in my-project/entry.jz | ||
*/ | ||
|
||
import 'source-map-support/register'; | ||
import { settings } from '../app'; | ||
import getUser from '../actions/getUser'; | ||
``` |
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,107 @@ | ||
import path from 'path' | ||
import find from 'lodash.find' | ||
import minimatch from 'minimatch' | ||
|
||
import importType from '../core/importType' | ||
import isStaticRequire from '../core/staticRequire' | ||
|
||
module.exports = function noReachingInside(context) { | ||
const options = context.options[0] || {} | ||
const dirname = path.dirname(context.getFilename()) | ||
const allowRegexps = (options.allow || []).map(p => minimatch.makeRe(p)) | ||
|
||
// test if reaching into this directory is allowed by the | ||
// config, '/' is automatically added so that globs like | ||
// "lodash/**" will match both "lodash" (which requires the trailing /) and "lodash/get" | ||
function reachingAllowed(someDir) { | ||
return !!find(allowRegexps, re => re.test(someDir) || re.test(someDir + '/')) | ||
} | ||
|
||
function isRelativeStep (step) { | ||
return step === '' || step === '.' || step === '..' | ||
} | ||
|
||
function normalizeSep(somePath) { | ||
return somePath.split('\\').join('/') | ||
} | ||
|
||
function report(reachedTo, node) { | ||
context.report({ | ||
node, | ||
message: `Reaching into "${normalizeSep(reachedTo)}" is not allowed.`, | ||
}) | ||
} | ||
|
||
function findNotAllowedReach(importPath, startingBase, join, ignoreStep) { | ||
const steps = normalizeSep(importPath).split('/').filter(Boolean) | ||
|
||
let parentDir = startingBase | ||
while (steps.length) { | ||
const step = steps.shift() | ||
parentDir = normalizeSep(join(parentDir, step)) | ||
|
||
if (ignoreStep && ignoreStep(step)) { | ||
continue | ||
} | ||
if (steps.length && !reachingAllowed(parentDir)) { | ||
return parentDir | ||
} | ||
} | ||
} | ||
|
||
function checkRelativeImportForReaching(importPath, node) { | ||
const reachedInto = findNotAllowedReach(importPath, dirname, path.resolve, isRelativeStep) | ||
if (reachedInto) { | ||
report(path.relative(dirname, reachedInto), node) | ||
} | ||
} | ||
|
||
function checkAbsoluteImportForReaching(importPath, node) { | ||
const reachedInto = findNotAllowedReach(importPath, '', path.join) | ||
if (reachedInto) { | ||
report(reachedInto, node) | ||
} | ||
} | ||
|
||
function checkImportForReaching(importPath, node) { | ||
switch (importType(importPath, context)) { | ||
case 'parent': | ||
case 'index': | ||
case 'sibling': | ||
return checkRelativeImportForReaching(importPath, node) | ||
|
||
case 'external': | ||
case 'internal': | ||
return checkAbsoluteImportForReaching(importPath, node) | ||
default: | ||
return | ||
} | ||
} | ||
|
||
return { | ||
ImportDeclaration(node) { | ||
checkImportForReaching(node.source.value, node.source) | ||
}, | ||
CallExpression(node) { | ||
if (isStaticRequire(node)) { | ||
const [ firstArgument ] = node.arguments | ||
checkImportForReaching(firstArgument.value, firstArgument) | ||
} | ||
}, | ||
} | ||
} | ||
|
||
module.exports.schema = [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
allow: { | ||
type: 'array', | ||
items: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
] |
Empty file.
Empty file.
Empty file.
Empty file.
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,94 @@ | ||
import { RuleTester } from 'eslint' | ||
import rule from 'rules/no-reaching-inside' | ||
|
||
import { test, testFilePath } from '../utils' | ||
|
||
const ruleTester = new RuleTester() | ||
|
||
ruleTester.run('no-reaching-inside', rule, { | ||
valid: [ | ||
test({ | ||
code: 'import a from "./plugin2"', | ||
filename: testFilePath('./reaching-inside/plugins/plugin.js'), | ||
options: [], | ||
}), | ||
test({ | ||
code: 'const a = require("./plugin2")', | ||
filename: testFilePath('./reaching-inside/plugins/plugin.js'), | ||
}), | ||
test({ | ||
code: 'const a = require("./plugin2/")', | ||
filename: testFilePath('./reaching-inside/plugins/plugin.js'), | ||
}), | ||
test({ | ||
code: 'const dynamic = "./plugin2/"; const a = require(dynamic)', | ||
filename: testFilePath('./reaching-inside/plugins/plugin.js'), | ||
}), | ||
test({ | ||
code: 'import b from "./internal.js"', | ||
filename: testFilePath('./reaching-inside/plugins/plugin2/index.js'), | ||
}), | ||
test({ | ||
code: 'import get from "lodash.get"', | ||
filename: testFilePath('./reaching-inside/plugins/plugin2/index.js'), | ||
}), | ||
test({ | ||
code: 'import b from "../../api/service"', | ||
filename: testFilePath('./reaching-inside/plugins/plugin2/internal.js'), | ||
options: [ { | ||
allow: [ '**/api' ], | ||
} ], | ||
}), | ||
test({ | ||
code: 'import "jquery/dist/jquery"', | ||
filename: testFilePath('./reaching-inside/plugins/plugin2/internal.js'), | ||
options: [ { | ||
allow: [ 'jquery/**' ], | ||
} ], | ||
}), | ||
test({ | ||
code: 'import "/app/index.js"', | ||
filename: testFilePath('./reaching-inside/plugins/plugin2/internal.js'), | ||
options: [ { | ||
allow: [ '/app' ], | ||
} ], | ||
}), | ||
], | ||
|
||
invalid: [ | ||
test({ | ||
code: 'import b from "./plugin2/internal"', | ||
filename: testFilePath('./reaching-inside/plugins/plugin.js'), | ||
errors: [ { | ||
message: 'Reaching into "plugin2" is not allowed.', | ||
line: 1, | ||
column: 15, | ||
} ], | ||
}), | ||
test({ | ||
code: 'import a from "../api/service/index"', | ||
filename: testFilePath('./reaching-inside/plugins/plugin.js'), | ||
options: [ { | ||
allow: [ '**/reaching-inside/*' ], | ||
} ], | ||
errors: [ | ||
{ | ||
message: 'Reaching into "../api/service" is not allowed.', | ||
line: 1, | ||
column: 15, | ||
}, | ||
], | ||
}), | ||
test({ | ||
code: 'import get from "lodash/get"', | ||
filename: testFilePath('./reaching-inside/plugins/plugin.js'), | ||
errors: [ | ||
{ | ||
message: 'Reaching into "lodash" is 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.
Can you make
minimatch patterns
a link that links to documentation about minimatch patterns?