@jandragsbaek/oxlint-plugin 0.2.0 → 0.3.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.md CHANGED
@@ -45,15 +45,16 @@ them all.
45
45
 
46
46
  ### Presets
47
47
 
48
- | Preset | For |
49
- | ---------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
50
- | `recommended` | Any TypeScript project. Built-in rule choices, plus `max-file-lines`, `max-file-lines-hard`, `namespace-node-imports`, `no-explanatory-comments`, `no-unscoped-has`, `require-suppression-reason`. |
51
- | `tests` | Projects with Vitest tests. The rules read the filename and check only test files. |
52
- | `effect` | Projects on [Effect](https://effect.website). `prefer-effect-fn` is off in test files. |
53
- | `react` | React projects. Strict hook and render rules, and `no-unreasoned-state-sync`. |
54
- | `reactCompiler({ files })` | Files the React Compiler compiles. The compiler owns memoization. Use with `react`. |
55
- | `productCopy({ files })` | Code that holds text users or agents read: no em dashes. |
56
- | `uiText({ files, message })` | Interface code with i18n catalogs: no inline JSX text. `message` tells the reader where the catalogs are. |
48
+ | Preset | For |
49
+ | ----------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
50
+ | `recommended` | Any TypeScript project. Built-in rule choices, plus `max-file-lines`, `max-file-lines-hard`, `namespace-node-imports`, `no-explanatory-comments`, `no-unscoped-has`, `require-suppression-reason`. |
51
+ | `tests` | Projects with Vitest tests. The rules read the filename and check only test files. |
52
+ | `effect` | Projects on [Effect](https://effect.website). `prefer-effect-fn` is off in test files. |
53
+ | `react` | React projects. Strict hook and render rules, and `no-unreasoned-state-sync`. |
54
+ | `reactCompiler({ files })` | Files the React Compiler compiles. The compiler owns memoization. Use with `react`. |
55
+ | `productCopy({ files })` | Code that holds text users or agents read: no em dashes. |
56
+ | `uiText({ files, message })` | Interface code with i18n catalogs: no inline JSX text. `message` tells the reader where the catalogs are. |
57
+ | `theming({ files, message })` | Interface code with themes: no color literals (hex, `rgb()`, `oklch()` and the like); colors come from theme tokens. `message` names the tokens. |
57
58
 
58
59
  The presets that take `files` skip test files. `excludeFiles` skips more. Globs in `files` and
59
60
  `excludeFiles` are relative to the config file. Glose's setup:
@@ -90,6 +91,7 @@ All rules have the `jandragsbaek/` prefix.
90
91
  | `no-manual-effect-runtime-in-tests` | Effect runtimes made or run by hand in tests; use `@effect/vitest`. |
91
92
  | `no-manual-memo` | `useMemo`, `useCallback` and memo directives where the React Compiler memoizes. |
92
93
  | `no-polling-loop-in-tests` | Test loops that spin on ticks until a condition holds. |
94
+ | `no-raw-color` | Color literals (hex, `rgb()`, `oklch()` and the like) in code; colors come from theme tokens. |
93
95
  | `no-real-time-wait-in-tests` | Real-clock waits in tests. |
94
96
  | `no-test-in-loop` | Tests declared in a `for` loop; use `.each`. |
95
97
  | `no-test-timeout-or-unreasoned-skip` | Per-test timeouts and retries, and skipped or focused tests with no tracking issue. |
package/dist/index.mjs CHANGED
@@ -2348,6 +2348,85 @@ var no_manual_memo_default = defineRule({
2348
2348
  };
2349
2349
  }
2350
2350
  });
