Skip to content

Commit

Permalink
improve readme. use builtin-modules/static instead or resolve/lib/cor…
Browse files Browse the repository at this point in the history
…e for the list of builtins.
  • Loading branch information
Will Honey committed Mar 23, 2019
1 parent e6668a2 commit 0542318
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 27 deletions.
59 changes: 40 additions & 19 deletions readme.md
Expand Up @@ -7,11 +7,12 @@ This package was created to supplement the rules provided by [eslint-plugin-impo
# Rules

#### [`order-imports`]
Enforce a _configurable_ convention in module import order

Enforce a _configurable_ convention in module import order

```javascript
// Given ESLint Config
rules: {
rules: {
'import-helpers/order-imports': [
'warn',
{
Expand All @@ -31,11 +32,11 @@ import SiblingComponent from './SiblingComponent';
import lodash from 'lodash';
import SharedComponent from '@shared/components/SharedComponent';
import React from 'react';

// into
import lodash from 'lodash';
import React from 'react';

import SharedComponent from '@shared/components/SharedComponent';

import SiblingComponent from './SiblingComponent';
Expand All @@ -58,13 +59,25 @@ npm install eslint-plugin-import-helpers --save-dev

To add a rule, update your `.eslintrc.(yml|json|js)`:

```yaml
plugins:
- import-helpers

rules:
import-helpers/order-imports: [2, { groups: [ ... ] } ]
# etc...
```js
{
// .eslintrc.js
plugins: ['eslint-plugin-import-helpers'],
rules: {
'import-helpers/order-imports': [
'warn',
{ // example configuration
'newlines-between': 'always',
groups: [
['builtin', 'external', 'internal'],
'/^@shared/',
['parent', 'sibling', 'index'],
],
alphabetize: { order: 'asc', ignoreCase: true },
},
],
}
}
```

# Settings
Expand All @@ -73,26 +86,34 @@ rules:
#### `import/core-modules`

An array of additional modules to consider as "core" modules--modules that should
be considered resolved but have no path on the filesystem (ie `builtins`). Your resolver may
already define some of these (for example, the Node resolver knows about `fs` and
An array of additional modules to consider as core/builtin modules--modules that should
be considered resolved but have no path on the filesystem. Current, we are using the [node resolver](https://github.com/benmosher/eslint-plugin-import/tree/master/resolvers/node), which knows about `fs` and
`path`), so you need not redefine those.

For example, Electron exposes an `electron` module:

```js
import 'electron'; // without extra config, will be flagged as unresolved!
import 'electron'; // without extra config, will be flagged as an "internal" module
```

that would otherwise be unresolved. To avoid this, you may provide `electron` as a
core module:

```yaml
# .eslintrc.yml
settings:
import/core-modules: [electron]
```js
{
// .eslintrc.js
settings: {
'core-modules': ['electron']
},
plugins: [ ... ],
rules: { ... }
}
```

#### `import/external-module-folders`

An array of folders. Resolved modules only from those folders will be considered as "external". By default - `["node_modules"]`. Makes sense if you have configured your path or webpack to handle your internal paths differently and want to considered modules from some folders, for example `bower_components` or `jspm_modules`, as "external".

# TypeScript

To use this plugin with TypeScript, you must use the TypeScript parser for ESLint. See [@typescript-eslint/parser](https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/parser) for more details.
4 changes: 2 additions & 2 deletions src/util/import-type.js
@@ -1,5 +1,5 @@
const cond = require('lodash/cond');
const coreModules = require('resolve/lib/core');
const coreModules = require('builtin-modules/static');
const { join } = require('path');

const resolve = require('eslint-module-utils/resolve').default;
Expand All @@ -24,7 +24,7 @@ function isAbsolute(name) {
function isBuiltIn(name, settings) {
const base = baseModule(name);
const extras = (settings && settings['import/core-modules']) || [];
return coreModules[base] || extras.indexOf(base) > -1;
return coreModules.indexOf(base) > -1 || extras.indexOf(base) > -1;
}

function isExternalPath(path, name, settings) {
Expand Down
18 changes: 12 additions & 6 deletions test/rules/order-imports-2.js
Expand Up @@ -10,17 +10,23 @@ ruleTester.run('order', rule, {
// Default order using import
test({
code: `
import fs from 'fs';
import async, {foo1} from 'async';
import fs from 'fs';
import async, {foo1} from 'async';
import relParent1 from '../foo';
import relParent2, {foo2} from '../foo/bar';
import relParent3 from '@shared';
import sibling, {foo3} from './foo';
import relParent2, {foo2} from '../foo/bar';
import relParent3 from '@shared';
import sibling, {foo3} from './foo';
import index from './';`,
options: [
{
groups: ['builtin', 'external', 'parent', '/@shared/', 'sibling', 'index'],
alphabetize: { order: 'asc', ignoreCase: true }
alphabetize: { order: 'asc', ignoreCase: true },
'newlines-between': 'always'
}
]
}),
Expand Down

0 comments on commit 0542318

Please sign in to comment.