designsnitch 0.4.0 → 0.6.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.ko.md +76 -26
- package/README.md +76 -26
- package/bin/designsnitch.js +5 -3
- package/package.json +2 -2
- package/skills/designsnitch-check/SKILL.md +9 -8
- package/skills/designsnitch-hooks/SKILL.md +21 -3
- package/skills/designsnitch-palette/SKILL.md +3 -3
- package/skills/designsnitch-setup/SKILL.md +10 -7
- package/src/check.js +83 -36
- package/src/cli.js +3 -4
- package/src/colors.js +16 -13
- package/src/config.js +1 -0
- package/src/core/classes.js +13 -21
- package/src/core/color.js +133 -9
- package/src/core/css.js +38 -10
- package/src/core/named-colors.js +35 -0
- package/src/core/oklch.js +58 -0
- package/src/core/tokens.js +96 -13
- package/src/format.js +2 -2
- package/src/hook.js +62 -11
- package/src/install.js +6 -0
- package/src/messages.js +46 -14
- package/src/rules/color-unknown.js +3 -2
- package/src/rules/color-use-token.js +43 -15
- package/src/rules/load-tokens.js +195 -12
- package/src/rules/tailwind-config.js +77 -0
- package/src/rules/unchecked-colors.js +28 -0
- package/src/rules/visit-style.js +82 -20
- package/src/rules/unchecked-hex.js +0 -25
package/src/rules/visit-style.js
CHANGED
|
@@ -1,36 +1,98 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { findColorValues, isColorProperty } from "../core/color.js";
|
|
2
|
+
|
|
3
|
+
// 색을 받는 이름. CSS 의 색 속성이거나 Color 로 끝난다. 예: backgroundColor, stroke, pointHoverBackgroundColor
|
|
4
|
+
const isColorKey = (name) => isColorProperty(name) || /color$/i.test(name);
|
|
5
|
+
const STYLE_NAME = /styles?$/i;
|
|
6
|
+
const STYLE_TYPE = /CSSProperties/;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 노드가 색을 값으로 받는 자리에 있는지 본다.
|
|
10
|
+
* - style: 스타일로 쓰이는 객체의 값. JSX 의 style 속성, 이름이 style 로 끝나는 변수, CSSProperties 타입의 객체.
|
|
11
|
+
* 여기서는 var(--토큰) 이 통한다.
|
|
12
|
+
* - data: 그 밖에 색을 받는 이름의 속성이나 JSX 속성. 예: 차트 옵션의 backgroundColor, <Line stroke="...">.
|
|
13
|
+
* 색인 것은 분명하지만 var() 가 통하는지는 알 수 없다.
|
|
14
|
+
* @param {import("estree").Node & { parent?: any }} node 문자열이나 템플릿 리터럴
|
|
15
|
+
* @param {import("eslint").SourceCode} [sourceCode] 주면 타입 표기(CSSProperties)도 본다
|
|
16
|
+
* @returns {{ property: string, kind: "style" | "data" } | null}
|
|
17
|
+
*/
|
|
18
|
+
export function colorContext(node, sourceCode) {
|
|
19
|
+
// color: on ? "#fff" : "#000", color: x || "#fff" 처럼 값이 고르는 식이면 그 식을 거슬러 올라간다.
|
|
20
|
+
while (
|
|
21
|
+
(node.parent?.type === "ConditionalExpression" && node.parent.test !== node) ||
|
|
22
|
+
node.parent?.type === "LogicalExpression"
|
|
23
|
+
) {
|
|
24
|
+
node = node.parent;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const holder = node.parent?.type === "JSXExpressionContainer" ? node.parent.parent : node.parent;
|
|
28
|
+
if (holder?.type === "JSXAttribute") {
|
|
29
|
+
const name = holder.name?.name;
|
|
30
|
+
return typeof name === "string" && isColorKey(name) ? { property: name, kind: "data" } : null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const property = node.parent;
|
|
34
|
+
if (property?.type !== "Property" || property.value !== node || property.computed) return null;
|
|
35
|
+
const { key } = property;
|
|
36
|
+
const name = key.type === "Identifier" ? key.name : typeof key.value === "string" ? key.value : null;
|
|
37
|
+
if (name === null || property.parent?.type !== "ObjectExpression") return null;
|
|
38
|
+
|
|
39
|
+
const style = styleObjectKind(property.parent, sourceCode);
|
|
40
|
+
if (style === "jsx") return { property: name, kind: "style" };
|
|
41
|
+
if (!isColorKey(name)) return null;
|
|
42
|
+
return { property: name, kind: style ? "style" : "data" };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** 객체가 스타일로 쓰이는지. JSX 의 style 속성이면 "jsx", 이름이나 타입으로 알 수 있으면 "named", 아니면 null. */
|
|
46
|
+
function styleObjectKind(object, sourceCode) {
|
|
47
|
+
const typed = (annotation) => Boolean(annotation && sourceCode && STYLE_TYPE.test(sourceCode.getText(annotation)));
|
|
48
|
+
for (let node = object; node; node = node.parent) {
|
|
49
|
+
const parent = node.parent;
|
|
50
|
+
if (!parent) return null;
|
|
51
|
+
if (parent.type === "TSAsExpression" || parent.type === "TSSatisfiesExpression") {
|
|
52
|
+
if (typed(parent.typeAnnotation)) return "named";
|
|
53
|
+
} else if (parent.type === "JSXExpressionContainer") {
|
|
54
|
+
const attribute = parent.parent;
|
|
55
|
+
return attribute?.type === "JSXAttribute" && attribute.name?.name === "style" ? "jsx" : null;
|
|
56
|
+
} else if (parent.type === "Property" && parent.value === node && parent.parent?.type === "ObjectExpression") {
|
|
57
|
+
// styles = { title: { color: ... } } 처럼 스타일 객체 안에 들어 있는 객체
|
|
58
|
+
node = parent;
|
|
59
|
+
} else if (parent.type === "VariableDeclarator" && parent.init === node) {
|
|
60
|
+
const { id } = parent;
|
|
61
|
+
if (id.type !== "Identifier") return null;
|
|
62
|
+
return STYLE_NAME.test(id.name) || typed(id.typeAnnotation) ? "named" : null;
|
|
63
|
+
} else {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
2
69
|
|
|
3
70
|
/**
|
|
4
|
-
* 노드가
|
|
71
|
+
* 노드가 스타일 객체의 값이면 CSS 속성 이름을, 아니면 null 을 돌려준다.
|
|
5
72
|
* 예: <div style={{ color: "#fff" }} /> 의 "#fff" → "color"
|
|
6
73
|
* @param {import("estree").Node & { parent?: any }} node
|
|
7
74
|
* @returns {string | null}
|
|
8
75
|
*/
|
|
9
76
|
export function inlineStyleProperty(node) {
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
const object = property.parent;
|
|
13
|
-
if (object?.type !== "ObjectExpression" || object.parent?.type !== "JSXExpressionContainer") return null;
|
|
14
|
-
const attribute = object.parent.parent;
|
|
15
|
-
if (attribute?.type !== "JSXAttribute" || attribute.name?.name !== "style") return null;
|
|
16
|
-
const { key } = property;
|
|
17
|
-
if (key.type === "Identifier") return key.name;
|
|
18
|
-
return typeof key.value === "string" ? key.value : null;
|
|
77
|
+
const context = colorContext(node);
|
|
78
|
+
return context?.kind === "style" ? context.property : null;
|
|
19
79
|
}
|
|
20
80
|
|
|
21
81
|
/**
|
|
22
|
-
*
|
|
82
|
+
* 색을 값으로 받는 자리에 문자열로 적은 색마다 onFound 를 부르는 visitor 를 만든다.
|
|
23
83
|
* @param {import("eslint").Rule.RuleContext} context
|
|
24
|
-
* @param {(found: { text: string, value: string, property: string }, where: { loc: import("estree").SourceLocation, range: [number, number] }) => void} onFound
|
|
84
|
+
* @param {(found: { text: string, value: string, property: string, kind: "style" | "data" }, where: { loc: import("estree").SourceLocation, range: [number, number] }) => void} onFound
|
|
25
85
|
*/
|
|
26
86
|
export function visitInlineStyleColors(context, onFound) {
|
|
27
87
|
const { sourceCode } = context;
|
|
28
88
|
|
|
29
|
-
function scan(node, property) {
|
|
30
|
-
|
|
89
|
+
function scan(node, { property, kind }) {
|
|
90
|
+
// 색 이름은 스타일의 색 속성에서만 색으로 본다. <Badge color="red"> 의 red 는 변형 이름일 수 있다.
|
|
91
|
+
const named = kind === "style" && isColorProperty(property);
|
|
92
|
+
for (const found of findColorValues(sourceCode.getText(node), { named })) {
|
|
31
93
|
const start = node.range[0] + found.index;
|
|
32
94
|
const end = start + found.text.length;
|
|
33
|
-
onFound({ ...found, property }, {
|
|
95
|
+
onFound({ ...found, property, kind }, {
|
|
34
96
|
loc: { start: sourceCode.getLocFromIndex(start), end: sourceCode.getLocFromIndex(end) },
|
|
35
97
|
range: [start, end],
|
|
36
98
|
});
|
|
@@ -40,13 +102,13 @@ export function visitInlineStyleColors(context, onFound) {
|
|
|
40
102
|
return {
|
|
41
103
|
Literal(node) {
|
|
42
104
|
if (typeof node.value !== "string") return;
|
|
43
|
-
const
|
|
44
|
-
if (
|
|
105
|
+
const where = colorContext(node, sourceCode);
|
|
106
|
+
if (where) scan(node, where);
|
|
45
107
|
},
|
|
46
108
|
TemplateLiteral(node) {
|
|
47
|
-
const
|
|
109
|
+
const where = colorContext(node, sourceCode);
|
|
48
110
|
// ${} 안의 코드는 다른 노드가 맡는다. 문자열 부분만 본다.
|
|
49
|
-
if (
|
|
111
|
+
if (where) for (const quasi of node.quasis) scan(quasi, where);
|
|
50
112
|
},
|
|
51
113
|
};
|
|
52
114
|
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { findArbitraryColors, findHexColors } from "../core/classes.js";
|
|
2
|
-
import { visitStrings } from "./visit-strings.js";
|
|
3
|
-
import { inlineStyleProperty } from "./visit-style.js";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* 검사 규칙이 보지 않는 문자열 속 hex 색을 알린다. 위반이 아니라 check 가 개수를 세는 데 쓴다.
|
|
7
|
-
* 임의 값 클래스(bg-[#fff])와 인라인 스타일의 hex 는 다른 규칙이 검사하므로 빼고 센다.
|
|
8
|
-
* @type {import("eslint").Rule.RuleModule}
|
|
9
|
-
*/
|
|
10
|
-
export default {
|
|
11
|
-
meta: { type: "suggestion", schema: [], messages: { unchecked: "{{color}}" } },
|
|
12
|
-
create(context) {
|
|
13
|
-
const findUnchecked = (text) => {
|
|
14
|
-
const inClasses = findArbitraryColors(text).map(({ index, text: found }) => [index, index + found.length]);
|
|
15
|
-
return findHexColors(text).filter(({ index }) => !inClasses.some(([start, end]) => index >= start && index < end));
|
|
16
|
-
};
|
|
17
|
-
const visitor = visitStrings(context, findUnchecked, (found, { loc }) =>
|
|
18
|
-
context.report({ loc, messageId: "unchecked", data: { color: found.value } }),
|
|
19
|
-
);
|
|
20
|
-
const skipStyle = (handler) => (node) => {
|
|
21
|
-
if (inlineStyleProperty(node) === null && inlineStyleProperty(node.parent ?? {}) === null) handler(node);
|
|
22
|
-
};
|
|
23
|
-
return { Literal: skipStyle(visitor.Literal), TemplateElement: skipStyle(visitor.TemplateElement) };
|
|
24
|
-
},
|
|
25
|
-
};
|