@atlaskit/eslint-plugin-ui-styling-standard
Version:
The eslint plugin to enforce and educate on Atlassian's UI Styling Standard
46 lines • 1.77 kB
JavaScript
import { getScope } from '@atlaskit/eslint-utils/context-compat';
import { CSS_IN_JS_IMPORTS, isCxFunction } from '@atlaskit/eslint-utils/is-supported-import';
import { createLintRule } from '../utils/create-rule';
var IMPORT_SOURCES = [CSS_IN_JS_IMPORTS.compiled, CSS_IN_JS_IMPORTS.atlaskitCss];
function getParentJSXAttribute(node) {
var parent = node.parent;
while (parent && parent.type !== 'JSXAttribute') {
parent = parent.parent;
}
if (parent && parent.type === 'JSXAttribute') {
return parent;
}
return null;
}
var rule = createLintRule({
meta: {
name: 'local-cx-xcss',
docs: {
description: 'Ensures the cx() function, which is part of the XCSS API, is only used within the xcss prop. This aids tracking what styles are applied to a jsx element.',
recommended: true,
severity: 'error'
},
messages: {
'local-cx-xcss': "Call cx() directly inside the xcss prop, for example xcss={cx(styles.base, active && styles.active)}, instead of storing its result elsewhere."
},
type: 'problem'
},
create: function create(context) {
return {
'CallExpression[callee.name="cx"]': function CallExpressionCalleeNameCx(node) {
if (node.type === 'CallExpression' && isCxFunction(node.callee, getScope(context, node).references, IMPORT_SOURCES)) {
var parentJSXAttribute = getParentJSXAttribute(node);
var propName = parentJSXAttribute === null || parentJSXAttribute === void 0 ? void 0 : parentJSXAttribute.name.name.toString();
if (propName && /[xX]css$/.test(propName)) {
return;
}
context.report({
node: node,
messageId: 'local-cx-xcss'
});
}
}
};
}
});
export default rule;