diff --git a/codemods/camelcase-sendfile/README.md b/codemods/camelcase-sendfile/README.md new file mode 100644 index 0000000..a707f0e --- /dev/null +++ b/codemods/camelcase-sendfile/README.md @@ -0,0 +1,21 @@ +# Migrate legacy `res.sendfile(file)` to `res.sendFile(file)` + +Migrates usage of the legacy APIs `res.sendfile(file)` to `res.sendFile(file)`. + +## Example + +### Migrating `res.sendfile(file)` + +The migration involves replacing instances of `res.sendfile(file)` with `res.sendFile(file)`. + +```diff +app.get('/some-route', (req, res) => { + // Some logic here +- res.sendfile('/path/to/file'); ++ res.sendFile('/path/to/file'); +}); +``` + +## References + +- [Migration of res.sendfile(file)](https://expressjs.com/en/guide/migrating-5.html#res.sendFile) \ No newline at end of file diff --git a/codemods/camelcase-sendfile/codemod.yaml b/codemods/camelcase-sendfile/codemod.yaml new file mode 100644 index 0000000..1f2a6ac --- /dev/null +++ b/codemods/camelcase-sendfile/codemod.yaml @@ -0,0 +1,25 @@ +schema_version: "1.0" +name: "@expressjs/camelcase-sendfile" +version: "1.0.0" +description: Migrates usage of the legacy API `res.sendfile(file)` to `res.sendFile(file)` +author: bjohansebas (Sebastian Beltran) +license: MIT +workflow: workflow.yaml +repository: "https://github.com/expressjs/codemod/tree/HEAD/codemods/camelcase-sendfile" +category: migration + +targets: + languages: + - javascript + - typescript + +keywords: + - transformation + - migration + - express + - sendFile + - files + +registry: + access: public + visibility: public \ No newline at end of file diff --git a/codemods/camelcase-sendfile/package.json b/codemods/camelcase-sendfile/package.json new file mode 100644 index 0000000..dc5275f --- /dev/null +++ b/codemods/camelcase-sendfile/package.json @@ -0,0 +1,22 @@ +{ + "name": "@expressjs/camelcase-sendfile", + "private": true, + "version": "1.0.0", + "description": "Migrates usage of the legacy API `res.sendfile(file)` to `res.sendFile(file)`.", + "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/camelcase-sendfile", + "bugs": "https://github.com/expressjs/codemod/issues" + }, + "author": "bjohansebas (Sebastian Beltran)", + "license": "MIT", + "homepage": "https://github.com/expressjs/codemod/blob/main/codemods/camelcase-sendfile/README.md", + "devDependencies": { + "@codemod.com/jssg-types": "^1.3.1" + } +} diff --git a/codemods/camelcase-sendfile/src/workflow.ts b/codemods/camelcase-sendfile/src/workflow.ts new file mode 100644 index 0000000..1238746 --- /dev/null +++ b/codemods/camelcase-sendfile/src/workflow.ts @@ -0,0 +1,35 @@ +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($$$METHOD)', + }, + constraints: { + METHOD: { regex: '^(sendfile)$' }, + }, + }) + + if (!nodes.length) return null + + const edits: Edit[] = [] + + for (const call of nodes) { + const method = call.getMatch('METHOD') + const obj = call.getMatch('OBJ') + if (!method || !obj) continue + + const objDef = obj.definition({ resolveExternal: false }) + if (!objDef) continue + + edits.push(method.replace('sendFile')) + } + + if (!edits.length) return null + return rootNode.commitEdits(edits) +} + +export default transform diff --git a/codemods/camelcase-sendfile/tests/expected/send-file.ts b/codemods/camelcase-sendfile/tests/expected/send-file.ts new file mode 100644 index 0000000..f222bc4 --- /dev/null +++ b/codemods/camelcase-sendfile/tests/expected/send-file.ts @@ -0,0 +1,67 @@ +import express from "express"; +import path from "path"; +import sendfile from "other-place" + +const app = express(); +const options = { + root: path.join(__dirname, 'public'), + dotfiles: 'deny', + headers: { + 'x-timestamp': Date.now(), + 'x-sent': true + } +} + +const sharedSendFile = (res, next, fileName) => { + res.sendFile(fileName, options, (err) => { + if (err) { + next(err) + } else { + console.log('Sent:', fileName) + } + }) +} + +app.get('/file/:name', (req, res, next) => { + res.sendFile() +}) + +app.get('/file/:name', (req, res, next) => { + res.sendFile("file.txt") +}) + +app.get('/file/:name', (req, res, next) => { + const fileName = req.params.name + + res.sendFile(fileName, options, (err) => { + if (err) { + next(err) + } else { + console.log('Sent:', fileName) + } + }) + sharedSendFile(res, next, fileName); +}) + +app.get('/filename/:name', function (req, res, next) { + const fileName = req.params.name + + res.sendFile(fileName, options, (err) => { + if (err) { + next(err) + } else { + console.log('Sent:', fileName) + } + }) + sharedSendFile(res, next, fileName); +}) + +app.get('/file-handler', (req, res, next) => { + sendfile('test', options, (err) => { + if (err) { + next(err) + } else { + console.log('Sent:', 'test') + } + }) +}) \ No newline at end of file diff --git a/codemods/camelcase-sendfile/tests/input/send-file.ts b/codemods/camelcase-sendfile/tests/input/send-file.ts new file mode 100644 index 0000000..37926ea --- /dev/null +++ b/codemods/camelcase-sendfile/tests/input/send-file.ts @@ -0,0 +1,67 @@ +import express from "express"; +import path from "path"; +import sendfile from "other-place" + +const app = express(); +const options = { + root: path.join(__dirname, 'public'), + dotfiles: 'deny', + headers: { + 'x-timestamp': Date.now(), + 'x-sent': true + } +} + +const sharedSendFile = (res, next, fileName) => { + res.sendfile(fileName, options, (err) => { + if (err) { + next(err) + } else { + console.log('Sent:', fileName) + } + }) +} + +app.get('/file/:name', (req, res, next) => { + res.sendfile() +}) + +app.get('/file/:name', (req, res, next) => { + res.sendfile("file.txt") +}) + +app.get('/file/:name', (req, res, next) => { + const fileName = req.params.name + + res.sendfile(fileName, options, (err) => { + if (err) { + next(err) + } else { + console.log('Sent:', fileName) + } + }) + sharedSendFile(res, next, fileName); +}) + +app.get('/filename/:name', function (req, res, next) { + const fileName = req.params.name + + res.sendfile(fileName, options, (err) => { + if (err) { + next(err) + } else { + console.log('Sent:', fileName) + } + }) + sharedSendFile(res, next, fileName); +}) + +app.get('/file-handler', (req, res, next) => { + sendfile('test', options, (err) => { + if (err) { + next(err) + } else { + console.log('Sent:', 'test') + } + }) +}) \ No newline at end of file diff --git a/codemods/camelcase-sendfile/workflow.yaml b/codemods/camelcase-sendfile/workflow.yaml new file mode 100644 index 0000000..0749364 --- /dev/null +++ b/codemods/camelcase-sendfile/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 `res.sendfile(file)` to `res.sendFile(file)` + 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 aeac2ae..f65e18b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -56,6 +56,14 @@ "@codemod.com/jssg-types": "^1.3.1" } }, + "codemods/sendfile-to-sendFile": { + "name": "@expressjs/sendfile-to-sendfile", + "version": "1.0.0", + "license": "MIT", + "devDependencies": { + "@codemod.com/jssg-types": "^1.3.1" + } + }, "node_modules/@ampproject/remapping": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/@ampproject/remapping/-/remapping-2.3.0.tgz", @@ -1017,6 +1025,10 @@ "resolved": "codemods/back-redirect-deprecated", "link": true }, + "node_modules/@expressjs/sendfile-to-sendfile": { + "resolved": "codemods/sendfile-to-sendFile", + "link": true + }, "node_modules/@istanbuljs/load-nyc-config": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz",