@atlaskit/eslint-plugin-design-system
Version:
The essential plugin for use with the Atlassian Design System.
46 lines (44 loc) • 1.72 kB
JavaScript
import { getSourceCode } from '@atlaskit/eslint-utils/context-compat';
import { Import } from '../../ast-nodes/import';
import { Root } from '../../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'`
*/
export function upsertCssMapImport(context, fixer) {
var root = 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.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.upsertNamedImportDeclaration({
module: CSS_IMPORT_MODULE,
specifiers: ['cssMap']
}, context, fixer);
}