@atlaskit/eslint-plugin-design-system
Version:
The essential plugin for use with the Atlassian Design System.
110 lines (106 loc) • 4.37 kB
JavaScript
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
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 { JSXElementHelper } from '../../../ast-nodes/jsx-element-helper';
import { Root } from '../../../ast-nodes/root';
import { addColorInheritAttributeFix } from './add-color-inherit-attribute-fix';
import { allowedAttrs } from './allowed-attrs';
import { updateTestIdAttributeFix } from './update-test-id-attribute-fix';
export var EmphasisElements = {
lint: function lint(node, _ref) {
var context = _ref.context,
config = _ref.config;
if (!isNodeOfType(node, 'JSXElement')) {
return;
}
// Check whether all criteria needed to make a transformation are met
var _EmphasisElements$_ch = EmphasisElements._check(node, {
context: context,
config: config
}),
success = _EmphasisElements$_ch.success,
autoFixable = _EmphasisElements$_ch.autoFixable;
if (success && autoFixable) {
var fix = EmphasisElements._fix(node, {
context: context,
config: config
});
context.report(_objectSpread({
node: node.openingElement,
messageId: 'preferPrimitivesText'
}, config.enableUnsafeAutofix ? {
fix: fix
} : {
suggest: [{
desc: "Convert to Text",
fix: fix
}]
}));
} else if (success && config.enableUnsafeReport) {
context.report({
node: node.openingElement,
messageId: 'preferPrimitivesText'
});
}
},
_check: function _check(node, _ref2) {
var context = _ref2.context,
config = _ref2.config;
if (!config.patterns.includes('emphasis-elements')) {
return {
success: false
};
}
var elementName = JSXElementHelper.getName(node);
if (elementName !== 'em') {
return {
success: false
};
}
if (!node.children.length) {
return {
success: false
};
}
// Element has no unallowed props
if (!JSXElementHelper.hasAllowedAttrsOnly(node, allowedAttrs)) {
return {
success: true,
autoFixable: false
};
}
// If there is more than one `@atlaskit/primitives` import, then it becomes difficult to determine which import to transform
var importDeclaration = Root.findImportsByModule(getSourceCode(context).ast.body, '@atlaskit/primitives');
if (importDeclaration.length > 1) {
return {
success: true,
autoFixable: false
};
}
return {
success: true,
autoFixable: true
};
},
_fix: function _fix(node, _ref3) {
var context = _ref3.context,
config = _ref3.config;
return function (fixer) {
var importFix = Root.upsertNamedImportDeclaration({
module: '@atlaskit/primitives',
specifiers: ['Text']
}, context, fixer);
var elementNameFixes = JSXElementHelper.updateName(node, 'Text', fixer);
var asAttributeFix = JSXElementHelper.addAttribute(node, 'as', 'em', fixer);
var colorAttributeFix = addColorInheritAttributeFix(node, config, fixer);
var testAttributeFix = updateTestIdAttributeFix(node, fixer);
return [importFix].concat(_toConsumableArray(elementNameFixes), [asAttributeFix, colorAttributeFix, testAttributeFix]).filter(function (fix) {
return Boolean(fix);
}); // Some of the transformers can return arrays with undefined, so filter them out
};
}
};