@plumeria/eslint-plugin 0.1.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/LICENSE +21 -0
- package/lib/index.d.ts +18 -0
- package/lib/index.js +47 -0
- package/lib/rules/no-inner-call.js +69 -0
- package/lib/rules/no-unused-keys.js +96 -0
- package/lib/rules/sort-properties.js +126 -0
- package/lib/rules/validate-values.js +2251 -0
- package/lib/util/colorData.js +211 -0
- package/lib/util/place.js +291 -0
- package/lib/util/propertyGroups.js +557 -0
- package/lib/util/unitData.js +42 -0
- package/lib/util/validData.js +1052 -0
- package/package.json +26 -0
- package/readme.md +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) zss-in-js contributer
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ESLint, Linter, Rule } from 'eslint';
|
|
2
|
+
|
|
3
|
+
declare const plugin: ESLint.Plugin & {
|
|
4
|
+
rules: {
|
|
5
|
+
'no-inner-call': Rule.RuleModule;
|
|
6
|
+
'no-unused-keys': Rule.RuleModule;
|
|
7
|
+
'sort-properties': Rule.RuleModule;
|
|
8
|
+
'validate-values': Rule.RuleModule;
|
|
9
|
+
};
|
|
10
|
+
configs: {
|
|
11
|
+
recommended: Linter.LegacyConfig;
|
|
12
|
+
};
|
|
13
|
+
flatConfigs: {
|
|
14
|
+
recommended: Linter.Config<Linter.RulesRecord>;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export = plugin;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const noInnerCall = require('./rules/no-inner-call.js');
|
|
4
|
+
const noUnusedKeys = require('./rules/no-unused-keys.js');
|
|
5
|
+
const sortProperties = require('./rules/sort-properties.js');
|
|
6
|
+
const validateValues = require('./rules/validate-values.js');
|
|
7
|
+
|
|
8
|
+
const rules = {
|
|
9
|
+
'no-inner-call': noInnerCall,
|
|
10
|
+
'no-unused-keys': noUnusedKeys,
|
|
11
|
+
'sort-properties': sortProperties,
|
|
12
|
+
'validate-values': validateValues,
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const configs = {
|
|
16
|
+
recommended: {
|
|
17
|
+
plugins: ['@plumeria'],
|
|
18
|
+
rules: {
|
|
19
|
+
'@plumeria/no-inner-call': 'error',
|
|
20
|
+
'@plumeria/no-unused-keys': 'warn',
|
|
21
|
+
'@plumeria/sort-properties': 'warn',
|
|
22
|
+
'@plumeria/validate-values': 'warn',
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const flatConfigs = {
|
|
28
|
+
recommended: {
|
|
29
|
+
plugins: {
|
|
30
|
+
'@plumeria': {
|
|
31
|
+
rules,
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
rules: {
|
|
35
|
+
'@plumeria/no-inner-call': 'error',
|
|
36
|
+
'@plumeria/no-unused-keys': 'warn',
|
|
37
|
+
'@plumeria/sort-properties': 'warn',
|
|
38
|
+
'@plumeria/validate-values': 'warn',
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
module.exports = {
|
|
44
|
+
rules,
|
|
45
|
+
configs,
|
|
46
|
+
flatConfigs,
|
|
47
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Restrict calls inside a function
|
|
3
|
+
* Compatible with eslint 8 and below or 9 and above
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
'use strict';
|
|
7
|
+
|
|
8
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
|
9
|
+
module.exports = {
|
|
10
|
+
meta: {
|
|
11
|
+
type: 'problem',
|
|
12
|
+
docs: {
|
|
13
|
+
description: 'An error occurs if a specific call is made within a function',
|
|
14
|
+
recommended: true,
|
|
15
|
+
},
|
|
16
|
+
messages: {
|
|
17
|
+
noInnerCall: 'Do not use {{name}} inside functions',
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
create(context) {
|
|
21
|
+
let functionDepth = 0;
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
FunctionDeclaration() {
|
|
25
|
+
functionDepth++;
|
|
26
|
+
},
|
|
27
|
+
FunctionExpression() {
|
|
28
|
+
functionDepth++;
|
|
29
|
+
},
|
|
30
|
+
ArrowFunctionExpression() {
|
|
31
|
+
functionDepth++;
|
|
32
|
+
},
|
|
33
|
+
'FunctionDeclaration:exit'() {
|
|
34
|
+
functionDepth--;
|
|
35
|
+
},
|
|
36
|
+
'FunctionExpression:exit'() {
|
|
37
|
+
functionDepth--;
|
|
38
|
+
},
|
|
39
|
+
'ArrowFunctionExpression:exit'() {
|
|
40
|
+
functionDepth--;
|
|
41
|
+
},
|
|
42
|
+
CallExpression(node) {
|
|
43
|
+
if (functionDepth > 0) {
|
|
44
|
+
// メンバー式(MemberExpression)の場合の処理
|
|
45
|
+
if (node.callee.type === 'MemberExpression') {
|
|
46
|
+
const objectName = node.callee.object.name;
|
|
47
|
+
const propertyName = node.callee.property.name;
|
|
48
|
+
|
|
49
|
+
if (objectName === 'css') {
|
|
50
|
+
const fullName = `${objectName}.${propertyName}`;
|
|
51
|
+
if (
|
|
52
|
+
propertyName === 'create' ||
|
|
53
|
+
propertyName === 'global' ||
|
|
54
|
+
propertyName === 'keyframes' ||
|
|
55
|
+
propertyName === 'defineThemeVars'
|
|
56
|
+
) {
|
|
57
|
+
context.report({
|
|
58
|
+
node,
|
|
59
|
+
messageId: 'noInnerCall',
|
|
60
|
+
data: { name: fullName },
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
},
|
|
69
|
+
};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Warns about unused keys in objects inside functions
|
|
3
|
+
* Compatible with eslint 8 and below or 9 and above
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
'use strict';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @param {import('eslint').Rule.RuleContext} context
|
|
10
|
+
* @returns {import('eslint').Filename}
|
|
11
|
+
*/
|
|
12
|
+
function getFilename(context) {
|
|
13
|
+
return context.getFilename ? context.getFilename() : context.filename;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
|
17
|
+
module.exports = {
|
|
18
|
+
meta: {
|
|
19
|
+
type: 'problem',
|
|
20
|
+
docs: {
|
|
21
|
+
description: 'Detect unused object keys if they are not referenced anywhere',
|
|
22
|
+
recommended: true,
|
|
23
|
+
},
|
|
24
|
+
messages: {
|
|
25
|
+
unusedKey: "The key '{{ key }}' is defined but never referenced anywhere.",
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
|
|
29
|
+
create(context) {
|
|
30
|
+
const filename = getFilename(context);
|
|
31
|
+
if (filename.endsWith('.ts')) {
|
|
32
|
+
return {};
|
|
33
|
+
}
|
|
34
|
+
const parserServices = context.parserServices;
|
|
35
|
+
const checker = parserServices?.program?.getTypeChecker();
|
|
36
|
+
const definedKeys = new Map();
|
|
37
|
+
const referencedKeys = new Set();
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
// Filter out `css.global` and find objects in functions
|
|
41
|
+
CallExpression(node) {
|
|
42
|
+
if (
|
|
43
|
+
node.callee.type === 'MemberExpression' &&
|
|
44
|
+
node.callee.object.name === 'css' &&
|
|
45
|
+
node.callee.property.name !== 'global' &&
|
|
46
|
+
node.callee.property.name !== 'defineThemeVars' &&
|
|
47
|
+
node.callee.property.name !== 'keyframes'
|
|
48
|
+
) {
|
|
49
|
+
const arg = node.arguments[0];
|
|
50
|
+
if (arg && arg.type === 'ObjectExpression' && node.parent.type === 'VariableDeclarator') {
|
|
51
|
+
const variableName = node.parent.id.name;
|
|
52
|
+
const keyMap = new Map();
|
|
53
|
+
|
|
54
|
+
arg.properties.forEach((prop) => {
|
|
55
|
+
if (prop.key && prop.key.type === 'Identifier' && prop.value.type === 'ObjectExpression') {
|
|
56
|
+
keyMap.set(prop.key.name, prop.key);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
definedKeys.set(variableName, keyMap);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
// `styles.header_main` の参照を記録
|
|
65
|
+
MemberExpression(node) {
|
|
66
|
+
if (node.object.type === 'Identifier' && node.property.type === 'Identifier') {
|
|
67
|
+
const fullKey = `${node.object.name}.${node.property.name}`;
|
|
68
|
+
referencedKeys.add(fullKey);
|
|
69
|
+
|
|
70
|
+
// TypeScript の型情報を取得し、ジャンプ可能かチェック
|
|
71
|
+
if (parserServices && checker) {
|
|
72
|
+
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
|
|
73
|
+
const symbol = checker.getSymbolAtLocation(tsNode);
|
|
74
|
+
if (symbol) {
|
|
75
|
+
referencedKeys.add(fullKey);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
'Program:exit'() {
|
|
81
|
+
definedKeys.forEach((keyMap, variableName) => {
|
|
82
|
+
keyMap.forEach((keyNode, keyName) => {
|
|
83
|
+
const fullKey = `${variableName}.${keyName}`;
|
|
84
|
+
if (!referencedKeys.has(fullKey)) {
|
|
85
|
+
context.report({
|
|
86
|
+
node: keyNode,
|
|
87
|
+
messageId: 'unusedKey',
|
|
88
|
+
data: { key: keyName },
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
},
|
|
96
|
+
};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview CSS properties recess base reorder fix feature
|
|
3
|
+
* Compatible with eslint 8 and below or 9 and above
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
'use strict';
|
|
7
|
+
|
|
8
|
+
const propertyGroups = require('../util/propertyGroups');
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param {import('eslint').Rule.RuleContext} context
|
|
12
|
+
* @returns {import('eslint').SourceCode}
|
|
13
|
+
*/
|
|
14
|
+
function getSourceCode(context) {
|
|
15
|
+
return context.getSourceCode ? context.getSourceCode() : context.sourceCode;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function getPropertyName(property) {
|
|
19
|
+
if (property.key.type === 'Literal' && Array.isArray(property.key.value)) {
|
|
20
|
+
return property.key.value;
|
|
21
|
+
}
|
|
22
|
+
return property.key.name || property.key.value || '';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function getPropertyIndex(property, isTopLevel = false) {
|
|
26
|
+
const name = getPropertyName(property);
|
|
27
|
+
|
|
28
|
+
if (
|
|
29
|
+
isTopLevel &&
|
|
30
|
+
(property.key.type !== 'Identifier' || name.startsWith('&') || name.startsWith(':') || name.startsWith('@'))
|
|
31
|
+
) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let lastGroupIndex = 0;
|
|
36
|
+
let maxPropIndex = 0;
|
|
37
|
+
|
|
38
|
+
for (let i = 0; i < propertyGroups.length; i++) {
|
|
39
|
+
const group = propertyGroups[i];
|
|
40
|
+
const propIndex = group.properties.indexOf(name);
|
|
41
|
+
|
|
42
|
+
if (propIndex !== -1) {
|
|
43
|
+
return i * 1000 + propIndex;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
lastGroupIndex = i;
|
|
47
|
+
maxPropIndex = Math.max(maxPropIndex, group.properties.length);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (typeof name === 'string') {
|
|
51
|
+
if (name.startsWith('&')) return (propertyGroups.length + 1) * 1000;
|
|
52
|
+
if (name.includes(':')) return (propertyGroups.length + 2) * 1000;
|
|
53
|
+
if (name.includes('@media')) return (propertyGroups.length + 3) * 1000;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return lastGroupIndex * 1000 + maxPropIndex + 1;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
module.exports = {
|
|
60
|
+
meta: {
|
|
61
|
+
type: 'suggestion',
|
|
62
|
+
docs: {
|
|
63
|
+
description: 'Sort CSS properties keeping original order for specific keys',
|
|
64
|
+
recommended: true,
|
|
65
|
+
},
|
|
66
|
+
fixable: 'code',
|
|
67
|
+
schema: [],
|
|
68
|
+
messages: {
|
|
69
|
+
sortProperties: 'Property "{{property}}" should be at position {{position}}',
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
create(context) {
|
|
74
|
+
return {
|
|
75
|
+
ObjectExpression(node) {
|
|
76
|
+
const sourceCode = getSourceCode(context);
|
|
77
|
+
const isTopLevel = !node.parent || node.parent.type !== 'Property';
|
|
78
|
+
const properties = node.properties.filter((prop) => prop.key);
|
|
79
|
+
|
|
80
|
+
const sorted = [...properties].sort((a, b) => {
|
|
81
|
+
const indexA = getPropertyIndex(a, isTopLevel);
|
|
82
|
+
const indexB = getPropertyIndex(b, isTopLevel);
|
|
83
|
+
return indexA === null || indexB === null ? 0 : indexA - indexB;
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const misordered = properties.filter((prop, i) => prop !== sorted[i]);
|
|
87
|
+
|
|
88
|
+
if (misordered.length === 0) return;
|
|
89
|
+
|
|
90
|
+
const match = sourceCode.getText(node).match(/^{\s*\n(\s*)/);
|
|
91
|
+
const indent = match ? match[1] : '';
|
|
92
|
+
const lineEnding = match ? '\n' : ' ';
|
|
93
|
+
|
|
94
|
+
misordered.forEach((prop) => {
|
|
95
|
+
context.report({
|
|
96
|
+
node: prop,
|
|
97
|
+
messageId: 'sortProperties',
|
|
98
|
+
data: {
|
|
99
|
+
position: sorted.indexOf(prop) + 1,
|
|
100
|
+
property: getPropertyName(prop),
|
|
101
|
+
},
|
|
102
|
+
fix(fixer) {
|
|
103
|
+
const newText = sorted
|
|
104
|
+
.map((p) => {
|
|
105
|
+
if (Array.isArray(getPropertyName(p))) {
|
|
106
|
+
const arrayKey = sourceCode.getText(p.key);
|
|
107
|
+
const arrayContent = p.value.properties
|
|
108
|
+
.map((inner) => `${indent} ${sourceCode.getText(inner)}`)
|
|
109
|
+
.join(`,${lineEnding}`);
|
|
110
|
+
return `${indent}${arrayKey}: {\n${arrayContent}\n${indent}}`;
|
|
111
|
+
}
|
|
112
|
+
return `${indent}${sourceCode.getText(p)}`;
|
|
113
|
+
})
|
|
114
|
+
.join(`,${lineEnding}`);
|
|
115
|
+
|
|
116
|
+
return fixer.replaceTextRange(
|
|
117
|
+
[node.range[0] + 1, node.range[1] - 1],
|
|
118
|
+
`${lineEnding}${newText}${lineEnding}`
|
|
119
|
+
);
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
},
|
|
126
|
+
};
|