|
| 1 | +import type * as AST from "@eslint-react/ast"; |
| 2 | +import { unit } from "@eslint-react/eff"; |
| 3 | +import { identity } from "@eslint-react/eff"; |
1 | 4 | import type { RuleContext } from "@eslint-react/kit"; |
2 | | -import * as VAR from "@eslint-react/var"; |
| 5 | +import type { TSESTree } from "@typescript-eslint/types"; |
3 | 6 | import { AST_NODE_TYPES as T } from "@typescript-eslint/types"; |
4 | | -import type { TSESTree } from "@typescript-eslint/utils"; |
| 7 | +import { getStaticValue } from "@typescript-eslint/utils/ast-utils"; |
5 | 8 | import { match, P } from "ts-pattern"; |
6 | 9 |
|
7 | 10 | /** |
8 | | - * Extracts the value of a JSX attribute by name |
9 | | - * @param context - ESLint rule context |
10 | | - * @param node - JSX attribute or spread attribute node |
11 | | - * @param name - Name of the attribute to extract |
12 | | - * @returns The extracted attribute value in a structured format |
| 11 | + * Represents possible JSX attribute value types that can be resolved |
13 | 12 | */ |
14 | | -export function getAttributeValue( |
15 | | - context: RuleContext, |
16 | | - node: TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute, |
17 | | - name: string, |
18 | | -): Exclude<VAR.LazyValue, { kind: "lazy" }> { |
19 | | - // Get the initial scope from the node's context |
20 | | - const initialScope = context.sourceCode.getScope(node); |
21 | | - switch (node.type) { |
22 | | - case T.JSXAttribute: |
23 | | - // Case 1: Literal value (e.g., className="container") |
24 | | - if (node.value?.type === T.Literal) { |
| 13 | +export type AttributeValue = |
| 14 | + | { kind: "boolean"; toStatic(): true } // Boolean attributes (e.g., disabled) |
| 15 | + // | { kind: "default"; toStatic(): unit | string } // Default attribute values |
| 16 | + | { kind: "element"; node: TSESTree.JSXElement; toStatic(): unknown } // JSX element as value (e.g., <Component element=<JSXElement /> />) |
| 17 | + | { kind: "literal"; node: TSESTree.Literal; toStatic(): TSESTree.Literal["value"] } // Literal values |
| 18 | + | { kind: "expression"; node: TSESTree.JSXExpressionContainer["expression"]; toStatic(): unknown } // Expression attributes (e.g., {value}, {...props}) |
| 19 | + | { kind: "spreadProps"; node: TSESTree.JSXSpreadAttribute["argument"]; toStatic(name?: string): unknown } // Spread props (e.g., {...props}) |
| 20 | + | { kind: "spreadChild"; node: TSESTree.JSXSpreadChild["expression"]; toStatic(): unknown }; // Spread children (e.g., {...["Hello", " ", "spread", " ", "children"]}) |
| 21 | + |
| 22 | +export function resolveAttributeValue(context: RuleContext, attribute: AST.TSESTreeJSXAttributeLike) { |
| 23 | + const initialScope = context.sourceCode.getScope(attribute); |
| 24 | + function handleJsxAttribute(node: TSESTree.JSXAttribute) { |
| 25 | + // Case 1: Boolean attribute with no value (e.g., disabled) |
| 26 | + if (node.value == null) { |
| 27 | + return { |
| 28 | + kind: "boolean", |
| 29 | + toStatic() { |
| 30 | + return true; |
| 31 | + }, |
| 32 | + } as const satisfies AttributeValue; |
| 33 | + } |
| 34 | + switch (node.value.type) { |
| 35 | + // Case 2: Literal value (e.g., className="container") |
| 36 | + case T.Literal: { |
| 37 | + const staticValue = node.value.value; |
25 | 38 | return { |
26 | | - kind: "some", |
| 39 | + kind: "literal", |
27 | 40 | node: node.value, |
28 | | - initialScope, |
29 | | - value: node.value.value, |
30 | | - } as const; |
| 41 | + toStatic() { |
| 42 | + return staticValue; |
| 43 | + }, |
| 44 | + } as const satisfies AttributeValue; |
31 | 45 | } |
32 | | - // Case 2: Expression container (e.g., className={variable}) |
33 | | - if (node.value?.type === T.JSXExpressionContainer) { |
34 | | - return VAR.toStaticValue({ |
35 | | - kind: "lazy", |
36 | | - node: node.value.expression, |
37 | | - initialScope, |
38 | | - }); |
39 | | - } |
40 | | - // Case 3: Boolean attribute with no value (e.g., disabled) |
41 | | - return { kind: "none", node, initialScope } as const; |
42 | | - case T.JSXSpreadAttribute: { |
43 | | - // For spread attributes (e.g., {...props}), try to extract static value |
44 | | - const staticValue = VAR.toStaticValue({ |
45 | | - kind: "lazy", |
46 | | - node: node.argument, |
47 | | - initialScope, |
48 | | - }); |
49 | | - // If can't extract static value, return none |
50 | | - if (staticValue.kind === "none") { |
51 | | - return staticValue; |
| 46 | + // Case 3: Expression container (e.g., className={variable}) |
| 47 | + case T.JSXExpressionContainer: { |
| 48 | + const expr = node.value.expression; |
| 49 | + return { |
| 50 | + kind: "expression", |
| 51 | + node: expr, |
| 52 | + toStatic() { |
| 53 | + return getStaticValue(expr, initialScope)?.value; |
| 54 | + }, |
| 55 | + } as const satisfies AttributeValue; |
52 | 56 | } |
53 | | - // If spread object contains the named property, extract its value |
54 | | - return match(staticValue.value) |
55 | | - .with({ [name]: P.select(P.any) }, (value) => ({ |
56 | | - kind: "some", |
57 | | - node: node.argument, |
58 | | - initialScope, |
59 | | - value, |
60 | | - } as const)) |
61 | | - .otherwise(() => ({ kind: "none", node, initialScope } as const)); |
| 57 | + // Case 4: JSX Element as value (e.g., element=<JSXElement />) |
| 58 | + case T.JSXElement: |
| 59 | + return { |
| 60 | + kind: "element", |
| 61 | + node: node.value, |
| 62 | + toStatic() { |
| 63 | + return unit; |
| 64 | + }, |
| 65 | + } as const satisfies AttributeValue; |
| 66 | + // Case 5: JSX spread children (e.g., <div>{...["Hello", " ", "spread", " ", "children"]}</div>) |
| 67 | + case T.JSXSpreadChild: |
| 68 | + return { |
| 69 | + kind: "spreadChild", |
| 70 | + node: node.value.expression, |
| 71 | + toStatic() { |
| 72 | + return unit; |
| 73 | + }, |
| 74 | + } as const satisfies AttributeValue; |
62 | 75 | } |
63 | | - default: |
64 | | - // Fallback case for unknown node types |
65 | | - return { kind: "none", node, initialScope } as const; |
| 76 | + } |
| 77 | + |
| 78 | + function handleJsxSpreadAttribute(node: TSESTree.JSXSpreadAttribute) { |
| 79 | + // For spread attributes (e.g., {...props}), try to extract static value |
| 80 | + return { |
| 81 | + kind: "spreadProps", |
| 82 | + node: node.argument, |
| 83 | + toStatic(name?: string) { |
| 84 | + if (name == null) return unit; |
| 85 | + // If spread object contains the named property, extract its value |
| 86 | + return match(getStaticValue(node.argument, initialScope)?.value) |
| 87 | + .with({ [name]: P.select(P.any) }, identity) |
| 88 | + .otherwise(() => unit); |
| 89 | + }, |
| 90 | + } as const satisfies AttributeValue; |
| 91 | + } |
| 92 | + switch (attribute.type) { |
| 93 | + case T.JSXAttribute: |
| 94 | + return handleJsxAttribute(attribute); |
| 95 | + case T.JSXSpreadAttribute: |
| 96 | + return handleJsxSpreadAttribute(attribute); |
66 | 97 | } |
67 | 98 | } |
0 commit comments