designsnitch 0.6.0 → 0.6.1

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 CHANGED
@@ -517,7 +517,7 @@ Tailwind 기본 팔레트(`bg-slate-100`, `text-indigo-600`)를 기준으로 쓰
517
517
  ## 검사 범위
518
518
 
519
519
  - **JS와 TS에서 보는 곳:** 문자열과 템플릿 리터럴 안의 임의 값 색 클래스(`className`과 `cn()`, `clsx()`, `cva()`의 인자 포함)와, 색을 받는 자리에 문자열로 적은 색입니다. `a ? "#fff" : "#000"`이나 `x || "#fff"`처럼 고르는 식의 값도 봅니다. 주석은 보지 않습니다.
520
- - **스타일:** JSX `style` 객체, 이름이 `style`이나 `styles`로 끝나는 변수의 객체, `CSSProperties` 타입의 객체입니다. 여기서는 `var(--토큰)`이 통하므로 다른 색과 똑같이 검사합니다.
520
+ - **스타일:** JSX `style` 객체와 `CSSProperties` 타입의 객체입니다. 여기서는 `var(--토큰)`이 통하므로 다른 색과 똑같이 검사합니다. `cardStyle` 같은 변수 이름은 근거로 삼지 않습니다. 차트 설정 묶음일 수 있기 때문입니다.
521
521
  - **그 밖의 색 속성:** 이름이 CSS 색 속성이거나 `Color`로 끝나는 속성과 JSX 속성입니다. 차트 옵션의 `backgroundColor`, SVG 요소의 `stroke` 같은 것입니다. 색은 알리고 `var(--토큰)`을 후보로 주지만 `ask`로 표시합니다. 캔버스처럼 `var()`가 통하지 않는 곳에 쓰일 수 있기 때문입니다.
522
522
  - **나머지:** 다른 이름의 키(`{ primary: "#3B82F6" }`)와 배열 안의 색은 코드만 보고 용도를 알 수 없어 검사하지 않습니다. 대신 있다는 것을 알 수 있도록 `unchecked`에 개수를 세고, `designsnitch colors`로 목록을 볼 수 있습니다.
523
523
  - **CSS에서 보는 곳:** `.css` 파일(CSS Modules 포함)의 선언 값에 있는 색과 `@apply`의 임의 값 색 클래스입니다. 토큰 CSS에서는 변수를 정의하는 선언(`--이름: ...`)을 토큰을 정의하는 곳으로 보고 건너뜁니다. 다른 CSS 파일에서는 변수 선언도 일반 선언처럼 검사합니다. CSS에서는 아직 색만 검사합니다.
@@ -528,7 +528,7 @@ Tailwind 기본 팔레트(`bg-slate-100`, `text-indigo-600`)를 기준으로 쓰
528
528
 
529
529
  감지는 하지만 토큰과 맞추지는 못하는 것:
530
530
 
531
- - 반투명한 색(`#3B82F680`, `rgb(0 0 0 / 50%)`)은 불투명한 색의 토큰에 불투명도를 붙인 후보를 받습니다. 클래스는 `bg-brand/50`, 스타일과 CSS 선언은 `color-mix(in srgb, var(--color-brand) 50%, transparent)`입니다. 불투명도를 정수 퍼센트로 반올림하므로 `ask`로 표시합니다.
531
+ - 반투명한 색(`#3B82F680`, `rgb(0 0 0 / 50%)`)은 불투명한 색의 토큰에 불투명도를 붙인 후보를 받습니다. 클래스는 `bg-brand/50`, 스타일과 CSS 선언은 `color-mix(in srgb, var(--color-brand) 50%, transparent)`입니다. 불투명도를 정수 퍼센트로 반올림하므로 `ask`로 표시합니다. `rgba(0, 0, 0, 0)`처럼 완전히 투명한 값은 색을 쓴 것이 아니므로 건너뜁니다.
532
532
  - Tailwind 플러그인이 더하는 색은 토큰으로 알지 못합니다. 그 색을 직접 쓰면 토큰 없는 색으로 알립니다.
533
533
  - `hwb()`, `lab()`, `lch()`, `oklab()`, `color()` 값과 sRGB 밖의 `oklch()` 색은 같은 값으로 적은 토큰과만 맞춥니다. 같은 색을 내는 hex가 없기 때문입니다.
