feat(migrate): add @choojs/migrate — the choo-migrate v7→v8 codemod
Regex-based on purpose: converts simple top-level CJS patterns to ESM, remaps package specifiers to their @choojs homes, points retired nano* packages at built-in replacements (choo-lazy-route → lazy()), and reports everything it refuses to guess at instead of guessing. Validated against choo's own v7 example app: migrated output runs on v8 unmodified. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014NgfSjHE11oFpoSnLVKLXd
This commit is contained in:
co-authored by
Claude Fable 5
parent
fd1d05cea0
commit
c101ee464a
@@ -0,0 +1,92 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert'
|
||||
|
||||
import transform from '../lib/transform.js'
|
||||
|
||||
test('converts a classic choo v7 header to v8 ESM', () => {
|
||||
const src = [
|
||||
"var choo = require('choo')",
|
||||
"var html = require('choo/html')",
|
||||
"var devtools = require('choo-devtools')",
|
||||
'',
|
||||
'var app = choo()',
|
||||
"app.use(devtools())",
|
||||
'module.exports = app'
|
||||
].join('\n')
|
||||
|
||||
const { code, changed, notes } = transform(src)
|
||||
assert.ok(changed)
|
||||
assert.match(code, /import choo from '@choojs\/core'/)
|
||||
assert.match(code, /import html from '@choojs\/html'/)
|
||||
assert.match(code, /import devtools from '@choojs\/devtools'/)
|
||||
assert.match(code, /export default app/)
|
||||
assert.deepStrictEqual(notes, [])
|
||||
})
|
||||
|
||||
test('destructured and property requires', () => {
|
||||
const src = [
|
||||
"const { render } = require('some-lib')",
|
||||
"const thing = require('other-lib').thing",
|
||||
"const dflt = require('third-lib').default",
|
||||
"require('./side-effect')"
|
||||
].join('\n')
|
||||
|
||||
const { code } = transform(src)
|
||||
assert.match(code, /import { render } from 'some-lib'/)
|
||||
assert.match(code, /import { thing as thing } from 'other-lib'/)
|
||||
assert.match(code, /import dflt from 'third-lib'/)
|
||||
assert.match(code, /import '\.\/side-effect'/)
|
||||
})
|
||||
|
||||
test('already-ESM sources get specifiers remapped', () => {
|
||||
const src = [
|
||||
"import choo from 'choo'",
|
||||
"import html from 'nanohtml'",
|
||||
"import raw from 'nanohtml/raw'",
|
||||
"import morph from 'nanomorph'",
|
||||
"import Component from 'nanocomponent'"
|
||||
].join('\n')
|
||||
|
||||
const { code } = transform(src)
|
||||
assert.match(code, /from '@choojs\/core'/)
|
||||
assert.match(code, /from '@choojs\/html'\n/)
|
||||
assert.match(code, /from '@choojs\/html\/raw'/)
|
||||
assert.match(code, /from '@choojs\/html\/morph'/)
|
||||
assert.match(code, /from '@choojs\/component'/)
|
||||
})
|
||||
|
||||
test('retired packages produce notes, not rewrites', () => {
|
||||
const src = [
|
||||
"var nanobus = require('nanobus')",
|
||||
"var lazyRoute = require('choo-lazy-route')"
|
||||
].join('\n')
|
||||
|
||||
const { code, notes } = transform(src)
|
||||
assert.match(code, /from 'nanobus'/, 'retired specifier left for the human')
|
||||
assert.ok(notes.some((n) => n.includes('nanobus')))
|
||||
assert.ok(notes.some((n) => n.includes('lazy()')), 'choo-lazy-route points at lazy()')
|
||||
})
|
||||
|
||||
test('dynamic import() specifiers are remapped too', () => {
|
||||
const { code } = transform("const mod = await import('choo/html')")
|
||||
assert.match(code, /import\('@choojs\/html'\)/)
|
||||
})
|
||||
|
||||
test('unconvertible CJS is reported honestly', () => {
|
||||
const src = [
|
||||
"module.exports.helper = function () {}",
|
||||
"function f () { const x = require('choo') }"
|
||||
].join('\n')
|
||||
|
||||
const { code, notes } = transform(src)
|
||||
assert.match(code, /require\('@choojs\/core'\)/, 'specifier remapped even inside functions')
|
||||
assert.ok(notes.some((n) => n.includes('module.exports')))
|
||||
assert.ok(notes.some((n) => n.includes('require() calls remain')))
|
||||
})
|
||||
|
||||
test('non-choo sources pass through untouched', () => {
|
||||
const src = "import fs from 'node:fs'\nexport const x = 1\n"
|
||||
const { changed, notes } = transform(src)
|
||||
assert.strictEqual(changed, false)
|
||||
assert.deepStrictEqual(notes, [])
|
||||
})
|
||||
Reference in New Issue
Block a user