Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: version range handling in update_pkg function #11716

Merged
merged 20 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silly-donkeys-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte-migrate": patch
---

fix: don't downgrade versions when bumping dependencies
1 change: 1 addition & 0 deletions packages/migrate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
A CLI for migrating Svelte(Kit) codebases.

Run it using

```
npx svelte-migrate [migration]
```
Expand Down
8 changes: 4 additions & 4 deletions packages/migrate/migrations/package/migrate_pkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ function analyze(config, file) {
const dest = svelte_extension
? name.slice(0, -svelte_extension.length) + '.svelte'
: name.endsWith('.d.ts')
? name
: name.endsWith('.ts')
? name.slice(0, -3) + '.js'
: name;
? name
: name.endsWith('.ts')
? name.slice(0, -3) + '.js'
: name;

return {
name,
Expand Down
4 changes: 2 additions & 2 deletions packages/migrate/migrations/routes/migrate_page_js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ export function migrate_page(content, filename) {
const message = is_string_like(nodes.error)
? nodes.error.getText()
: is_new(nodes.error, 'Error')
? /** @type {string | undefined} */ (nodes.error.arguments[0]?.getText())
: false;
? /** @type {string | undefined} */ (nodes.error.arguments[0]?.getText())
: false;

if (message !== false) {
automigration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export function migrate_page_server(content, filename) {
const load_name = match?.[3]
? `LayoutServerLoad.${match[3]}`
: match?.[2]
? 'LayoutServerLoad'
: 'PageServerLoad';
? 'LayoutServerLoad'
: 'PageServerLoad';

const has_load = file.exports.map.has('GET');
const has_action = non_get_methods.some((method) => file.exports.map.has(method));
Expand Down
2 changes: 1 addition & 1 deletion packages/migrate/migrations/routes/migrate_server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export function migrate_server(content) {
init.length > 0
? `new Response(${body}, {${join_whitespace}${init.join(
`,${join_whitespace}`
)}${end_whitespace}})`
)}${end_whitespace}})`
: `new Response(${body})`;

if (safe_body) {
Expand Down
1 change: 0 additions & 1 deletion packages/migrate/migrations/svelte-4/migrate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,6 @@ test('Update package.json', () => {
);
});


test('Does not downgrade versions', () => {
const result = update_pkg_json_content(`{
"devDependencies": {
Expand Down
4 changes: 3 additions & 1 deletion packages/migrate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
},
"scripts": {
"test": "vitest run --silent",
"check": "tsc"
"check": "tsc",
"lint": "prettier --check .",
"format": "pnpm lint --write"
}
}
37 changes: 22 additions & 15 deletions packages/migrate/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,23 +215,30 @@ export function update_pkg(content, updates) {
* @param {'dependencies' | 'devDependencies' | undefined} [insert]
*/
function update_pkg(name, version, additional = '', insert) {
if (pkg.dependencies?.[name]) {
const existing_range = pkg.dependencies[name];

if (semver.validRange(existing_range) && !semver.subset(existing_range, version)) {
log_migration(`Updated ${name} to ${version} ${additional}`);
pkg.dependencies[name] = version;
/**
* @param {string} type
*/
const updateVersion = (type) => {
const existingRange = pkg[type]?.[name];

if (
existingRange &&
semver.validRange(existingRange) &&
!semver.subset(existingRange, version)
) {
// Check if the new version range is an upgrade
const minExistingVersion = semver.minVersion(existingRange);
const minNewVersion = semver.minVersion(version);

if (minExistingVersion && minNewVersion && semver.gt(minNewVersion, minExistingVersion)) {
log_migration(`Updated ${name} to ${version}`);
pkg[type][name] = version;
}
}
}

if (pkg.devDependencies?.[name]) {
const existing_range = pkg.devDependencies[name];
};

if (semver.validRange(existing_range) && !semver.subset(existing_range, version)) {
log_migration(`Updated ${name} to ${version} ${additional}`);
pkg.devDependencies[name] = version;
}
}
updateVersion('dependencies');
updateVersion('devDependencies');

if (insert && !pkg[insert]?.[name]) {
if (!pkg[insert]) pkg[insert] = {};
Expand Down
166 changes: 93 additions & 73 deletions packages/migrate/utils.spec.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,93 @@
import { assert, test } from 'vitest';
import { update_pkg } from './utils.js';

test('Inserts package at correct position (1)', () => {
const result = update_pkg(
`{
"dependencies": {
"a": "1",
"z": "3",
"c": "4"
}
}`,
[['b', '2', '', 'dependencies']]
);

assert.equal(
result,
`{
"dependencies": {
"a": "1",
"b": "2",
"z": "3",
"c": "4"
}
}`
);
});

test('Inserts package at correct position (2)', () => {
const result = update_pkg(
`{
"dependencies": {
"a": "1",
"b": "2"
}
}`,
[['c', '3', '', 'dependencies']]
);

assert.equal(
result,
`{
"dependencies": {
"a": "1",
"b": "2",
"c": "3"
}
}`
);
});

test('Inserts package at correct position (3)', () => {
const result = update_pkg(
`{
"dependencies": {
"b": "2",
"c": "3"
}
}`,
[['a', '1', '', 'dependencies']]
);

assert.equal(
result,
`{
"dependencies": {
"a": "1",
"b": "2",
"c": "3"
}
}`
);
});
import { assert, test } from 'vitest';
import { update_pkg } from './utils.js';

test('Inserts package at correct position (1)', () => {
const result = update_pkg(
`{
"dependencies": {
"a": "1",
"z": "3",
"c": "4"
}
}`,
[['b', '2', '', 'dependencies']]
);

assert.equal(
result,
`{
"dependencies": {
"a": "1",
"b": "2",
"z": "3",
"c": "4"
}
}`
);
});

test('Inserts package at correct position (2)', () => {
const result = update_pkg(
`{
"dependencies": {
"a": "1",
"b": "2"
}
}`,
[['c', '3', '', 'dependencies']]
);

assert.equal(
result,
`{
"dependencies": {
"a": "1",
"b": "2",
"c": "3"
}
}`
);
});

test('Inserts package at correct position (3)', () => {
const result = update_pkg(
`{
"dependencies": {
"b": "2",
"c": "3"
}
}`,
[['a', '1', '', 'dependencies']]
);

assert.equal(
result,
`{
"dependencies": {
"a": "1",
"b": "2",
"c": "3"
}
}`
);
});

test('Does not downgrade versions', () => {
const result = update_pkg(
`{
"devDependencies": {
"@sveltejs/kit": "^2.4.3"
}
}`,
[['@sveltejs/kit', '^2.0.0']]
);

assert.equal(
result,
`{
"devDependencies": {
"@sveltejs/kit": "^2.4.3"
}
}`
);
});
Loading