eslint-plugin-grouped-import 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -0
- package/lib/grouped-imports.js +20 -8
- package/package.json +3 -4
package/README.md
CHANGED
|
@@ -42,6 +42,52 @@ However, the rule checks if there exists a similar, more specific path in option
|
|
|
42
42
|
If so, the import node will be sorted into the group with the more specific path.
|
|
43
43
|
**Note**: imports with extensions, i.e. `.css`, will **ALWAYS** take precedence.
|
|
44
44
|
|
|
45
|
+
If there is a relative option path in the config, the same precedence rule applies.
|
|
46
|
+
Extension imports will be sorted into extension groups first, then the imports will be sorted according to the option paths.
|
|
47
|
+
|
|
48
|
+
For example, with the config
|
|
49
|
+
```json
|
|
50
|
+
{
|
|
51
|
+
"relative": [
|
|
52
|
+
{
|
|
53
|
+
"path": "./"
|
|
54
|
+
}
|
|
55
|
+
],
|
|
56
|
+
"landing": [
|
|
57
|
+
{
|
|
58
|
+
"path": "./landing"
|
|
59
|
+
}
|
|
60
|
+
],
|
|
61
|
+
"images": [
|
|
62
|
+
{
|
|
63
|
+
"path": ".png"
|
|
64
|
+
}
|
|
65
|
+
]
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
and imports
|
|
70
|
+
```js
|
|
71
|
+
import Layout from './landing/Layout';
|
|
72
|
+
import banner from './landing/banner.png';
|
|
73
|
+
import data from './data.json';
|
|
74
|
+
import landingData from './landing/data.json';
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
the plugin will group the imports thus:
|
|
78
|
+
```js
|
|
79
|
+
// landing
|
|
80
|
+
import Layout from './landing/Layout';
|
|
81
|
+
import landingData from './landing/data.json'
|
|
82
|
+
|
|
83
|
+
// images
|
|
84
|
+
import banner from './landing/banner.png';
|
|
85
|
+
|
|
86
|
+
// relative
|
|
87
|
+
import data from './data.json';
|
|
88
|
+
;
|
|
89
|
+
```
|
|
90
|
+
|
|
45
91
|
The rule checks for 7 specific things which are described in the *Rule examples* section
|
|
46
92
|
|
|
47
93
|
## Rule examples
|
package/lib/grouped-imports.js
CHANGED
|
@@ -3,6 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ruleMessages = void 0;
|
|
6
7
|
const lodash_1 = __importDefault(require("lodash"));
|
|
7
8
|
exports.ruleMessages = {
|
|
8
9
|
noComments: 'Imports must be accompanied by comments',
|
|
@@ -226,17 +227,28 @@ const getImportsByGroup = (options, allOptionsPaths, importNodes) => {
|
|
|
226
227
|
return lodash_1.default.reduce(options, (acc, option, key) => {
|
|
227
228
|
const groupPaths = lodash_1.default.map(option, o => o.path);
|
|
228
229
|
const filteredImports = lodash_1.default.filter(importNodes, (node) => {
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
const similarOptionValue = lodash_1.default.find(allOptionsPaths, optionValue => lodash_1.default.includes(optionValue, groupPath) && optionValue !== groupPath);
|
|
230
|
+
const isCurrentNodeInGroupAndPath = (lodash_1.default.includes(allOptionsPaths, node.source.value) && !lodash_1.default.includes(groupPaths, node.source.value));
|
|
231
|
+
return !isCurrentNodeInGroupAndPath && lodash_1.default.some(groupPaths, (groupPath) => {
|
|
232
232
|
const importValue = node.source.value;
|
|
233
|
-
const
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
233
|
+
const groupedImport = lodash_1.default.includes(importValue, groupPath);
|
|
234
|
+
// check if there's a more specific path in option values
|
|
235
|
+
const moreSpecificOptionPath = lodash_1.default.find(allOptionsPaths, optionValue => lodash_1.default.includes(optionValue, groupPath) && optionValue !== groupPath && lodash_1.default.includes(importValue, optionValue));
|
|
236
|
+
const importWithExtension = (/\.\w/gi).test(importValue);
|
|
237
|
+
if (importWithExtension && groupedImport) {
|
|
238
|
+
// check if there's a more specific option path with extension
|
|
239
|
+
const moreSpecificExtensionPath = lodash_1.default.find(allOptionsPaths, (optionValue) => {
|
|
240
|
+
// if the option path has an extension, check if it corresponds to the import value extension
|
|
241
|
+
const isExtensionOptionPath = (/\.\w/gi).test(optionValue);
|
|
242
|
+
const notCurrentGroupPath = groupPath !== optionValue;
|
|
243
|
+
const importValInOptionPath = lodash_1.default.includes(importValue, optionValue);
|
|
244
|
+
return isExtensionOptionPath && notCurrentGroupPath && importValInOptionPath;
|
|
245
|
+
});
|
|
246
|
+
return groupedImport && !Boolean(moreSpecificExtensionPath) && !Boolean(moreSpecificOptionPath);
|
|
247
|
+
}
|
|
248
|
+
return groupedImport && !Boolean(moreSpecificOptionPath);
|
|
237
249
|
});
|
|
238
250
|
});
|
|
239
|
-
return Object.assign({}, acc, { [key]: filteredImports });
|
|
251
|
+
return Object.assign(Object.assign({}, acc), { [key]: filteredImports });
|
|
240
252
|
}, {});
|
|
241
253
|
};
|
|
242
254
|
const composeNewCodeLines = (lines, lasCommentLine, lastImportLine) => (newLines, index, excludeLineNumbers) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-grouped-import",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "Group imports based on the import path",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -25,14 +25,13 @@
|
|
|
25
25
|
"repository": "github:kairome/eslint-plugin-grouped-import",
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@types/eslint": "^4.16.6",
|
|
28
|
-
"@types/lodash": "^4.14.155",
|
|
29
28
|
"@types/node": "^11.10.4",
|
|
30
29
|
"@typescript-eslint/parser": "^1.4.2",
|
|
31
30
|
"eslint": "^7.32.0",
|
|
32
31
|
"jest": "^23.6.0",
|
|
33
|
-
"lodash": "^4.17.
|
|
32
|
+
"lodash": "^4.17.21",
|
|
34
33
|
"ts-jest": "^23.10.4",
|
|
35
|
-
"typescript": "
|
|
34
|
+
"typescript": "^4.9.5"
|
|
36
35
|
},
|
|
37
36
|
"peerDependencies": {
|
|
38
37
|
"eslint": "^5.0.0 || ^7.0.0"
|