UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

157 lines (150 loc) 6.63 kB
import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; 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 { getSourceCode } from '@atlaskit/eslint-utils/context-compat'; import { findVariable } from '@atlaskit/eslint-utils/find-variable'; export var getCssMapObject = function getCssMapObject(node) { // We assume the argument `node` is already a cssMap() call. // Things like the number of arguments to cssMap and the type of // cssMap's argument are handled by the TypeScript compiler, so // we don't bother with creating eslint errors for these here if (node.arguments.length !== 1 || node.arguments[0].type !== 'ObjectExpression') { return; } return node.arguments[0]; }; // eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports export var UnusedCssMapChecker = /*#__PURE__*/function () { function UnusedCssMapChecker(cssMapObject, context, cssMapCallNode) { _classCallCheck(this, UnusedCssMapChecker); this.cssMapObject = cssMapObject; this.cssMapCallNode = cssMapCallNode; this.report = context.report; this.context = context; } return _createClass(UnusedCssMapChecker, [{ key: "checkForUnusedStyles", value: function checkForUnusedStyles() { // Get all defined style keys var definedStyles = new Map(); var _iterator = _createForOfIteratorHelper(this.cssMapObject.properties), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var _property = _step.value; if (_property.type === 'Property' && _property.key.type === 'Identifier') { definedStyles.set(_property.key.name, _property); } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } if (definedStyles.size === 0) { return; } // Find the variable that holds the cssMap result var cssMapVariable = this.findCssMapVariable(); if (!cssMapVariable) { return; } // Early return if no references - all styles are unused if (cssMapVariable.references.length === 0) { var _iterator2 = _createForOfIteratorHelper(definedStyles), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var _step2$value = _slicedToArray(_step2.value, 2), styleName = _step2$value[0], property = _step2$value[1]; this.report({ node: property.key, messageId: 'unusedCssMapStyle', data: { styleName: styleName } }); } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } return; } var usedStyles = new Set(); var _iterator3 = _createForOfIteratorHelper(cssMapVariable.references), _step3; try { for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) { var ref = _step3.value; var node = ref.identifier; var parent = node.parent; if ((parent === null || parent === void 0 ? void 0 : parent.type) === 'MemberExpression') { if (!parent.computed && parent.property.type === 'Identifier') { // Static access: styles.danger (not computed) usedStyles.add(parent.property.name); // Early exit if all styles are found if (usedStyles.size === definedStyles.size) { return; } } else { // Dynamic access: styles[key], styles['danger'], etc. (computed) // Immediately exit - no styles will be reported as unused return; } } } // No dynamic access - report all unused styles } catch (err) { _iterator3.e(err); } finally { _iterator3.f(); } var _iterator4 = _createForOfIteratorHelper(definedStyles), _step4; try { for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) { var _step4$value = _slicedToArray(_step4.value, 2), _styleName = _step4$value[0], _property2 = _step4$value[1]; if (!usedStyles.has(_styleName)) { this.report({ node: _property2.key, messageId: 'unusedCssMapStyle', data: { styleName: _styleName } }); } } } catch (err) { _iterator4.e(err); } finally { _iterator4.f(); } } }, { key: "findCssMapVariable", value: function findCssMapVariable() { var callNode = this.cssMapCallNode; var parent = callNode.parent; if ((parent === null || parent === void 0 ? void 0 : parent.type) === 'VariableDeclarator' && parent.id.type === 'Identifier') { return findVariable({ identifier: parent.id, sourceCode: getSourceCode(this.context) }); } return null; } }, { key: "run", value: function run() { this.checkForUnusedStyles(); } }]); }();