UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

248 lines (241 loc) • 10.1 kB
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } import { isNodeOfType } from 'eslint-codemod-utils'; import { createLintRule } from '../utils/create-lint-rule'; /** * The value of a static string: `'popover'` or `` `popover` ``. `null` for anything else. */ function getStaticString(node) { if (!node) { return null; } if (isNodeOfType(node, 'Literal') && typeof node.value === 'string') { return node.value; } if (isNodeOfType(node, 'TemplateLiteral') && node.expressions.length === 0) { var _node$quasis$0$value$, _node$quasis$; return (_node$quasis$0$value$ = (_node$quasis$ = node.quasis[0]) === null || _node$quasis$ === void 0 ? void 0 : _node$quasis$.value.cooked) !== null && _node$quasis$0$value$ !== void 0 ? _node$quasis$0$value$ : null; } return null; } /** * `true` for a string value: a static string, `'auto' as const`, or a ternary with a static string * branch, such as `isHint ? 'hint' : 'auto'`. */ function isStringValue(node) { if (getStaticString(node) !== null) { return true; } var _ref = node, type = _ref.type; if (type === 'TSAsExpression' || type === 'TSSatisfiesExpression') { return isStringValue(node.expression); } if (isNodeOfType(node, 'ConditionalExpression')) { return isStringValue(node.consequent) || isStringValue(node.alternate); } return false; } /** * Calls that create an element from a tag name: `createElement('dialog')` (from `document`, `React` * or an import) and the JSX runtime's `jsx('dialog')`. */ var createElementNames = new Set(['createElement', 'jsx', 'jsxs', 'jsxDEV']); /** * The static property name of `element.name` or `element['name']`. `null` if it is not static. */ function getPropertyName(node) { if (!node.computed && isNodeOfType(node.property, 'Identifier')) { return node.property.name; } if (node.computed) { return getStaticString(node.property); } return null; } /** * `true` for an HTML element name, such as `div` or `my-element`. A component name starts with a * capital letter. */ function isIntrinsicElementName(name) { return /^[a-z]/.test(name); } /** * The HTML element that a JSX tag renders: `div` for `<div>`, and for a wrapper such as * `<motion.div>`. `null` for a component, such as `<Popup>` or `<Layers.Item>`. */ function getIntrinsicElementName(name) { if (isNodeOfType(name, 'JSXIdentifier')) { return isIntrinsicElementName(name.name) ? name.name : null; } if (isNodeOfType(name, 'JSXMemberExpression')) { return isIntrinsicElementName(name.property.name) ? name.property.name : null; } return null; } /** * `true` for props that set `popover`: `{ popover: 'auto' }` or `{ popover }`, also inside a ternary * or `&&`, such as `isOpen ? { popover: 'auto' } : {}`. Props only known at runtime, such as * `{...props}`, are not checked. */ function hasPopoverProp(node) { if (!node) { return false; } if (isNodeOfType(node, 'ObjectExpression')) { return node.properties.some(function (property) { return isNodeOfType(property, 'Property') && (!property.computed && isNodeOfType(property.key, 'Identifier') && property.key.name === 'popover' || getStaticString(property.key) === 'popover'); }); } if (isNodeOfType(node, 'ConditionalExpression')) { return hasPopoverProp(node.consequent) || hasPopoverProp(node.alternate); } if (isNodeOfType(node, 'LogicalExpression')) { return hasPopoverProp(node.right); } return false; } var rule = createLintRule({ meta: { name: 'no-native-popover-or-dialog', type: 'problem', docs: { description: 'Disallow native `popover` and `<dialog>` elements. Use a Design System component, or `@atlaskit/top-layer` if no Design System component fits.', recommended: false, severity: 'error' }, messages: { noNativeDialog: 'Use a Design System component instead of a native `<dialog>`, such as Modal dialog or Drawer. Use `@atlaskit/top-layer` only if no Design System component fits.', noNativePopover: 'Use a Design System component instead of a native `popover`, such as Popup, Dropdown menu, Tooltip, Inline dialog or Flag. Use `@atlaskit/top-layer` only if no Design System component fits.' } }, create: function create(context) { return { // <dialog>, <motion.dialog>, <div {...{ popover: 'auto' }}> JSXOpeningElement: function JSXOpeningElement(node) { if (!isNodeOfType(node, 'JSXOpeningElement')) { return; } var elementName = getIntrinsicElementName(node.name); if (!elementName) { return; } if (elementName === 'dialog') { context.report({ node: node.name, messageId: 'noNativeDialog' }); } var _iterator = _createForOfIteratorHelper(node.attributes), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var attribute = _step.value; if (isNodeOfType(attribute, 'JSXSpreadAttribute') && hasPopoverProp(attribute.argument)) { context.report({ node: attribute, messageId: 'noNativePopover' }); } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } }, // <div popover="auto">, <motion.div popover="auto"> JSXAttribute: function (_JSXAttribute) { function JSXAttribute(_x) { return _JSXAttribute.apply(this, arguments); } JSXAttribute.toString = function () { return _JSXAttribute.toString(); }; return JSXAttribute; }(function (node) { if (!isNodeOfType(node, 'JSXAttribute')) { return; } if (!isNodeOfType(node.name, 'JSXIdentifier') || node.name.name !== 'popover') { return; } // A component can take its own `popover` prop. var parent = node.parent; if (!parent || !isNodeOfType(parent, 'JSXOpeningElement') || !getIntrinsicElementName(parent.name)) { return; } context.report({ node: node, messageId: 'noNativePopover' }); }), // document.createElement('dialog'), createElement('div', { popover: 'auto' }), // styled('dialog'), element.setAttribute('popover', 'auto') CallExpression: function CallExpression(node) { var _getStaticString; if (!isNodeOfType(node, 'CallExpression')) { return; } var method = null; if (isNodeOfType(node.callee, 'MemberExpression')) { method = getPropertyName(node.callee); } else if (isNodeOfType(node.callee, 'Identifier')) { method = node.callee.name; } var firstArgument = (_getStaticString = getStaticString(node.arguments[0])) === null || _getStaticString === void 0 ? void 0 : _getStaticString.toLowerCase(); if (method && createElementNames.has(method) && firstArgument) { if (firstArgument === 'dialog') { context.report({ node: node, messageId: 'noNativeDialog' }); } else if (isIntrinsicElementName(firstArgument) && hasPopoverProp(node.arguments[1])) { context.report({ node: node, messageId: 'noNativePopover' }); } return; } if (method === 'styled' && firstArgument === 'dialog') { context.report({ node: node, messageId: 'noNativeDialog' }); return; } if ((method === 'setAttribute' || method === 'toggleAttribute') && firstArgument === 'popover') { context.report({ node: node, messageId: 'noNativePopover' }); } }, // styled.dialog MemberExpression: function MemberExpression(node) { if (isNodeOfType(node, 'MemberExpression') && isNodeOfType(node.object, 'Identifier') && node.object.name === 'styled' && getPropertyName(node) === 'dialog') { context.report({ node: node, messageId: 'noNativeDialog' }); } }, // element.popover = 'auto' AssignmentExpression: function AssignmentExpression(node) { if (!isNodeOfType(node, 'AssignmentExpression') || !isNodeOfType(node.left, 'MemberExpression') || getPropertyName(node.left) !== 'popover') { return; } if (!isStringValue(node.right)) { return; } context.report({ node: node, messageId: 'noNativePopover' }); } }; } }); export default rule;