Skip to content

Commit cc2b96a

Browse files
committed
[new] core: add internal-regex setting for marking packages as internal
1 parent 5e143b2 commit cc2b96a

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,20 @@ settings:
402402
[`eslint_d`]: https://www.npmjs.com/package/eslint_d
403403
[`eslint-loader`]: https://www.npmjs.com/package/eslint-loader
404404

405+
#### `import/internal-regex`
406+
407+
A regex for packages should be treated as internal. Useful when you are utilizing a monorepo setup or developing a set of packages that depend on each other.
408+
409+
By default, any package referenced from [`import/external-module-folders`](#importexternal-module-folders) will be considered as "external", including packages in a monorepo like yarn workspace or lerna emvironentment. If you want to mark these packages as "internal" this will be useful.
410+
411+
For example, if you pacakges in a monorepo are all in `@scope`, you can configure `import/internal-regex` like this
412+
413+
```yaml
414+
# .eslintrc.yml
415+
settings:
416+
import/internal-regex: ^@scope/
417+
```
418+
405419

406420
## SublimeLinter-eslint
407421

docs/rules/order.md

+4
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,8 @@ import sibling from './foo';
168168

169169
- [`import/external-module-folders`] setting
170170

171+
- [`import/internal-regex`] setting
172+
171173
[`import/external-module-folders`]: ../../README.md#importexternal-module-folders
174+
175+
[`import/internal-regex`]: ../../README.md#importinternal-regex

src/core/importType.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ export function isScopedMain(name) {
5454
}
5555

5656
function isInternalModule(name, settings, path) {
57+
const internalScope = (settings && settings['import/internal-regex'])
5758
const matchesScopedOrExternalRegExp = scopedRegExp.test(name) || externalModuleRegExp.test(name)
58-
return (matchesScopedOrExternalRegExp && !isExternalPath(path, name, settings))
59+
return (matchesScopedOrExternalRegExp && (internalScope && new RegExp(internalScope).test(name) || !isExternalPath(path, name, settings)))
5960
}
6061

6162
function isRelativeToParent(name) {

tests/src/core/importType.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('importType(name)', function () {
5151
const pathContext = testContext({ 'import/resolver': { node: { paths: [pathToTestFiles] } } })
5252
expect(importType('@importType/index', pathContext)).to.equal('internal')
5353
})
54-
54+
5555
it("should return 'internal' for internal modules that are referenced by aliases", function () {
5656
const pathContext = testContext({ 'import/resolver': { node: { paths: [pathToTestFiles] } } })
5757
expect(importType('@my-alias/fn', pathContext)).to.equal('internal')
@@ -130,6 +130,16 @@ describe('importType(name)', function () {
130130
expect(importType('resolve', foldersContext)).to.equal('internal')
131131
})
132132

133+
it("should return 'internal' for module from 'node_modules' if its name matched 'internal-regex'", function() {
134+
const foldersContext = testContext({ 'import/internal-regex': '^@org' })
135+
expect(importType('@org/foobar', foldersContext)).to.equal('internal')
136+
})
137+
138+
it("should return 'external' for module from 'node_modules' if its name did not match 'internal-regex'", function() {
139+
const foldersContext = testContext({ 'import/internal-regex': '^@bar' })
140+
expect(importType('@org/foobar', foldersContext)).to.equal('external')
141+
})
142+
133143
it("should return 'external' for module from 'node_modules' if 'node_modules' contained in 'external-module-folders'", function() {
134144
const foldersContext = testContext({ 'import/external-module-folders': ['node_modules'] })
135145
expect(importType('resolve', foldersContext)).to.equal('external')

0 commit comments

Comments
 (0)