UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

47 lines (46 loc) 1.91 kB
import { getIsException } from '../utils/get-is-exception'; import { isChildOfType } from '../utils/is-child-of-type'; import { isDecendantOfGlobalToken } from '../utils/is-decendant-of-global-token'; import { isDecendantOfStyleBlock } from '../utils/is-decendant-of-style-block'; import { isLegacyElevation } from '../utils/is-elevation'; import { isLegacyColor } from '../utils/is-legacy-color'; import { isLegacyNamedColor } from '../utils/is-legacy-named-color'; import { getElevationTokenExample } from './get-elevation-token-example'; import { getTokenSuggestion } from './get-token-suggestion'; var getNodeColumn = function getNodeColumn(node) { return node.loc ? node.loc.start.column : 0; }; // TemplateLiteral > Identifier export var lintTemplateIdentifierForColor = function lintTemplateIdentifierForColor(node, context, config) { if (node.type !== 'Identifier') { return; } if (isDecendantOfGlobalToken(node) || !isDecendantOfStyleBlock(node)) { return; } var elevation = isLegacyElevation(node.name); if (elevation) { context.report({ messageId: 'legacyElevation', node: node, data: { example: getElevationTokenExample(elevation) }, fix: function fix(fixer) { if (isChildOfType(node, 'TemplateLiteral') && node.range) { return fixer.replaceTextRange([node.range[0] - 2, node.range[1] + 1], "background-color: ${token('".concat(elevation.background, "')};\n").concat(' '.repeat(getNodeColumn(node) - 2), "box-shadow: ${token('").concat(elevation.shadow, "')}")); } return null; } }); } var isException = getIsException(config.exceptions); if (isLegacyColor(node.name) || isLegacyNamedColor(node.name) && !isException(node)) { context.report({ messageId: 'hardCodedColor', node: node, suggest: getTokenSuggestion(node, node.name, config) }); return; } };