import/order: Enforce a convention in module import order
Enforce a convention in the order of require() / import statements. +(fixable) The --fix option on the [command line] automatically fixes problems reported by this rule.
With the groups option set to ["builtin", "external", "internal", "parent", "sibling", "index", "object", "type"] the order is as shown in the following example:
Unassigned imports are ignored, as the order they are imported in may be important.
Statements using the ES6 import syntax must appear before any require() statements.
Fail
Pass
Options
This rule supports the following options:
groups: [array]:
How groups are defined, and the order to respect. groups must be an array of string or [string]. The only allowed strings are: "builtin", "external", "internal", "unknown", "parent", "sibling", "index", "object", "type". The enforced order is the same as the order of each element in a group. Omitted types are implicitly grouped together as the last element. Example:
The default value is ["builtin", "external", "parent", "sibling", "index"].
You can set the options like this:
pathGroups: [array of objects]:
To be able to group by paths mostly needed with aliases pathGroups can be defined.
Properties of the objects
| property | required | type | description |
|---|---|---|---|
| pattern | x | string | minimatch pattern for the paths to be in this group (will not be used for builtins or externals) |
| patternOptions | object | options for minimatch, default: { nocomment: true } | |
| group | x | string | one of the allowed groups, the pathGroup will be positioned relative to this group |
| position | string | defines where around the group the pathGroup will be positioned, can be 'after' or 'before', if not provided pathGroup will be positioned like the group |
pathGroupsExcludedImportTypes: [array]:
This defines import types that are not handled by configured pathGroups. This is mostly needed when you want to handle path groups that look like external imports.
Example:
You can also use patterns(e.g., react, react-router-dom, etc).
Example:
The default value is ["builtin", "external"].
newlines-between: [ignore|always|always-and-inside-groups|never]:
Enforces or forbids new lines between import groups:
If set to
ignore, no errors related to new lines between import groups will be reported.If set to
always, at least one new line between each group will be enforced, and new lines inside a group will be forbidden. To prevent multiple lines between imports, coreno-multiple-empty-linesrule can be used.If set to
always-and-inside-groups, it will act likealwaysexcept newlines are allowed inside import groups.If set to
never, no new lines are allowed in the entire import section.
The default value is "ignore".
With the default group setting, the following will be invalid:
while those will be valid:
alphabetize: {order: asc|desc|ignore, caseInsensitive: true|false}:
Sort the order within each group in alphabetical manner based on import path:
order: useascto sort in ascending order, anddescto sort in descending order (default:ignore).caseInsensitive: usetrueto ignore case, andfalseto consider case (default:false).
Example setting:
This will fail the rule check:
While this will pass:
warnOnUnassignedImports: true|false:
default:
false
Warns when unassigned imports are out of order. These warning will not be fixed with --fix because unassigned imports are used for side-effects and changing the import of order of modules with side effects can not be done automatically in a way that is safe.
This will fail the rule check:
While this will pass:
Related
import/external-module-folderssettingimport/internal-regexsetting