534
534
  - 색 이름은 색 속성(`color`, `background`, `border` 등), 임의 값 클래스(`bg-[white]`), 값 전체가 색 이름인 변수 선언에서만 읽습니다. 다른 곳의 `red` 같은 단어는 색으로 보지 않습니다.
package/README.md CHANGED
@@ -516,7 +516,7 @@ Without `palette`, designsnitch does not flag default palette classes, and does
516
516
  ## Scope
517
517
 
518
518
  - **Where it looks in JS and TS:** arbitrary color classes in string literals and template literals, which covers `className` and the arguments of `cn()`, `clsx()` and `cva()`, and colors written as strings where a color is expected, including the branches of `a ? "#fff" : "#000"` and `x || "#fff"`. Comments are ignored.
519
- - **Styles:** a JSX `style` object, an object in a variable whose name ends in `style` or `styles`, and an object typed `CSSProperties`. `var(--token)` works there, so these are checked like any other color.
519
+ - **Styles:** a JSX `style` object and an object typed `CSSProperties`. `var(--token)` works there, so these are checked like any other color. A variable name such as `cardStyle` is not taken as proof: it may hold chart settings.
520
520
  - **Other color properties:** a property or JSX attribute whose name is a CSS color property or ends in `Color`, such as `backgroundColor` in chart options or `stroke` on an SVG element. The color is reported, with `var(--token)` as a candidate, but marked `ask`: the tool cannot tell whether `var()` works where the value ends up, for example on a canvas.
521
521
  - **Everything else:** colors under other keys (`{ primary: "#3B82F6" }`) and in arrays are not checked, because their purpose cannot be told from the code. The check counts them in `unchecked` so that you know they are there, and `designsnitch colors` lists them.
522
522
  - **Where it looks in CSS:** colors in declaration values and arbitrary color classes in `@apply`, in `.css` files including CSS Modules. In the token CSS, declarations that define a variable (`--name: ...`) are skipped, since that is where tokens are defined. In every other CSS file they are checked like any other declaration. Only colors are checked in CSS for now.
@@ -527,7 +527,7 @@ Without `palette`, designsnitch does not flag default palette classes, and does
527
527
 
528
528
  These are detected, but not matched to a token:
529
529
 
530
- - Translucent colors (`#3B82F680`, `rgb(0 0 0 / 50%)`) get the token of the opaque color with an opacity: `bg-brand/50` for a class, `color-mix(in srgb, var(--color-brand) 50%, transparent)` for a style or a CSS declaration. The opacity is rounded to a whole percent, so these are marked `ask`.
530
+ - Translucent colors (`#3B82F680`, `rgb(0 0 0 / 50%)`) get the token of the opaque color with an opacity: `bg-brand/50` for a class, `color-mix(in srgb, var(--color-brand) 50%, transparent)` for a style or a CSS declaration. The opacity is rounded to a whole percent, so these are marked `ask`. Fully transparent values such as `rgba(0, 0, 0, 0)` are not colors in use and are skipped.
531
531
  - Colors a Tailwind plugin adds are not known as tokens, so writing one by hand is reported as a color with no token.
532
532
  - `hwb()`, `lab()`, `lch()`, `oklab()` and `color()` values, and `oklch()` colors outside sRGB, match only a token written with the same value: no hex color renders the same.
533
533
  - Color names are read in color properties (`color`, `background`, `border`, ...), in arbitrary classes (`bg-[white]`), and as a whole custom property value. Elsewhere a word such as `red` is not taken for a color.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "designsnitch",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "Keep AI-written UI code manageable and consistent, even without a design system. CLI, agent skills, ESLint plugin and agent hook for React/Next.js with Tailwind CSS v4.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
@@ -130,3 +130,4 @@ Report what you fixed and what you left, separately. For what you left, use the
130
130
  - Do not edit `designsnitch.config.json` or the token CSS to make the check pass. They are the standard the code is judged against. If one of them seems wrong, explain why and ask the user.
131
131
  - Do not add `eslint-disable` or `designsnitch-disable-next-line` comments, lower a rule's severity, or turn a rule off to make the check pass. If an exception seems necessary, ask the user.
