eslint-plugin-grouped-import 1.0.4 → 2.0.0

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 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
@@ -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',
@@ -228,16 +229,26 @@ const getImportsByGroup = (options, allOptionsPaths, importNodes) => {
228
229
  const filteredImports = lodash_1.default.filter(importNodes, (node) => {
229
230
  const isCurrentNodeInGroupAndPath = (lodash_1.default.includes(allOptionsPaths, node.source.value) && !lodash_1.default.includes(groupPaths, node.source.value));
230
231
  return !isCurrentNodeInGroupAndPath && lodash_1.default.some(groupPaths, (groupPath) => {
231
- // check if there's a more specific path in option values
232
- const similarOptionValue = lodash_1.default.find(allOptionsPaths, optionValue => lodash_1.default.includes(optionValue, groupPath) && optionValue !== groupPath);
233
232
  const importValue = node.source.value;
234
- const regularImport = lodash_1.default.includes(importValue, groupPath);
235
- const similarImport = lodash_1.default.includes(importValue, similarOptionValue) ||
236
- (/\.\w/gi.test(importValue) && !/\.\w/gi.test(groupPath));
237
- return regularImport && !similarImport;
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);
238
249
  });
239
250
  });
240
- return Object.assign({}, acc, { [key]: filteredImports });
251
+ return Object.assign(Object.assign({}, acc), { [key]: filteredImports });
241
252
  }, {});
242
253
  };
243
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.4",
3
+ "version": "2.0.0",
4
4
  "description": "Group imports based on the import path",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -24,18 +24,18 @@
24
24
  ],
25
25
  "repository": "github:kairome/eslint-plugin-grouped-import",
26
26
  "devDependencies": {
27
- "@types/eslint": "^4.16.6",
28
- "@types/lodash": "^4.14.155",
29
- "@types/node": "^11.10.4",
30
- "@typescript-eslint/parser": "^1.4.2",
31
- "eslint": "^7.32.0",
32
- "jest": "^23.6.0",
33
- "lodash": "^4.17.15",
34
- "ts-jest": "^23.10.4",
35
- "typescript": "~3.4.0"
27
+ "@types/eslint": "^9.6.1",
28
+ "@types/lodash": "^4.14.195",
29
+ "@types/node": "^20.0.0",
30
+ "@typescript-eslint/parser": "^7.0.0",
31
+ "eslint": "^9.0.0",
32
+ "jest": "^29.0.0",
33
+ "lodash": "^4.17.21",
34
+ "ts-jest": "^29.0.0",
35
+ "typescript": "^5.0.0"
36
36
  },
37
37
  "peerDependencies": {
38
- "eslint": "^5.0.0 || ^7.0.0"
38
+ "eslint": "^9.0.0"
39
39
  },
40
40
  "jest": {
41
41
  "transform": {