designsnitch 0.4.0 → 0.5.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 +58 -23
- package/README.md +58 -23
- package/package.json +1 -1
- package/skills/designsnitch-check/SKILL.md +9 -8
- package/skills/designsnitch-palette/SKILL.md +3 -3
- package/skills/designsnitch-setup/SKILL.md +10 -7
- package/src/check.js +66 -34
- 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 +34 -5
- package/src/format.js +2 -2
- package/src/messages.js +38 -14
- package/src/rules/color-unknown.js +2 -2
- package/src/rules/color-use-token.js +15 -8
- package/src/rules/load-tokens.js +195 -12
- package/src/rules/tailwind-config.js +77 -0
- package/src/rules/{unchecked-hex.js → unchecked-colors.js} +5 -4
- package/src/rules/visit-style.js +10 -3
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { normalizeColor } from "../core/color.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @config 로 불러오는 Tailwind JavaScript 설정에서 색을 읽는다. Tailwind v4 의 동작을 따른다.
|
|
6
|
+
* - theme.extend.colors 는 색을 더한다. theme.colors 는 기본 팔레트를 대체한다.
|
|
7
|
+
* - 프리셋(presets)의 색도 같은 방식으로 합친다.
|
|
8
|
+
* - 이 색들은 클래스(bg-brand-500)로만 쓸 수 있다. Tailwind 가 CSS 변수를 만들지 않기 때문이다.
|
|
9
|
+
* 설정 파일을 실행하므로, 그 파일이 require 하는 것이 설치되어 있어야 한다.
|
|
10
|
+
* @param {string} file 설정 파일의 절대 경로
|
|
11
|
+
* @returns {{ colors: Map<string, string>, replacesPalette: boolean }} colors 는 토큰 이름(--color-brand-500) → 비교용 색 표기
|
|
12
|
+
* @throws 설정을 불러오지 못했을 때
|
|
13
|
+
*/
|
|
14
|
+
export function loadConfigColors(file) {
|
|
15
|
+
const require = createRequire(file);
|
|
16
|
+
const resolved = require.resolve(file);
|
|
17
|
+
// 에디터처럼 오래 떠 있는 프로세스에서도 바뀐 설정을 다시 읽는다. ESM 설정은 Node 가 캐시를 비워 주지 않는다.
|
|
18
|
+
delete require.cache[resolved];
|
|
19
|
+
const loaded = require(resolved);
|
|
20
|
+
|
|
21
|
+
let palette = {};
|
|
22
|
+
try {
|
|
23
|
+
const module = require("tailwindcss/colors");
|
|
24
|
+
palette = module.default ?? module;
|
|
25
|
+
} catch {
|
|
26
|
+
// tailwindcss/colors 를 쓰지 않는 설정이면 없어도 된다.
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const result = { colors: new Map(), replacesPalette: false };
|
|
30
|
+
collect(loaded?.default ?? loaded, palette, result);
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function collect(config, palette, result) {
|
|
35
|
+
if (!config || typeof config !== "object") return;
|
|
36
|
+
for (const preset of Array.isArray(config.presets) ? config.presets : []) collect(preset?.default ?? preset, palette, result);
|
|
37
|
+
|
|
38
|
+
const section = (value) => {
|
|
39
|
+
if (typeof value !== "function") return value;
|
|
40
|
+
try {
|
|
41
|
+
return value({ colors: palette, theme: () => undefined });
|
|
42
|
+
} catch {
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
const replaced = section(config.theme?.colors);
|
|
47
|
+
if (replaced && typeof replaced === "object") {
|
|
48
|
+
result.replacesPalette = true;
|
|
49
|
+
result.colors.clear();
|
|
50
|
+
flattenColors(replaced, result.colors);
|
|
51
|
+
}
|
|
52
|
+
flattenColors(section(config.theme?.extend?.colors), result.colors);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Tailwind 의 색 객체를 토큰 이름 → 비교용 색 표기로 편다.
|
|
57
|
+
* 예: { brand: { DEFAULT: "#f00", 500: "#a00" } } → --color-brand, --color-brand-500
|
|
58
|
+
* getter 는 읽지 않는다. tailwindcss/colors 의 옛 이름(lightBlue 등)은 읽기만 해도 경고를 찍는다.
|
|
59
|
+
* @param {unknown} colors
|
|
60
|
+
* @param {Map<string, string>} [out]
|
|
61
|
+
* @param {string} [prefix]
|
|
62
|
+
*/
|
|
63
|
+
export function flattenColors(colors, out = new Map(), prefix = "--color") {
|
|
64
|
+
if (!colors || typeof colors !== "object") return out;
|
|
65
|
+
for (const [key, descriptor] of Object.entries(Object.getOwnPropertyDescriptors(colors))) {
|
|
66
|
+
if (!descriptor.enumerable || !("value" in descriptor)) continue;
|
|
67
|
+
const { value } = descriptor;
|
|
68
|
+
const name = key === "DEFAULT" ? prefix : `${prefix}-${key}`;
|
|
69
|
+
if (typeof value === "string") {
|
|
70
|
+
const color = normalizeColor(value);
|
|
71
|
+
if (color && name !== "--color") out.set(name, color);
|
|
72
|
+
} else {
|
|
73
|
+
flattenColors(value, out, name);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { findArbitraryColors
|
|
1
|
+
import { findArbitraryColors } from "../core/classes.js";
|
|
2
|
+
import { findColorValues } from "../core/color.js";
|
|
2
3
|
import { visitStrings } from "./visit-strings.js";
|
|
3
4
|
import { inlineStyleProperty } from "./visit-style.js";
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
|
-
* 검사 규칙이 보지 않는 문자열 속
|
|
7
|
-
* 임의 값 클래스(bg-[#fff])와 인라인 스타일의
|
|
7
|
+
* 검사 규칙이 보지 않는 문자열 속 색을 알린다. 위반이 아니라 check 가 개수를 세는 데 쓴다.
|
|
8
|
+
* 임의 값 클래스(bg-[#fff])와 인라인 스타일의 색은 다른 규칙이 검사하므로 빼고 센다.
|
|
8
9
|
* @type {import("eslint").Rule.RuleModule}
|
|
9
10
|
*/
|
|
10
11
|
export default {
|
|
@@ -12,7 +13,7 @@ export default {
|
|
|
12
13
|
create(context) {
|
|
13
14
|
const findUnchecked = (text) => {
|
|
14
15
|
const inClasses = findArbitraryColors(text).map(({ index, text: found }) => [index, index + found.length]);
|
|
15
|
-
return
|
|
16
|
+
return findColorValues(text).filter(({ index }) => !inClasses.some(([start, end]) => index >= start && index < end));
|
|
16
17
|
};
|
|
17
18
|
const visitor = visitStrings(context, findUnchecked, (found, { loc }) =>
|
|
18
19
|
context.report({ loc, messageId: "unchecked", data: { color: found.value } }),
|
package/src/rules/visit-style.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { findColorValues, isColorProperty } from "../core/color.js";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* 노드가 JSX style 속성 객체의 값이면 CSS 속성 이름을, 아니면 null 을 돌려준다.
|
|
@@ -7,6 +7,13 @@ import { findHexColors } from "../core/classes.js";
|
|
|
7
7
|
* @returns {string | null}
|
|
8
8
|
*/
|
|
9
9
|
export function inlineStyleProperty(node) {
|
|
10
|
+
// color: on ? "#fff" : "#000", color: x || "#fff" 처럼 값이 고르는 식이면 그 식을 거슬러 올라간다.
|
|
11
|
+
while (
|
|
12
|
+
(node.parent?.type === "ConditionalExpression" && node.parent.test !== node) ||
|
|
13
|
+
node.parent?.type === "LogicalExpression"
|
|
14
|
+
) {
|
|
15
|
+
node = node.parent;
|
|
16
|
+
}
|
|
10
17
|
const property = node.parent;
|
|
11
18
|
if (property?.type !== "Property" || property.value !== node || property.computed) return null;
|
|
12
19
|
const object = property.parent;
|
|
@@ -19,7 +26,7 @@ export function inlineStyleProperty(node) {
|
|
|
19
26
|
}
|
|
20
27
|
|
|
21
28
|
/**
|
|
22
|
-
* JSX style 속성에 문자열로 적은
|
|
29
|
+
* JSX style 속성에 문자열로 적은 색마다 onFound 를 부르는 visitor 를 만든다.
|
|
23
30
|
* @param {import("eslint").Rule.RuleContext} context
|
|
24
31
|
* @param {(found: { text: string, value: string, property: string }, where: { loc: import("estree").SourceLocation, range: [number, number] }) => void} onFound
|
|
25
32
|
*/
|
|
@@ -27,7 +34,7 @@ export function visitInlineStyleColors(context, onFound) {
|
|
|
27
34
|
const { sourceCode } = context;
|
|
28
35
|
|
|
29
36
|
function scan(node, property) {
|
|
30
|
-
for (const found of
|
|
37
|
+
for (const found of findColorValues(sourceCode.getText(node), { named: isColorProperty(property) })) {
|
|
31
38
|
const start = node.range[0] + found.index;
|
|
32
39
|
const end = start + found.text.length;
|
|
33
40
|
onFound({ ...found, property }, {
|