@atlaskit/eslint-plugin-design-system
Version:
The essential plugin for use with the Atlassian Design System.
59 lines (57 loc) • 2.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.upsertFlexImport = upsertFlexImport;
var _contextCompat = require("@atlaskit/eslint-utils/context-compat");
var _import = require("../../ast-nodes/import");
var _root = require("../../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'`
*/
function upsertFlexImport(context, fixer) {
var root = (0, _contextCompat.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.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.Root.upsertNamedImportDeclaration({
module: FLEX_IMPORT_MODULE,
specifiers: ['Flex']
}, context, fixer);
}