|
| 1 | +const cssVars = require('../utils/new-color-css-vars-map') |
| 2 | + |
| 3 | +const reportError = (propertyName, valueNode, context) => { |
| 4 | + // performance optimisation: exit early |
| 5 | + if (valueNode.type !== 'Literal' && valueNode.type !== 'TemplateElement') return |
| 6 | + // get property value |
| 7 | + const value = valueNode.type === 'Literal' ? valueNode.value : valueNode.value.cooked |
| 8 | + // return if value is not a string |
| 9 | + if (typeof value !== 'string') return |
| 10 | + // return if value does not include variable |
| 11 | + if (!value.includes('var(')) return |
| 12 | + |
| 13 | + const varRegex = /var\([^(),)]+\)/g |
| 14 | + |
| 15 | + const match = value.match(varRegex) |
| 16 | + // return if no matches |
| 17 | + if (!match) return |
| 18 | + const vars = match.flatMap(match => |
| 19 | + match |
| 20 | + .slice(4, -1) |
| 21 | + .trim() |
| 22 | + .split(/\s*,\s*/g), |
| 23 | + ) |
| 24 | + for (const cssVar of vars) { |
| 25 | + // return if no repalcement exists |
| 26 | + if (!cssVars?.includes(cssVar)) return |
| 27 | + // report the error |
| 28 | + context.report({ |
| 29 | + node: valueNode, |
| 30 | + message: `Expected a fallback value for CSS variable ${cssVar}. New color variables fallbacks, check primer.style/primitives to find the correct value.`, |
| 31 | + }) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +const reportOnObject = (node, context) => { |
| 36 | + const propertyName = node.key.name |
| 37 | + if (node.value?.type === 'Literal') { |
| 38 | + reportError(propertyName, node.value, context) |
| 39 | + } else if (node.value?.type === 'ConditionalExpression') { |
| 40 | + reportError(propertyName, node.value.consequent, context) |
| 41 | + reportError(propertyName, node.value.alternate, context) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +const reportOnProperty = (node, context) => { |
| 46 | + const propertyName = node.name.name |
| 47 | + if (node.value?.type === 'Literal') { |
| 48 | + reportError(propertyName, node.value, context) |
| 49 | + } else if (node.value?.type === 'JSXExpressionContainer' && node.value.expression?.type === 'ConditionalExpression') { |
| 50 | + reportError(propertyName, node.value.expression.consequent, context) |
| 51 | + reportError(propertyName, node.value.expression.alternate, context) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +const reportOnValue = (node, context) => { |
| 56 | + if (node?.type === 'Literal') { |
| 57 | + reportError(undefined, node, context) |
| 58 | + } else if (node?.type === 'JSXExpressionContainer' && node.expression?.type === 'ConditionalExpression') { |
| 59 | + reportError(undefined, node.value.expression.consequent, context) |
| 60 | + reportError(undefined, node.value.expression.alternate, context) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +const reportOnTemplateElement = (node, context) => { |
| 65 | + reportError(undefined, node, context) |
| 66 | +} |
| 67 | + |
| 68 | +module.exports = { |
| 69 | + meta: { |
| 70 | + type: 'suggestion', |
| 71 | + }, |
| 72 | + /** @param {import('eslint').Rule.RuleContext} context */ |
| 73 | + create(context) { |
| 74 | + return { |
| 75 | + // sx OR style property on elements |
| 76 | + ['JSXAttribute:matches([name.name=sx], [name.name=style]) ObjectExpression Property']: node => |
| 77 | + reportOnObject(node, context), |
| 78 | + // property on element like stroke or fill |
| 79 | + ['JSXAttribute[name.name!=sx][name.name!=style]']: node => reportOnProperty(node, context), |
| 80 | + // variable that is a value |
| 81 | + [':matches(VariableDeclarator, ReturnStatement) > Literal']: node => reportOnValue(node, context), |
| 82 | + // variable that is a value |
| 83 | + [':matches(VariableDeclarator, ReturnStatement) > TemplateElement']: node => |
| 84 | + reportOnTemplateElement(node, context), |
| 85 | + } |
| 86 | + }, |
| 87 | +} |
0 commit comments