Skip to content
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

[core]: regex marking packages as internal #1491

Merged
merged 1 commit into from Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel

## [Unreleased]

### Added
- [`internal-regex`]: regex pattern for marking packages "internal" ([#1491], thanks [@Librazy])

### Added
- [`group-exports`]: make aggregate module exports valid ([#1472], thanks [@atikenny])
- [`no-namespace`]: Make rule fixable ([#1401], thanks [@TrevorBurnham])
Expand Down
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -402,6 +402,20 @@ settings:
[`eslint_d`]: https://www.npmjs.com/package/eslint_d
[`eslint-loader`]: https://www.npmjs.com/package/eslint-loader

#### `import/internal-regex`

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.

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.

For example, if you pacakges in a monorepo are all in `@scope`, you can configure `import/internal-regex` like this

```yaml
# .eslintrc.yml
settings:
import/internal-regex: ^@scope/
```


## SublimeLinter-eslint

Expand Down
4 changes: 4 additions & 0 deletions docs/rules/order.md
Expand Up @@ -168,4 +168,8 @@ import sibling from './foo';

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

- [`import/internal-regex`] setting

[`import/external-module-folders`]: ../../README.md#importexternal-module-folders

[`import/internal-regex`]: ../../README.md#importinternal-regex
3 changes: 2 additions & 1 deletion src/core/importType.js
Expand Up @@ -54,8 +54,9 @@ export function isScopedMain(name) {
}

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

function isRelativeToParent(name) {
Expand Down
12 changes: 11 additions & 1 deletion tests/src/core/importType.js
Expand Up @@ -51,7 +51,7 @@ describe('importType(name)', function () {
const pathContext = testContext({ 'import/resolver': { node: { paths: [pathToTestFiles] } } })
expect(importType('@importType/index', pathContext)).to.equal('internal')
})

it("should return 'internal' for internal modules that are referenced by aliases", function () {
const pathContext = testContext({ 'import/resolver': { node: { paths: [pathToTestFiles] } } })
expect(importType('@my-alias/fn', pathContext)).to.equal('internal')
Expand Down Expand Up @@ -130,6 +130,16 @@ describe('importType(name)', function () {
expect(importType('resolve', foldersContext)).to.equal('internal')
})

it("should return 'internal' for module from 'node_modules' if its name matched 'internal-regex'", function() {
const foldersContext = testContext({ 'import/internal-regex': '^@org' })
expect(importType('@org/foobar', foldersContext)).to.equal('internal')
})

it("should return 'external' for module from 'node_modules' if its name did not match 'internal-regex'", function() {
const foldersContext = testContext({ 'import/internal-regex': '^@bar' })
expect(importType('@org/foobar', foldersContext)).to.equal('external')
})

it("should return 'external' for module from 'node_modules' if 'node_modules' contained in 'external-module-folders'", function() {
const foldersContext = testContext({ 'import/external-module-folders': ['node_modules'] })
expect(importType('resolve', foldersContext)).to.equal('external')
Expand Down