UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

70 lines (64 loc) 2.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Root = void 0; var _eslintCodemodUtils = require("eslint-codemod-utils"); var _contextCompat = require("@atlaskit/eslint-utils/context-compat"); var _import = require("./import"); /* eslint-disable @repo/internal/react/require-jsdoc */ // Little bit unreadable, but better than duplicating the type var Root = exports.Root = { /** * Note: This can return multiple ImportDeclarations for cases like: * ``` * import { Stack } from '@atlaskit/primitives' * import type { StackProps } from '@atlaskit/primitives' * ``` */ findImportsByModule: function findImportsByModule(root, name) { return root.filter(function (node) { if (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'ImportDeclaration')) { return false; } var names = typeof name === 'string' ? [name] : name; if (!names.some(function (name) { return (0, _eslintCodemodUtils.hasImportDeclaration)(node, name); })) { return false; } return true; }); }, insertImport: function insertImport(root, data, fixer) { return fixer.insertTextBefore(root[0], "".concat((0, _eslintCodemodUtils.insertImportDeclaration)(data.module, data.specifiers), ";\n")); }, upsertNamedImportDeclaration: function upsertNamedImportDeclaration(_ref, context, fixer) { var module = _ref.module, specifiers = _ref.specifiers; // Find any imports that match the packageName var root = (0, _contextCompat.getSourceCode)(context).ast.body; var importDeclarations = this.findImportsByModule(root, module); // The named import doesn't exist yet, we can just insert a whole new one if (importDeclarations.length === 0) { return this.insertImport(root, { module: module, specifiers: specifiers }, fixer); } // The import exists so, modify the existing one return _import.Import.insertNamedSpecifiers(importDeclarations[0], specifiers, fixer); }, upsertDefaultImportDeclaration: function upsertDefaultImportDeclaration(_ref2, context, fixer) { var module = _ref2.module, localName = _ref2.localName; // Find any imports that match the packageName var root = (0, _contextCompat.getSourceCode)(context).ast.body; var importDeclarations = this.findImportsByModule(root, module); // The import already exist exist if (importDeclarations.length > 0) { return undefined; } return fixer.insertTextBefore(root[0], "import ".concat(localName, " from '").concat(module, "';\n")); } };