From a98990b8f0e7391d78a047f7537502bc7ee998f5 Mon Sep 17 00:00:00 2001 From: FrustratedBoy420 Date: Thu, 12 Nov 2026 17:54:40 +0530 Subject: [PATCH 1/3] feat(DEP0210,DEP0211): add node:sqlite class renames codemod Rename `DatabaseSync` to `Database` and `StatementSync` to `Statement` for bindings that resolve to `node:sqlite`. Support ESM named, namespace, and default imports, CommonJS destructuring and property access, aliases, `await import()`, re-exports, and TypeScript type references. Keep the old local name as an alias when the new name is already used or the old name is declared again, and keep public export names. Leave third-party `sqlite` packages and unrelated identifiers unchanged. Closes #560 Assisted-by: Claude Code (claude-opus-5-5) --- package-lock.json | 15 + recipes/sqlite-class-renames/README.md | 66 +++ recipes/sqlite-class-renames/codemod.yaml | 24 + recipes/sqlite-class-renames/package.json | 24 + recipes/sqlite-class-renames/src/workflow.ts | 485 ++++++++++++++++++ .../tests/already-migrated/expected.mjs | 7 + .../tests/already-migrated/input.mjs | 7 + .../tests/binding-collision/expected.mjs | 7 + .../tests/binding-collision/input.mjs | 7 + .../tests/cjs-destructuring/expected.js | 4 + .../tests/cjs-destructuring/input.js | 4 + .../tests/cjs-property-access/expected.js | 4 + .../tests/cjs-property-access/input.js | 4 + .../tests/dynamic-import/expected.mjs | 5 + .../tests/dynamic-import/input.mjs | 5 + .../tests/esm-alias/expected.mjs | 4 + .../tests/esm-alias/input.mjs | 4 + .../tests/esm-named/expected.mjs | 5 + .../tests/esm-named/input.mjs | 5 + .../tests/esm-namespace/expected.mjs | 7 + .../tests/esm-namespace/input.mjs | 7 + .../tests/re-exports/expected.mjs | 6 + .../tests/re-exports/input.mjs | 6 + .../tests/shadowed-identifiers/expected.js | 8 + .../tests/shadowed-identifiers/input.js | 8 + .../tests/typescript/expected.ts | 9 + .../tests/typescript/input.ts | 9 + .../tests/unrelated/expected.mjs | 6 + .../tests/unrelated/input.mjs | 6 + recipes/sqlite-class-renames/workflow.yaml | 25 + 30 files changed, 783 insertions(+) create mode 100644 recipes/sqlite-class-renames/README.md create mode 100644 recipes/sqlite-class-renames/codemod.yaml create mode 100644 recipes/sqlite-class-renames/package.json create mode 100644 recipes/sqlite-class-renames/src/workflow.ts create mode 100644 recipes/sqlite-class-renames/tests/already-migrated/expected.mjs create mode 100644 recipes/sqlite-class-renames/tests/already-migrated/input.mjs create mode 100644 recipes/sqlite-class-renames/tests/binding-collision/expected.mjs create mode 100644 recipes/sqlite-class-renames/tests/binding-collision/input.mjs create mode 100644 recipes/sqlite-class-renames/tests/cjs-destructuring/expected.js create mode 100644 recipes/sqlite-class-renames/tests/cjs-destructuring/input.js create mode 100644 recipes/sqlite-class-renames/tests/cjs-property-access/expected.js create mode 100644 recipes/sqlite-class-renames/tests/cjs-property-access/input.js create mode 100644 recipes/sqlite-class-renames/tests/dynamic-import/expected.mjs create mode 100644 recipes/sqlite-class-renames/tests/dynamic-import/input.mjs create mode 100644 recipes/sqlite-class-renames/tests/esm-alias/expected.mjs create mode 100644 recipes/sqlite-class-renames/tests/esm-alias/input.mjs create mode 100644 recipes/sqlite-class-renames/tests/esm-named/expected.mjs create mode 100644 recipes/sqlite-class-renames/tests/esm-named/input.mjs create mode 100644 recipes/sqlite-class-renames/tests/esm-namespace/expected.mjs create mode 100644 recipes/sqlite-class-renames/tests/esm-namespace/input.mjs create mode 100644 recipes/sqlite-class-renames/tests/re-exports/expected.mjs create mode 100644 recipes/sqlite-class-renames/tests/re-exports/input.mjs create mode 100644 recipes/sqlite-class-renames/tests/shadowed-identifiers/expected.js create mode 100644 recipes/sqlite-class-renames/tests/shadowed-identifiers/input.js create mode 100644 recipes/sqlite-class-renames/tests/typescript/expected.ts create mode 100644 recipes/sqlite-class-renames/tests/typescript/input.ts create mode 100644 recipes/sqlite-class-renames/tests/unrelated/expected.mjs create mode 100644 recipes/sqlite-class-renames/tests/unrelated/input.mjs create mode 100644 recipes/sqlite-class-renames/workflow.yaml 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..129febe9 --- /dev/null +++ b/recipes/sqlite-class-renames/README.md @@ -0,0 +1,66 @@ +--- +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 references. 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. 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..1e48722e --- /dev/null +++ b/recipes/sqlite-class-renames/src/workflow.ts @@ -0,0 +1,485 @@ +import type { + Edit, + Range, + Rule, + SgNode, + SgRoot, +} from '@codemod.com/jssg-types/main'; +import type Js from '@codemod.com/jssg-types/langs/javascript'; + +/** + * 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 } }, + ], +}; + +const IMPORT_CALL: Rule = { + kind: 'call_expression', + all: [ + { has: { field: 'function', kind: 'import' } }, + { 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(); + + const imports = rootNode.findAll({ + rule: { + kind: 'import_statement', + has: { field: 'source', ...SQLITE_SOURCE }, + }, + }); + const reExports = rootNode.findAll({ + rule: { + kind: 'export_statement', + has: { field: 'source', ...SQLITE_SOURCE }, + }, + }); + const declarators = rootNode.findAll({ + rule: { + kind: 'variable_declarator', + has: { + field: 'value', + any: [REQUIRE_CALL, { kind: 'await_expression', has: IMPORT_CALL }], + }, + }, + }); + 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: [...imports, ...reExports, ...declarators].map((node) => + node.range(), + ), + }; + + for (const statement of imports) { + migrateImportStatement(root, ctx, statement); + } + + for (const statement of reExports) { + migrateReExport(ctx, statement); + } + + for (const declarator of declarators) { + migrateDeclarator(root, ctx, declarator); + } + + for (const access of directAccesses) { + renameProperty(ctx, access.field('property')); + } + + if (!ctx.edits.length) return null; + + return rootNode.commitEdits(ctx.edits); +} + +/** + * `import { DatabaseSync } from 'node:sqlite'`, + * `import * as sqlite from 'node:sqlite'`, and + * `import sqlite from 'node:sqlite'`. + */ +function migrateImportStatement( + root: SgRoot, + ctx: Context, + statement: SgNode, +): void { + const clause = statement.find({ rule: { kind: 'import_clause' } }); + if (!clause) return; + + for (const child of clause.children()) { + switch (child.kind()) { + case 'identifier': + migrateNamespace(root, ctx, child); + break; + case 'namespace_import': { + const local = child.find({ rule: { kind: 'identifier' } }); + if (local) migrateNamespace(root, ctx, local); + break; + } + case 'named_imports': + for (const specifier of child.findAll({ + rule: { kind: 'import_specifier' }, + })) { + migrateImportSpecifier(ctx, specifier); + } + break; + } + } +} + +function migrateImportSpecifier(ctx: Context, specifier: SgNode): void { + const name = specifier.field('name'); + if (!name) return; + + const oldName = name.text(); + const newName = RENAMES.get(oldName); + if (!newName) return; + + if (specifier.field('alias')) { + ctx.edits.push(name.replace(newName)); + return; + } + + if (canRenameBinding(ctx, oldName, newName)) { + ctx.edits.push(name.replace(newName)); + renameReferences(ctx, oldName, newName); + return; + } + + ctx.edits.push(name.replace(`${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}`, + ), + ); + } +} + +/** + * `const { DatabaseSync } = require('node:sqlite')`, + * `const sqlite = require('node:sqlite')`, and the same forms with + * `await import('node:sqlite')`. + */ +function migrateDeclarator( + root: SgRoot, + ctx: Context, + declarator: SgNode, +): void { + const name = declarator.field('name'); + if (!name) return; + + if (name.kind() === 'identifier') { + migrateNamespace(root, ctx, name); + return; + } + + if (name.kind() !== 'object_pattern') return; + + for (const entry of name.children()) { + if (entry.kind() === 'pair_pattern') { + const key = entry.field('key'); + const newName = key && RENAMES.get(key.text()); + + if (key?.kind() === 'property_identifier' && newName) { + ctx.edits.push(key.replace(newName)); + } + continue; + } + + if (entry.kind() !== 'shorthand_property_identifier_pattern') continue; + + const oldName = entry.text(); + const newName = RENAMES.get(oldName); + if (!newName) continue; + + if (canRenameBinding(ctx, oldName, newName)) { + ctx.edits.push(entry.replace(newName)); + renameReferences(ctx, oldName, newName); + } else { + ctx.edits.push(entry.replace(`${newName}: ${oldName}`)); + } + } +} + +/** + * Renames `sqlite.DatabaseSync` (value or TypeScript type) for a binding that + * holds the whole `node:sqlite` module. + */ +function migrateNamespace( + root: SgRoot, + ctx: Context, + local: SgNode, +): void { + const namespace = local.text(); + + if (findNames(ctx, namespace).some(isBindingPosition)) { + warn( + root, + local, + `\`${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.kind() === 'shorthand_property_identifier') { + ctx.edits.push(node.replace(`${oldName}: ${newName}`)); + continue; + } + + const parent = node.parent(); + + if (parent?.kind() === '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?.kind() === 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.kind() === '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..5b716fc8 --- /dev/null +++ b/recipes/sqlite-class-renames/tests/typescript/expected.ts @@ -0,0 +1,9 @@ +import { Database, type 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..668e0260 --- /dev/null +++ b/recipes/sqlite-class-renames/tests/typescript/input.ts @@ -0,0 +1,9 @@ +import { DatabaseSync, type 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 From 84ec1f8421ee2282e933b7c0dc32267caa5f50f5 Mon Sep 17 00:00:00 2001 From: FrustratedBoy420 Date: Fri, 13 Nov 2026 15:29:00 +0530 Subject: [PATCH 2/3] refactor(sqlite-class-renames): use shared module helpers Find `node:sqlite` bindings with `getModuleDependencies` and `resolveBindingPath` from `@nodejs/codemod-utils` instead of custom import, require, and dynamic import queries. Type-only imports are not resolved by `resolveBindingPath`, so they are now left unchanged and documented as a limitation. Assisted-by: Claude Code (claude-opus-5-5) --- recipes/sqlite-class-renames/README.md | 3 +- recipes/sqlite-class-renames/src/workflow.ts | 199 +++++++----------- .../tests/typescript/expected.ts | 2 +- .../tests/typescript/input.ts | 2 +- 4 files changed, 76 insertions(+), 130 deletions(-) diff --git a/recipes/sqlite-class-renames/README.md b/recipes/sqlite-class-renames/README.md index 129febe9..3d9ab7bf 100644 --- a/recipes/sqlite-class-renames/README.md +++ b/recipes/sqlite-class-renames/README.md @@ -6,7 +6,7 @@ authors: FrustratedBoy420 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 references. Run it only on projects whose supported Node.js versions include 26.11.0 or later. +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 @@ -64,3 +64,4 @@ When the new name is already used in the file, or the old name is declared again - 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/src/workflow.ts b/recipes/sqlite-class-renames/src/workflow.ts index 1e48722e..e4c69ece 100644 --- a/recipes/sqlite-class-renames/src/workflow.ts +++ b/recipes/sqlite-class-renames/src/workflow.ts @@ -6,6 +6,8 @@ import type { 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 @@ -31,14 +33,6 @@ const REQUIRE_CALL: Rule = { ], }; -const IMPORT_CALL: Rule = { - kind: 'call_expression', - all: [ - { has: { field: 'function', kind: 'import' } }, - { 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 @@ -116,27 +110,19 @@ type Context = { export default function transform(root: SgRoot): string | null { const rootNode = root.root(); - const imports = rootNode.findAll({ - rule: { - kind: 'import_statement', - has: { field: 'source', ...SQLITE_SOURCE }, - }, - }); + // 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 declarators = rootNode.findAll({ - rule: { - kind: 'variable_declarator', - has: { - field: 'value', - any: [REQUIRE_CALL, { kind: 'await_expression', has: IMPORT_CALL }], - }, - }, - }); const directAccesses = rootNode.findAll({ rule: { kind: 'member_expression', @@ -150,21 +136,34 @@ export default function transform(root: SgRoot): string | null { const ctx: Context = { rootNode, edits: [], - ownRanges: [...imports, ...reExports, ...declarators].map((node) => - node.range(), - ), + 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')?.kind() === 'member_expression') continue; + + for (const [oldName, newName] of RENAMES) { + const binding = resolveBindingPath(statement, `$.${oldName}`); + if (!binding) continue; - for (const statement of imports) { - migrateImportStatement(root, ctx, statement); + const [local, member] = binding.split('.'); + + if (member) { + namespaces.set(local, statement); + } else { + migrateNamedBinding(ctx, statement, oldName, newName, local); + } + } } - for (const statement of reExports) { - migrateReExport(ctx, statement); + for (const [namespace, statement] of namespaces) { + migrateNamespace(root, ctx, namespace, statement); } - for (const declarator of declarators) { - migrateDeclarator(root, ctx, declarator); + for (const statement of reExports) { + migrateReExport(ctx, statement); } for (const access of directAccesses) { @@ -177,59 +176,52 @@ export default function transform(root: SgRoot): string | null { } /** - * `import { DatabaseSync } from 'node:sqlite'`, - * `import * as sqlite from 'node:sqlite'`, and - * `import sqlite from 'node:sqlite'`. + * 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 migrateImportStatement( - root: SgRoot, +function migrateNamedBinding( ctx: Context, statement: SgNode, + oldName: string, + newName: string, + local: string, ): void { - const clause = statement.find({ rule: { kind: 'import_clause' } }); - if (!clause) return; - - for (const child of clause.children()) { - switch (child.kind()) { - case 'identifier': - migrateNamespace(root, ctx, child); - break; - case 'namespace_import': { - const local = child.find({ rule: { kind: 'identifier' } }); - if (local) migrateNamespace(root, ctx, local); - break; - } - case 'named_imports': - for (const specifier of child.findAll({ - rule: { kind: 'import_specifier' }, - })) { - migrateImportSpecifier(ctx, specifier); - } - break; - } - } -} - -function migrateImportSpecifier(ctx: Context, specifier: SgNode): void { - const name = specifier.field('name'); - if (!name) return; - - const oldName = name.text(); - const newName = RENAMES.get(oldName); - if (!newName) return; - - if (specifier.field('alias')) { - ctx.edits.push(name.replace(newName)); - return; - } + 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 (canRenameBinding(ctx, oldName, newName)) { - ctx.edits.push(name.replace(newName)); - renameReferences(ctx, oldName, newName); + if (local !== oldName || canRenameBinding(ctx, oldName, newName)) { + ctx.edits.push(imported.replace(newName)); + if (local === oldName) renameReferences(ctx, oldName, newName); return; } - ctx.edits.push(name.replace(`${newName} as ${oldName}`)); + // Keep the old local name so no other code has to change. + ctx.edits.push( + imported.replace( + imported.kind() === 'shorthand_property_identifier_pattern' + ? `${newName}: ${oldName}` + : `${newName} as ${oldName}`, + ), + ); } /** @@ -255,52 +247,6 @@ function migrateReExport(ctx: Context, statement: SgNode): void { } } -/** - * `const { DatabaseSync } = require('node:sqlite')`, - * `const sqlite = require('node:sqlite')`, and the same forms with - * `await import('node:sqlite')`. - */ -function migrateDeclarator( - root: SgRoot, - ctx: Context, - declarator: SgNode, -): void { - const name = declarator.field('name'); - if (!name) return; - - if (name.kind() === 'identifier') { - migrateNamespace(root, ctx, name); - return; - } - - if (name.kind() !== 'object_pattern') return; - - for (const entry of name.children()) { - if (entry.kind() === 'pair_pattern') { - const key = entry.field('key'); - const newName = key && RENAMES.get(key.text()); - - if (key?.kind() === 'property_identifier' && newName) { - ctx.edits.push(key.replace(newName)); - } - continue; - } - - if (entry.kind() !== 'shorthand_property_identifier_pattern') continue; - - const oldName = entry.text(); - const newName = RENAMES.get(oldName); - if (!newName) continue; - - if (canRenameBinding(ctx, oldName, newName)) { - ctx.edits.push(entry.replace(newName)); - renameReferences(ctx, oldName, newName); - } else { - ctx.edits.push(entry.replace(`${newName}: ${oldName}`)); - } - } -} - /** * Renames `sqlite.DatabaseSync` (value or TypeScript type) for a binding that * holds the whole `node:sqlite` module. @@ -308,14 +254,13 @@ function migrateDeclarator( function migrateNamespace( root: SgRoot, ctx: Context, - local: SgNode, + namespace: string, + statement: SgNode, ): void { - const namespace = local.text(); - if (findNames(ctx, namespace).some(isBindingPosition)) { warn( root, - local, + statement, `\`${namespace}\` is redeclared in this file; review \`${namespace}.DatabaseSync\` and \`${namespace}.StatementSync\` manually (DEP0210, DEP0211).`, ); return; diff --git a/recipes/sqlite-class-renames/tests/typescript/expected.ts b/recipes/sqlite-class-renames/tests/typescript/expected.ts index 5b716fc8..d8d27495 100644 --- a/recipes/sqlite-class-renames/tests/typescript/expected.ts +++ b/recipes/sqlite-class-renames/tests/typescript/expected.ts @@ -1,4 +1,4 @@ -import { Database, type Statement } from 'node:sqlite'; +import { Database, Statement } from 'node:sqlite'; import * as sqlite from 'node:sqlite'; import type * as other from './types.js'; diff --git a/recipes/sqlite-class-renames/tests/typescript/input.ts b/recipes/sqlite-class-renames/tests/typescript/input.ts index 668e0260..a9b1fbf9 100644 --- a/recipes/sqlite-class-renames/tests/typescript/input.ts +++ b/recipes/sqlite-class-renames/tests/typescript/input.ts @@ -1,4 +1,4 @@ -import { DatabaseSync, type StatementSync } from 'node:sqlite'; +import { DatabaseSync, StatementSync } from 'node:sqlite'; import * as sqlite from 'node:sqlite'; import type * as other from './types.js'; From 95edc81ea7bbc7d8a8b7489b4cbf8d9de5df4228 Mon Sep 17 00:00:00 2001 From: FrustratedBoy420 Date: Fri, 13 Nov 2026 15:50:38 +0530 Subject: [PATCH 3/3] refactor(sqlite-class-renames): use `SgNode.is()` for kind checks Replace `node.kind() === '...'` comparisons with `node.is('...')`, as suggested by @AugustinMauroy in review. Assisted-by: Claude Code (claude-opus-5-5) --- recipes/sqlite-class-renames/src/workflow.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/recipes/sqlite-class-renames/src/workflow.ts b/recipes/sqlite-class-renames/src/workflow.ts index e4c69ece..3c881eac 100644 --- a/recipes/sqlite-class-renames/src/workflow.ts +++ b/recipes/sqlite-class-renames/src/workflow.ts @@ -142,7 +142,7 @@ export default function transform(root: SgRoot): string | null { for (const statement of statements) { // `require('node:sqlite').DatabaseSync` is handled by `directAccesses`. - if (statement.field('value')?.kind() === 'member_expression') continue; + if (statement.field('value')?.is('member_expression')) continue; for (const [oldName, newName] of RENAMES) { const binding = resolveBindingPath(statement, `$.${oldName}`); @@ -217,7 +217,7 @@ function migrateNamedBinding( // Keep the old local name so no other code has to change. ctx.edits.push( imported.replace( - imported.kind() === 'shorthand_property_identifier_pattern' + imported.is('shorthand_property_identifier_pattern') ? `${newName}: ${oldName}` : `${newName} as ${oldName}`, ), @@ -330,14 +330,14 @@ function renameReferences( newName: string, ): void { for (const node of findNames(ctx, oldName)) { - if (node.kind() === 'shorthand_property_identifier') { + if (node.is('shorthand_property_identifier')) { ctx.edits.push(node.replace(`${oldName}: ${newName}`)); continue; } const parent = node.parent(); - if (parent?.kind() === 'export_specifier') { + if (parent?.is('export_specifier')) { // `export { X as DatabaseSync }`: the alias is a public name. if (isField(parent, 'alias', node)) continue; @@ -373,7 +373,7 @@ function findNames(ctx: Context, name: string): SgNode[] { function isQualifiedTypeName(node: SgNode): boolean { const parent = node.parent(); - return parent?.kind() === NESTED_TYPE_KIND && isField(parent, 'name', node); + return parent?.is(NESTED_TYPE_KIND) && isField(parent, 'name', node); } /** @@ -382,7 +382,7 @@ function isQualifiedTypeName(node: SgNode): boolean { * treated as bindings too, because they must never be renamed. */ function isBindingPosition(node: SgNode): boolean { - if (node.kind() === 'shorthand_property_identifier_pattern') return true; + if (node.is('shorthand_property_identifier_pattern')) return true; const parent = node.parent(); if (!parent) return false;