UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

40 lines • 1.92 kB
import { isNodeOfType } from 'eslint-codemod-utils'; import { getScope } from '@atlaskit/eslint-utils/context-compat'; import { findIdentifierInParentScope } from '../utils/find-in-parent'; import { getIsException } from '../utils/get-is-exception'; import { includesHardCodedColor } from '../utils/includes-hard-coded-color'; import { isHardCodedColor } from '../utils/is-hard-coded-color'; import { isLegacyColor } from '../utils/is-legacy-color'; import { getTokenSuggestion } from './get-token-suggestion'; // JSXExpressionContainer > Identifier export var lintJSXIdentifierForColor = function lintJSXIdentifierForColor(node, context, config) { var _definition$parent; // To force the correct node type if (node.type !== 'Identifier') { return; } var isException = getIsException(config.exceptions); if (isException(node)) { return; } var variable = findIdentifierInParentScope({ scope: getScope(context, node), identifierName: node.name }); var definition = variable === null || variable === void 0 ? void 0 : variable.defs[0]; if ((definition === null || definition === void 0 ? void 0 : definition.type) === 'Variable' && ((_definition$parent = definition.parent) === null || _definition$parent === void 0 ? void 0 : _definition$parent.kind) === 'const' && isNodeOfType(definition.node, 'VariableDeclarator') && definition.node.init && isNodeOfType(definition.node.init, 'Literal')) { var value = definition.node.init.value; // A color word in an identifier does not make an account ID or other constant a color. if (typeof value !== 'string' || !isHardCodedColor(value) && !includesHardCodedColor(value)) { return; } } if (isLegacyColor(node.name) || includesHardCodedColor(node.name)) { context.report({ messageId: 'hardCodedColor', node: node, suggest: getTokenSuggestion(node, node.name, config) }); return; } };