@slashsuite/oxlint-ui 0.5.0 → 0.5.2

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.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/package.json +3 -2
  3. package/plugin.js +177 -18
package/README.md CHANGED
@@ -15,7 +15,7 @@ Status: `0.x`. License: MIT.
15
15
  ## Install
16
16
 
17
17
  ```sh
18
- bun add -d -E @slashsuite/oxlint-ui oxlint@1.94.0
18
+ bun add -d -E @slashsuite/oxlint-ui oxlint@1.95.0
19
19
  ```
20
20
 
21
21
  The oxlint peer is an exact version: oxlint's `jsPlugins`, which loads this package's plugin, is
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@slashsuite/oxlint-ui",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "description": "An oxlint config and plugin that holds an app to building its UI from @slashsuite/ui-react: no <style>, no raw colour, no dark: variant, no styling prop, no look-alike component, no inline colour and no raw element that has a part.",
5
5
  "keywords": [
6
6
  "design-system",
@@ -45,11 +45,12 @@
45
45
  "prepack": "bun scripts/data.ts"
46
46
  },
47
47
  "devDependencies": {
48
+ "@oxlint/plugins": "1.95.0",
48
49
  "@types/bun": "1.4.5",
49
50
  "vitest": "5.1.1"
50
51
  },
51
52
  "peerDependencies": {
52
- "oxlint": "1.94.0"
53
+ "oxlint": "1.95.0"
53
54
  },
