UNPKG

@atlaskit/eslint-plugin-ui-styling-standard

Version:

The eslint plugin to enforce and educate on Atlassian's UI Styling Standard

52 lines (51 loc) • 1.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _contextCompat = require("@atlaskit/eslint-utils/context-compat"); var _isSupportedImport = require("@atlaskit/eslint-utils/is-supported-import"); var _createRule = require("../utils/create-rule"); var IMPORT_SOURCES = [_isSupportedImport.CSS_IN_JS_IMPORTS.compiled, _isSupportedImport.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 = (0, _createRule.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' && (0, _isSupportedImport.isCxFunction)(node.callee, (0, _contextCompat.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' }); } } }; } }); var _default = exports.default = rule;