@atlaskit/eslint-plugin-design-system
Version:
The essential plugin for use with the Atlassian Design System.
158 lines (151 loc) • 7.25 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.checkIfSupportedExport = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _contextCompat = require("@atlaskit/eslint-utils/context-compat");
var _isStyledComponent = require("./is-styled-component");
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; }
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; }
var getStack = function getStack(context, node) {
var _getSourceCode = (0, _contextCompat.getSourceCode)(context),
scopeManager = _getSourceCode.scopeManager;
var stack = {
nodes: [],
root: node
};
var scope;
for (var current = node; current.type !== 'Program'; current = current.parent) {
if (!scope) {
var currentScope = scopeManager.acquire(current);
if (currentScope) {
scope = currentScope;
}
}
switch (current.type) {
case 'ExportDefaultDeclaration':
case 'ExportNamedDeclaration':
stack.root = current;
break;
case 'VariableDeclarator':
stack.root = current;
break;
case 'ExportSpecifier':
case 'ObjectExpression':
case 'VariableDeclaration':
break;
default:
stack.nodes.unshift(current);
}
}
return _objectSpread(_objectSpread({}, stack), {}, {
scope: scope !== null && scope !== void 0 ? scope : (0, _contextCompat.getScope)(context, node)
});
};
var matches = function matches(defs, refs) {
// When there are no defs, the definition is inlined. This must be a match as we know the refs contain the initial
// definition.
if (!defs.length) {
return true;
}
// When there are no refs, the reference refers to the entire definition and therefore must be a match.
if (!refs.length) {
return true;
}
// When both the references and definitions exist, they should match in length
if (defs.length !== refs.length) {
return false;
}
return defs.every(function (def, i) {
var ref = refs[i];
if (def.type === 'Property') {
// There is a match between the def and the ref when both names match:
//
// const fooDef = { bar: '' };
// const barRef = fooDef.bar
//
// There is no match when the ref property does not match the definition key name:
//
// const barRef = fooDef.notFound
return def.key.type === 'Identifier' && ref.type === 'MemberExpression' && ref.property.type === 'Identifier' && ref.property.name === def.key.name;
}
// Anything here is either unsupported or should not match...
return false;
});
};
var _checkIfSupportedExport = exports.checkIfSupportedExport = function checkIfSupportedExport(context, node, importSources) {
var _resolved$references;
var scope = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : (0, _contextCompat.getScope)(context, node);
// Ignore any expression defined outside of the global or module scope as we have no way of statically analysing them
if (scope.type !== 'global' && scope.type !== 'module') {
return {
isExport: false
};
}
var _getStack = getStack(context, node.parent),
root = _getStack.root,
nodes = _getStack.nodes;
// Exporting a component with a css reference should be allowed
if ((0, _isStyledComponent.isStyledComponent)(nodes, context, importSources)) {
return {
isExport: false
};
}
if (root.type === 'ExportDefaultDeclaration' || root.type === 'ExportNamedDeclaration') {
return {
isExport: true,
node: root
};
}
if (root.type !== 'VariableDeclarator') {
return {
isExport: false
};
}
// Find the reference to the variable declarator
var reference = scope.references.find(function (_ref) {
var identifier = _ref.identifier;
return identifier === root.id;
});
if (!reference) {
return {
isExport: false
};
}
// Iterate through all of the references to the resolved variable declarator node
var resolved = reference.resolved;
var _iterator = _createForOfIteratorHelper((_resolved$references = resolved === null || resolved === void 0 ? void 0 : resolved.references) !== null && _resolved$references !== void 0 ? _resolved$references : []),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var identifier = _step.value.identifier;
// Skip references to the root, since it has already been processed above
if (identifier === root.id) {
continue;
}
var _getStack2 = getStack(context, identifier.parent),
refs = _getStack2.nodes,
nextScope = _getStack2.scope;
// Only validate the resolved reference if it accesses the definition node
if (matches(nodes, refs.reverse())) {
// Now validate the identifier reference as a definition
var validity = _checkIfSupportedExport(context, identifier, importSources, nextScope);
if (validity.isExport) {
return validity;
}
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return {
isExport: false
};
};