UNPKG

@navikt/aksel

Version:

Aksel command line interface. Handles css-imports, codemods and more

75 lines (74 loc) 2.96 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = transformer; const chalk_1 = __importDefault(require("chalk")); const lineterminator_1 = require("../../../utils/lineterminator"); const packageImports_1 = __importDefault(require("../../../utils/packageImports")); const removeProps_1 = __importDefault(require("../../../utils/removeProps")); /** * @param {import('jscodeshift').FileInfo} file * @param {import('jscodeshift').API} api */ function transformer(file, api) { const j = api.jscodeshift; let root; try { root = j(file.source); } catch (_a) { return file.source; } const toName = "CopyButton"; const localName = (0, packageImports_1.default)(j, root, { fromImport: "@navikt/ds-react-internal", toImport: "@navikt/ds-react", fromName: "CopyToClipboard", toName, ignoreAlias: true, }); if (localName === null) { return root.toSource((0, lineterminator_1.getLineTerminator)(file.source)); } /* Finds and replaces import from CopyToClipboard -> CopyButton */ if (root.findJSXElements(localName)) { (0, removeProps_1.default)(j, root, localName, [ "popoverText", "popoverPlacement", "iconPosition", "variant", ]); const component = root.findJSXElements(localName); component.forEach((node) => { const children = node.node.children; let flagged = false; if (children.length > 0 && !node.node.openingElement.attributes.some((attr) => attr.name.name === "text")) { if (children.length === 1 && children[0].type === "JSXText") { node.node.openingElement.attributes.push(j.jsxAttribute(j.jsxIdentifier("text"), j.literal(children[0].value.trim()))); } else { flagged = true; console.info(chalk_1.default.yellow(`\n\nWarning: Detected advanced children-type!\nCodemod can't convert into "text" prop so you will need to update this manually.`)); } } if (!flagged) { node.node.children = []; node.node.openingElement.selfClosing = true; node.node.closingElement = null; } }); const compRoot = root.find(j.JSXElement, { openingElement: { name: { name: localName } }, }); compRoot.forEach((x) => { x.node.openingElement.name.name = "CopyButton"; if (x.node.children.length > 0) { x.node.closingElement.name.name = "CopyButton"; } }); } return root.toSource((0, lineterminator_1.getLineTerminator)(file.source)); }