eslint-plugin-turmag-special-rules 0.0.1 → 0.0.3
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 +9 -7
- package/lib/rules/add-vue-extension.js +56 -0
- package/lib/rules/use-shortest-alias.js +64 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -24,15 +24,15 @@ pnpm add eslint-plugin-turmag-special-rules --save-dev
|
|
|
24
24
|
|
|
25
25
|
## Usage
|
|
26
26
|
|
|
27
|
-
In your [configuration file](https://eslint.org/docs/latest/use/configure/configuration-files#configuration-file), import the plugin `eslint-plugin-turmag-special-rules` and add `
|
|
27
|
+
In your [configuration file](https://eslint.org/docs/latest/use/configure/configuration-files#configuration-file), import the plugin `eslint-plugin-turmag-special-rules` and add `turmagEslintSpecialRules` to the `plugins` key:
|
|
28
28
|
|
|
29
29
|
```js
|
|
30
|
-
import
|
|
30
|
+
import turmagEslintSpecialRules from "eslint-plugin-turmag-special-rules";
|
|
31
31
|
|
|
32
32
|
export default [
|
|
33
33
|
{
|
|
34
34
|
plugins: {
|
|
35
|
-
|
|
35
|
+
turmagEslintSpecialRules
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
];
|
|
@@ -42,15 +42,15 @@ export default [
|
|
|
42
42
|
Then configure the rules you want to use under the `rules` key.
|
|
43
43
|
|
|
44
44
|
```js
|
|
45
|
-
import
|
|
45
|
+
import turmagEslintSpecialRules from "eslint-plugin-turmag-special-rules";
|
|
46
46
|
|
|
47
47
|
export default [
|
|
48
48
|
{
|
|
49
49
|
plugins: {
|
|
50
|
-
|
|
50
|
+
turmagEslintSpecialRules
|
|
51
51
|
},
|
|
52
52
|
rules: {
|
|
53
|
-
"
|
|
53
|
+
"turmagEslintSpecialRules/rule-name": "warn"
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
];
|
|
@@ -74,7 +74,9 @@ TODO: Run eslint-doc-generator to generate the configs list (or delete this sect
|
|
|
74
74
|
|
|
75
75
|
| Name | Description | 🔧 |
|
|
76
76
|
| :------------------------------------------------------------------------------- | :------------------------------------------------------------- | :- |
|
|
77
|
-
| [
|
|
77
|
+
| [add-vue-extension](docs/rules/add-vue-extension.md) | require .vue in vue files | 🔧 |
|
|
78
|
+
| [prefer-true-attribute-shorthand](docs/rules/prefer-true-attribute-shorthand.md) | require shorthand form attribute when `v-bind` value is `true` | 🔧 |
|
|
79
|
+
| [use-shortest-alias](docs/rules/use-shortest-alias.md) | There are can be used shortest alias | 🔧 |
|
|
78
80
|
|
|
79
81
|
<!-- end auto-generated rules list -->
|
|
80
82
|
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
const path = require('node:path');
|
|
2
|
+
const fs = require('node:fs');
|
|
3
|
+
|
|
4
|
+
module.exports = {
|
|
5
|
+
meta: {
|
|
6
|
+
fixable: 'code',
|
|
7
|
+
type: 'suggestion',
|
|
8
|
+
docs: {
|
|
9
|
+
description: 'require .vue in vue files',
|
|
10
|
+
},
|
|
11
|
+
messages: {
|
|
12
|
+
proper: 'Use proper import of vue files',
|
|
13
|
+
},
|
|
14
|
+
schema: [{
|
|
15
|
+
type: 'object',
|
|
16
|
+
properties: { aliases: { type: 'object' } },
|
|
17
|
+
}],
|
|
18
|
+
},
|
|
19
|
+
create(context) {
|
|
20
|
+
return {
|
|
21
|
+
ImportDeclaration(node) {
|
|
22
|
+
const aliases = Object.entries(context.options[0].aliases);
|
|
23
|
+
if (!aliases.length) return;
|
|
24
|
+
|
|
25
|
+
const basedir = path.dirname(path.resolve(context.getFilename()));
|
|
26
|
+
let nodeName = node.source.value;
|
|
27
|
+
|
|
28
|
+
aliases.every(([key, value]) => {
|
|
29
|
+
if (nodeName.includes(key)) nodeName = nodeName.replace(key, value);
|
|
30
|
+
else return nodeName === node.source.value;
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const filePath = path.resolve(basedir, nodeName);
|
|
34
|
+
|
|
35
|
+
const vueExt = '.vue';
|
|
36
|
+
const findRealExtension = filePath => {
|
|
37
|
+
let realExt = fs.existsSync(filePath) ? path.extname(filePath) : null;
|
|
38
|
+
if (realExt === null) realExt = fs.existsSync(`${filePath}${vueExt}`) ? path.extname(`${filePath}${vueExt}`) : null;
|
|
39
|
+
|
|
40
|
+
return realExt;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const nodeNameExt = path.extname(nodeName);
|
|
44
|
+
const realExt = findRealExtension(filePath);
|
|
45
|
+
|
|
46
|
+
if (realExt === vueExt && realExt !== nodeNameExt) {
|
|
47
|
+
context.report({
|
|
48
|
+
node,
|
|
49
|
+
messageId: 'proper',
|
|
50
|
+
fix: fixer => fixer.replaceText(node, node.specifiers.map(specifier => `import ${specifier.local.name} from '${node.source.value}${realExt}';`).join('\n')),
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
meta: {
|
|
3
|
+
fixable: 'code',
|
|
4
|
+
type: 'suggestion',
|
|
5
|
+
docs: {
|
|
6
|
+
description: 'There are can be used shortest alias',
|
|
7
|
+
},
|
|
8
|
+
messages: {
|
|
9
|
+
shortest: 'Use shortest alias',
|
|
10
|
+
},
|
|
11
|
+
schema: [{
|
|
12
|
+
type: 'object',
|
|
13
|
+
properties: { aliases: { type: 'object' } },
|
|
14
|
+
}],
|
|
15
|
+
},
|
|
16
|
+
create(context) {
|
|
17
|
+
return {
|
|
18
|
+
ImportDeclaration(node) {
|
|
19
|
+
const aliases = Object.entries(context.options[0].aliases);
|
|
20
|
+
if (!aliases.length) return;
|
|
21
|
+
|
|
22
|
+
const nodeName = node.source.value;
|
|
23
|
+
let nodeNameWithoutAlias = nodeName;
|
|
24
|
+
let resultNodeName = nodeName;
|
|
25
|
+
|
|
26
|
+
aliases.every(([key, value]) => {
|
|
27
|
+
if (nodeNameWithoutAlias.includes(key)) nodeNameWithoutAlias = nodeNameWithoutAlias.replace(key, value);
|
|
28
|
+
else return nodeNameWithoutAlias === nodeName;
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
nodeNameWithoutAlias = nodeNameWithoutAlias.replace(/\\/ig, '/');
|
|
32
|
+
|
|
33
|
+
aliases.every(([key, value]) => {
|
|
34
|
+
value = value.replace(/\\/ig, '/');
|
|
35
|
+
if (nodeNameWithoutAlias.includes(value))
|
|
36
|
+
resultNodeName = nodeNameWithoutAlias.replace(value, key).replace(/\\/ig, '/');
|
|
37
|
+
else return resultNodeName === nodeName;
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
if (nodeName !== resultNodeName) {
|
|
41
|
+
context.report({
|
|
42
|
+
node,
|
|
43
|
+
messageId: 'shortest',
|
|
44
|
+
fix: fixer => {
|
|
45
|
+
let replaceText = '';
|
|
46
|
+
if (node.specifiers[0].type === 'ImportDefaultSpecifier')
|
|
47
|
+
replaceText = `import ${node.specifiers[0].local.name} from '${resultNodeName}';`;
|
|
48
|
+
else {
|
|
49
|
+
const specifiersArr = [];
|
|
50
|
+
node.specifiers.forEach(specifier => specifiersArr.push(specifier.local.name));
|
|
51
|
+
|
|
52
|
+
const replaceSign = specifiersArr.length > 2 ? '\n' : ' ';
|
|
53
|
+
const replaceShiftSign = specifiersArr.length > 2 ? '\n ' : ' ';
|
|
54
|
+
replaceText = `import {${replaceShiftSign}${specifiersArr.join(`,${replaceShiftSign}`)}${specifiersArr.length > 2 ? ',' : ''}${replaceSign}} from '${resultNodeName}';`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return fixer.replaceText(node, replaceText);
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
},
|
|
64
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-turmag-special-rules",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "Special eslint rules for your awesome projects",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"eslint",
|
|
@@ -24,8 +24,8 @@
|
|
|
24
24
|
"requireindex": "^1.2.0"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"eslint": "^9.0.0",
|
|
28
27
|
"@eslint/js": "^9.0.0",
|
|
28
|
+
"eslint": "^9.0.0",
|
|
29
29
|
"eslint-doc-generator": "^1.0.0",
|
|
30
30
|
"eslint-plugin-eslint-plugin": "^6.0.0",
|
|
31
31
|
"eslint-plugin-n": "^17.0.0",
|