UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

52 lines (49 loc) 1.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.upsertCssMapImport = upsertCssMapImport; var _contextCompat = require("@atlaskit/eslint-utils/context-compat"); var _import = require("../../ast-nodes/import"); var _root = require("../../ast-nodes/root"); var CSS_IMPORT_MODULE = '@atlaskit/css'; /** * Upserts `cssMap` from `@atlaskit/css`, handling: * 1. Already imported → no-op * 2. `@atlaskit/css` exists but missing `cssMap` → add specifier * 3. No import → insert new `import { cssMap } from '@atlaskit/css'` */ function upsertCssMapImport(context, fixer) { var root = (0, _contextCompat.getSourceCode)(context).ast.body; var cssImports = root.filter(function (node) { return node.type === 'ImportDeclaration' && node.source.value === CSS_IMPORT_MODULE; }); if (cssImports.length > 0) { var decl = cssImports[0]; var hasCssMap = _import.Import.containsNamedSpecifier(decl, 'cssMap'); if (hasCssMap) { return undefined; } var specifiers = decl.specifiers.filter(function (s) { return s.type === 'ImportSpecifier'; }).map(function (s) { return s.local.name; }); specifiers.push('cssMap'); specifiers.sort(); return fixer.replaceText(decl, "import { ".concat(specifiers.join(', '), " } from '").concat(CSS_IMPORT_MODULE, "';")); } // If cssMap is already imported from another package (e.g. @compiled/react), don't add a duplicate var cssMapAlreadyImported = root.some(function (node) { return node.type === 'ImportDeclaration' && node.specifiers.some(function (s) { return s.type === 'ImportSpecifier' && s.local.name === 'cssMap'; }); }); if (cssMapAlreadyImported) { return undefined; } return _root.Root.upsertNamedImportDeclaration({ module: CSS_IMPORT_MODULE, specifiers: ['cssMap'] }, context, fixer); }