diff --git a/package-lock.json b/package-lock.json index 287f3675..19ec5507 100644 --- a/package-lock.json +++ b/package-lock.json @@ -585,6 +585,10 @@ "resolved": "recipes/slow-buffer-to-buffer-alloc-unsafe-slow", "link": true }, + "node_modules/@nodejs/sqlite-class-renames": { + "resolved": "recipes/sqlite-class-renames", + "link": true + }, "node_modules/@nodejs/timers-deprecations": { "resolved": "recipes/timers-deprecations", "link": true @@ -1290,6 +1294,17 @@ "@codemod.com/jssg-types": "^1.6.3" } }, + "recipes/sqlite-class-renames": { + "name": "@nodejs/sqlite-class-renames", + "version": "1.0.0", + "license": "MIT", + "dependencies": { + "@nodejs/codemod-utils": "*" + }, + "devDependencies": { + "@codemod.com/jssg-types": "^1.6.3" + } + }, "recipes/timers-deprecations": { "name": "@nodejs/timers-deprecations", "dependencies": { diff --git a/recipes/sqlite-class-renames/README.md b/recipes/sqlite-class-renames/README.md new file mode 100644 index 00000000..3d9ab7bf --- /dev/null +++ b/recipes/sqlite-class-renames/README.md @@ -0,0 +1,67 @@ +--- +authors: FrustratedBoy420 +--- + +# DEP0210 and DEP0211: `node:sqlite` class renames + +Node.js 26.11.0 renamed two classes exported by `node:sqlite`. `DatabaseSync` became `Database` ([DEP0210](https://nodejs.org/api/deprecations.html#DEP0210)) and `StatementSync` became `Statement` ([DEP0211](https://nodejs.org/api/deprecations.html#DEP0211)). The old names remain as documentation-deprecated aliases. The APIs are still synchronous. + +This codemod migrates imports and references that resolve to `node:sqlite`. It supports ESM named, namespace, and default imports, CommonJS destructuring and property access, aliases, `await import('node:sqlite')`, re-exports, and TypeScript type annotations. Run it only on projects whose supported Node.js versions include 26.11.0 or later. + +## Usage + +Run this codemod with: + +```sh +npx codemod @nodejs/sqlite-class-renames +``` + +## Examples + +### Named imports and references + +```diff +-import { DatabaseSync, StatementSync } from 'node:sqlite'; ++import { Database, Statement } from 'node:sqlite'; + +-const db = new DatabaseSync(':memory:'); ++const db = new Database(':memory:'); + const statement = db.prepare('SELECT 1'); +-console.log(statement instanceof StatementSync); ++console.log(statement instanceof Statement); +``` + +### Explicit aliases are kept + +```diff +-const { DatabaseSync: DB } = require('node:sqlite'); ++const { Database: DB } = require('node:sqlite'); + + const db = new DB(':memory:'); +``` + +### Public export names are kept + +```diff +-export { DatabaseSync } from 'node:sqlite'; ++export { Database as DatabaseSync } from 'node:sqlite'; +``` + +### Name collisions and shadowing + +When the new name is already used in the file, or the old name is declared again (for example as a parameter), the codemod keeps the old local name as an alias. Other code in the file does not change. + +```diff + import Database from 'better-sqlite3'; +-import { DatabaseSync } from 'node:sqlite'; ++import { Database as DatabaseSync } from 'node:sqlite'; + + const db = new DatabaseSync(':memory:'); +``` + +## Limitations + +- Third-party packages such as `sqlite` or `better-sqlite3` are not changed. Only the `node:sqlite` specifier is resolved, because `sqlite` without the prefix is not a Node.js built-in. +- If the variable that holds the whole module (for example `sqlite` in `import * as sqlite`) is declared again in the file, its member accesses are left unchanged and a warning is printed. +- Computed access such as `sqlite['DatabaseSync']` and `import('node:sqlite').then(...)` callbacks are not changed. +- Type-only imports (`import type { DatabaseSync }` and inline `import { type DatabaseSync }`) are not changed. The old names still work as deprecated aliases. diff --git a/recipes/sqlite-class-renames/codemod.yaml b/recipes/sqlite-class-renames/codemod.yaml new file mode 100644 index 00000000..aab83e3b --- /dev/null +++ b/recipes/sqlite-class-renames/codemod.yaml @@ -0,0 +1,24 @@ +schema_version: "1.0" +name: "@nodejs/sqlite-class-renames" +version: "1.0.0" +description: Handle DEP0210 and DEP0211 by renaming node:sqlite DatabaseSync and StatementSync to Database and Statement. +author: FrustratedBoy420 +license: MIT +workflow: workflow.yaml +category: migration +repository: https://github.com/nodejs/userland-migrations + +targets: + languages: + - javascript + - typescript + +keywords: + - transformation + - migration + - nodejs + - sqlite + +registry: + access: public + visibility: public diff --git a/recipes/sqlite-class-renames/package.json b/recipes/sqlite-class-renames/package.json new file mode 100644 index 00000000..f4e1ef2a --- /dev/null +++ b/recipes/sqlite-class-renames/package.json @@ -0,0 +1,24 @@ +{ + "name": "@nodejs/sqlite-class-renames", + "version": "1.0.0", + "description": "Handle DEP0210 and DEP0211 by renaming node:sqlite DatabaseSync and StatementSync to Database and Statement", + "type": "module", + "scripts": { + "test": "npx codemod jssg test -l typescript ./src/workflow.ts" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/nodejs/userland-migrations.git", + "directory": "recipes/sqlite-class-renames", + "bugs": "https://github.com/nodejs/userland-migrations/issues" + }, + "author": "FrustratedBoy420", + "license": "MIT", + "homepage": "https://github.com/nodejs/userland-migrations/blob/main/recipes/sqlite-class-renames/README.md", + "devDependencies": { + "@codemod.com/jssg-types": "^1.6.3" + }, + "dependencies": { + "@nodejs/codemod-utils": "*" + } +} diff --git a/recipes/sqlite-class-renames/src/workflow.ts b/recipes/sqlite-class-renames/src/workflow.ts new file mode 100644 index 00000000..3c881eac --- /dev/null +++ b/recipes/sqlite-class-renames/src/workflow.ts @@ -0,0 +1,430 @@ +import type { + Edit, + Range, + Rule, + SgNode, + SgRoot, +} from '@codemod.com/jssg-types/main'; +import type Js from '@codemod.com/jssg-types/langs/javascript'; +import { getModuleDependencies } from '@nodejs/codemod-utils/ast-grep/module-dependencies'; +import { resolveBindingPath } from '@nodejs/codemod-utils/ast-grep/resolve-binding-path'; + +/** + * Deprecated `node:sqlite` class names (DEP0210, DEP0211) and their + * replacements. + */ +const RENAMES = new Map([ + ['DatabaseSync', 'Database'], + ['StatementSync', 'Statement'], +]); + +const RENAME_REGEX = `^(${[...RENAMES.keys()].join('|')})$`; + +const SQLITE_SOURCE: Rule = { + kind: 'string', + has: { kind: 'string_fragment', regex: '^node:sqlite$' }, +}; + +const REQUIRE_CALL: Rule = { + kind: 'call_expression', + all: [ + { has: { field: 'function', kind: 'identifier', regex: '^require$' } }, + { has: { field: 'arguments', has: SQLITE_SOURCE } }, + ], +}; + +/** + * Every node that can name a binding or a reference. `type_identifier` is + * only produced by the TypeScript grammar, so it is missing from the + * JavaScript kind union. + */ +const NAME_RULE = { + any: [ + 'identifier', + 'type_identifier', + 'shorthand_property_identifier', + 'shorthand_property_identifier_pattern', + ].map((kind) => ({ kind })), +} as Rule; + +/** `Namespace.Type` in TypeScript; missing from the JavaScript kind union. */ +const NESTED_TYPE_KIND: string = 'nested_type_identifier'; + +/** Declarations whose `name` field introduces a binding. */ +const NAMED_DECLARATIONS = new Set([ + 'abstract_class_declaration', + 'class', + 'class_declaration', + 'enum_declaration', + 'function_declaration', + 'function_expression', + 'function_signature', + 'generator_function', + 'generator_function_declaration', + 'interface_declaration', + 'internal_module', + 'module', + 'type_alias_declaration', +]); + +/** Parents whose direct identifier children are always bindings. */ +const BINDING_PARENTS = new Set([ + 'array_pattern', + 'formal_parameters', + 'import_clause', + 'import_specifier', + 'namespace_import', + 'rest_pattern', + 'type_parameter', +]); + +/** Field that holds the binding for parents that also contain references. */ +const BINDING_FIELDS: Record = { + arrow_function: 'parameter', + assignment_pattern: 'left', + catch_clause: 'parameter', + for_in_statement: 'left', + optional_parameter: 'pattern', + pair_pattern: 'value', + required_parameter: 'pattern', + variable_declarator: 'name', +}; + +type Context = { + rootNode: SgNode; + edits: Edit[]; + /** Ranges of the `node:sqlite` import, export, and require statements. */ + ownRanges: Range[]; +}; + +/** + * Handle DEP0210 and DEP0211: rename the `node:sqlite` classes `DatabaseSync` + * and `StatementSync` to `Database` and `Statement`. + * + * Only bindings that resolve to `node:sqlite` are migrated. When renaming a + * local binding could collide with or be shadowed by another binding, the + * import keeps the old local name as an alias, for example + * `import { Database as DatabaseSync } from 'node:sqlite'`. Public export + * names are never changed. + */ +export default function transform(root: SgRoot): string | null { + const rootNode = root.root(); + + // The shared helpers also match the third-party `sqlite` package, so keep + // only statements that load the `node:sqlite` built-in. + const statements = getModuleDependencies(root, 'sqlite').filter((statement) => + statement.find({ + rule: { kind: 'string_fragment', regex: '^node:sqlite$' }, + }), + ); + const reExports = rootNode.findAll({ + rule: { + kind: 'export_statement', + has: { field: 'source', ...SQLITE_SOURCE }, + }, + }); + const directAccesses = rootNode.findAll({ + rule: { + kind: 'member_expression', + all: [ + { has: { field: 'object', ...REQUIRE_CALL } }, + { has: { field: 'property', regex: RENAME_REGEX } }, + ], + }, + }); + + const ctx: Context = { + rootNode, + edits: [], + ownRanges: [...statements, ...reExports].map((node) => node.range()), + }; + const namespaces = new Map>(); + + for (const statement of statements) { + // `require('node:sqlite').DatabaseSync` is handled by `directAccesses`. + if (statement.field('value')?.is('member_expression')) continue; + + for (const [oldName, newName] of RENAMES) { + const binding = resolveBindingPath(statement, `$.${oldName}`); + if (!binding) continue; + + const [local, member] = binding.split('.'); + + if (member) { + namespaces.set(local, statement); + } else { + migrateNamedBinding(ctx, statement, oldName, newName, local); + } + } + } + + for (const [namespace, statement] of namespaces) { + migrateNamespace(root, ctx, namespace, statement); + } + + for (const statement of reExports) { + migrateReExport(ctx, statement); + } + + for (const access of directAccesses) { + renameProperty(ctx, access.field('property')); + } + + if (!ctx.edits.length) return null; + + return rootNode.commitEdits(ctx.edits); +} + +/** + * Renames one destructured or named binding, for example + * `import { DatabaseSync } from 'node:sqlite'` or + * `const { DatabaseSync: DB } = require('node:sqlite')`. + * + * Only the imported name is edited, so other bindings in the same statement + * stay untouched. + */ +function migrateNamedBinding( + ctx: Context, + statement: SgNode, + oldName: string, + newName: string, + local: string, +): void { + const imported = statement.find({ + rule: { + regex: `^${oldName}$`, + any: [ + { + kind: 'identifier', + inside: { kind: 'import_specifier', field: 'name' }, + }, + { + kind: 'property_identifier', + inside: { kind: 'pair_pattern', field: 'key' }, + }, + { kind: 'shorthand_property_identifier_pattern' }, + ], + }, + }); + if (!imported) return; + + if (local !== oldName || canRenameBinding(ctx, oldName, newName)) { + ctx.edits.push(imported.replace(newName)); + if (local === oldName) renameReferences(ctx, oldName, newName); + return; + } + + // Keep the old local name so no other code has to change. + ctx.edits.push( + imported.replace( + imported.is('shorthand_property_identifier_pattern') + ? `${newName}: ${oldName}` + : `${newName} as ${oldName}`, + ), + ); +} + +/** + * `export { DatabaseSync } from 'node:sqlite'` keeps its public name: + * `export { Database as DatabaseSync } from 'node:sqlite'`. + */ +function migrateReExport(ctx: Context, statement: SgNode): void { + for (const specifier of statement.findAll({ + rule: { kind: 'export_specifier' }, + })) { + const name = specifier.field('name'); + if (!name) continue; + + const oldName = name.text(); + const newName = RENAMES.get(oldName); + if (!newName) continue; + + ctx.edits.push( + name.replace( + specifier.field('alias') ? newName : `${newName} as ${oldName}`, + ), + ); + } +} + +/** + * Renames `sqlite.DatabaseSync` (value or TypeScript type) for a binding that + * holds the whole `node:sqlite` module. + */ +function migrateNamespace( + root: SgRoot, + ctx: Context, + namespace: string, + statement: SgNode, +): void { + if (findNames(ctx, namespace).some(isBindingPosition)) { + warn( + root, + statement, + `\`${namespace}\` is redeclared in this file; review \`${namespace}.DatabaseSync\` and \`${namespace}.StatementSync\` manually (DEP0210, DEP0211).`, + ); + return; + } + + const namespaceRegex = `^${namespace}$`; + + const members = ctx.rootNode.findAll({ + rule: { + kind: 'member_expression', + all: [ + { has: { field: 'object', kind: 'identifier', regex: namespaceRegex } }, + { has: { field: 'property', regex: RENAME_REGEX } }, + ], + }, + }); + + for (const member of members) { + renameProperty(ctx, member.field('property')); + } + + const types = ctx.rootNode.findAll({ + rule: { + kind: NESTED_TYPE_KIND, + all: [ + { has: { field: 'module', kind: 'identifier', regex: namespaceRegex } }, + { has: { field: 'name', regex: RENAME_REGEX } }, + ], + } as Rule, + }); + + for (const type of types) { + renameProperty(ctx, type.field('name')); + } +} + +function renameProperty(ctx: Context, property: SgNode | null): void { + const newName = property && RENAMES.get(property.text()); + + if (property && newName) { + ctx.edits.push(property.replace(newName)); + } +} + +/** + * A binding can be renamed in place only when the new name is unused in the + * file and the old name is not declared anywhere else, so that no reference + * can be captured or shadowed. + */ +function canRenameBinding( + ctx: Context, + oldName: string, + newName: string, +): boolean { + const newNameUsed = ctx.rootNode + .findAll({ rule: NAME_RULE }) + .some((node) => node.text() === newName && !isQualifiedTypeName(node)); + + if (newNameUsed) return false; + + return !findNames(ctx, oldName).some(isBindingPosition); +} + +function renameReferences( + ctx: Context, + oldName: string, + newName: string, +): void { + for (const node of findNames(ctx, oldName)) { + if (node.is('shorthand_property_identifier')) { + ctx.edits.push(node.replace(`${oldName}: ${newName}`)); + continue; + } + + const parent = node.parent(); + + if (parent?.is('export_specifier')) { + // `export { X as DatabaseSync }`: the alias is a public name. + if (isField(parent, 'alias', node)) continue; + + ctx.edits.push( + node.replace( + parent.field('alias') ? newName : `${newName} as ${oldName}`, + ), + ); + continue; + } + + ctx.edits.push(node.replace(newName)); + } +} + +/** + * Identifiers, type identifiers, and shorthand properties named `name`, + * outside the `node:sqlite` statements themselves. + */ +function findNames(ctx: Context, name: string): SgNode[] { + return ctx.rootNode + .findAll({ + rule: { ...NAME_RULE, regex: `^${name}$` }, + }) + .filter( + (node) => + !isQualifiedTypeName(node) && + !ctx.ownRanges.some((range) => isWithin(node.range(), range)), + ); +} + +/** `Other.DatabaseSync` used as a TypeScript type. */ +function isQualifiedTypeName(node: SgNode): boolean { + const parent = node.parent(); + + return parent?.is(NESTED_TYPE_KIND) && isField(parent, 'name', node); +} + +/** + * Whether the node declares a binding (variable, parameter, function, class, + * type, import) instead of referencing one. Re-exports from other modules are + * treated as bindings too, because they must never be renamed. + */ +function isBindingPosition(node: SgNode): boolean { + if (node.is('shorthand_property_identifier_pattern')) return true; + + const parent = node.parent(); + if (!parent) return false; + + const parentKind = parent.kind(); + + if (NAMED_DECLARATIONS.has(parentKind)) return isField(parent, 'name', node); + + if (BINDING_PARENTS.has(parentKind)) return true; + + if (parentKind in BINDING_FIELDS) { + return isField(parent, BINDING_FIELDS[parentKind], node); + } + + if (parentKind === 'export_specifier') { + return Boolean(parent.parent()?.parent()?.field('source')); + } + + return false; +} + +function isField(parent: SgNode, field: string, node: SgNode): boolean { + // biome-ignore lint/suspicious/noExplicitAny: field names vary by node kind. + const child = parent.field(field as any) as SgNode | null; + + return Boolean(child && isSameRange(child.range(), node.range())); +} + +function isSameRange(a: Range, b: Range): boolean { + return a.start.index === b.start.index && a.end.index === b.end.index; +} + +function isWithin(inner: Range, outer: Range): boolean { + return ( + inner.start.index >= outer.start.index && inner.end.index <= outer.end.index + ); +} + +function warn(root: SgRoot, node: SgNode, message: string): void { + const { start } = node.range(); + + console.warn( + `${root.filename()}:${start.line + 1}:${start.column + 1}: ${message}`, + ); +} diff --git a/recipes/sqlite-class-renames/tests/already-migrated/expected.mjs b/recipes/sqlite-class-renames/tests/already-migrated/expected.mjs new file mode 100644 index 00000000..b4ffb553 --- /dev/null +++ b/recipes/sqlite-class-renames/tests/already-migrated/expected.mjs @@ -0,0 +1,7 @@ +import { Database, Statement as Stmt } from 'node:sqlite'; +import { Database as DatabaseSync } from 'node:sqlite'; + +export { Database as DatabaseSync2 } from 'node:sqlite'; + +const db = new Database(':memory:'); +console.log(db instanceof DatabaseSync, Stmt); diff --git a/recipes/sqlite-class-renames/tests/already-migrated/input.mjs b/recipes/sqlite-class-renames/tests/already-migrated/input.mjs new file mode 100644 index 00000000..b4ffb553 --- /dev/null +++ b/recipes/sqlite-class-renames/tests/already-migrated/input.mjs @@ -0,0 +1,7 @@ +import { Database, Statement as Stmt } from 'node:sqlite'; +import { Database as DatabaseSync } from 'node:sqlite'; + +export { Database as DatabaseSync2 } from 'node:sqlite'; + +const db = new Database(':memory:'); +console.log(db instanceof DatabaseSync, Stmt); diff --git a/recipes/sqlite-class-renames/tests/binding-collision/expected.mjs b/recipes/sqlite-class-renames/tests/binding-collision/expected.mjs new file mode 100644 index 00000000..b14e5213 --- /dev/null +++ b/recipes/sqlite-class-renames/tests/binding-collision/expected.mjs @@ -0,0 +1,7 @@ +import Database from 'better-sqlite3'; +import { Database as DatabaseSync, Statement as StatementSync } from 'node:sqlite'; + +const legacy = new Database('app.db'); +const db = new DatabaseSync(':memory:'); +const Statement = db.prepare('SELECT 1'); +console.log(Statement instanceof StatementSync); diff --git a/recipes/sqlite-class-renames/tests/binding-collision/input.mjs b/recipes/sqlite-class-renames/tests/binding-collision/input.mjs new file mode 100644 index 00000000..8708601c --- /dev/null +++ b/recipes/sqlite-class-renames/tests/binding-collision/input.mjs @@ -0,0 +1,7 @@ +import Database from 'better-sqlite3'; +import { DatabaseSync, StatementSync } from 'node:sqlite'; + +const legacy = new Database('app.db'); +const db = new DatabaseSync(':memory:'); +const Statement = db.prepare('SELECT 1'); +console.log(Statement instanceof StatementSync); diff --git a/recipes/sqlite-class-renames/tests/cjs-destructuring/expected.js b/recipes/sqlite-class-renames/tests/cjs-destructuring/expected.js new file mode 100644 index 00000000..2d291de2 --- /dev/null +++ b/recipes/sqlite-class-renames/tests/cjs-destructuring/expected.js @@ -0,0 +1,4 @@ +const { Database: DB, Statement } = require('node:sqlite'); + +const db = new DB(':memory:'); +module.exports = { isStatement: (value) => value instanceof Statement }; diff --git a/recipes/sqlite-class-renames/tests/cjs-destructuring/input.js b/recipes/sqlite-class-renames/tests/cjs-destructuring/input.js new file mode 100644 index 00000000..2f434c16 --- /dev/null +++ b/recipes/sqlite-class-renames/tests/cjs-destructuring/input.js @@ -0,0 +1,4 @@ +const { DatabaseSync: DB, StatementSync } = require('node:sqlite'); + +const db = new DB(':memory:'); +module.exports = { isStatement: (value) => value instanceof StatementSync }; diff --git a/recipes/sqlite-class-renames/tests/cjs-property-access/expected.js b/recipes/sqlite-class-renames/tests/cjs-property-access/expected.js new file mode 100644 index 00000000..fc6548dc --- /dev/null +++ b/recipes/sqlite-class-renames/tests/cjs-property-access/expected.js @@ -0,0 +1,4 @@ +const sqlite = require('node:sqlite'); + +const db = new sqlite.Database(':memory:'); +const Stmt = require('node:sqlite').Statement; diff --git a/recipes/sqlite-class-renames/tests/cjs-property-access/input.js b/recipes/sqlite-class-renames/tests/cjs-property-access/input.js new file mode 100644 index 00000000..d1867f15 --- /dev/null +++ b/recipes/sqlite-class-renames/tests/cjs-property-access/input.js @@ -0,0 +1,4 @@ +const sqlite = require('node:sqlite'); + +const db = new sqlite.DatabaseSync(':memory:'); +const Stmt = require('node:sqlite').StatementSync; diff --git a/recipes/sqlite-class-renames/tests/dynamic-import/expected.mjs b/recipes/sqlite-class-renames/tests/dynamic-import/expected.mjs new file mode 100644 index 00000000..e8424da0 --- /dev/null +++ b/recipes/sqlite-class-renames/tests/dynamic-import/expected.mjs @@ -0,0 +1,5 @@ +const { Database } = await import('node:sqlite'); +const sqlite = await import('node:sqlite'); + +const db = new Database(':memory:'); +const check = (value) => value instanceof sqlite.Statement; diff --git a/recipes/sqlite-class-renames/tests/dynamic-import/input.mjs b/recipes/sqlite-class-renames/tests/dynamic-import/input.mjs new file mode 100644 index 00000000..b4a77ea1 --- /dev/null +++ b/recipes/sqlite-class-renames/tests/dynamic-import/input.mjs @@ -0,0 +1,5 @@ +const { DatabaseSync } = await import('node:sqlite'); +const sqlite = await import('node:sqlite'); + +const db = new DatabaseSync(':memory:'); +const check = (value) => value instanceof sqlite.StatementSync; diff --git a/recipes/sqlite-class-renames/tests/esm-alias/expected.mjs b/recipes/sqlite-class-renames/tests/esm-alias/expected.mjs new file mode 100644 index 00000000..19746004 --- /dev/null +++ b/recipes/sqlite-class-renames/tests/esm-alias/expected.mjs @@ -0,0 +1,4 @@ +import { Database as DB, Statement as Stmt } from 'node:sqlite'; + +const db = new DB(':memory:'); +console.log(db.prepare('SELECT 1') instanceof Stmt); diff --git a/recipes/sqlite-class-renames/tests/esm-alias/input.mjs b/recipes/sqlite-class-renames/tests/esm-alias/input.mjs new file mode 100644 index 00000000..9a427e71 --- /dev/null +++ b/recipes/sqlite-class-renames/tests/esm-alias/input.mjs @@ -0,0 +1,4 @@ +import { DatabaseSync as DB, StatementSync as Stmt } from 'node:sqlite'; + +const db = new DB(':memory:'); +console.log(db.prepare('SELECT 1') instanceof Stmt); diff --git a/recipes/sqlite-class-renames/tests/esm-named/expected.mjs b/recipes/sqlite-class-renames/tests/esm-named/expected.mjs new file mode 100644 index 00000000..9b6f82db --- /dev/null +++ b/recipes/sqlite-class-renames/tests/esm-named/expected.mjs @@ -0,0 +1,5 @@ +import { Database, Statement } from 'node:sqlite'; + +const db = new Database(':memory:'); +const statement = db.prepare('SELECT 1'); +console.log(statement instanceof Statement); diff --git a/recipes/sqlite-class-renames/tests/esm-named/input.mjs b/recipes/sqlite-class-renames/tests/esm-named/input.mjs new file mode 100644 index 00000000..3e45a36f --- /dev/null +++ b/recipes/sqlite-class-renames/tests/esm-named/input.mjs @@ -0,0 +1,5 @@ +import { DatabaseSync, StatementSync } from 'node:sqlite'; + +const db = new DatabaseSync(':memory:'); +const statement = db.prepare('SELECT 1'); +console.log(statement instanceof StatementSync); diff --git a/recipes/sqlite-class-renames/tests/esm-namespace/expected.mjs b/recipes/sqlite-class-renames/tests/esm-namespace/expected.mjs new file mode 100644 index 00000000..ad2ac749 --- /dev/null +++ b/recipes/sqlite-class-renames/tests/esm-namespace/expected.mjs @@ -0,0 +1,7 @@ +import * as sqlite from 'node:sqlite'; +import nodeSqlite from 'node:sqlite'; + +const db = new sqlite.Database(':memory:'); +const other = new nodeSqlite.Database(':memory:'); +console.log(db.prepare('SELECT 1') instanceof sqlite.Statement); +console.log(sqlite.constants, other.DatabaseSync); diff --git a/recipes/sqlite-class-renames/tests/esm-namespace/input.mjs b/recipes/sqlite-class-renames/tests/esm-namespace/input.mjs new file mode 100644 index 00000000..cbf44204 --- /dev/null +++ b/recipes/sqlite-class-renames/tests/esm-namespace/input.mjs @@ -0,0 +1,7 @@ +import * as sqlite from 'node:sqlite'; +import nodeSqlite from 'node:sqlite'; + +const db = new sqlite.DatabaseSync(':memory:'); +const other = new nodeSqlite.DatabaseSync(':memory:'); +console.log(db.prepare('SELECT 1') instanceof sqlite.StatementSync); +console.log(sqlite.constants, other.DatabaseSync); diff --git a/recipes/sqlite-class-renames/tests/re-exports/expected.mjs b/recipes/sqlite-class-renames/tests/re-exports/expected.mjs new file mode 100644 index 00000000..b1030fd2 --- /dev/null +++ b/recipes/sqlite-class-renames/tests/re-exports/expected.mjs @@ -0,0 +1,6 @@ +import { Database } from 'node:sqlite'; + +export { Statement as StatementSync } from 'node:sqlite'; +export { Database as SqliteDatabase } from 'node:sqlite'; +export { Database as DatabaseSync }; +export const api = { DatabaseSync: Database, open: () => new Database(':memory:') }; diff --git a/recipes/sqlite-class-renames/tests/re-exports/input.mjs b/recipes/sqlite-class-renames/tests/re-exports/input.mjs new file mode 100644 index 00000000..1c916dd5 --- /dev/null +++ b/recipes/sqlite-class-renames/tests/re-exports/input.mjs @@ -0,0 +1,6 @@ +import { DatabaseSync } from 'node:sqlite'; + +export { StatementSync } from 'node:sqlite'; +export { DatabaseSync as SqliteDatabase } from 'node:sqlite'; +export { DatabaseSync }; +export const api = { DatabaseSync, open: () => new DatabaseSync(':memory:') }; diff --git a/recipes/sqlite-class-renames/tests/shadowed-identifiers/expected.js b/recipes/sqlite-class-renames/tests/shadowed-identifiers/expected.js new file mode 100644 index 00000000..52eab9c6 --- /dev/null +++ b/recipes/sqlite-class-renames/tests/shadowed-identifiers/expected.js @@ -0,0 +1,8 @@ +const { Database: DatabaseSync, Statement } = require('node:sqlite'); + +function open(DatabaseSync) { + return new DatabaseSync(':memory:'); +} + +const db = open(DatabaseSync); +const check = (value) => value instanceof Statement; diff --git a/recipes/sqlite-class-renames/tests/shadowed-identifiers/input.js b/recipes/sqlite-class-renames/tests/shadowed-identifiers/input.js new file mode 100644 index 00000000..2816dc89 --- /dev/null +++ b/recipes/sqlite-class-renames/tests/shadowed-identifiers/input.js @@ -0,0 +1,8 @@ +const { DatabaseSync, StatementSync } = require('node:sqlite'); + +function open(DatabaseSync) { + return new DatabaseSync(':memory:'); +} + +const db = open(DatabaseSync); +const check = (value) => value instanceof StatementSync; diff --git a/recipes/sqlite-class-renames/tests/typescript/expected.ts b/recipes/sqlite-class-renames/tests/typescript/expected.ts new file mode 100644 index 00000000..d8d27495 --- /dev/null +++ b/recipes/sqlite-class-renames/tests/typescript/expected.ts @@ -0,0 +1,9 @@ +import { Database, Statement } from 'node:sqlite'; +import * as sqlite from 'node:sqlite'; +import type * as other from './types.js'; + +const db: Database = new Database(':memory:'); +function run(statement: Statement): sqlite.Statement { + return statement; +} +let legacy: other.DatabaseSync; diff --git a/recipes/sqlite-class-renames/tests/typescript/input.ts b/recipes/sqlite-class-renames/tests/typescript/input.ts new file mode 100644 index 00000000..a9b1fbf9 --- /dev/null +++ b/recipes/sqlite-class-renames/tests/typescript/input.ts @@ -0,0 +1,9 @@ +import { DatabaseSync, StatementSync } from 'node:sqlite'; +import * as sqlite from 'node:sqlite'; +import type * as other from './types.js'; + +const db: DatabaseSync = new DatabaseSync(':memory:'); +function run(statement: StatementSync): sqlite.StatementSync { + return statement; +} +let legacy: other.DatabaseSync; diff --git a/recipes/sqlite-class-renames/tests/unrelated/expected.mjs b/recipes/sqlite-class-renames/tests/unrelated/expected.mjs new file mode 100644 index 00000000..60da59c4 --- /dev/null +++ b/recipes/sqlite-class-renames/tests/unrelated/expected.mjs @@ -0,0 +1,6 @@ +import { DatabaseSync } from 'sqlite'; +import { StatementSync } from './local.js'; + +const thirdParty = require('sqlite').DatabaseSync; +const config = { DatabaseSync: true }; +console.log(config.DatabaseSync, new DatabaseSync(), StatementSync); diff --git a/recipes/sqlite-class-renames/tests/unrelated/input.mjs b/recipes/sqlite-class-renames/tests/unrelated/input.mjs new file mode 100644 index 00000000..60da59c4 --- /dev/null +++ b/recipes/sqlite-class-renames/tests/unrelated/input.mjs @@ -0,0 +1,6 @@ +import { DatabaseSync } from 'sqlite'; +import { StatementSync } from './local.js'; + +const thirdParty = require('sqlite').DatabaseSync; +const config = { DatabaseSync: true }; +console.log(config.DatabaseSync, new DatabaseSync(), StatementSync); diff --git a/recipes/sqlite-class-renames/workflow.yaml b/recipes/sqlite-class-renames/workflow.yaml new file mode 100644 index 00000000..4117e8de --- /dev/null +++ b/recipes/sqlite-class-renames/workflow.yaml @@ -0,0 +1,25 @@ +# yaml-language-server: $schema=https://raw.githubusercontent.com/codemod-com/codemod/refs/heads/main/schemas/workflow.json + +version: "1" + +nodes: + - id: apply-transforms + name: Apply AST Transformations + type: automatic + steps: + - name: Handle DEP0210 and DEP0211 by renaming node:sqlite DatabaseSync and StatementSync + js-ast-grep: + js_file: src/workflow.ts + base_path: . + include: + - "**/*.js" + - "**/*.jsx" + - "**/*.mjs" + - "**/*.cjs" + - "**/*.cts" + - "**/*.mts" + - "**/*.ts" + - "**/*.tsx" + exclude: + - "**/node_modules/**" + language: typescript