eslint-plugin-simple-import-sort
Advanced tools
Comparing version 3.1.0 to 3.1.1
@@ -0,1 +1,6 @@ | ||
### Version 3.1.1 (2019-05-16) | ||
- Fixed: Semicolon-free code style is now supported. The plugin now leaves a | ||
semicolon at the start of a line of code after an import alone. | ||
### Version 3.1.0 (2019-03-30) | ||
@@ -2,0 +7,0 @@ |
{ | ||
"name": "eslint-plugin-simple-import-sort", | ||
"version": "3.1.0", | ||
"version": "3.1.1", | ||
"license": "MIT", | ||
@@ -38,3 +38,3 @@ "author": "Simon Lydell", | ||
"devDependencies": { | ||
"@typescript-eslint/parser": "1.5.0", | ||
"@typescript-eslint/parser": "1.9.0", | ||
"babel-eslint": "10.0.1", | ||
@@ -45,9 +45,9 @@ "cross-spawn": "6.0.5", | ||
"eslint-config-lydell": "14.0.0", | ||
"eslint-plugin-import": "2.16.0", | ||
"eslint-plugin-jest": "22.4.1", | ||
"eslint-plugin-prettier": "3.0.1", | ||
"eslint-plugin-import": "2.17.2", | ||
"eslint-plugin-jest": "22.5.1", | ||
"eslint-plugin-prettier": "3.1.0", | ||
"eslint-plugin-vue": "5.2.2", | ||
"jest": "24.5.0", | ||
"prettier": "1.16.4", | ||
"typescript": "3.3.4000" | ||
"jest": "24.8.0", | ||
"prettier": "1.17.1", | ||
"typescript": "3.4.5" | ||
}, | ||
@@ -54,0 +54,0 @@ "peerDependencies": { |
@@ -17,2 +17,5 @@ # eslint-plugin-simple-import-sort [![Build Status][travis-badge]][travis-link] | ||
This is for those who use `eslint --fix` (autofix) a lot and want to completely | ||
forget about sorting imports! | ||
## Contents | ||
@@ -36,2 +39,3 @@ | ||
- [The sorting autofix causes some odd whitespace!](#the-sorting-autofix-causes-some-odd-whitespace) | ||
- [Can I use this without autofix?](#can-i-use-this-without-autofix) | ||
- [Development](#development) | ||
@@ -147,3 +151,3 @@ - [npm scripts](#npm-scripts) | ||
This example uses the following extra plugins: | ||
This example uses the following extra (optional) plugins: | ||
@@ -181,2 +185,20 @@ - [eslint-plugin-prettier] | ||
- `simple-import-sort/sort` is turned on by default. | ||
- The standard [sort-imports] rule is turned off, in case you extend a config | ||
that includes it. | ||
- [prettier/prettier] runs [Prettier] inside ESLint and helps formatting your | ||
imports (and all other code) nicely. (autofixable) | ||
- [import/first] makes sure all imports are at the top of the file. | ||
(autofixable) | ||
- [import/newline-after-import] makes sure there’s a newline after the imports. | ||
(autofixable) | ||
- [import/no-duplicates] merges import statements of the same file. | ||
(autofixable, mostly) | ||
- For Node.js code, `simple-import-sort/sort` is turned off and replaced with | ||
[import/order] for sorting of `require` calls. | ||
With the above configuration, you don’t need to scroll to the top of the file to | ||
add another import. Just put it above your function! ESLint will then snap it | ||
into place (at the top of the file, in order, and without duplicates). | ||
## Sort order | ||
@@ -498,2 +520,12 @@ | ||
### Can I use this without autofix? | ||
Not really. The error message for this rule is literally “Run autofix to sort | ||
these imports!” Why? To actively encourage you to use autofix, and not waste | ||
time on manually doing something that the computer does a lot better. I’ve seen | ||
people painstakingly fixing cryptic (and annoying!) sorting errors from other | ||
rules one by one, not realizing they could have been autofixed. Finally, not | ||
trying to make more detailed messages makes the code of this plugin _much_ | ||
easier to work with. | ||
## Development | ||
@@ -541,2 +573,5 @@ | ||
[import/first]: https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/first.md | ||
[import/first]: https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/first.md | ||
[import/newline-after-import]: https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/newline-after-import.md | ||
[import/no-duplicates]: https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-duplicates.md | ||
[import/order]: https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/order.md | ||
@@ -543,0 +578,0 @@ [jest]: https://jestjs.io/ |
@@ -130,7 +130,8 @@ "use strict"; | ||
// Wrap the import nodes in `imports` in objects with more data about the | ||
// Wrap the import nodes in `passedImports` in objects with more data about the | ||
// import. Most importantly there’s a `code` property that contains the import | ||
// node as a string, with comments (if any). Finding the corresponding comments | ||
// is the hard part. | ||
function getImportItems(imports, sourceCode) { | ||
function getImportItems(passedImports, sourceCode) { | ||
const imports = handleLastSemicolon(passedImports, sourceCode); | ||
return imports.map((importNode, importIndex) => { | ||
@@ -201,2 +202,47 @@ const lastLine = | ||
// Parsers think that a semicolon after a statement belongs to that statement. | ||
// But in a semicolon-free code style it might belong to the next statement: | ||
// | ||
// import x from "x" | ||
// ;[].forEach() | ||
// | ||
// If the last import of a chunk ends with a semicolon, and that semicolon isn’t | ||
// located on the same line as the `from` string, adjust the import node to end | ||
// at the `from` string instead. | ||
// | ||
// In the above example, the import is adjusted to end after `"x"`. | ||
function handleLastSemicolon(imports, sourceCode) { | ||
const lastIndex = imports.length - 1; | ||
const lastImport = imports[lastIndex]; | ||
const [nextToLastToken, lastToken] = sourceCode.getLastTokens(lastImport, { | ||
count: 2, | ||
}); | ||
const lastIsSemicolon = isPunctuator(lastToken, ";"); | ||
if (!lastIsSemicolon) { | ||
return imports; | ||
} | ||
const semicolonBelongsToImport = | ||
nextToLastToken.loc.end.line === lastToken.loc.start.line || | ||
// If there’s no more code after the last import the semicolon has to belong | ||
// to the import, even if it is not on the same line. | ||
sourceCode.getTokenAfter(lastToken) == null; | ||
if (semicolonBelongsToImport) { | ||
return imports; | ||
} | ||
// Preserve the start position, but use the end position of the `from` string. | ||
const newLastImport = Object.assign({}, lastImport, { | ||
range: [lastImport.range[0], nextToLastToken.range[1]], | ||
loc: { | ||
start: lastImport.loc.start, | ||
end: nextToLastToken.loc.end, | ||
}, | ||
}); | ||
return imports.slice(0, lastIndex).concat(newLastImport); | ||
} | ||
function printSortedSpecifiers(importNode, sourceCode) { | ||
@@ -689,3 +735,4 @@ const allTokens = getAllTokens(importNode, sourceCode); | ||
// trying to compare anything else, `import {a, a} from "mod"` is a syntax | ||
// error anyway (but babel-eslint supports it). | ||
// error anyway (but babel-eslint kind of supports it). | ||
// istanbul ignore next | ||
itemA.index - itemB.index | ||
@@ -692,0 +739,0 @@ ); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
50666
761
588