Skip to content

Commit

Permalink
chore: upgrade to ts-morph 24 (#12781)
Browse files Browse the repository at this point in the history
Supports TypeScript 5.6 and removes a bunch of dependencies
  • Loading branch information
benmccann authored Oct 9, 2024
1 parent 4c1e3c0 commit aba2162
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 78 deletions.
5 changes: 5 additions & 0 deletions .changeset/young-toes-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte-migrate': patch
---

chore: upgrade to ts-morph 24
22 changes: 12 additions & 10 deletions packages/migrate/migrations/svelte-4/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,18 +260,20 @@ function update_typeof_svelte_component(source, is_ts) {
for (const type of imports) {
if (type) {
const name = type.getAliasNode() ?? type.getNameNode();
name.findReferencesAsNodes().forEach((ref) => {
const parent = ref.getParent();
if (parent && Node.isTypeQuery(parent)) {
const id = parent.getFirstChildByKind(ts.SyntaxKind.Identifier);
if (id?.getText() === name.getText()) {
const typeArguments = parent.getTypeArguments();
if (typeArguments.length === 0) {
parent.addTypeArgument('any');
if (Node.isIdentifier(name)) {
name.findReferencesAsNodes().forEach((ref) => {
const parent = ref.getParent();
if (parent && Node.isTypeQuery(parent)) {
const id = parent.getFirstChildByKind(ts.SyntaxKind.Identifier);
if (id?.getText() === name.getText()) {
const typeArguments = parent.getTypeArguments();
if (typeArguments.length === 0) {
parent.addTypeArgument('any');
}
}
}
}
});
});
}
}
}

Expand Down
40 changes: 23 additions & 17 deletions packages/migrate/migrations/sveltekit-2/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,19 @@ function remove_throws(source) {

/** @param {string} id */
function remove_throw(id) {
const namedImport = get_import(source, '@sveltejs/kit', id);
if (!namedImport) return;
for (const id of namedImport.getNameNode().findReferencesAsNodes()) {
const call_expression = id.getParent();
const throw_stmt = call_expression?.getParent();
if (Node.isCallExpression(call_expression) && Node.isThrowStatement(throw_stmt)) {
throw_stmt.replaceWithText((writer) => {
writer.setIndentationLevel(0);
writer.write(call_expression.getText() + ';');
});
const named_import = get_import(source, '@sveltejs/kit', id);
if (!named_import) return;
const name_node = named_import.getNameNode();
if (Node.isIdentifier(name_node)) {
for (const id of name_node.findReferencesAsNodes()) {
const call_expression = id.getParent();
const throw_stmt = call_expression?.getParent();
if (Node.isCallExpression(call_expression) && Node.isThrowStatement(throw_stmt)) {
throw_stmt.replaceWithText((writer) => {
writer.setIndentationLevel(0);
writer.write(call_expression.getText() + ';');
});
}
}
}
}
Expand Down Expand Up @@ -271,21 +274,24 @@ function add_cookie_note(file_path, source) {
* @param {import('ts-morph').SourceFile} source
*/
function replace_resolve_path(source) {
const namedImport = get_import(source, '@sveltejs/kit', 'resolvePath');
if (!namedImport) return;
const named_import = get_import(source, '@sveltejs/kit', 'resolvePath');
if (!named_import) return;

const logger = log_on_ts_modification(
source,
'Replaced `resolvePath` with `resolveRoute`: https://kit.svelte.dev/docs/migrating-to-sveltekit-2#resolvePath-has-been-removed'
);

for (const id of namedImport.getNameNode().findReferencesAsNodes()) {
id.replaceWithText('resolveRoute');
const name_node = named_import.getNameNode();
if (Node.isIdentifier(name_node)) {
for (const id of name_node.findReferencesAsNodes()) {
id.replaceWithText('resolveRoute');
}
}
if (namedImport.getParent().getParent().getNamedImports().length === 1) {
namedImport.getParent().getParent().getParent().remove();
if (named_import.getParent().getParent().getNamedImports().length === 1) {
named_import.getParent().getParent().getParent().remove();
} else {
namedImport.remove();
named_import.remove();
}

const paths_import = source.getImportDeclaration(
Expand Down
2 changes: 1 addition & 1 deletion packages/migrate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"prompts": "^2.4.2",
"semver": "^7.5.4",
"tiny-glob": "^0.2.9",
"ts-morph": "^23.0.0",
"ts-morph": "^24.0.0",
"typescript": "^5.3.3",
"zimmerframe": "^1.1.2"
},
Expand Down
Loading

0 comments on commit aba2162

Please sign in to comment.