@atlaskit/eslint-plugin-design-system
Version:
The essential plugin for use with the Atlassian Design System.
46 lines (45 loc) • 2.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.lintJSXIdentifierForColor = void 0;
var _eslintCodemodUtils = require("eslint-codemod-utils");
var _contextCompat = require("@atlaskit/eslint-utils/context-compat");
var _findInParent = require("../utils/find-in-parent");
var _getIsException = require("../utils/get-is-exception");
var _includesHardCodedColor = require("../utils/includes-hard-coded-color");
var _isHardCodedColor = require("../utils/is-hard-coded-color");
var _isLegacyColor = require("../utils/is-legacy-color");
var _getTokenSuggestion = require("./get-token-suggestion");
// JSXExpressionContainer > Identifier
var lintJSXIdentifierForColor = exports.lintJSXIdentifierForColor = function lintJSXIdentifierForColor(node, context, config) {
var _definition$parent;
// To force the correct node type
if (node.type !== 'Identifier') {
return;
}
var isException = (0, _getIsException.getIsException)(config.exceptions);
if (isException(node)) {
return;
}
var variable = (0, _findInParent.findIdentifierInParentScope)({
scope: (0, _contextCompat.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' && (0, _eslintCodemodUtils.isNodeOfType)(definition.node, 'VariableDeclarator') && definition.node.init && (0, _eslintCodemodUtils.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' || !(0, _isHardCodedColor.isHardCodedColor)(value) && !(0, _includesHardCodedColor.includesHardCodedColor)(value)) {
return;
}
}
if ((0, _isLegacyColor.isLegacyColor)(node.name) || (0, _includesHardCodedColor.includesHardCodedColor)(node.name)) {
context.report({
messageId: 'hardCodedColor',
node: node,
suggest: (0, _getTokenSuggestion.getTokenSuggestion)(node, node.name, config)
});
return;
}
};