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

Allow unassigned imports #37

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion docs/rules/order-imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import main from './';

Notes:

- Unassigned imports are ignored (ex: `import 'polyfill'`), as the order they are imported in may be important.
- Unassigned imports are ignored (ex: `import 'polyfill'`), as the order they are imported in may be important. Use 'unassignedImports' option if you'd like to allow them.
- Statements using the ES6 `import` syntax must appear before any `require()` statements.

## Usage
Expand Down Expand Up @@ -149,6 +149,60 @@ import bar from 'bar';
import Baz from 'Baz';
```

### `unassignedImports: [ignore|allow] (default: ignore)`

Unassigned imports refers to imports which are not assigned to any variable but are imported globally.

Example:
```js
import 'polyfill'
import 'styles.scss'
```

By default unassigned imports are ignored, as the order they are imported in may be important.

- If set to `allow`, considers unassigned imports like any other imports when ordering.
- If set to `ignore`, does not consider the ordering for this import.

Examples:

#### ignore
```js
/* eslint import-helpers/order-imports: [
"error",
{
unassignedImports: 'ignore',
groups: [['module'], '/\.scss$/']
},
] */

/* Any placement of 'styles.scss' is VALID */
import 'styles.scss';
import fs from 'fs';
import path from 'path';
```

#### allow
```js
/* eslint import-helpers/order-imports: [
"error",
{
unassignedImports: 'allow',
groups: [['module'], '/\.scss$/']
},
] */

/* INVALID */
import 'styles.scss'
import fs from 'fs';
import path from 'path';

/* VALID */
import fs from 'fs';
import path from 'path';
import 'styles.scss'
```

## Upgrading from v0.14 to v1

### `builtin` | `external` | `internal` → `module`
Expand Down
10 changes: 9 additions & 1 deletion src/rules/order-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ const alphabetizeOptions: AlphabetizeOption[] = ['ignore', 'asc', 'desc'];
type Groups = (ValidImportType | ValidImportType[])[];
const defaultGroups: Groups = ['absolute', 'module', 'parent', 'sibling', 'index'];

type UnassignedImportsOption = 'allow' | 'ignore';
const unassignedImportsOption: UnassignedImportsOption[] = ['allow', 'ignore'];

type RuleOptions = {
groups?: Groups;
newlinesBetween?: NewLinesBetweenOption;
alphabetize?: Partial<AlphabetizeConfig>;
unassignedImports?: UnassignedImportsOption;
};

type ImportType = 'require' | 'import';
Expand Down Expand Up @@ -496,6 +500,9 @@ module.exports = {
newlinesBetween: {
enum: newLinesBetweenOptions,
},
unassignedImports: {
enum: unassignedImportsOption,
},
alphabetize: {
type: 'object',
properties: {
Expand All @@ -518,6 +525,7 @@ module.exports = {
create: function importOrderRule(context) {
const options: RuleOptions = context.options[0] || {};
const newlinesBetweenImports: NewLinesBetweenOption = options.newlinesBetween || 'ignore';
const unassignedImports = options.unassignedImports || 'ignore';

let alphabetize: AlphabetizeConfig;
let ranks: Ranks;
Expand All @@ -543,7 +551,7 @@ module.exports = {

return {
ImportDeclaration: function handleImports(node) {
if (node.specifiers.length) {
if (node.specifiers.length || unassignedImports === 'allow') {
// Ignoring unassigned imports
const name: string = node.source.value;
registerNode(node, name, 'import', ranks, regExpGroups, imported);
Expand Down
26 changes: 26 additions & 0 deletions test/rules/order-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -1471,5 +1471,31 @@ comment3 */", // the spacing here is really sensitive
},
],
}),
// Option unassignedImports: 'allow' should consider unassigned imports when sorting for respective groups
test({
code: `
import './an-unassigned-relative';
import path from 'path';
import _ from './relative';
import 'an-unassigned-module';
`,
output: `
import './an-unassigned-relative';
import path from 'path';
import _ from './relative';
import 'an-unassigned-module';
`,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does eslint compare this output after running '--fix' against this plugin. If so, shouldn't this be re-ordered because when running locally against a file, the fixer does re-order as per the errors mentioned below.

Probably I'm missing something here?

options: [{ unassignedImports: 'allow' }],
errors: [
{
line: 3,
message: '`path` import should occur before import of `./an-unassigned-relative`',
},
{
line: 5,
message: '`an-unassigned-module` import should occur before import of `./an-unassigned-relative`',
},
],
}),
],
});