@atlaskit/eslint-plugin-design-system
Version:
The essential plugin for use with the Atlassian Design System.
96 lines (91 loc) • 4.42 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.FontFamily = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _eslintCodemodUtils = require("eslint-codemod-utils");
var _contextCompat = require("@atlaskit/eslint-utils/context-compat");
var _root = require("../../../ast-nodes/root");
var _getNodeSource = require("../../utils/get-node-source");
var _isDecendantOfStyleBlock = require("../../utils/is-decendant-of-style-block");
var _isDecendantOfType = require("../../utils/is-decendant-of-type");
var _findFontFamilyTokenForValue = require("../find-font-family-token-for-value");
var _insertTokensImport = require("../insert-tokens-import");
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) { (0, _defineProperty2.default)(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 */
var FontFamily = exports.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 (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'Property')) {
return false;
}
if (!(0, _isDecendantOfStyleBlock.isDecendantOfStyleBlock)(node) && !(0, _isDecendantOfType.isDecendantOfType)(node, 'JSXExpressionContainer')) {
return false;
}
var isFontFamilyProperty = (0, _eslintCodemodUtils.isNodeOfType)(node.key, 'Identifier') && node.key.name === 'fontFamily';
var valueNodeSource = (0, _getNodeSource.getNodeSource)((0, _contextCompat.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 (!(0, _eslintCodemodUtils.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 = (0, _findFontFamilyTokenForValue.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 = (0, _contextCompat.getSourceCode)(context).ast.body;
var tokensImportDeclarations = _root.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((0, _insertTokensImport.insertTokensImport)(body, fixer));
}
return fixes;
};
}
};