132
132
  - Do not pick one of several candidates on your own.
133
+ - Do not change how the tokens are declared, such as turning `@theme` into `@theme static`, to make a `var(--token)` candidate work. Tailwind outputs a theme variable as soon as a class, a style or a CSS file uses it. A variable missing from the compiled CSS before the replacement is expected.
package/src/colors.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
3
  import { findArbitraryColors, findPaletteClasses } from "./core/classes.js";
4
- import { colorLabel, isColorProperty, isNamedColor, parseColor } from "./core/color.js";
4
+ import { colorLabel, isColorProperty, isNamedColor, isVisibleColor } from "./core/color.js";
5
5
  import { findCssColors } from "./core/css.js";
6
6
  import { createEngine } from "./engine.js";
7
7
  import { splitPaths } from "./files.js";
@@ -37,7 +37,7 @@ const collectRule = {
37
37
  // 문자열 전체가 색인 경우. 예: style={{ color: "#3B82F6" }}
38
38
  // 색 이름은 인라인 스타일의 색 속성에서만 색으로 센다. 다른 곳에서는 그냥 단어일 수 있다.
39
39
  const isColor = (value) =>
40
- parseColor(value) !== null && (!isNamedColor(value) || isColorProperty(inlineStyleProperty(node) ?? ""));
40
+ isVisibleColor(value) && (!isNamedColor(value) || isColorProperty(inlineStyleProperty(node) ?? ""));
41
41
  if (typeof node.value === "string" && isColor(node.value)) {
42
42
  report(node.loc, { kind: "hex", key: colorLabel(node.value), utility: null });
43
43
  return;
@@ -1,4 +1,4 @@
1
- import { parseColor } from "./color.js";
1
+ import { isVisibleColor } from "./color.js";
2
2
 
3
3
  // 예: bg-[#3B82F6], ring-offset-[#fff], text-[rgb(59,130,246)], bg-[oklch(62.3%_0.214_259.815)], bg-[white]
4
4
  const ARBITRARY_COLOR =
@@ -11,7 +11,7 @@ const ARBITRARY_COLOR =
11
11
  */
12
12
  export function findArbitraryColors(text) {
13
13
  return [...text.matchAll(ARBITRARY_COLOR)]
14
- .filter((match) => parseColor(match[2]) !== null)
14
+ .filter((match) => isVisibleColor(match[2]))
15
15
  .map((match) => ({
16
16
  index: match.index,
17
17
  text: match[0],
package/src/core/color.js CHANGED
@@ -57,6 +57,12 @@ export function parseColor(value) {
57
57
  return { key: `#${rgb.map(toByte).join("")}`, alpha, opaque: alpha >= 1 };
58
58
  }
59
59
 
60
+ /** 색으로 읽히고 완전히 투명하지 않은 값인지. rgba(0, 0, 0, 0) 은 색을 쓴 것이 아니다. */
61
+ export function isVisibleColor(value) {
62
+ const color = parseColor(value);
63
+ return color !== null && color.alpha > 0;
64
+ }
65
+
60
66
  /**
61
67
  * 색을 세거나 묶을 때 쓰는 이름. 표기가 달라도 같은 색이면 같은 이름이 된다.
62
68
  * 불투명한 색은 normalizeColor 와 같고, 반투명한 색은 #rrggbbaa 다. 색이 아니면 소문자로만 맞춘다.
@@ -89,7 +95,7 @@ export function normalizeColor(value) {
89
95
  export function findColorValues(text, { named = false } = {}) {
90
96
  const matches = [...text.matchAll(COLOR_VALUE), ...(named ? text.matchAll(NAMED_VALUE) : [])];
91
97
  return matches
92
- .filter((match) => parseColor(match[0]) !== null)
98
+ .filter((match) => isVisibleColor(match[0]))
93
99
  .sort((a, b) => a.index - b.index)
94
100
  .map((match) => ({ index: match.index, text: match[0], value: match[0] }));
95
101
  }
@@ -124,7 +124,9 @@ export function tokenVar(name, prefix) {
124
124
  export function lookupColor(colors, value) {
125
125
  const color = parseColor(value);
126
126
  if (!color) return { names: [], opacity: null };
127
- return { names: colors.get(color.key) ?? [], opacity: color.opaque ? null : Math.round(color.alpha * 100) };
127
+ // 0 은 아니지만 반올림하면 0% 가 되는 값은 1% 로 본다. 0% 는 완전히 투명하다는 다른 뜻이다.
128
+ const opacity = color.opaque ? null : Math.max(1, Math.round(color.alpha * 100));
129
+ return { names: colors.get(color.key) ?? [], opacity };
128
130
  }
129
131
 
130
132
  /** 토큰을 CSS 값으로 쓰는 표기. 불투명도가 있으면 투명과 섞는다. */
@@ -2,13 +2,13 @@ import { findColorValues, isColorProperty } from "../core/color.js";
2
2
 
3
3
  // 색을 받는 이름. CSS 의 색 속성이거나 Color 로 끝난다. 예: backgroundColor, stroke, pointHoverBackgroundColor
4
4
  const isColorKey = (name) => isColorProperty(name) || /color$/i.test(name);
5
- const STYLE_NAME = /styles?$/i;
6
5
  const STYLE_TYPE = /CSSProperties/;
7
6
 
8
7
  /**
9
8
  * 노드가 색을 값으로 받는 자리에 있는지 본다.
10
- * - style: 스타일로 쓰이는 객체의 값. JSX 의 style 속성, 이름이 style 로 끝나는 변수, CSSProperties 타입의 객체.
11
- * 여기서는 var(--토큰) 이 통한다.
9
+ * - style: 스타일로 쓰이는 객체의 값. JSX 의 style 속성과 CSSProperties 타입의 객체.
10
+ * 여기서는 var(--토큰) 이 통한다. 변수 이름(cardStyle, VARIANT_STYLES)은 근거로 삼지 않는다.
11
+ * 이름만 그럴 뿐 차트 설정 묶음일 수 있고, 그러면 var() 로 바꾸는 것이 안전하지 않다.
12
12
  * - data: 그 밖에 색을 받는 이름의 속성이나 JSX 속성. 예: 차트 옵션의 backgroundColor, <Line stroke="...">.
13
13
  * 색인 것은 분명하지만 var() 가 통하는지는 알 수 없다.
14
14
  * @param {import("estree").Node & { parent?: any }} node 문자열이나 템플릿 리터럴
@@ -42,14 +42,14 @@ export function colorContext(node, sourceCode) {
42
42
  return { property: name, kind: style ? "style" : "data" };
43
43
  }
44
44
 
45
- /** 객체가 스타일로 쓰이는지. JSX 의 style 속성이면 "jsx", 이름이나 타입으로 알 수 있으면 "named", 아니면 null. */
45
+ /** 객체가 스타일로 쓰이는지. JSX 의 style 속성이면 "jsx", CSSProperties 타입이면 "typed", 아니면 null. */
46
46
  function styleObjectKind(object, sourceCode) {
47
47
  const typed = (annotation) => Boolean(annotation && sourceCode && STYLE_TYPE.test(sourceCode.getText(annotation)));
48
48
  for (let node = object; node; node = node.parent) {
49
49
  const parent = node.parent;
50
50
  if (!parent) return null;
51
51
  if (parent.type === "TSAsExpression" || parent.type === "TSSatisfiesExpression") {
52
- if (typed(parent.typeAnnotation)) return "named";
52
+ if (typed(parent.typeAnnotation)) return "typed";
53
53
  } else if (parent.type === "JSXExpressionContainer") {
54
54
  const attribute = parent.parent;
55
55
  return attribute?.type === "JSXAttribute" && attribute.name?.name === "style" ? "jsx" : null;
@@ -59,7 +59,7 @@ function styleObjectKind(object, sourceCode) {
59
59
  } else if (parent.type === "VariableDeclarator" && parent.init === node) {
60
60
  const { id } = parent;
61
61
  if (id.type !== "Identifier") return null;
62
- return STYLE_NAME.test(id.name) || typed(id.typeAnnotation) ? "named" : null;
62
+ return typed(id.typeAnnotation) ? "typed" : null;
63
63
  } else {
64
64
  return null;
65
65
  }