@atlaskit/eslint-plugin-design-system
Version:
The essential plugin for use with the Atlassian Design System.
53 lines (52 loc) • 2.13 kB
JavaScript
import { getSourceCode } from '@atlaskit/eslint-utils/context-compat';
import { Import } from '../../ast-nodes/import';
import { Root } from '../../ast-nodes/root';
var FLEX_IMPORT_MODULE = '@atlaskit/primitives/compiled';
var FLEX_IMPORT_MODULE_NON_COMPILED = '@atlaskit/primitives';
/**
* Upserts `Flex` from `@atlaskit/primitives/compiled`, handling:
* 1. Already imported from `/compiled` → add Flex if missing
* 2. Import from `@atlaskit/primitives` (non-compiled) → migrate path + add Flex
* 3. No import → insert new `import { Flex } from '@atlaskit/primitives/compiled'`
*/
export function upsertFlexImport(context, fixer) {
var root = getSourceCode(context).ast.body;
var findExactImports = function findExactImports(module) {
return root.filter(function (node) {
return node.type === 'ImportDeclaration' && node.source.value === module;
});
};
var compiledImports = findExactImports(FLEX_IMPORT_MODULE);
if (compiledImports.length > 0) {
var decl = compiledImports[0];
if (Import.containsNamedSpecifier(decl, 'Flex')) {
return undefined;
}
var specifiers = decl.specifiers.filter(function (s) {
return s.type === 'ImportSpecifier';
}).map(function (s) {
return s.local.name;
});
specifiers.push('Flex');
specifiers.sort();
return fixer.replaceText(decl, "import { ".concat(specifiers.join(', '), " } from '").concat(FLEX_IMPORT_MODULE, "';"));
}
var nonCompiledImports = findExactImports(FLEX_IMPORT_MODULE_NON_COMPILED);
if (nonCompiledImports.length > 0) {
var _decl = nonCompiledImports[0];
var _specifiers = _decl.specifiers.filter(function (s) {
return s.type === 'ImportSpecifier';
}).map(function (s) {
return s.local.name;
});
if (!_specifiers.includes('Flex')) {
_specifiers.push('Flex');
}
_specifiers.sort();
return fixer.replaceText(_decl, "import { ".concat(_specifiers.join(', '), " } from '").concat(FLEX_IMPORT_MODULE, "';"));
}
return Root.upsertNamedImportDeclaration({
module: FLEX_IMPORT_MODULE,
specifiers: ['Flex']
}, context, fixer);
}