|
| 1 | +import { |
| 2 | + getDeprecatedTokens, |
| 3 | + getTaggedTemplateCaller, |
| 4 | + isPandaAttribute, |
| 5 | + isPandaIsh, |
| 6 | + isPandaProp, |
| 7 | + isRecipeVariant, |
| 8 | +} from '../utils/helpers' |
| 9 | +import { type Rule, createRule } from '../utils' |
| 10 | +import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils' |
| 11 | +import { isNodeOfTypes } from '@typescript-eslint/utils/ast-utils' |
| 12 | +import { isIdentifier, isJSXExpressionContainer, isLiteral, isTemplateLiteral } from '../utils/nodes' |
| 13 | +import type { DeprecatedToken } from '../utils/worker' |
| 14 | + |
| 15 | +export const RULE_NAME = 'no-deprecated-tokens' |
| 16 | + |
| 17 | +const rule: Rule = createRule({ |
| 18 | + name: RULE_NAME, |
| 19 | + meta: { |
| 20 | + docs: { |
| 21 | + description: 'Disallow the use of deprecated tokens within token function syntax.', |
| 22 | + }, |
| 23 | + messages: { |
| 24 | + noDeprecatedTokenPaths: '`{{token}}` is a deprecated token.', |
| 25 | + noDeprecatedTokens: '`{{token}}` is a deprecated {{category}} token.', |
| 26 | + }, |
| 27 | + type: 'problem', |
| 28 | + schema: [], |
| 29 | + }, |
| 30 | + defaultOptions: [], |
| 31 | + create(context) { |
| 32 | + // Cache for deprecated tokens to avoid redundant computations |
| 33 | + const deprecatedTokensCache = new Map<string, DeprecatedToken[]>() |
| 34 | + |
| 35 | + const sendReport = (prop: string, node: TSESTree.Node, value: string | undefined) => { |
| 36 | + if (!value) return |
| 37 | + |
| 38 | + let tokens: DeprecatedToken[] | undefined = deprecatedTokensCache.get(value) |
| 39 | + if (!tokens) { |
| 40 | + tokens = getDeprecatedTokens(prop, value, context) |
| 41 | + deprecatedTokensCache.set(value, tokens) |
| 42 | + } |
| 43 | + |
| 44 | + if (tokens.length === 0) return |
| 45 | + |
| 46 | + tokens.forEach((token) => { |
| 47 | + context.report({ |
| 48 | + node, |
| 49 | + messageId: typeof token === 'string' ? 'noDeprecatedTokenPaths' : 'noDeprecatedTokens', |
| 50 | + data: { |
| 51 | + token: typeof token === 'string' ? token : token.value, |
| 52 | + category: typeof token === 'string' ? undefined : token.category, |
| 53 | + }, |
| 54 | + }) |
| 55 | + }) |
| 56 | + } |
| 57 | + |
| 58 | + const handleLiteralOrTemplate = (prop: string, node: TSESTree.Node | undefined) => { |
| 59 | + if (!node) return |
| 60 | + |
| 61 | + if (isLiteral(node)) { |
| 62 | + const value = node.value?.toString() |
| 63 | + sendReport(prop, node, value) |
| 64 | + } else if (isTemplateLiteral(node) && node.expressions.length === 0) { |
| 65 | + const value = node.quasis[0].value.raw |
| 66 | + sendReport(prop, node.quasis[0], value) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return { |
| 71 | + JSXAttribute(node: TSESTree.JSXAttribute) { |
| 72 | + if (!node.value || !isPandaProp(node, context)) return |
| 73 | + |
| 74 | + const prop = node.name.name as string |
| 75 | + |
| 76 | + if (isLiteral(node.value)) { |
| 77 | + handleLiteralOrTemplate(prop, node.value) |
| 78 | + } else if (isJSXExpressionContainer(node.value)) { |
| 79 | + handleLiteralOrTemplate(prop, node.value.expression) |
| 80 | + } |
| 81 | + }, |
| 82 | + |
| 83 | + Property(node: TSESTree.Property) { |
| 84 | + if ( |
| 85 | + !isIdentifier(node.key) || |
| 86 | + !isNodeOfTypes([AST_NODE_TYPES.Literal, AST_NODE_TYPES.TemplateLiteral])(node.value) || |
| 87 | + !isPandaAttribute(node, context) || |
| 88 | + isRecipeVariant(node, context) |
| 89 | + ) { |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + const prop = node.key.name as string |
| 94 | + |
| 95 | + handleLiteralOrTemplate(prop, node.value) |
| 96 | + }, |
| 97 | + |
| 98 | + TaggedTemplateExpression(node: TSESTree.TaggedTemplateExpression) { |
| 99 | + const caller = getTaggedTemplateCaller(node) |
| 100 | + if (!caller || !isPandaIsh(caller, context)) return |
| 101 | + |
| 102 | + const quasis = node.quasi.quasis |
| 103 | + quasis.forEach((quasi) => { |
| 104 | + const styles = quasi.value.raw |
| 105 | + if (!styles) return |
| 106 | + |
| 107 | + // Use the same pattern as sendReport function |
| 108 | + let tokens: DeprecatedToken[] | undefined = deprecatedTokensCache.get(styles) |
| 109 | + if (!tokens) { |
| 110 | + // For template literals, we don't have a specific prop, so we use an empty string |
| 111 | + tokens = getDeprecatedTokens('', styles, context) |
| 112 | + deprecatedTokensCache.set(styles, tokens) |
| 113 | + } |
| 114 | + |
| 115 | + if (tokens.length === 0) return |
| 116 | + |
| 117 | + tokens.forEach((token) => { |
| 118 | + const tokenValue = typeof token === 'string' ? token : token.value |
| 119 | + let index = styles.indexOf(tokenValue) |
| 120 | + |
| 121 | + while (index !== -1) { |
| 122 | + const start = quasi.range[0] + index + 1 // +1 for the backtick |
| 123 | + const end = start + tokenValue.length |
| 124 | + |
| 125 | + context.report({ |
| 126 | + loc: { |
| 127 | + start: context.sourceCode.getLocFromIndex(start), |
| 128 | + end: context.sourceCode.getLocFromIndex(end), |
| 129 | + }, |
| 130 | + messageId: typeof token === 'string' ? 'noDeprecatedTokenPaths' : 'noDeprecatedTokens', |
| 131 | + data: { |
| 132 | + token: tokenValue, |
| 133 | + category: typeof token === 'string' ? undefined : token.category, |
| 134 | + }, |
| 135 | + }) |
| 136 | + |
| 137 | + // Check for other occurrences of the deprecated token |
| 138 | + index = styles.indexOf(tokenValue, index + tokenValue.length) |
| 139 | + } |
| 140 | + }) |
| 141 | + }) |
| 142 | + }, |
| 143 | + } |
| 144 | + }, |
| 145 | +}) |
| 146 | + |
| 147 | +export default rule |
0 commit comments