UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

124 lines (116 loc) 4.52 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.rule = exports.default = void 0; var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _eslintCodemodUtils = require("eslint-codemod-utils"); var _createLintRule = require("../utils/create-lint-rule"); var schema = { type: 'array', items: [{ type: 'object', properties: { autofix: { type: 'boolean' } }, additionalProperties: false }] }; var defaultConfig = { autofix: false }; function readConfig(context) { var _ref = context.options, _ref2 = (0, _slicedToArray2.default)(_ref, 1), config = _ref2[0]; return Object.assign({}, defaultConfig, config); } var PACKAGE_NAME = '@atlaskit/primitives'; var COMPILED_PACKAGE_NAME = '@atlaskit/primitives/compiled'; /** * The set of Emotion entrypoints that have a direct Compiled equivalent under * `@atlaskit/primitives/compiled/<subpath>`. Imports from these entrypoints can be * safely autofixed by prefixing the subpath with `compiled/`. */ var AUTOFIXABLE_ENTRYPOINTS = new Set(['anchor', 'bleed', 'box', 'flex', 'grid', 'hide', 'inline', 'media-helper', 'metric-text', 'pressable', 'show', 'stack', 'surface-provider', 'text']); /** * Determines whether an import source points at an Emotion `@atlaskit/primitives` entrypoint * that should be migrated to Compiled. * * Returns the suggested Compiled import source to use for the autofix, or `null` when the * import should be ignored (i.e. it is not an `@atlaskit/primitives` import, or it is already * a Compiled entrypoint). * * When the import is an Emotion entrypoint that has no direct Compiled equivalent, the rule * still reports a violation but there is no safe autofix; in that case the original source is * returned so the fixer becomes a no-op replacement. */ function getCompiledSource(importSource) { // The barrel import, e.g. `@atlaskit/primitives`. if (importSource === PACKAGE_NAME) { return COMPILED_PACKAGE_NAME; } // Ignore anything that isn't scoped to `@atlaskit/primitives/...`. if (!importSource.startsWith("".concat(PACKAGE_NAME, "/"))) { return null; } var subpath = importSource.slice("".concat(PACKAGE_NAME, "/").length); // Compiled entrypoints (`@atlaskit/primitives/compiled` and `@atlaskit/primitives/compiled/*`) // are already valid and should be ignored. if (subpath === 'compiled' || subpath.startsWith('compiled/')) { return null; } // Emotion entrypoint with a known Compiled equivalent — safe to autofix. if (AUTOFIXABLE_ENTRYPOINTS.has(subpath)) { return "".concat(COMPILED_PACKAGE_NAME, "/").concat(subpath); } // Emotion entrypoint without a direct Compiled equivalent (e.g. `xcss`, `constants`, // `responsive/*`). Report the violation but leave the source unchanged so no broken // autofix is produced. return importSource; } var rule = exports.rule = (0, _createLintRule.createLintRule)({ meta: { name: 'no-emotion-primitives', type: 'problem', fixable: 'code', docs: { description: 'Ensures usage of Compiled Primitives import instead of Emotion entrypoint.', severity: 'warn', recommended: false }, messages: { 'no-emotion-primitives': 'Use @atlaskit/primitives/compiled instead of @atlaskit/primitives. Refer to go/akcss for more information.' }, schema: schema }, create: function create(context) { var config = readConfig(context); return { ImportDeclaration: function ImportDeclaration(node) { var importSource = node.source.value; var compiledSource = getCompiledSource(importSource); if (compiledSource === null) { return; } context.report({ node: node.source, messageId: 'no-emotion-primitives', fix: function fix(fixer) { // No safe autofix when there is no Compiled equivalent for this entrypoint. if (!config.autofix || compiledSource === importSource) { return null; } var newSource = (0, _eslintCodemodUtils.literal)(compiledSource); return fixer.replaceText(node.source, newSource.raw || "'".concat(compiledSource, "'")); } }); } }; } }); // eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports var _default = exports.default = rule;