2351
+ // src/rules/no-raw-color.ts
2352
+ const DEFAULT_MESSAGE$1 = "Colors come from the theme. Read a CSS custom property (var(--token)) instead of a color literal, so every theme and its light and dark variants apply.";
2353
+ const readMessage$1 = (options) => {
2354
+ const [first] = options;
2355
+ return typeof first === "object" && first !== null && "message" in first && typeof first.message === "string" ? first.message : DEFAULT_MESSAGE$1;
2356
+ };
2357
+ const NAMED_COLORS = new Set("aliceblue antiquewhite aqua aquamarine azure beige bisque black blanchedalmond blue blueviolet brown burlywood cadetblue chartreuse chocolate coral cornflowerblue cornsilk crimson cyan darkblue darkcyan darkgoldenrod darkgray darkgreen darkgrey darkkhaki darkmagenta darkolivegreen darkorange darkorchid darkred darksalmon darkseagreen darkslateblue darkslategray darkslategrey darkturquoise darkviolet deeppink deepskyblue dimgray dimgrey dodgerblue firebrick floralwhite forestgreen fuchsia gainsboro ghostwhite gold goldenrod gray green greenyellow grey honeydew hotpink indianred indigo ivory khaki lavender lavenderblush lawngreen lemonchiffon lightblue lightcoral lightcyan lightgoldenrodyellow lightgray lightgreen lightgrey lightpink lightsalmon lightseagreen lightskyblue lightslategray lightslategrey lightsteelblue lightyellow lime limegreen linen magenta maroon mediumaquamarine mediumblue mediumorchid mediumpurple mediumseagreen mediumslateblue mediumspringgreen mediumturquoise mediumvioletred midnightblue mintcream mistyrose moccasin navajowhite navy oldlace olive olivedrab orange orangered orchid palegoldenrod palegreen paleturquoise palevioletred papayawhip peachpuff peru pink plum powderblue purple rebeccapurple red rosybrown royalblue saddlebrown salmon sandybrown seagreen seashell sienna silver skyblue slateblue slategray slategrey snow springgreen steelblue tan teal thistle tomato turquoise violet wheat white whitesmoke yellow yellowgreen".split(" "));
2358
+ const HEX = "#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})";
2359
+ const HEX_COLOR = new RegExp(`^\\s*${HEX}\\s*$`, "i");
2360
+ const HEX_IN_VALUE = new RegExp(`(?:^|[\\s,(:])${HEX}(?=$|[\\s,;)!])`, "i");
2361
+ const ENCODED_HEX = /%23(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})\b/i;
2362
+ const COLOR_FUNCTION = /\b(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch|color)\(/i;
2363
+ const CSS_VALUE = /\b(?:solid|dashed|dotted|double|inset|gradient|-?[\d.]+(?:px|em|rem|%|deg))\b|^\s*[a-z-]+\s*:/i;
2364
+ const ONLY_LENGTHS_AND_HEX = new RegExp(`^\\s*(?:(?:-?[\\d.]+[a-z%]*|${HEX})\\s*)+$`, "i");
2365
+ const WORDS = /[a-z]+/gi;
2366
+ const COLOR_PLACE = /(?:color|colour|background|fill|stroke|border|outline|shadow|style)$/i;
2367
+ const SELECTOR_CALLS = /* @__PURE__ */ new Set([
2368
+ "querySelector",
2369
+ "querySelectorAll",
2370
+ "closest",
2371
+ "matches"
2372
+ ]);
2373
+ const hasNamedColor = (text) => Array.from(text.matchAll(WORDS), ([word]) => word.toLowerCase()).some((word) => NAMED_COLORS.has(word));
2374
+ const isValueText = (text) => HEX_COLOR.test(text) || COLOR_FUNCTION.test(text) || ENCODED_HEX.test(text) || ONLY_LENGTHS_AND_HEX.test(text) && HEX_IN_VALUE.test(text) || CSS_VALUE.test(text) && (HEX_IN_VALUE.test(text) || hasNamedColor(text));
2375
+ const nameOf = (node) => {
2376
+ if (node === null || node === void 0) return null;
2377
+ if (node.type === "Identifier") return node.name;
2378
+ if (node.type === "Literal" && typeof node.value === "string") return node.value;
2379
+ if (node.type === "JSXIdentifier") return node.name;
2380
+ if (node.type === "MemberExpression") return nameOf(node.property);
2381
+ return null;
2382
+ };
2383
+ const isColorPlace = (node) => {
2384
+ const parent = node.parent;
2385
+ if (parent === void 0 || parent === null) return false;
2386
+ if (parent.type === "JSXExpressionContainer") return isColorPlace(parent);
2387
+ if (parent.type === "Property" && parent.value === node) return COLOR_PLACE.test(nameOf(parent.key) ?? "");
2388
+ if (parent.type === "AssignmentExpression" && parent.right === node) return COLOR_PLACE.test(nameOf(parent.left) ?? "");
2389
+ if (parent.type === "JSXAttribute") return COLOR_PLACE.test(nameOf(parent.name) ?? "");
2390
+ if (parent.type === "CallExpression" && parent.arguments[1] === node) return nameOf(parent.callee) === "setProperty";
2391
+ return false;
2392
+ };
2393
+ const isSelector = (node) => {
2394
+ const parent = node.parent;
2395
+ return parent?.type === "CallExpression" && parent.arguments[0] === node && SELECTOR_CALLS.has(nameOf(parent.callee) ?? "");
2396
+ };
2397
+ var no_raw_color_default = defineRule({
2398
+ meta: {
2399
+ type: "problem",
2400
+ docs: { description: "Disallow color literals in code, so colors come from theme tokens that follow light and dark." },
2401
+ schema: [{
2402
+ type: "object",
2403
+ properties: { message: {
2404
+ type: "string",
2405
+ description: "Names the project's theme tokens and where they live."
2406
+ } },
2407
+ additionalProperties: false
2408
+ }]
2409
+ },
2410
+ create(context) {
2411
+ const message = readMessage$1(context.options);
2412
+ return {
2413
+ Literal(node) {
2414
+ if (typeof node.value !== "string" || isSelector(node)) return;
2415
+ const text = node.value;
2416
+ if (NAMED_COLORS.has(text.trim().toLowerCase()) && isColorPlace(node) || isValueText(text)) context.report({
2417
+ node,
2418
+ message
2419
+ });
2420
+ },
2421
+ TemplateElement(node) {
2422
+ if (isValueText(node.value.cooked ?? node.value.raw)) context.report({
2423
+ node,
2424
+ message
2425
+ });
2426
+ }
2427
+ };
2428
+ }
2429
+ });
2351
2430
  // src/rules/no-polling-loop-in-tests.ts
2352
2431
  const TEST_FILE_PATTERN$4 = /\.(?:test|spec)\.[cm]?[jt]sx?$/u;
2353
2432
  const FUNCTIONS$2 = /* @__PURE__ */ new Set([
@@ -3278,6 +3357,7 @@ var src_default = definePlugin({
3278
3357
  "no-manual-effect-runtime-in-tests": no_manual_effect_runtime_in_tests_default,
3279
3358
  "no-manual-memo": no_manual_memo_default,
3280
3359
  "no-polling-loop-in-tests": no_polling_loop_in_tests_default,
3360
+ "no-raw-color": no_raw_color_default,
3281
3361
  "no-real-time-wait-in-tests": no_real_time_wait_in_tests_default,
3282
3362
  "no-test-in-loop": no_test_in_loop_default,
3283
3363
  "no-test-timeout-or-unreasoned-skip": no_test_timeout_or_unreasoned_skip_default,
@@ -13,4 +13,7 @@ export declare const productCopy: (scope: FileScope) => OxlintConfig;
13
13
  export declare const uiText: (scope: FileScope & {
14
14
  readonly message?: string;
15
15
  }) => OxlintConfig;
16
+ export declare const theming: (scope: FileScope & {
17
+ readonly message?: string;
18
+ }) => OxlintConfig;
16
19
  export {};
package/dist/presets.mjs CHANGED
@@ -129,4 +129,8 @@ const uiText = (scope) => ({
129
129
  jsPlugins,
130
130
  overrides: [scoped(scope, { "jandragsbaek/no-untranslated-ui-text": scope.message === void 0 ? "error" : ["error", { message: scope.message }] })]
131
131
  });
132
- export { TEST_FILES, effect, productCopy, react, reactCompiler, recommended, tests, uiText };
132
+ const theming = (scope) => ({
133
+ jsPlugins,
134
+ overrides: [scoped(scope, { "jandragsbaek/no-raw-color": scope.message === void 0 ? "error" : ["error", { message: scope.message }] })]
135
+ });
136
+ export { TEST_FILES, effect, productCopy, react, reactCompiler, recommended, tests, theming, uiText };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jandragsbaek/oxlint-plugin",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Oxlint rules and presets for tests, Effect, React and product copy.",
5
5
  "keywords": [
6
6
  "effect",