UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

97 lines (92 loc) 4.57 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.FontWeight = 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 _findFontWeightTokenForValue = require("../find-font-weight-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 FontWeight = exports.FontWeight = { 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 = FontWeight._check(node, { context: context, config: config }); if (success) { return context.report(_objectSpread({ node: node, messageId: 'noRawFontWeightValues' }, config.enableUnsafeAutofix ? { fix: FontWeight._fix(node, context) } : { suggest: [{ desc: 'Convert to font weight token', fix: FontWeight._fix(node, context) }] })); } }, _check: function _check(node, _ref2) { var context = _ref2.context, config = _ref2.config; if (!config.patterns.includes('font-weight')) { return false; } if (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'Property')) { return false; } if (!(0, _isDecendantOfStyleBlock.isDecendantOfStyleBlock)(node) && !(0, _isDecendantOfType.isDecendantOfType)(node, 'JSXExpressionContainer')) { return false; } var isFontWeightProperty = (0, _eslintCodemodUtils.isNodeOfType)(node.key, 'Identifier') && node.key.name === 'fontWeight'; var valueNodeSource = (0, _getNodeSource.getNodeSource)((0, _contextCompat.getSourceCode)(context), node.value); if (isFontWeightProperty && valueNodeSource.match(/(font\.weight.|inherit)/)) { return false; } return true; }, _fix: function _fix(node, context) { return function (fixer) { var _findFontWeightTokenF; 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 = (_findFontWeightTokenF = (0, _findFontWeightTokenForValue.findFontWeightTokenForValue)(String(node.value.value))) === null || _findFontWeightTokenF === void 0 ? void 0 : _findFontWeightTokenF.tokenName; 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; }; } };