54
55
  "nx": {
55
56
  "name": "oxlint-ui"
package/plugin.js CHANGED
@@ -1,5 +1,7 @@
1
1
  import { existsSync, readFileSync } from "node:fs";
2
2
 
3
+ /** @import { Context, ESTree, Plugin, Rule, Visitor } from "@oxlint/plugins" */
4
+
3
5
  /**
4
6
  * The UISlash rules, loaded by oxlint's `jsPlugins` as `uislash`: the hard rules the `ui-review`
5
7
  * pull request check runs on a diff, here on the AST of every file an app lints. Plain ESM over
@@ -15,7 +17,61 @@ if (!existsSync(DATA)) {
15
17
  "@slashsuite/oxlint-ui: ui-react.json is missing. It is written when the package is packed; in the uislash repo, run `bun packages/oxlint-ui/scripts/data.ts`.",
16
18
  );
17
19
  }
18
- const data = JSON.parse(readFileSync(DATA, "utf8"));
20
+
21
+ /**
22
+ * What `ui-react.json` holds (`scripts/data.ts` writes it).
23
+ * @typedef {object} UiData
24
+ * @property {string[]} components
25
+ * @property {Record<string, string>} palette
26
+ * @property {string[]} namedColours
27
+ * @property {Record<string, string>} rawElements
28
+ * @property {Record<string, string>} inputTypes
29
+ * @property {string[]} plainInputs
30
+ */
31
+
32
+ /**
33
+ * @param {unknown} value
34
+ * @returns {value is string[]}
35
+ */
36
+ const isStrings = (value) =>
37
+ Array.isArray(value) && value.every((item) => typeof item === "string");
38
+
39
+ /**
40
+ * @param {unknown} value
41
+ * @returns {value is Record<string, string>}
42
+ */
43
+ const isTable = (value) =>
44
+ typeof value === "object" &&
45
+ value !== null &&
46
+ Object.values(value).every((item) => typeof item === "string");
47
+
48
+ /**
49
+ * @param {unknown} value
50
+ * @returns {value is UiData}
51
+ */
52
+ function isUiData(value) {
53
+ if (typeof value !== "object" || value === null) return false;
54
+ /** @type {Map<string, unknown>} */
55
+ const fields = new Map(Object.entries(value));
56
+ return (
57
+ ["components", "namedColours", "plainInputs"].every((key) => isStrings(fields.get(key))) &&
58
+ ["palette", "rawElements", "inputTypes"].every((key) => isTable(fields.get(key)))
59
+ );
60
+ }
61
+
62
+ /** @returns {UiData} */
63
+ function readData() {
64
+ /** @type {unknown} */
65
+ const raw = JSON.parse(readFileSync(DATA, "utf8"));
66
+ if (!isUiData(raw)) {
67
+ throw new Error(
68
+ "@slashsuite/oxlint-ui: ui-react.json does not hold the tables the plugin reads. In the uislash repo, run `bun packages/oxlint-ui/scripts/data.ts`.",
69
+ );
70
+ }
71
+ return raw;
72
+ }
73
+
74
+ const data = readData();
19
75
  const components = new Set(data.components);
20
76
  const palette = new Map(Object.entries(data.palette));
21
77
 
@@ -40,12 +96,21 @@ const FUNCTION = "(?<![\\w-])(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch|color)\\(";
40
96
  const NAMED = `(?<![\\w-])(?:${data.namedColours.join("|")})(?![\\w-])`;
41
97
  const STYLE_COLOUR = new RegExp(`${HEX}|${FUNCTION}|${NAMED}`, "gi");
42
98
 
99
+ /**
100
+ * @param {string} description
101
+ * @param {(context: Context) => Visitor} create
102
+ * @returns {Rule}
103
+ */
43
104
  const rule = (description, create) => ({
44
105
  meta: { type: "problem", docs: { description } },
45
106
  create,
46
107
  });
47
108
 
48
- /** A JSX name as written: `Button`, `svg:g`, `UI.Button`. */
109
+ /**
110
+ * A JSX name as written: `Button`, `svg:g`, `UI.Button`.
111
+ * @param {ESTree.JSXElementName | ESTree.JSXAttributeName} node
112
+ * @returns {string}
113
+ */
49
114
  function jsxName(node) {
50
115
  if (node.type === "JSXIdentifier") return node.name;
51
116
  if (node.type === "JSXNamespacedName") return `${node.namespace.name}:${node.name.name}`;
@@ -53,8 +118,13 @@ function jsxName(node) {
53
118
  return "";
54
119
  }
55
120
 
56
- /** The local names a file imports, as values, from `@slashsuite/ui-react`. */
121
+ /**
122
+ * The local names a file imports, as values, from `@slashsuite/ui-react`.
123
+ * @param {ESTree.Program} program
124
+ * @returns {Set<string>}
125
+ */
57
126
  function importedFromUi(program) {
127
+ /** @type {Set<string>} */
58
128
  const names = new Set();
59
129
  for (const statement of program.body) {
60
130
  if (statement.type !== "ImportDeclaration" || statement.importKind === "type") continue;
@@ -69,24 +139,60 @@ function importedFromUi(program) {
69
139
  }
70
140
 
71
141
  const SKIPPED_KEYS = new Set(["parent", "loc", "range"]);
142
+ /**
143
+ * @param {unknown} value
144
+ * @returns {value is ESTree.Node}
145
+ */
72
146
  const isNode = (value) =>
73
- value !== null && typeof value === "object" && typeof value.type === "string";
147
+ value !== null && typeof value === "object" && "type" in value && typeof value.type === "string";
74
148
 
75
- /** The AST nodes directly under `node`. */
149
+ /**
150
+ * A property's value as a list: an array as it is, anything else as a list of one.
151
+ * @param {unknown} value
152
+ * @returns {unknown[]}
153
+ */
154
+ const listOf = (value) => (Array.isArray(value) ? value : [value]);
155
+
156
+ /**
157
+ * The AST nodes directly under `node`.
158
+ * @param {ESTree.Node} node
159
+ * @returns {ESTree.Node[]}
160
+ */
76
161
  function childrenOf(node) {
77
- return Object.entries(node)
162
+ /** @type {[string, unknown][]} */
163
+ const entries = Object.entries(node);
164
+ return entries
78
165
  .filter(([key]) => !SKIPPED_KEYS.has(key))
79
- .flatMap(([, value]) => (Array.isArray(value) ? value : [value]))
166
+ .flatMap(([, value]) => listOf(value))
80
167
  .filter(isNode);
81
168
  }
82
169
 
83
- /** The text of a string literal or a template literal's text, as written; `undefined` otherwise. */
170
+ /**
171
+ * The text of a string literal or a template literal's text, as written; `undefined` otherwise.
172
+ * @param {ESTree.Node} node
173
+ * @returns {string | undefined}
174
+ */
84
175
  function stringText(node) {
85
176
  if (node.type === "Literal" && typeof node.value === "string") return node.raw ?? node.value;
86
177
  return node.type === "TemplateElement" ? node.value.raw : undefined;
87
178
  }
88
179
 
89
- /** Each string piece of the code under `node`: string literals and template literal text. */
180
+ /**
181
+ * One string piece of the code: its text, the line it starts on and its node.
182
+ * @typedef {{ text: string, line: number, node: ESTree.Node }} StringPiece
183
+ */
184
+
185
+ /**
186
+ * What a rule reports on one line: the first node there and each match found on it.
187
+ * @typedef {{ node: ESTree.Node, found: string[] }} LineEntry
188
+ */
189
+
190
+ /**
191
+ * Each string piece of the code under `node`: string literals and template literal text.
192
+ * @param {ESTree.Node} node
193
+ * @param {StringPiece[]} found
194
+ * @returns {StringPiece[]}
195
+ */
90
196
  function stringsUnder(node, found = []) {
91
197
  const text = stringText(node);
92
198
  if (text !== undefined) found.push({ text, line: node.loc.start.line, node });
@@ -94,7 +200,13 @@ function stringsUnder(node, found = []) {
94
200
  return found;
95
201
  }
96
202
 
97
- /** Every match of `pattern` in the strings, by the line it sits on, keeping the first node. */
203
+ /**
204
+ * Every match of `pattern` in the strings, by the line it sits on, keeping the first node.
205
+ * @param {StringPiece[]} strings
206
+ * @param {RegExp} pattern
207
+ * @param {Map<number, LineEntry>} byLine
208
+ * @returns {Map<number, LineEntry>}
209
+ */
98
210
  function matchesByLine(strings, pattern, byLine = new Map()) {
99
211
  for (const { text, line, node } of strings) {
100
212
  for (const match of text.matchAll(pattern)) {
@@ -107,7 +219,12 @@ function matchesByLine(strings, pattern, byLine = new Map()) {
107
219
  return byLine;
108
220
  }
109
221
 
110
- /** Report one message per line, at that line. */
222
+ /**
223
+ * Report one message per line, at that line.
224
+ * @param {Context} context
225
+ * @param {Map<number, LineEntry>} byLine
226
+ * @param {(found: string[]) => string} message
227
+ */
111
228
  function reportByLine(context, byLine, message) {
112
229
  for (const [line, { found }] of [...byLine].toSorted(([a], [b]) => a - b)) {
113
230
  context.report({
@@ -117,7 +234,12 @@ function reportByLine(context, byLine, message) {
117
234
  }
118
235
  }
119
236
 
120
- /** A rule that reads every string piece in the file once it is parsed. */
237
+ /**
238
+ * A rule that reads every string piece in the file once it is parsed.
239
+ * @param {string} description
240
+ * @param {(context: Context, strings: StringPiece[]) => void} check
241
+ * @returns {Rule}
242
+ */
121
243
  const onStrings = (description, check) =>
122
244
  rule(description, (context) => ({
123
245
  "Program:exit"(program) {
@@ -125,6 +247,7 @@ const onStrings = (description, check) =>
125
247
  },
126
248
  }));
127
249
 
250
+ /** @type {Rule} */
128
251
  const styleBlock = rule("no <style> element in JSX", (context) => ({
129
252
  JSXOpeningElement(node) {
130
253
  if (jsxName(node.name) !== "style") return;
@@ -136,6 +259,7 @@ const styleBlock = rule("no <style> element in JSX", (context) => ({
136
259
  },
137
260
  }));
138
261
 
262
+ /** @type {Rule} */
139
263
  const rawColour = onStrings("no stock Tailwind colour utility", (context, strings) => {
140
264
  const stock = matchesByLine(strings, COLOUR);
141
265
  const arbitrary = matchesByLine(strings, ARBITRARY_COLOUR);
@@ -151,7 +275,9 @@ const rawColour = onStrings("no stock Tailwind colour utility", (context, string
151
275
  });
152
276
  });
153
277
 
278
+ /** @type {Rule} */
154
279
  const darkVariant = onStrings("no dark: variant", (context, strings) => {
280
+ /** @type {Map<number, LineEntry>} */
155
281
  const byLine = new Map();
156
282
  for (const { text, line, node } of strings) {
157
283
  text.split("\n").forEach((part, i) => {
@@ -166,7 +292,9 @@ const darkVariant = onStrings("no dark: variant", (context, strings) => {
166
292
  );
167
293
  });
168
294
 
295
+ /** @type {Rule} */
169
296
  const stylingProp = rule("no className or style on a @slashsuite/ui-react component", (context) => {
297
+ /** @type {Set<string>} */
170
298
  let imported = new Set();
171
299
  return {
172
300
  Program(program) {
@@ -188,24 +316,39 @@ const stylingProp = rule("no className or style on a @slashsuite/ui-react compon
188
316
  };
189
317
  });
190
318
 
319
+ /** @param {string} name */
191
320
  const duplicateMessage = (name) =>
192
321
  `Local component \`${name}\` has the same name as an @slashsuite/ui-react export. If it does the same job, import the UISlash one, and if that one does not fit, ask uislash for what is missing rather than building a look-alike. If it is a different thing, rename it so it is not mistaken for the UISlash one.`;
193
322
 
194
323
  const WRAPPERS = new Set(["memo", "forwardRef"]);
195
324
 
196
- /** An initialiser that makes a component: a function, `memo(…)`, `forwardRef(…)`, `React.…`. */
325
+ /**
326
+ * An initialiser that makes a component: a function, `memo(…)`, `forwardRef(…)`, `React.…`.
327
+ * @param {ESTree.Expression | null} init
328
+ * @returns {boolean}
329
+ */
197
330
  function makesComponent(init) {
198
331
  if (!init) return false;
199
332
  if (init.type === "ArrowFunctionExpression" || init.type === "FunctionExpression") return true;
200
333
  const callee = init.type === "CallExpression" ? init.callee : init;
201
334
  if (callee.type === "Identifier") return init !== callee && WRAPPERS.has(callee.name);
202
- return callee.type === "MemberExpression" && callee.object.name === "React";
335
+ return (
336
+ callee.type === "MemberExpression" &&
337
+ callee.object.type === "Identifier" &&
338
+ callee.object.name === "React"
339
+ );
203
340
  }
204
341
 
342
+ /** @type {Rule} */
205
343
  const duplicateComponent = rule(
206
344
  "no local component named like a @slashsuite/ui-react export",
207
345
  (context) => {
346
+ /** @type {Set<string>} */
208
347
  let imported = new Set();
348
+ /**
349
+ * @param {ESTree.Node} node
350
+ * @param {string} name
351
+ */
209
352
  const check = (node, name) => {
210
353
  if (components.has(name) && !imported.has(name)) {
211
354
  context.report({ node, message: duplicateMessage(name) });
@@ -235,6 +378,7 @@ const duplicateComponent = rule(
235
378
  },
236
379
  );
237
380
 
381
+ /** @type {Rule} */
238
382
  const inlineColour = rule("no colour literal in a style value", (context) => ({
239
383
  JSXAttribute(node) {
240
384
  if (jsxName(node.name) !== "style" || !node.value) return;
@@ -251,7 +395,12 @@ const inlineColour = rule("no colour literal in a style value", (context) => ({
251
395
  },
252
396
  }));
253
397
 
254
- /** The part to use for a raw element, `undefined` when the element is allowed. */
398
+ /**
399
+ * The part to use for a raw element, `undefined` when the element is allowed.
400
+ * @param {string} element
401
+ * @param {string | undefined} inputType
402
+ * @returns {string | undefined}
403
+ */
255
404
  function partFor(element, inputType) {
256
405
  if (!Object.hasOwn(data.rawElements, element)) return undefined;
257
406
  if (element !== "input" || inputType === undefined) return data.rawElements[element];
@@ -259,13 +408,20 @@ function partFor(element, inputType) {
259
408
  return Object.hasOwn(data.inputTypes, inputType) ? data.inputTypes[inputType] : "`Input`";
260
409
  }
261
410
 
262
- /** An `<input>`'s `type`, when it is written as a plain string. */
411
+ /**
412
+ * An `<input>`'s `type`, when it is written as a plain string.
413
+ * @param {ESTree.JSXOpeningElement} node
414
+ * @returns {string | undefined}
415
+ */
263
416
  function inputTypeOf(node) {
264
- const type = node.attributes.find((a) => a.type === "JSXAttribute" && jsxName(a.name) === "type");
417
+ const type = node.attributes
418
+ .filter((a) => a.type === "JSXAttribute")
419
+ .find((a) => jsxName(a.name) === "type");
265
420
  const value = type?.value;
266
421
  return value?.type === "Literal" && /^[\w-]+$/.test(value.value) ? value.value : undefined;
267
422
  }
268
423
 
424
+ /** @type {Rule} */
269
425
  const rawElement = rule("no raw HTML element that has a @slashsuite/ui-react part", (context) => ({
270
426
  JSXOpeningElement(node) {
271
427
  if (!JSX_FILE.test(context.filename)) return;
@@ -279,7 +435,8 @@ const rawElement = rule("no raw HTML element that has a @slashsuite/ui-react par
279
435
  },
280
436
  }));
281
437
 
282
- export default {
438
+ /** @type {Plugin} */
439
+ const plugin = {
283
440
  meta: { name: "uislash" },
284
441
  rules: {
285
442
  "style-block": styleBlock,
@@ -291,3 +448,5 @@ export default {
291
448
  "raw-element": rawElement,
292
449
  },
293
450
  };
451
+
452
+ export default plugin;