@atlaskit/eslint-plugin-design-system
Version:
The essential plugin for use with the Atlassian Design System.
91 lines (86 loc) • 3.91 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
/* eslint-disable @repo/internal/react/require-jsdoc */
import { isNodeOfType } from 'eslint-codemod-utils';
import { getSourceCode } from '@atlaskit/eslint-utils/context-compat';
import { Root } from '../../../ast-nodes/root';
import { getNodeSource } from '../../utils/get-node-source';
import { isDecendantOfStyleBlock } from '../../utils/is-decendant-of-style-block';
import { isDecendantOfType } from '../../utils/is-decendant-of-type';
import { findFontFamilyTokenForValue } from '../find-font-family-token-for-value';
import { insertTokensImport } from '../insert-tokens-import';
export var FontFamily = {
lint: function lint(node, _ref) {
var context = _ref.context,
config = _ref.config;
// Check whether all criteria needed to make a transformation are met
var success = FontFamily._check(node, {
context: context,
config: config
});
if (success) {
return context.report(_objectSpread({
node: node,
messageId: 'noRawFontFamilyValues'
}, config.enableUnsafeAutofix ? {
fix: FontFamily._fix(node, context)
} : {
suggest: [{
desc: 'Convert to font family token',
fix: FontFamily._fix(node, context)
}]
}));
}
},
_check: function _check(node, _ref2) {
var context = _ref2.context,
config = _ref2.config;
if (!config.patterns.includes('font-family')) {
return false;
}
if (!isNodeOfType(node, 'Property')) {
return false;
}
if (!isDecendantOfStyleBlock(node) && !isDecendantOfType(node, 'JSXExpressionContainer')) {
return false;
}
var isFontFamilyProperty = isNodeOfType(node.key, 'Identifier') && node.key.name === 'fontFamily';
var valueNodeSource = getNodeSource(getSourceCode(context), node.value);
if (isFontFamilyProperty && valueNodeSource.match(/(font\.family.|inherit)/)) {
return false;
}
return true;
},
_fix: function _fix(node, context) {
return function (fixer) {
var fixes = [];
// Type assertions to force the correct node type
if (!isNodeOfType(node.value, 'Literal')) {
return fixes;
}
if (!node.value.raw) {
return fixes;
}
// Replace raw value with token if there is a token match
var matchingToken = findFontFamilyTokenForValue(String(node.value.value));
if (!matchingToken) {
return fixes;
}
var fontWeightValueFix = fixer.replaceText(node.value, "token('".concat(matchingToken, "')"));
fixes.push(fontWeightValueFix);
// Add import if it doesn't exist
var body = getSourceCode(context).ast.body;
var tokensImportDeclarations = Root.findImportsByModule(body, '@atlaskit/tokens');
// If there is more than one `@atlaskit/tokens` import, then it becomes difficult to determine which import to transform
if (tokensImportDeclarations.length > 1) {
return fixes;
}
var tokensImportDeclaration = tokensImportDeclarations[0];
if (!tokensImportDeclaration) {
fixes.push(insertTokensImport(body, fixer));
}
return fixes;
};
}
};