in packages/eslint-plugin/src/rules/no_css_color.ts [153:252]
property: String(propertyNode.key.name),
line: String(propertyNode.value.loc.start.line),
variableName: memberExpressionRootName,
},
});
}
});
} else if (expressionRootDeclarationInit?.type === 'CallExpression') {
// TODO: if this object was returned from invoking a function the best we can do is probably validate that the method invoked is one that returns an euitheme object
}
}
return didReport;
};
/**
*
* @description style object declaration have a depth of 1, this function handles the properties of the object
*/
const handleObjectProperties = (
context: RuleContext<MessageIds, []>,
propertyParentNode: TSESTree.JSXAttribute,
property: TSESTree.ObjectLiteralElement,
reportMessage: ReportDescriptor<MessageIds>
) => {
if (property.type === 'Property') {
raiseReportIfPropertyHasInvalidCssColor(context, property, reportMessage);
} else if (property.type === 'SpreadElement') {
const spreadElementIdentifierName = (
property.argument as TSESTree.Identifier
).name;
const spreadElementDeclaration = context.sourceCode
.getScope((propertyParentNode!.value as TSESTree.JSXExpressionContainer).expression!)
.references.find(
(ref: { identifier: { name: string } }) =>
ref.identifier.name === spreadElementIdentifierName
)?.resolved;
if (!spreadElementDeclaration) {
return;
}
reportMessage = {
loc: propertyParentNode.loc,
messageId: 'noCSSColorSpecificDeclaredVariable',
data: {
// @ts-expect-error the key name is always present else this code will not execute
property: String(property.argument.name),
variableName: spreadElementIdentifierName,
line: String(property.loc.start.line),
},
};
const spreadElementDeclarationNode =
'init' in spreadElementDeclaration.defs[0].node
? spreadElementDeclaration.defs[0].node.init
: undefined;
// evaluate only statically defined declarations, other possibilities like callExpressions in this context complicate things
if (spreadElementDeclarationNode?.type === 'ObjectExpression') {
(
spreadElementDeclarationNode as TSESTree.ObjectExpression
).properties.forEach((spreadProperty) => {
handleObjectProperties(
context,
propertyParentNode,
spreadProperty,
reportMessage
);
});
}
}
};
export const NoCssColor = ESLintUtils.RuleCreator.withoutDocs({
create(context) {
return {
// accounts for instances where declarations are created using the template tagged css function
TaggedTemplateExpression(node) {
if (
node.tag.type !== 'Identifier' ||
(node.tag.type === 'Identifier' && node.tag.name !== 'css')
) {
return;
}
for (let i = 0; i < node.quasi.quasis.length; i++) {
const declarationTemplateNode = node.quasi.quasis[i];
if (
htmlElementColorDeclarationRegex.test(
declarationTemplateNode.value.raw
)
) {
const cssText = declarationTemplateNode.value.raw
.replace(/(\{|\}|\\n)/g, '')
.trim();
cssText.split(';').forEach((declaration) => {