Skip to content

Commit

Permalink
Bugfix (migrate/skeleton-3): Correctly handle conflicting renames/imp… (
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugos68 authored Jan 23, 2025
1 parent 83c2dc6 commit 082edc3
Show file tree
Hide file tree
Showing 8 changed files with 387 additions and 97 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-yaks-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@skeletonlabs/skeleton-cli': patch
---

Bugfix (migrate/skeleton-3): Correctly handle conflicting renames/imports.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { transformClasses } from './transform-classes';
import { Node } from 'ts-morph';
import { addNamedImport } from '../../../../../utility/ts-morph/add-named-import';
import { parseSourceFile } from '../../../../../utility/ts-morph/parse-source-file';
import { RENAMED_EXPORTS } from '../utility/exports/renamed-exports';
import { REMOVED_EXPORTS } from '../utility/exports/removed-exports';
import { EXPORT_MAPPINGS } from '../utility/export-mappings';

function transformModule(code: string) {
const file = parseSourceFile(code);
Expand All @@ -16,29 +15,33 @@ function transformModule(code: string) {
}
if (Node.isImportSpecifier(node)) {
const name = node.getName();
if (Object.hasOwn(RENAMED_EXPORTS, name) && !RENAMED_EXPORTS[name].match(/^[A-Za-z]+\.[A-Za-z]+$/)) {
node.remove();
addNamedImport(file, '@skeletonlabs/skeleton-svelte', RENAMED_EXPORTS[name]);
}
if (REMOVED_EXPORTS.includes(name)) {
const parent = node.getImportDeclaration();
if (parent.getNamedImports().length === 1 && !parent.getDefaultImport() && !parent.getNamespaceImport()) {
parent.remove();
} else {
const exportMapping = EXPORT_MAPPINGS[name];
if (exportMapping) {
if (exportMapping.identifier.type === 'renamed' && !exportMapping.identifier.value.match(/^[A-Za-z]+\.[A-Za-z]+$/)) {
node.remove();
addNamedImport(file, '@skeletonlabs/skeleton-svelte', exportMapping.identifier.value);
} else if (exportMapping.namedImport.type === 'removed') {
const parent = node.getImportDeclaration();
if (parent.getNamedImports().length === 1 && !parent.getDefaultImport() && !parent.getNamespaceImport()) {
parent.remove();
} else {
node.remove();
}
}
}
}
if (!node.wasForgotten() && Node.isIdentifier(node)) {
const name = node.getText();
if (Object.hasOwn(RENAMED_EXPORTS, name)) {
node.replaceWithText(RENAMED_EXPORTS[name]);
const exportMapping = EXPORT_MAPPINGS[name];
if (exportMapping && exportMapping.identifier.type === 'renamed') {
node.replaceWithText(exportMapping.identifier.value);
}
}
if (!node.wasForgotten() && Node.isStringLiteral(node) && !Node.isImportDeclaration(node.getParent())) {
node.replaceWithText(transformClasses(node.getText()).code);
}
});

return {
code: file.getFullText()
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { walk } from 'zimmerframe';
import MagicString from 'magic-string';
import { transformClasses } from './transform-classes';
import { transformModule } from './transform-module';
import { RENAMED_EXPORTS } from '../utility/exports/renamed-exports';
import { EXPORT_MAPPINGS } from '../utility/export-mappings';

function renameComponent(s: MagicString, node: AST.Component, name: string) {
const adjustedStart = node.start + 1;
Expand Down Expand Up @@ -66,8 +66,9 @@ function transformFragment(s: MagicString, fragment: AST.Fragment) {
ctx.next();
},
Component(node, ctx) {
if (Object.hasOwn(RENAMED_EXPORTS, node.name) && hasRange(node)) {
renameComponent(s, node, RENAMED_EXPORTS[node.name]);
const exportMapping = EXPORT_MAPPINGS[node.name];
if (exportMapping && exportMapping.identifier.type === 'renamed' && hasRange(node)) {
renameComponent(s, node, exportMapping.identifier.value);
}
ctx.next();
}
Expand Down
Loading

0 comments on commit 082edc3

Please sign in to comment.