diff --git a/codemods/route-del-to-delete/README.md b/codemods/route-del-to-delete/README.md new file mode 100644 index 0000000..250e5c6 --- /dev/null +++ b/codemods/route-del-to-delete/README.md @@ -0,0 +1,21 @@ +# Migrate legacy `app.del()` to `app.delete()` + +Migrates usage of the legacy APIs `app.del()` to `app.delete()`. +Initially, `del` was used instead of `delete`, because `delete` is a reserved keyword in JavaScript. However, as of ECMAScript 6, `delete` and other reserved keywords can legally be used as property names. The `app.del()` method was deprecated in Express 4 and removed in Express 5. + +## Example + +### Migrating `app.del()` + +The migration involves replacing instances of `app.del()` with `app.delete()`. + +```diff +- app.del('/some-route', (req, res) => { ++ app.delete('/some-route', (req, res) => { + // Some logic here + }); +``` + +## References + +- [Migration of app.del()](https://expressjs.com/en/guide/migrating-5.html#app.del) diff --git a/codemods/route-del-to-delete/codemod.yaml b/codemods/route-del-to-delete/codemod.yaml new file mode 100644 index 0000000..cece85c --- /dev/null +++ b/codemods/route-del-to-delete/codemod.yaml @@ -0,0 +1,25 @@ +schema_version: "1.0" +name: "@expressjs/route-del-to-delete" +version: "1.0.0" +description: Migrates usage of the legacy APIs `app.del()` to `app.delete()` +author: bjohansebas (Sebastian Beltran) +license: MIT +workflow: workflow.yaml +repository: "https://github.com/expressjs/codemod/tree/HEAD/codemods/route-del-to-delete" +category: migration + +targets: + languages: + - javascript + - typescript + +keywords: + - transformation + - migration + - express + - redirect + - location + +registry: + access: public + visibility: public \ No newline at end of file diff --git a/codemods/route-del-to-delete/package.json b/codemods/route-del-to-delete/package.json new file mode 100644 index 0000000..5482aed --- /dev/null +++ b/codemods/route-del-to-delete/package.json @@ -0,0 +1,22 @@ +{ + "name": "@expressjs/route-del-to-delete", + "private": true, + "version": "1.0.0", + "description": "Migrates usage of the legacy APIs `app.del` to `app.delete`.", + "type": "module", + "scripts": { + "test": "npx codemod jssg test -l typescript ./src/workflow.ts ./" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/expressjs/codemod.git", + "directory": "codemods/route-del-to-delete", + "bugs": "https://github.com/expressjs/codemod/issues" + }, + "author": "bjohansebas (Sebastian Beltran)", + "license": "MIT", + "homepage": "https://github.com/expressjs/codemod/blob/main/codemods/route-del-to-delete/README.md", + "devDependencies": { + "@codemod.com/jssg-types": "^1.3.1" + } +} diff --git a/codemods/route-del-to-delete/src/workflow.ts b/codemods/route-del-to-delete/src/workflow.ts new file mode 100644 index 0000000..6d74a97 --- /dev/null +++ b/codemods/route-del-to-delete/src/workflow.ts @@ -0,0 +1,43 @@ +import type Js from '@codemod.com/jssg-types/src/langs/javascript' +import type { Edit, SgRoot } from '@codemod.com/jssg-types/src/main' + +async function transform(root: SgRoot): Promise { + const rootNode = root.root() + + const nodes = rootNode.findAll({ + rule: { + pattern: '$OBJ.$METHOD($$$ARG)', + }, + constraints: { + METHOD: { regex: '^(del)$' }, + }, + }) + + if (!nodes.length) return null + + const edits: Edit[] = [] + + for (const call of nodes) { + const method = call.getMatch('METHOD') + const args = call.getMultipleMatches('ARG') + if (!method) continue + + // $$$ARG yields argument nodes interleaved with separators, so arg nodes are at 0,2,4... + const first = args[0] + if (!first) continue + + const isString = first.is('string') + const isRegexp = first.is('regexp') || first.is('regex') || first.is('regular_expression') + const isArray = first.is('array') || first.is('array_expression') + + if (!isString && !isRegexp && !isArray) continue + + edits.push(method.replace('delete')) + } + + if (!edits.length) return null + + return rootNode.commitEdits(edits) +} + +export default transform diff --git a/codemods/route-del-to-delete/tests/expected/delete.ts b/codemods/route-del-to-delete/tests/expected/delete.ts new file mode 100644 index 0000000..b1dbf42 --- /dev/null +++ b/codemods/route-del-to-delete/tests/expected/delete.ts @@ -0,0 +1,39 @@ +import express from "express"; + +const app = express(); + +app.get("/", (req, res) => {}); + +app.delete([""],() => { + myImportantLogic(); +}); + +app.delete([],() => { + myImportantLogic(); +}); + +app.delete(/d/,() => { + myImportantLogic(); +}); + +app.del(() => { + myImportantLogic(); +}); + +app.del(function () { + myImportantLogic(); +}); + +app.delete("/old", () => { + myImportantLogic(); +}); + +function noModify() { + let a + + app.del(a) +} + +const myImportantLogic = () => { + console.log("making sure it's there"); +}; diff --git a/codemods/route-del-to-delete/tests/input/delete.ts b/codemods/route-del-to-delete/tests/input/delete.ts new file mode 100644 index 0000000..aeab3e3 --- /dev/null +++ b/codemods/route-del-to-delete/tests/input/delete.ts @@ -0,0 +1,39 @@ +import express from "express"; + +const app = express(); + +app.get("/", (req, res) => {}); + +app.del([""],() => { + myImportantLogic(); +}); + +app.del([],() => { + myImportantLogic(); +}); + +app.del(/d/,() => { + myImportantLogic(); +}); + +app.del(() => { + myImportantLogic(); +}); + +app.del(function () { + myImportantLogic(); +}); + +app.del("/old", () => { + myImportantLogic(); +}); + +function noModify() { + let a + + app.del(a) +} + +const myImportantLogic = () => { + console.log("making sure it's there"); +}; diff --git a/codemods/route-del-to-delete/workflow.yaml b/codemods/route-del-to-delete/workflow.yaml new file mode 100644 index 0000000..0253be9 --- /dev/null +++ b/codemods/route-del-to-delete/workflow.yaml @@ -0,0 +1,28 @@ +# 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 + runtime: + type: direct + steps: + - name: Migrates usage of the legacy APIs `app.del()` to `app.delete()` + js-ast-grep: + js_file: src/workflow.ts + base_path: . + semantic_analysis: file + include: + - "**/*.cjs" + - "**/*.js" + - "**/*.jsx" + - "**/*.mjs" + - "**/*.cts" + - "**/*.mts" + - "**/*.ts" + - "**/*.tsx" + exclude: + - "**/node_modules/**" + language: typescript \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index cb10744..1bf6ff7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -72,6 +72,14 @@ "@codemod.com/jssg-types": "^1.3.1" } }, + "codemods/route-del-to-delete": { + "name": "@expressjs/route-del-to-delete", + "version": "1.0.0", + "license": "MIT", + "devDependencies": { + "@codemod.com/jssg-types": "^1.3.1" + } + }, "codemods/status-send-order": { "name": "@expressjs/status-send-order", "version": "1.0.0", @@ -1049,6 +1057,10 @@ "resolved": "codemods/pluralize-method-names", "link": true }, + "node_modules/@expressjs/route-del-to-delete": { + "resolved": "codemods/route-del-to-delete", + "link": true + }, "node_modules/@expressjs/status-send-order": { "resolved": "codemods/status-send-order", "link": true