eslint-plugin-turmag-special-rules 1.0.31 → 1.0.32
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/lib/index.ts +2 -4
- package/lib/rules/{add-vue-extension.js → add-vue-extension.ts} +6 -4
- package/lib/rules/import-entities-by-column-or-line.ts +1 -1
- package/lib/rules/{use-shortest-alias.js → use-shortest-alias.ts} +9 -5
- package/lib/rules/{variable-entities-by-column-or-line.js → variable-entities-by-column-or-line.ts} +16 -5
- package/package.json +1 -1
- package/lib/rules/import-right-order.js +0 -110
package/lib/index.ts
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import addVueExtension from './rules/add-vue-extension.js';
|
|
2
2
|
import importEntitesByColumnOrLine from './rules/import-entities-by-column-or-line';
|
|
3
|
-
import importRightOrder from './rules/import-right-order.js';
|
|
4
3
|
import preferTrueAttributeShorthand from './rules/prefer-true-attribute-shorthand';
|
|
5
|
-
import useShortestAlias from './rules/use-shortest-alias
|
|
6
|
-
import variableEntitiesByColumnOrLine from './rules/variable-entities-by-column-or-line
|
|
4
|
+
import useShortestAlias from './rules/use-shortest-alias';
|
|
5
|
+
import variableEntitiesByColumnOrLine from './rules/variable-entities-by-column-or-line';
|
|
7
6
|
|
|
8
7
|
export const rules = {
|
|
9
8
|
'add-vue-extension': addVueExtension,
|
|
10
9
|
'import-entities-by-column-or-line': importEntitesByColumnOrLine,
|
|
11
|
-
'import-right-order': importRightOrder,
|
|
12
10
|
'prefer-true-attribute-shorthand': preferTrueAttributeShorthand,
|
|
13
11
|
'use-shortest-alias': useShortestAlias,
|
|
14
12
|
'variable-entities-by-column-or-line': variableEntitiesByColumnOrLine,
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import { TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
|
|
1
3
|
const path = require('node:path');
|
|
2
4
|
const fs = require('node:fs');
|
|
3
5
|
|
|
4
|
-
|
|
6
|
+
export default {
|
|
5
7
|
meta: {
|
|
6
8
|
fixable: 'code',
|
|
7
9
|
type: 'suggestion',
|
|
@@ -16,9 +18,9 @@ module.exports = {
|
|
|
16
18
|
properties: { aliases: { type: 'object' } },
|
|
17
19
|
}],
|
|
18
20
|
},
|
|
19
|
-
create(context) {
|
|
21
|
+
create(context: { options: { aliases: { [key: string]: string; } | ArrayLike<string>; }[]; getFilename: () => string; report: (arg0: { node: unknown; messageId: string; fix: (fixer: any) => any; }) => void; }) {
|
|
20
22
|
return {
|
|
21
|
-
ImportDeclaration(node) {
|
|
23
|
+
ImportDeclaration(node: TSESTree.ImportDeclaration) {
|
|
22
24
|
const aliases = Object.entries(context.options[0].aliases);
|
|
23
25
|
if (!aliases.length) return;
|
|
24
26
|
|
|
@@ -33,7 +35,7 @@ module.exports = {
|
|
|
33
35
|
const filePath = path.resolve(basedir, nodeName);
|
|
34
36
|
|
|
35
37
|
const vueExt = '.vue';
|
|
36
|
-
const findRealExtension = filePath => {
|
|
38
|
+
const findRealExtension = (filePath: string) => {
|
|
37
39
|
let realExt = fs.existsSync(filePath) ? path.extname(filePath) : null;
|
|
38
40
|
if (realExt === null) realExt = fs.existsSync(`${filePath}${vueExt}`) ? path.extname(`${filePath}${vueExt}`) : null;
|
|
39
41
|
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
import { TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
import { Rule } from 'eslint';
|
|
3
|
+
|
|
4
|
+
export default {
|
|
2
5
|
meta: {
|
|
3
6
|
fixable: 'code',
|
|
4
7
|
type: 'suggestion',
|
|
@@ -13,10 +16,11 @@ module.exports = {
|
|
|
13
16
|
properties: { aliases: { type: 'object' } },
|
|
14
17
|
}],
|
|
15
18
|
},
|
|
19
|
+
// @ts-expect-error context type
|
|
16
20
|
create(context) {
|
|
17
21
|
return {
|
|
18
|
-
ImportDeclaration(node) {
|
|
19
|
-
const aliases = Object.entries(context.options[0].aliases);
|
|
22
|
+
ImportDeclaration(node: TSESTree.ImportDeclaration) {
|
|
23
|
+
const aliases = Object.entries(context.options[0].aliases) as unknown as string[];
|
|
20
24
|
if (!aliases.length) return;
|
|
21
25
|
|
|
22
26
|
const nodeName = node.source.value;
|
|
@@ -41,12 +45,12 @@ module.exports = {
|
|
|
41
45
|
context.report({
|
|
42
46
|
node,
|
|
43
47
|
messageId: 'shortest',
|
|
44
|
-
fix: fixer => {
|
|
48
|
+
fix: (fixer: Rule.RuleFixer) => {
|
|
45
49
|
let replaceText = '';
|
|
46
50
|
if (node.specifiers[0].type === 'ImportDefaultSpecifier')
|
|
47
51
|
replaceText = `import ${node.specifiers[0].local.name} from '${resultNodeName}';`;
|
|
48
52
|
else {
|
|
49
|
-
const specifiersArr = [];
|
|
53
|
+
const specifiersArr: string[] = [];
|
|
50
54
|
node.specifiers.forEach(specifier => specifiersArr.push(specifier.local.name));
|
|
51
55
|
|
|
52
56
|
const replaceSign = specifiersArr.length > 2 ? '\n' : ' ';
|
package/lib/rules/{variable-entities-by-column-or-line.js → variable-entities-by-column-or-line.ts}
RENAMED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
import { Rule } from 'eslint';
|
|
3
|
+
|
|
1
4
|
export default {
|
|
2
5
|
meta: {
|
|
3
6
|
fixable: 'code',
|
|
@@ -12,15 +15,17 @@ export default {
|
|
|
12
15
|
properties: { minProperties: { type: 'number' } },
|
|
13
16
|
}],
|
|
14
17
|
},
|
|
18
|
+
// @ts-expect-error context type
|
|
15
19
|
create(context) {
|
|
16
20
|
return {
|
|
17
|
-
VariableDeclaration(node) {
|
|
21
|
+
VariableDeclaration(node: TSESTree.VariableDeclaration) {
|
|
18
22
|
if (!node.declarations[0]) return;
|
|
19
23
|
if (node.declarations[0].id.type !== 'ObjectPattern') return;
|
|
20
24
|
if (!node.declarations[0].init) return;
|
|
21
25
|
const minProperties = context.options[0].minProperties;
|
|
22
26
|
const kind = node.kind;
|
|
23
27
|
const declaration = node.declarations[0];
|
|
28
|
+
// @ts-expect-error properties type
|
|
24
29
|
const properties = declaration.id.properties;
|
|
25
30
|
const sourceCode = context.sourceCode;
|
|
26
31
|
const rightSideText = sourceCode.getText(declaration.init);
|
|
@@ -28,6 +33,7 @@ export default {
|
|
|
28
33
|
let areLinesRepeated = false;
|
|
29
34
|
|
|
30
35
|
let isObjectDestructured = false;
|
|
36
|
+
// @ts-expect-error property type
|
|
31
37
|
properties.forEach(property => {
|
|
32
38
|
if (property.value?.type === 'ObjectPattern') isObjectDestructured = true;
|
|
33
39
|
});
|
|
@@ -37,15 +43,18 @@ export default {
|
|
|
37
43
|
if (properties.length < minProperties) {
|
|
38
44
|
if (properties[0].loc.start.line !== properties[0].parent.loc.start.line) areSmallAttributesInColumn = true;
|
|
39
45
|
} else {
|
|
40
|
-
|
|
46
|
+
// @ts-expect-error property type
|
|
47
|
+
properties.every((property, i: number) => {
|
|
41
48
|
if (i === 0) return true;
|
|
42
49
|
if (property.loc.start.line === property.loc.end.line && properties[i - 1].loc.start.line === property.loc.start.line) areLinesRepeated = true;
|
|
43
50
|
else return !areLinesRepeated;
|
|
44
51
|
});
|
|
45
52
|
}
|
|
46
53
|
|
|
54
|
+
// @ts-expect-error properties type
|
|
47
55
|
const getPropertiesArr = properties => {
|
|
48
|
-
const propertiesArr = [];
|
|
56
|
+
const propertiesArr: string[] = [];
|
|
57
|
+
// @ts-expect-error property type
|
|
49
58
|
properties.forEach(property => {
|
|
50
59
|
if (property.type === 'RestElement') {
|
|
51
60
|
propertiesArr.push(`...${property.argument.name}`);
|
|
@@ -72,9 +81,10 @@ export default {
|
|
|
72
81
|
context.report({
|
|
73
82
|
node,
|
|
74
83
|
messageId: 'column',
|
|
75
|
-
fix: fixer => {
|
|
84
|
+
fix: (fixer: Rule.RuleFixer) => {
|
|
76
85
|
const propertiesArr = getPropertiesArr(properties);
|
|
77
86
|
const replaceShiftSign = '\n ';
|
|
87
|
+
// @ts-expect-error node type
|
|
78
88
|
return fixer.replaceText(node, `${kind} {${replaceShiftSign}${propertiesArr.join(`,${replaceShiftSign}`)},\n} = ${rightSideText};`);
|
|
79
89
|
},
|
|
80
90
|
});
|
|
@@ -82,9 +92,10 @@ export default {
|
|
|
82
92
|
context.report({
|
|
83
93
|
node,
|
|
84
94
|
messageId: 'line',
|
|
85
|
-
fix: fixer => {
|
|
95
|
+
fix: (fixer: Rule.RuleFixer) => {
|
|
86
96
|
const propertiesArr = getPropertiesArr(properties);
|
|
87
97
|
const replaceShiftSign = ' ';
|
|
98
|
+
// @ts-expect-error node type
|
|
88
99
|
return fixer.replaceText(node, `${kind} {${replaceShiftSign}${propertiesArr.join(`,${replaceShiftSign}`)} } = ${rightSideText};`);
|
|
89
100
|
},
|
|
90
101
|
});
|
package/package.json
CHANGED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* DEPRECATED!
|
|
3
|
-
* It's better to use rule sort-imports with custom groups from plugin eslint-plugin-perfectionist
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
const entitiesOrder = ['external', 'vue', 'component', 'composable', 'store', 'mixin', 'type', 'constant', 'method', 'api'];
|
|
7
|
-
const determineEntity = (value, specifiers) => {
|
|
8
|
-
let entity = 'external';
|
|
9
|
-
value = value.toLowerCase();
|
|
10
|
-
|
|
11
|
-
if (value === 'vue') entity = 'vue';
|
|
12
|
-
else if (value.includes('mixin')) entity = 'mixin';
|
|
13
|
-
else if (value.includes('.vue') || value.includes('kit')) entity = 'component';
|
|
14
|
-
else if (value.includes('composable') || specifiers && specifiers[0]?.local.name.toLowerCase().includes('use')) entity = 'composable';
|
|
15
|
-
else if (value.includes('store')) entity = 'store';
|
|
16
|
-
else if (value.includes('type')) entity = 'type';
|
|
17
|
-
else if (value.includes('constant')) entity = 'constant';
|
|
18
|
-
else if (value.includes('helper') || value.includes('utils')) entity = 'method';
|
|
19
|
-
else if (value.includes('api')) entity = 'api';
|
|
20
|
-
|
|
21
|
-
return entity;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
module.exports = {
|
|
25
|
-
meta: {
|
|
26
|
-
fixable: 'code',
|
|
27
|
-
type: 'suggestion',
|
|
28
|
-
docs: {
|
|
29
|
-
description: 'Prefered right import order',
|
|
30
|
-
},
|
|
31
|
-
messages: {
|
|
32
|
-
order: 'Use right import order',
|
|
33
|
-
},
|
|
34
|
-
|
|
35
|
-
schema: [],
|
|
36
|
-
},
|
|
37
|
-
create(context) {
|
|
38
|
-
let prevNode = null;
|
|
39
|
-
const nodesArr = [];
|
|
40
|
-
let isErrorOrderNode = false;
|
|
41
|
-
let startReplaceRange = 0;
|
|
42
|
-
let endReplaceRange = 0;
|
|
43
|
-
|
|
44
|
-
return {
|
|
45
|
-
ImportDeclaration(node) {
|
|
46
|
-
nodesArr.push(node);
|
|
47
|
-
|
|
48
|
-
if (prevNode) {
|
|
49
|
-
const prevNodeEntity = determineEntity(prevNode.source.value, prevNode.specifiers);
|
|
50
|
-
const currentNodeEntity = determineEntity(node.source.value, node.specifiers);
|
|
51
|
-
|
|
52
|
-
const prevNodeIndex = entitiesOrder.indexOf(prevNodeEntity);
|
|
53
|
-
const currentNodeIndex = entitiesOrder.indexOf(currentNodeEntity);
|
|
54
|
-
|
|
55
|
-
prevNode.sortIndex = prevNodeIndex;
|
|
56
|
-
node.sortIndex = currentNodeIndex;
|
|
57
|
-
|
|
58
|
-
if (prevNodeIndex > currentNodeIndex && !isErrorOrderNode) {
|
|
59
|
-
isErrorOrderNode = true;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
prevNode = node;
|
|
64
|
-
|
|
65
|
-
if (isErrorOrderNode) {
|
|
66
|
-
context.report({
|
|
67
|
-
node,
|
|
68
|
-
messageId: 'order',
|
|
69
|
-
fix: fixer => {
|
|
70
|
-
const sortedNodesArr = structuredClone(nodesArr).sort((a, b) => a.sortIndex - b.sortIndex);
|
|
71
|
-
|
|
72
|
-
const replacedNodesArr = [];
|
|
73
|
-
sortedNodesArr.forEach((node, i) => {
|
|
74
|
-
if (replacedNodesArr.length) replacedNodesArr.push(node);
|
|
75
|
-
if (node.source.value !== nodesArr[i].source.value && !replacedNodesArr.length) {
|
|
76
|
-
replacedNodesArr.push(node);
|
|
77
|
-
startReplaceRange = nodesArr[i].range[0];
|
|
78
|
-
}
|
|
79
|
-
if (i === sortedNodesArr.length - 1) endReplaceRange = nodesArr[i].range[1];
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
let replaceText = '';
|
|
83
|
-
|
|
84
|
-
replaceText = '';
|
|
85
|
-
replacedNodesArr.forEach((node, i) => {
|
|
86
|
-
if (!node.specifiers[0]) {
|
|
87
|
-
replaceText += `import '${node.source.value}';`;
|
|
88
|
-
} else if (node.specifiers[0].type === 'ImportDefaultSpecifier') {
|
|
89
|
-
replaceText += `import ${node.specifiers[0].local.name} from '${node.source.value}';`;
|
|
90
|
-
} else if (node.specifiers[0].type === 'ImportNamespaceSpecifier') {
|
|
91
|
-
replaceText += `import * as ${node.specifiers[0].local.name} from '${node.source.value}';`;
|
|
92
|
-
} else {
|
|
93
|
-
const specifiersArr = [];
|
|
94
|
-
node.specifiers.forEach(specifier => specifiersArr.push(specifier.local.name));
|
|
95
|
-
|
|
96
|
-
const replaceSign = specifiersArr.length > 2 ? '\n' : ' ';
|
|
97
|
-
const replaceShiftSign = specifiersArr.length > 2 ? '\n ' : ' ';
|
|
98
|
-
replaceText += `import {${replaceShiftSign}${specifiersArr.join(`,${replaceShiftSign}`)}${specifiersArr.length > 2 ? ',' : ''}${replaceSign}} from '${node.source.value}';`;
|
|
99
|
-
}
|
|
100
|
-
if (i < replacedNodesArr.length - 1) replaceText += '\n';
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
return fixer.replaceTextRange([startReplaceRange, endReplaceRange], replaceText);
|
|
104
|
-
},
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
},
|
|
108
|
-
};
|
|
109
|
-
},
|
|
110
|
-
}
|