Skip to content

Commit

Permalink
[Fix] no-internal-modules: also check export from syntax
Browse files Browse the repository at this point in the history
Fixes #1481.
  • Loading branch information
Alexandre Djerbetian authored and ljharb committed Mar 22, 2020
1 parent 6dd28ea commit 9dfef28
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- TypeScript: Add nested namespace handling ([#1763], thanks [@julien1619])
- [`namespace`]/`ExportMap`: Fix interface declarations for TypeScript ([#1764], thanks [@julien1619])
- [`no-unused-modules`]: avoid order-dependence ([#1744], thanks [@darkartur])
- [`no-internal-modules`]: also check `export from` syntax ([#1691], thanks [@adjerbetian])

### Changed
- [Refactor] `no-extraneous-dependencies`: use moduleVisitor ([#1735], thanks [@adamborowski])
Expand Down Expand Up @@ -702,6 +703,7 @@ for info on changes for earlier releases.
[#1722]: https://github.com/benmosher/eslint-plugin-import/issues/1722
[#1719]: https://github.com/benmosher/eslint-plugin-import/pull/1719
[#1702]: https://github.com/benmosher/eslint-plugin-import/issues/1702
[#1691]: https://github.com/benmosher/eslint-plugin-import/pull/1691
[#1690]: https://github.com/benmosher/eslint-plugin-import/pull/1690
[#1681]: https://github.com/benmosher/eslint-plugin-import/pull/1681
[#1676]: https://github.com/benmosher/eslint-plugin-import/pull/1676
Expand Down Expand Up @@ -1186,3 +1188,4 @@ for info on changes for earlier releases.
[@MikeyBeLike]: https://github.com/MikeyBeLike
[@barbogast]: https://github.com/barbogast
[@adamborowski]: https://github.com/adamborowski
[@adjerbetian]: https://github.com/adjerbetian
6 changes: 6 additions & 0 deletions docs/rules/no-internal-modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ The following patterns are considered problems:
import { settings } from './app/index'; // Reaching to "./app/index" is not allowed
import userReducer from './reducer/user'; // Reaching to "./reducer/user" is not allowed
import configureStore from './redux/configureStore'; // Reaching to "./redux/configureStore" is not allowed

export { settings } from './app/index'; // Reaching to "./app/index" is not allowed
export * from './reducer/user'; // Reaching to "./reducer/user" is not allowed
```

The following patterns are NOT considered problems:
Expand All @@ -61,4 +64,7 @@ The following patterns are NOT considered problems:
import 'source-map-support/register';
import { settings } from '../app';
import getUser from '../actions/getUser';

export * from 'source-map-support/register';
export { settings } from '../app';
```
6 changes: 6 additions & 0 deletions src/rules/no-internal-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ module.exports = {
ImportDeclaration(node) {
checkImportForReaching(node.source.value, node.source)
},
ExportAllDeclaration(node) {
checkImportForReaching(node.source.value, node.source)
},
ExportNamedDeclaration(node) {
checkImportForReaching(node.source.value, node.source)
},
CallExpression(node) {
if (isStaticRequire(node)) {
const [ firstArgument ] = node.arguments
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
103 changes: 103 additions & 0 deletions tests/src/rules/no-internal-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const ruleTester = new RuleTester()

ruleTester.run('no-internal-modules', rule, {
valid: [
// imports
test({
code: 'import a from "./plugin2"',
filename: testFilePath('./internal-modules/plugins/plugin.js'),
Expand Down Expand Up @@ -57,9 +58,44 @@ ruleTester.run('no-internal-modules', rule, {
allow: [ '**/index{.js,}' ],
} ],
}),
// exports
test({
code: 'export {a} from "./internal.js"',
filename: testFilePath('./internal-modules/plugins/plugin2/index.js'),
}),
test({
code: 'export * from "lodash.get"',
filename: testFilePath('./internal-modules/plugins/plugin2/index.js'),
}),
test({
code: 'export {b} from "@org/package"',
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
}),
test({
code: 'export {b} from "../../api/service"',
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
options: [ {
allow: [ '**/api/*' ],
} ],
}),
test({
code: 'export * from "jquery/dist/jquery"',
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
options: [ {
allow: [ 'jquery/dist/*' ],
} ],
}),
test({
code: 'export * from "./app/index.js";\nexport * from "./app/index"',
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
options: [ {
allow: [ '**/index{.js,}' ],
} ],
}),
],

invalid: [
// imports
test({
code: 'import "./plugin2/index.js";\nimport "./plugin2/app/index"',
filename: testFilePath('./internal-modules/plugins/plugin.js'),
Expand Down Expand Up @@ -126,5 +162,72 @@ ruleTester.run('no-internal-modules', rule, {
},
],
}),
// exports
test({
code: 'export * from "./plugin2/index.js";\nexport * from "./plugin2/app/index"',
filename: testFilePath('./internal-modules/plugins/plugin.js'),
options: [ {
allow: [ '*/index.js' ],
} ],
errors: [ {
message: 'Reaching to "./plugin2/app/index" is not allowed.',
line: 2,
column: 15,
} ],
}),
test({
code: 'export * from "./app/index.js"',
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
errors: [ {
message: 'Reaching to "./app/index.js" is not allowed.',
line: 1,
column: 15,
} ],
}),
test({
code: 'export {b} from "./plugin2/internal"',
filename: testFilePath('./internal-modules/plugins/plugin.js'),
errors: [ {
message: 'Reaching to "./plugin2/internal" is not allowed.',
line: 1,
column: 17,
} ],
}),
test({
code: 'export {a} from "../api/service/index"',
filename: testFilePath('./internal-modules/plugins/plugin.js'),
options: [ {
allow: [ '**/internal-modules/*' ],
} ],
errors: [
{
message: 'Reaching to "../api/service/index" is not allowed.',
line: 1,
column: 17,
},
],
}),
test({
code: 'export {b} from "@org/package/internal"',
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
errors: [
{
message: 'Reaching to "@org/package/internal" is not allowed.',
line: 1,
column: 17,
},
],
}),
test({
code: 'export {get} from "debug/node"',
filename: testFilePath('./internal-modules/plugins/plugin.js'),
errors: [
{
message: 'Reaching to "debug/node" is not allowed.',
line: 1,
column: 19,
},
],
}),
],
})

0 comments on commit 9dfef28

Please sign in to comment.