@teacss/linter 0.0.0-stage → 0.10.15

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/dist/sites.js ADDED
@@ -0,0 +1,383 @@
1
+ import { exactStart, splitTokens } from "./tokens.js";
2
+ const reading = (inlined) => ({ texts: [], unreadable: [], open: false, inlined, path: "" });
3
+ function elementName(node) {
4
+ if (node.type === "JSXIdentifier")
5
+ return node.name;
6
+ if (node.type !== "JSXMemberExpression" || node.property.type !== "JSXIdentifier")
7
+ return null;
8
+ const owner = elementName(node.object);
9
+ return owner === null ? null : `${owner}.${node.property.name}`;
10
+ }
11
+ const INTRINSIC = /^[a-z][^.]*$/;
12
+ export function along(a, b) {
13
+ return a === b || a.startsWith(`${b} `) || b.startsWith(`${a} `);
14
+ }
15
+ export function apart(a, b) {
16
+ const arms = new Map(a.split(" ").map((mark) => [mark.slice(0, -1), mark]));
17
+ return b.split(" ").some((mark) => (arms.get(mark.slice(0, -1)) ?? mark) !== mark);
18
+ }
19
+ const DEPTH = 4;
20
+ const RECIPE = "recipe";
21
+ const HOLE = "\0";
22
+ const CLASS_ATTRIBUTES = new Set(["className", "class"]);
23
+ export function unwrap(node) {
24
+ switch (node.type) {
25
+ case "TSAsExpression":
26
+ case "TSSatisfiesExpression":
27
+ case "TSNonNullExpression":
28
+ case "TSTypeAssertion":
29
+ case "ParenthesizedExpression":
30
+ case "ChainExpression":
31
+ return unwrap(node.expression);
32
+ default:
33
+ return node;
34
+ }
35
+ }
36
+ export class Reader {
37
+ #source;
38
+ #text;
39
+ #functions;
40
+ constructor(source, functions) {
41
+ this.#source = source;
42
+ this.#text = source.getText();
43
+ this.#functions = functions;
44
+ }
45
+ site(node) {
46
+ const site = reading(null);
47
+ if (node.type === "JSXAttribute") {
48
+ const value = this.#attribute(node);
49
+ if (value === null)
50
+ return null;
51
+ this.#read(value, site, false, true);
52
+ return site;
53
+ }
54
+ if (node.type === "TaggedTemplateExpression") {
55
+ const name = this.#classFunction(node.tag);
56
+ if (name === null || name === RECIPE)
57
+ return null;
58
+ this.#read(node.quasi, site, true, true);
59
+ return site;
60
+ }
61
+ if (node.type !== "CallExpression")
62
+ return null;
63
+ const name = this.#classFunction(node.callee);
64
+ if (name === null)
65
+ return null;
66
+ if (name !== RECIPE)
67
+ for (const argument of node.arguments)
68
+ this.#read(argument, site, true, true);
69
+ else if (node.arguments[0] !== undefined)
70
+ this.#recipe(node.arguments[0], site, (slot) => this.#read(slot, site, true, true));
71
+ return site;
72
+ }
73
+ lists(node) {
74
+ const site = reading([]);
75
+ const list = (element) => ({ texts: site.texts, complete: element !== null && INTRINSIC.test(element) && !site.open && site.unreadable.length === 0, inlined: site.inlined ?? [], element });
76
+ if (node.type === "JSXAttribute") {
77
+ const value = this.#attribute(node);
78
+ if (value === null)
79
+ return [];
80
+ this.#read(value, site, false, true);
81
+ return [list(node.parent?.type === "JSXOpeningElement" ? elementName(node.parent.name) : null)];
82
+ }
83
+ const name = node.type === "TaggedTemplateExpression" ? this.#classFunction(node.tag) : node.type === "CallExpression" ? this.#classFunction(node.callee) : null;
84
+ if (name === null)
85
+ return [];
86
+ if (name !== RECIPE) {
87
+ this.#arguments(node, site, true);
88
+ return [list(null)];
89
+ }
90
+ const lists = [];
91
+ if (node.type === "CallExpression" && node.arguments[0] !== undefined) {
92
+ this.#recipe(node.arguments[0], reading(null), (slot) => {
93
+ const option = reading([]);
94
+ this.#read(slot, option, true, true);
95
+ lists.push({ texts: option.texts, complete: false, inlined: option.inlined ?? [], element: null });
96
+ });
97
+ }
98
+ return lists;
99
+ }
100
+ #attribute(node) {
101
+ if (node.type !== "JSXAttribute" || node.name.type !== "JSXIdentifier" || !CLASS_ATTRIBUTES.has(node.name.name) || node.value === null)
102
+ return null;
103
+ return node.value.type === "JSXExpressionContainer" ? node.value.expression : node.value;
104
+ }
105
+ #arguments(node, site, follow) {
106
+ if (node.type === "TaggedTemplateExpression")
107
+ this.#read(node.quasi, site, true, follow);
108
+ else if (node.type === "CallExpression")
109
+ for (const argument of node.arguments)
110
+ this.#read(argument, site, true, follow);
111
+ }
112
+ #inline(node, site, follow) {
113
+ site.inlined?.push(node);
114
+ this.#arguments(node, site, follow);
115
+ }
116
+ #branch(site, mark, read) {
117
+ const outer = site.path;
118
+ site.path = `${outer} ${mark}`;
119
+ read();
120
+ site.path = outer;
121
+ }
122
+ bind(node, depth = DEPTH) {
123
+ for (let scope = this.#source.getScope(node); scope !== null; scope = scope.upper) {
124
+ const definition = scope.set.get(node.name)?.defs[0];
125
+ if (definition === undefined)
126
+ continue;
127
+ const pattern = definition.name.parent;
128
+ const fallback = pattern?.type === "AssignmentPattern" && pattern.left === definition.name ? pattern.right : null;
129
+ if (definition.type === "Parameter")
130
+ return { kind: "parameter", fallback };
131
+ if (definition.type === "ImportBinding")
132
+ return { kind: "import" };
133
+ const declarator = definition.node;
134
+ if (definition.type !== "Variable" || declarator.type !== "VariableDeclarator" || declarator.init === null)
135
+ return { kind: "other" };
136
+ if (definition.parent?.type !== "VariableDeclaration" || definition.parent.kind !== "const")
137
+ return { kind: "other" };
138
+ if (declarator.id.type === "Identifier")
139
+ return { kind: "const", init: declarator.init };
140
+ return depth > 0 && this.fromParameter(declarator.init, depth - 1) ? { kind: "parameter", fallback } : { kind: "other" };
141
+ }
142
+ return { kind: "other" };
143
+ }
144
+ fromParameter(node, depth = DEPTH) {
145
+ let root = unwrap(node);
146
+ while (root.type === "MemberExpression")
147
+ root = unwrap(root.object);
148
+ if (root.type === "ThisExpression")
149
+ return true;
150
+ if (root.type !== "Identifier")
151
+ return false;
152
+ const binding = this.bind(root, depth);
153
+ return binding.kind === "parameter" || (binding.kind === "const" && depth > 0 && this.fromParameter(binding.init, depth - 1));
154
+ }
155
+ resolve(node) {
156
+ const inner = unwrap(node);
157
+ if (inner.type !== "Identifier")
158
+ return inner;
159
+ const binding = this.bind(inner);
160
+ return binding.kind === "const" ? unwrap(binding.init) : inner;
161
+ }
162
+ #classFunction(callee) {
163
+ return callee.type === "Identifier" && this.#functions.has(callee.name) ? callee.name : null;
164
+ }
165
+ #recipeInstance(node) {
166
+ const callee = unwrap(node);
167
+ if (callee.type === "CallExpression")
168
+ return this.#classFunction(callee.callee) === RECIPE;
169
+ if (callee.type !== "Identifier")
170
+ return false;
171
+ const binding = this.bind(callee);
172
+ if (binding.kind === "import")
173
+ return true;
174
+ const init = binding.kind === "const" ? unwrap(binding.init) : null;
175
+ return init?.type === "CallExpression" && this.#classFunction(init.callee) === RECIPE;
176
+ }
177
+ #recipeCallee(node) {
178
+ const callee = unwrap(node);
179
+ if (callee.type !== "MemberExpression")
180
+ return this.#recipeInstance(callee);
181
+ const owner = unwrap(callee.object);
182
+ const binding = !callee.computed && owner.type === "Identifier" ? this.bind(owner) : null;
183
+ const init = binding?.kind === "const" ? unwrap(binding.init) : null;
184
+ return init?.type === "CallExpression" && this.#recipeInstance(init.callee);
185
+ }
186
+ #recipeCall(node, site) {
187
+ const [first, ...rest] = node.arguments;
188
+ const selection = first === undefined ? null : unwrap(first);
189
+ if (!this.#recipeCallee(node.callee) || rest.length > 0 || (selection !== null && selection.type !== "ObjectExpression"))
190
+ return false;
191
+ if (selection?.type === "ObjectExpression")
192
+ for (const [name, value] of this.#fields(selection, site))
193
+ if (name === "className")
194
+ this.#read(value, site, true, true);
195
+ return true;
196
+ }
197
+ #push(site, node, value, exact, groups) {
198
+ site.texts.push({ value, node, groups, path: site.path, locate: (start, end) => (exact === null ? [node.start, node.end] : [exact + start, exact + end]) });
199
+ }
200
+ #template(node, site, groups, follow) {
201
+ const starts = [];
202
+ const holes = [];
203
+ let value = "";
204
+ for (const quasi of node.quasis) {
205
+ starts.push(value.length);
206
+ value += quasi.value.cooked ?? HOLE;
207
+ if (!quasi.tail)
208
+ holes.push(value.length);
209
+ if (!quasi.tail)
210
+ value += HOLE;
211
+ }
212
+ for (const span of splitTokens(value)) {
213
+ if (!span.text.includes(HOLE))
214
+ continue;
215
+ for (const [index, hole] of holes.entries()) {
216
+ const expression = node.expressions[index];
217
+ if (expression === undefined || hole < span.start || hole >= span.end)
218
+ continue;
219
+ if (span.text === HOLE)
220
+ this.#read(expression, site, groups, follow);
221
+ else
222
+ site.unreadable.push(expression);
223
+ }
224
+ value = value.slice(0, span.start) + " ".repeat(span.end - span.start) + value.slice(span.end);
225
+ }
226
+ const locate = (start, end) => {
227
+ const index = starts.findLastIndex((from) => from <= start);
228
+ const quasi = node.quasis[index];
229
+ const exact = quasi.value.raw === quasi.value.cooked ? exactStart(this.#text, quasi.start, quasi.value.raw) : null;
230
+ const from = starts[index];
231
+ return exact === null ? [quasi.start, quasi.end] : [exact + start - from, exact + end - from];
232
+ };
233
+ site.texts.push({ value, node, groups, path: site.path, locate });
234
+ }
235
+ #read(expression, site, groups, follow) {
236
+ const node = unwrap(expression);
237
+ switch (node.type) {
238
+ case "Literal":
239
+ if (typeof node.value === "string")
240
+ this.#push(site, node, node.value, node.raw.slice(1, -1) === node.value ? exactStart(this.#text, node.start, node.value) : null, groups);
241
+ return;
242
+ case "TemplateLiteral":
243
+ this.#template(node, site, groups, follow);
244
+ return;
245
+ case "ConditionalExpression":
246
+ this.#branch(site, `${node.start}-${node.end}?0`, () => this.#read(node.consequent, site, groups, follow));
247
+ this.#branch(site, `${node.start}-${node.end}?1`, () => this.#read(node.alternate, site, groups, follow));
248
+ return;
249
+ case "LogicalExpression":
250
+ if (node.operator === "&&")
251
+ this.#branch(site, `${node.start}-${node.end}&_`, () => this.#read(node.right, site, groups, follow));
252
+ else {
253
+ this.#branch(site, `${node.start}-${node.end}|0`, () => this.#read(node.left, site, groups, follow));
254
+ this.#branch(site, `${node.start}-${node.end}|1`, () => this.#read(node.right, site, groups, follow));
255
+ }
256
+ return;
257
+ case "ArrayExpression":
258
+ for (const element of node.elements)
259
+ if (element !== null)
260
+ this.#read(element, site, groups, follow);
261
+ return;
262
+ case "SpreadElement":
263
+ this.#read(node.argument, site, groups, follow);
264
+ return;
265
+ case "ObjectExpression":
266
+ for (const property of node.properties) {
267
+ if (property.type === "SpreadElement")
268
+ this.#read(property.argument, site, groups, follow);
269
+ else {
270
+ const key = property.key;
271
+ this.#branch(site, `${property.start}-${property.end}:_`, () => {
272
+ if (property.computed || key.type !== "Identifier")
273
+ this.#read(key, site, groups, follow);
274
+ else
275
+ this.#push(site, key, key.name, key.start, groups);
276
+ });
277
+ }
278
+ }
279
+ return;
280
+ case "Identifier": {
281
+ if (node.name === "undefined")
282
+ return;
283
+ const binding = this.bind(node);
284
+ const source = binding.kind === "parameter" ? binding.fallback : binding.kind === "const" && follow ? binding.init : null;
285
+ if (binding.kind === "parameter")
286
+ site.open = true;
287
+ if (source === null) {
288
+ if (binding.kind !== "parameter")
289
+ site.unreadable.push(node);
290
+ return;
291
+ }
292
+ const hop = { ...site, texts: [], unreadable: [] };
293
+ this.#read(source, hop, groups, false);
294
+ site.texts.push(...hop.texts);
295
+ site.open = hop.open;
296
+ if (hop.unreadable.length > 0)
297
+ site.unreadable.push(node);
298
+ return;
299
+ }
300
+ case "MemberExpression":
301
+ if (this.fromParameter(node))
302
+ site.open = true;
303
+ else
304
+ site.unreadable.push(node);
305
+ return;
306
+ case "CallExpression": {
307
+ const name = this.#classFunction(node.callee);
308
+ if (name !== null && name !== RECIPE && site.inlined !== null)
309
+ this.#inline(node, site, follow);
310
+ else if (name !== null || this.#recipeCall(node, site))
311
+ site.open = true;
312
+ else
313
+ site.unreadable.push(node);
314
+ return;
315
+ }
316
+ case "UnaryExpression":
317
+ if (node.operator !== "void")
318
+ site.unreadable.push(node);
319
+ return;
320
+ case "TaggedTemplateExpression": {
321
+ const name = this.#classFunction(node.tag);
322
+ if (name === null)
323
+ site.unreadable.push(node);
324
+ else if (name !== RECIPE && site.inlined !== null)
325
+ this.#inline(node, site, follow);
326
+ else
327
+ site.open = true;
328
+ return;
329
+ }
330
+ default:
331
+ site.unreadable.push(node);
332
+ }
333
+ }
334
+ #object(node, site) {
335
+ const value = this.resolve(node);
336
+ if (value.type === "ObjectExpression")
337
+ return value;
338
+ site.unreadable.push(node);
339
+ return null;
340
+ }
341
+ #fields(node, site) {
342
+ const fields = [];
343
+ for (const property of this.#object(node, site)?.properties ?? []) {
344
+ if (property.type === "SpreadElement" || property.computed)
345
+ site.unreadable.push(property);
346
+ else if (property.key.type === "Identifier")
347
+ fields.push([property.key.name, property.value]);
348
+ else if (property.key.type === "Literal")
349
+ fields.push([String(property.key.value), property.value]);
350
+ }
351
+ return fields;
352
+ }
353
+ #slots(node, site, read) {
354
+ const value = this.resolve(node);
355
+ if (value.type !== "ObjectExpression")
356
+ read(node);
357
+ else
358
+ for (const [, slot] of this.#fields(value, site))
359
+ read(slot);
360
+ }
361
+ #recipe(definition, site, read) {
362
+ for (const [field, value] of this.#fields(definition, site)) {
363
+ if (field === "defaults")
364
+ this.#slots(value, site, read);
365
+ else if (field === "variants")
366
+ for (const [, choices] of this.#fields(value, site))
367
+ for (const [, choice] of this.#fields(choices, site))
368
+ this.#slots(choice, site, read);
369
+ else if (field === "compoundVariants") {
370
+ const compounds = this.resolve(value);
371
+ if (compounds.type !== "ArrayExpression")
372
+ site.unreadable.push(value);
373
+ else
374
+ for (const compound of compounds.elements)
375
+ if (compound !== null)
376
+ for (const [name, output] of this.#fields(compound, site))
377
+ if (name === "className")
378
+ this.#slots(output, site, read);
379
+ }
380
+ }
381
+ }
382
+ }
383
+ //# sourceMappingURL=sites.js.map
@@ -0,0 +1,15 @@
1
+ export declare const CSS_MARKER = ":css:";
2
+ export interface Span {
3
+ text: string;
4
+ start: number;
5
+ end: number;
6
+ }
7
+ export interface ClassToken extends Span {
8
+ group: boolean;
9
+ }
10
+ export declare function splitTokens(value: string): Span[];
11
+ export declare function conditionsOf(token: string): string[];
12
+ export declare function groupMembers(group: Span): Span[] | null;
13
+ export declare function readTokens(value: string, groups: boolean, expand: (group: string) => string[]): ClassToken[];
14
+ export declare function exactStart(source: string, start: number, value: string): number | null;
15
+ //# sourceMappingURL=tokens.d.ts.map
package/dist/tokens.js ADDED
@@ -0,0 +1,99 @@
1
+ export const CSS_MARKER = ":css:";
2
+ const SPACES = " \t\n\r\f";
3
+ const OPENERS = "([{";
4
+ const CLOSERS = ")]}";
5
+ const isSpace = (char) => char !== undefined && SPACES.includes(char);
6
+ function cut(value, from, to, separator, offset) {
7
+ const spans = [];
8
+ let depth = 0;
9
+ let start = from;
10
+ const flush = (end) => {
11
+ let head = start;
12
+ let tail = end;
13
+ while (head < tail && isSpace(value[head]))
14
+ head++;
15
+ while (tail > head && isSpace(value[tail - 1]))
16
+ tail--;
17
+ if (tail > head)
18
+ spans.push({ text: value.slice(head, tail), start: offset + head, end: offset + tail });
19
+ };
20
+ for (let index = from; index < to; index++) {
21
+ const char = value[index];
22
+ if (char === "\\")
23
+ index++;
24
+ else if (OPENERS.includes(char))
25
+ depth++;
26
+ else if (CLOSERS.includes(char))
27
+ depth = Math.max(0, depth - 1);
28
+ else if (depth === 0 && separator(char)) {
29
+ flush(index);
30
+ start = index + 1;
31
+ }
32
+ }
33
+ flush(to);
34
+ return spans;
35
+ }
36
+ export function splitTokens(value) {
37
+ return cut(value, 0, value.length, isSpace, 0);
38
+ }
39
+ export function conditionsOf(token) {
40
+ return cut(token, 0, token.length, (char) => char === "@", 0)
41
+ .slice(token.startsWith("@") ? 0 : 1)
42
+ .map(({ text }) => text);
43
+ }
44
+ function closingBrace(text) {
45
+ let depth = 0;
46
+ for (let index = 0; index < text.length; index++) {
47
+ const char = text[index];
48
+ if (char === "\\")
49
+ index++;
50
+ else if (OPENERS.includes(char))
51
+ depth++;
52
+ else if (CLOSERS.includes(char) && --depth === 0)
53
+ return char === "}" ? index : -1;
54
+ }
55
+ return -1;
56
+ }
57
+ export function groupMembers(group) {
58
+ const close = closingBrace(group.text);
59
+ if (close < 0)
60
+ return null;
61
+ const members = [];
62
+ for (const member of cut(group.text, 1, close, (char) => char === ";", group.start)) {
63
+ if (!member.text.startsWith("{"))
64
+ members.push(member);
65
+ else {
66
+ const nested = groupMembers(member);
67
+ if (nested === null)
68
+ return null;
69
+ members.push(...nested);
70
+ }
71
+ }
72
+ return members;
73
+ }
74
+ export function readTokens(value, groups, expand) {
75
+ const spans = splitTokens(value);
76
+ const expands = groups || spans.some((span) => span.text === CSS_MARKER);
77
+ const tokens = [];
78
+ for (const span of spans) {
79
+ if (span.text === CSS_MARKER)
80
+ continue;
81
+ if (!span.text.startsWith("{"))
82
+ tokens.push({ ...span, group: false });
83
+ else if (!expands)
84
+ tokens.push({ ...span, group: true });
85
+ else {
86
+ const texts = expand(span.text);
87
+ const members = groupMembers(span);
88
+ for (const [index, text] of texts.entries()) {
89
+ const at = members?.length === texts.length ? members[index] : span;
90
+ tokens.push({ text, start: at.start, end: at.end, group: false });
91
+ }
92
+ }
93
+ }
94
+ return tokens;
95
+ }
96
+ export function exactStart(source, start, value) {
97
+ return source.startsWith(value, start + 1) ? start + 1 : null;
98
+ }
99
+ //# sourceMappingURL=tokens.js.map
package/package.json CHANGED
@@ -1,6 +1,56 @@
1
1
  {
2
2
  "name": "@teacss/linter",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.10.15",
4
+ "description": "oxlint plugin for TeaCSS: limits which classes a project may write",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/teacss/teacss-rs.git",
10
+ "directory": "packages/linter"
11
+ },
12
+ "homepage": "https://github.com/teacss/teacss-rs/tree/master/packages/linter#readme",
13
+ "main": "./dist/index.js",
14
+ "types": "./dist/index.d.ts",
15
+ "exports": {
16
+ ".": {
17
+ "types": "./dist/index.d.ts",
18
+ "default": "./dist/index.js"
19
+ },
20
+ "./package.json": "./package.json"
21
+ },
22
+ "imports": {
23
+ "#src/*": "./dist/*"
24
+ },
25
+ "files": [
26
+ "dist/**/*.js",
27
+ "dist/**/*.d.ts",
28
+ "README.md",
29
+ "LICENSE"
30
+ ],
31
+ "sideEffects": false,
32
+ "engines": {
33
+ "node": ">=26"
34
+ },
35
+ "dependencies": {
36
+ "@teacss/engine": "0.10.15"
37
+ },
38
+ "peerDependencies": {
39
+ "oxlint": "1.94.x"
40
+ },
41
+ "devDependencies": {
42
+ "@types/node": "^22.20.5",
43
+ "oxlint": "1.94.0",
44
+ "typescript": "^5.9.3"
45
+ },
46
+ "scripts": {
47
+ "build:ts": "node -e \"for (const d of ['build','dist']) require('fs').rmSync(d,{recursive:true,force:true})\" && tsc -p tsconfig.json && tsc -p tsconfig.test.json",
48
+ "build": "npm run build:ts",
49
+ "test": "npm run build:ts && node --test \"build/tests/**/*.test.js\"",
50
+ "check:types": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.test.json --noEmit"
51
+ },
52
+ "publishConfig": {
53
+ "access": "public",
54
+ "registry": "https://registry.npmjs.org/"
55
+ }
56
+ }