@atlaskit/eslint-plugin-design-system
Version:
The essential plugin for use with the Atlassian Design System.
35 lines • 1.91 kB
JavaScript
import { getSourceCode } from '@atlaskit/eslint-utils/context-compat';
import { findVariable } from '@atlaskit/eslint-utils/find-variable';
const importSources = new Set(['@atlaskit/primitives', '@atlaskit/primitives/xcss', '@atlaskit/primitives/xcss/xcss']);
export function isXcssValue(node, context) {
var _variable$defs$some;
let current = node;
while (current.parent) {
const parent = current.parent;
if (parent.type === 'Property' && parent.value === current) {
current = parent;
} else if (parent.type === 'ObjectExpression') {
current = parent;
} else {
break;
}
}
const call = current.parent;
if ((call === null || call === void 0 ? void 0 : call.type) !== 'CallExpression' || call.arguments[0] !== current) {
return false;
}
const callee = call.callee;
const isNamespaceCall = callee.type === 'MemberExpression' && !callee.computed && callee.property.type === 'Identifier' && callee.property.name === 'xcss';
const identifier = callee.type === 'Identifier' ? callee : isNamespaceCall ? callee.object : null;
if ((identifier === null || identifier === void 0 ? void 0 : identifier.type) !== 'Identifier') {
return false;
}
const variable = findVariable({
identifier,
sourceCode: getSourceCode(context)
});
return (_variable$defs$some = variable === null || variable === void 0 ? void 0 : variable.defs.some(def => {
var _def$parent;
return def.type === 'ImportBinding' && ((_def$parent = def.parent) === null || _def$parent === void 0 ? void 0 : _def$parent.type) === 'ImportDeclaration' && importSources.has(String(def.parent.source.value)) && (isNamespaceCall ? def.node.type === 'ImportNamespaceSpecifier' : def.node.type === 'ImportSpecifier' && def.node.imported.type === 'Identifier' && def.node.imported.name === 'xcss');
})) !== null && _variable$defs$some !== void 0 ? _variable$defs$some : false;
}