UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

107 lines (106 loc) • 4.34 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 _createLintRule = require("../utils/create-lint-rule"); var _checkIfSupportedExport = require("../utils/create-no-exported-rule/check-if-supported-export"); var _getCssMapObject = require("./get-css-map-object"); var _utils = require("./utils"); var IMPORT_SOURCES = [_isSupportedImport.CSS_IN_JS_IMPORTS.compiled, _isSupportedImport.CSS_IN_JS_IMPORTS.atlaskitCss]; var reportIfExported = function reportIfExported(node, context) { var state = (0, _checkIfSupportedExport.checkIfSupportedExport)(context, node, IMPORT_SOURCES); if (!state.isExport) { return; } context.report({ messageId: 'noExportedCssMap', node: state.node }); }; var reportIfNotTopLevelScope = function reportIfNotTopLevelScope(node, context) { // Treat `export` keyword as valid because the reportIfExported function already handles those var validTypes = ['ExportDefaultDeclaration', 'ExportNamedDeclaration', 'Program', 'VariableDeclaration', 'VariableDeclarator']; var parentNode = node.parent; while (parentNode) { if (!validTypes.includes(parentNode.type)) { context.report({ node: node, messageId: 'mustBeTopLevelScope' }); return; } parentNode = parentNode.parent; } }; var createCssMapRule = function createCssMapRule(context) { var _getSourceCode = (0, _contextCompat.getSourceCode)(context), text = _getSourceCode.text; if (IMPORT_SOURCES.every(function (importSource) { return !text.includes(importSource); })) { return {}; } return { CallExpression: function CallExpression(node) { var references = (0, _contextCompat.getScope)(context, node).references; if (!(0, _isSupportedImport.isCssMap)(node.callee, references, IMPORT_SOURCES)) { return; } reportIfExported(node, context); reportIfNotTopLevelScope(node, context); var cssMapObject = (0, _getCssMapObject.getCssMapObject)(node); if (!cssMapObject) { return; } var cssMapObjectChecker = new _utils.CssMapObjectChecker(cssMapObject, context); cssMapObjectChecker.run(); } }; }; var noInvalidCssMapRule = (0, _createLintRule.createLintRule)({ meta: { name: 'no-invalid-css-map', docs: { description: "Checks the validity of a CSS map created through cssMap. This is intended to be used alongside TypeScript's type-checking.", recommended: true, severity: 'error', pluginConfig: { allowedFunctionCalls: [['@atlaskit/tokens', 'token']] } }, messages: { mustBeTopLevelScope: 'Move this cssMap() declaration to module scope, outside components and callbacks; select its static variants when rendering.', noNonStaticallyEvaluable: 'Replace this cssMap value with a local build-time constant or literal. Use a static variant lookup for finite choices, or the style prop for runtime-only values.', noExportedCssMap: 'Remove the cssMap export and keep its declaration beside its usage. Export a component if styling must be shared across files.', noInlineFunctions: 'Replace the function inside cssMap with static variants selected in JSX, or pass runtime-only values through the style prop.', noFunctionCalls: 'Replace this function call inside cssMap with a build-time literal or an allowed token() call. Pass runtime-only values through the style prop.', noSpreadElement: 'Declare cssMap variant properties directly. Compose local variants where they are applied with css={[a, b]} or xcss={cx(a, b)} instead of spreading styles.' }, schema: [{ type: 'object', properties: { allowedFunctionCalls: { type: 'array', items: { type: 'array', minItems: 2, maxItems: 2, items: [{ type: 'string' }, { type: 'string' }] }, uniqueItems: true } }, additionalProperties: false }], type: 'problem' }, create: createCssMapRule }); var _default = exports.default = noInvalidCssMapRule;