@atlaskit/eslint-plugin-ui-styling-standard
Version:
The eslint plugin to enforce and educate on Atlassian's UI Styling Standard
120 lines (119 loc) • 5.74 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
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';
var styleFunctionNames = new Set(['css', 'cssMap', 'keyframes', 'styled', 'xcss']);
var defaultStyledImportSources = new Set(['@emotion/styled', 'styled' + '-components']);
var styleCallsBySourceCode = new WeakMap();
/**
* Visit imported style calls without resolving every CallExpression against its current scope.
*
* ESLint has already linked import bindings to all of their references. Building this index once
* per source file makes each styling rule proportional to the number of style calls instead of the
* number of all calls in the file.
*/
export function getStyleCalls(context) {
var sourceCode = getSourceCode(context);
var styleCalls = styleCallsBySourceCode.get(sourceCode);
if (!styleCalls) {
styleCalls = buildStyleCallIndex(sourceCode);
styleCallsBySourceCode.set(sourceCode, styleCalls);
}
return styleCalls;
}
function buildStyleCallIndex(sourceCode) {
var styleCalls = [];
var seenCalls = new Set();
var scopeManager = sourceCode.scopeManager;
if (!scopeManager) {
return styleCalls;
}
var _iterator = _createForOfIteratorHelper(sourceCode.ast.body),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var statement = _step.value;
if (statement.type !== 'ImportDeclaration' || typeof statement.source.value !== 'string') {
continue;
}
var importSource = statement.source.value;
var _iterator2 = _createForOfIteratorHelper(statement.specifiers),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var specifier = _step2.value;
var styleFunction = getStyleFunctionFromSpecifier(specifier, importSource);
if (!styleFunction) {
continue;
}
var _scopeManager$getDecl = scopeManager.getDeclaredVariables(specifier),
_scopeManager$getDecl2 = _slicedToArray(_scopeManager$getDecl, 1),
variable = _scopeManager$getDecl2[0];
if (!variable) {
continue;
}
var _iterator3 = _createForOfIteratorHelper(variable.references),
_step3;
try {
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
var reference = _step3.value;
var call = getStyleCall(reference.identifier, styleFunction);
if (!call || seenCalls.has(call)) {
continue;
}
seenCalls.add(call);
styleCalls.push({
importSource: importSource,
node: call,
styleFunction: styleFunction
});
}
} catch (err) {
_iterator3.e(err);
} finally {
_iterator3.f();
}
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return styleCalls;
}
function getStyleFunctionFromSpecifier(specifier, importSource) {
if (specifier.type === 'ImportSpecifier' && specifier.imported.type === 'Identifier' && styleFunctionNames.has(specifier.imported.name)) {
return specifier.imported.name;
}
if (specifier.type !== 'ImportDefaultSpecifier') {
return null;
}
if (styleFunctionNames.has(specifier.local.name)) {
return specifier.local.name;
}
return defaultStyledImportSources.has(importSource) ? 'styled' : null;
}
function getStyleCall(identifier, styleFunction) {
var _parent2, _parent$parent;
var callee = identifier;
var parent = callee.parent;
while (((_parent = parent) === null || _parent === void 0 ? void 0 : _parent.type) === 'MemberExpression' && parent.object === callee) {
var _parent;
callee = parent;
parent = callee.parent;
}
if (((_parent2 = parent) === null || _parent2 === void 0 ? void 0 : _parent2.type) !== 'CallExpression' || parent.callee !== callee) {
return null;
}
if (styleFunction === 'styled' && ((_parent$parent = parent.parent) === null || _parent$parent === void 0 ? void 0 : _parent$parent.type) === 'CallExpression' && parent.parent.callee === parent) {
return parent.parent;
}
return parent;
}