UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

35 lines (34 loc) 1.32 kB
import { getSourceCode } from '@atlaskit/eslint-utils/context-compat'; import { Import } from '../../ast-nodes/import'; import { Root } from '../../ast-nodes/root'; var TOKEN_IMPORT_MODULE = '@atlaskit/tokens'; /** * Upserts `token` from `@atlaskit/tokens`, handling: * 1. Already imported → no-op * 2. `@atlaskit/tokens` exists but missing `token` → add specifier * 3. No import → insert new `import { token } from '@atlaskit/tokens'` */ export function upsertTokenImport(context, fixer) { var root = getSourceCode(context).ast.body; var tokenImports = root.filter(function (node) { return node.type === 'ImportDeclaration' && node.source.value === TOKEN_IMPORT_MODULE; }); if (tokenImports.length > 0) { var decl = tokenImports[0]; if (Import.containsNamedSpecifier(decl, 'token')) { return undefined; } var specifiers = decl.specifiers.filter(function (s) { return s.type === 'ImportSpecifier'; }).map(function (s) { return s.local.name; }); specifiers.push('token'); specifiers.sort(); return fixer.replaceText(decl, "import { ".concat(specifiers.join(', '), " } from '").concat(TOKEN_IMPORT_MODULE, "';")); } return Root.upsertNamedImportDeclaration({ module: TOKEN_IMPORT_MODULE, specifiers: ['token'] }, context, fixer); }