diff --git a/.gitignore b/.gitignore index ccc2e3c..755450a 100644 --- a/.gitignore +++ b/.gitignore @@ -25,4 +25,7 @@ yarn-error.log* *.d.ts *.js -*.js.map \ No newline at end of file +*.js.map + +# VSCode +.vscode \ No newline at end of file diff --git a/config.ts b/config.ts index 312deed..1581753 100644 --- a/config.ts +++ b/config.ts @@ -9,5 +9,9 @@ export const TRANSFORM_OPTIONS = [ value: 'pluralized-methods', version: '5.0.0', }, - //{ description: 'Transform the deprecated signatures in Express v4', value: 'signature-deprecated', version: '5.0.0' }, + { + description: 'Transform the deprecated signatures in Express v4', + value: 'v4-deprecated-signatures', + version: '5.0.0' + }, ] diff --git a/transforms/__test__/v4-deprecated-signatures.spec.ts b/transforms/__test__/v4-deprecated-signatures.spec.ts new file mode 100644 index 0000000..4df36b4 --- /dev/null +++ b/transforms/__test__/v4-deprecated-signatures.spec.ts @@ -0,0 +1,3 @@ +import { testSpecBuilder } from './util' + +testSpecBuilder('v4-deprecated-signatures') diff --git a/transforms/__testfixtures__/v4-deprecated-signatures/json.input.ts b/transforms/__testfixtures__/v4-deprecated-signatures/json.input.ts new file mode 100644 index 0000000..56fc2b4 --- /dev/null +++ b/transforms/__testfixtures__/v4-deprecated-signatures/json.input.ts @@ -0,0 +1,57 @@ +import express from "express"; + +const app = express(); + +app.get("/json", function (req, res) { + res.json({ user: "Username", isValid: true }, 200); +}); + +app.get("/json", function (req, response) { + response.json({ user: "Username", isValid: true }, 200); +}); + +app.get("/json", (req, res) => { + res.json({ user: "Username", isValid: true }, 200); +}); + +app.get("/json", (req, response) => { + response.json({ user: "Username", isValid: true }, 200); +}); + +app.get("/json", function (req, res) { + res.json({}, 200); +}); + +app.get("/json", function (req, response) { + response.json({}, 200); +}); + +app.get("/json", (req, res) => { + res.json({}, 200); +}); + +app.get("/json", (req, response) => { + response.json({}, 200); +}); + +// Still valid syntax -- START +app.get("/json", function (req, res) { + res.json(null) + res.json({ user: 'tobi' }) +}) + +app.get("/json", function (req, response) { + response.json(null) + response.json({ user: 'tobi' }) +}) + +app.get("/json", function (req, res) { + res.json(null) + res.json({ user: 'tobi' }) +}) + +app.get("/json", function (req, response) { + response.json(null) + response.json({ user: 'tobi' }) +}) +// Still valid syntax -- END \ No newline at end of file diff --git a/transforms/__testfixtures__/v4-deprecated-signatures/json.output.ts b/transforms/__testfixtures__/v4-deprecated-signatures/json.output.ts new file mode 100644 index 0000000..92905a1 --- /dev/null +++ b/transforms/__testfixtures__/v4-deprecated-signatures/json.output.ts @@ -0,0 +1,57 @@ +import express from "express"; + +const app = express(); + +app.get("/json", function (req, res) { + res.status(200).json({ user: "Username", isValid: true }); +}); + +app.get("/json", function (req, response) { + response.status(200).json({ user: "Username", isValid: true }); +}); + +app.get("/json", (req, res) => { + res.status(200).json({ user: "Username", isValid: true }); +}); + +app.get("/json", (req, response) => { + response.status(200).json({ user: "Username", isValid: true }); +}); + +app.get("/json", function (req, res) { + res.status(200).json({}); +}); + +app.get("/json", function (req, response) { + response.status(200).json({}); +}); + +app.get("/json", (req, res) => { + res.status(200).json({}); +}); + +app.get("/json", (req, response) => { + response.status(200).json({}); +}); + +// Still valid syntax -- START +app.get("/json", function (req, res) { + res.json(null) + res.json({ user: 'tobi' }) +}) + +app.get("/json", function (req, response) { + response.json(null) + response.json({ user: 'tobi' }) +}) + +app.get("/json", function (req, res) { + res.json(null) + res.json({ user: 'tobi' }) +}) + +app.get("/json", function (req, response) { + response.json(null) + response.json({ user: 'tobi' }) +}) +// Still valid syntax -- END \ No newline at end of file diff --git a/transforms/__testfixtures__/v4-deprecated-signatures/jsonp.input.ts b/transforms/__testfixtures__/v4-deprecated-signatures/jsonp.input.ts new file mode 100644 index 0000000..99b2b2f --- /dev/null +++ b/transforms/__testfixtures__/v4-deprecated-signatures/jsonp.input.ts @@ -0,0 +1,57 @@ +import express from "express"; + +const app = express(); + +app.get("/jsonp", function (req, res) { + res.jsonp({ user: "Username", isValid: true }, 200); +}); + +app.get("/jsonp", function (req, response) { + response.jsonp({ user: "Username", isValid: true }, 200); +}); + +app.get("/jsonp", (req, res) => { + res.jsonp({ user: "Username", isValid: true }, 200); +}); + +app.get("/jsonp", (req, response) => { + response.jsonp({ user: "Username", isValid: true }, 200); +}); + +app.get("/jsonp", function (req, res) { + res.jsonp({}, 200); +}); + +app.get("/jsonp", function (req, response) { + response.jsonp({}, 200); +}); + +app.get("/jsonp", (req, res) => { + res.jsonp({}, 200) +}); + +app.get("/jsonp", (req, response) => { + response.jsonp({}, 200) +}); + +// Still valid syntax -- START +app.get("/jsonp", function (req, res) { + res.jsonp(null) + res.jsonp({ user: 'tobi' }) +}) + +app.get("/jsonp", function (req, response) { + response.jsonp(null) + response.jsonp({ user: 'tobi' }) +}) + +app.get("/jsonp", function (req, res) { + res.jsonp(null) + res.jsonp({ user: 'tobi' }) +}) + +app.get("/jsonp", function (req, response) { + response.jsonp(null) + response.jsonp({ user: 'tobi' }) +}) +// Still valid syntax -- END \ No newline at end of file diff --git a/transforms/__testfixtures__/v4-deprecated-signatures/jsonp.output.ts b/transforms/__testfixtures__/v4-deprecated-signatures/jsonp.output.ts new file mode 100644 index 0000000..2a8e4c1 --- /dev/null +++ b/transforms/__testfixtures__/v4-deprecated-signatures/jsonp.output.ts @@ -0,0 +1,57 @@ +import express from "express"; + +const app = express(); + +app.get("/jsonp", function (req, res) { + res.status(200).jsonp({ user: "Username", isValid: true }); +}); + +app.get("/jsonp", function (req, response) { + response.status(200).jsonp({ user: "Username", isValid: true }); +}); + +app.get("/jsonp", (req, res) => { + res.status(200).jsonp({ user: "Username", isValid: true }); +}); + +app.get("/jsonp", (req, response) => { + response.status(200).jsonp({ user: "Username", isValid: true }); +}); + +app.get("/jsonp", function (req, res) { + res.status(200).jsonp({}); +}); + +app.get("/jsonp", function (req, response) { + response.status(200).jsonp({}); +}); + +app.get("/jsonp", (req, res) => { + res.status(200).jsonp({}) +}); + +app.get("/jsonp", (req, response) => { + response.status(200).jsonp({}) +}); + +// Still valid syntax -- START +app.get("/jsonp", function (req, res) { + res.jsonp(null) + res.jsonp({ user: 'tobi' }) +}) + +app.get("/jsonp", function (req, response) { + response.jsonp(null) + response.jsonp({ user: 'tobi' }) +}) + +app.get("/jsonp", function (req, res) { + res.jsonp(null) + res.jsonp({ user: 'tobi' }) +}) + +app.get("/jsonp", function (req, response) { + response.jsonp(null) + response.jsonp({ user: 'tobi' }) +}) +// Still valid syntax -- END \ No newline at end of file diff --git a/transforms/__testfixtures__/v4-deprecated-signatures/send.input.ts b/transforms/__testfixtures__/v4-deprecated-signatures/send.input.ts new file mode 100644 index 0000000..d2d38fa --- /dev/null +++ b/transforms/__testfixtures__/v4-deprecated-signatures/send.input.ts @@ -0,0 +1,61 @@ +import express from "express"; + +const app = express(); + +app.get("/send", function (req, res) { + res.send(200, { hello: "world" }); +}); + +app.get("/send", function (req, response) { + response.send(200, "Hello World"); +}); + +app.get("/send", function (req, res) { + res.send(200); +}); + +app.get("/send", function (req, res) { + res.send(200, true); +}); + +app.get("/send", (req, res) => { + res.send(200, { hello: "world" }); +}); + +app.get("/send", (req, res) => { + res.send(200); +}); + +app.get("/send", (req, response) => { + response.send(200); +}); + +app.get("/send", (req, response) => { + response.send(200, true); +}); + +// Still valid syntax -- START +app.get("/send", function (req, res) { + res.send(Buffer.from('whoop')); + res.send({ some: 'json' }); + res.send('
some html
'); +}); + +app.get("/send", function (req, response) { + response.send(Buffer.from('whoop')); + response.send({ some: 'json' }); + response.send('some html
'); +}); + +app.get("/send", (req, response) => { + response.send(Buffer.from('whoop')); + response.send({ some: 'json' }); + response.send('some html
'); +}); + +app.get("/send", (req, res) => { + res.send(Buffer.from('whoop')); + res.send({ some: 'json' }); + res.send('some html
'); +}); +// Still valid syntax -- END \ No newline at end of file diff --git a/transforms/__testfixtures__/v4-deprecated-signatures/send.output.ts b/transforms/__testfixtures__/v4-deprecated-signatures/send.output.ts new file mode 100644 index 0000000..f317983 --- /dev/null +++ b/transforms/__testfixtures__/v4-deprecated-signatures/send.output.ts @@ -0,0 +1,61 @@ +import express from "express"; + +const app = express(); + +app.get("/send", function (req, res) { + res.status(200).send({ hello: "world" }); +}); + +app.get("/send", function (req, response) { + response.status(200).send("Hello World"); +}); + +app.get("/send", function (req, res) { + res.sendStatus(200); +}); + +app.get("/send", function (req, res) { + res.status(200).send(true); +}); + +app.get("/send", (req, res) => { + res.status(200).send({ hello: "world" }); +}); + +app.get("/send", (req, res) => { + res.sendStatus(200); +}); + +app.get("/send", (req, response) => { + response.sendStatus(200); +}); + +app.get("/send", (req, response) => { + response.status(200).send(true); +}); + +// Still valid syntax -- START +app.get("/send", function (req, res) { + res.send(Buffer.from('whoop')); + res.send({ some: 'json' }); + res.send('some html
'); +}); + +app.get("/send", function (req, response) { + response.send(Buffer.from('whoop')); + response.send({ some: 'json' }); + response.send('some html
'); +}); + +app.get("/send", (req, response) => { + response.send(Buffer.from('whoop')); + response.send({ some: 'json' }); + response.send('some html
'); +}); + +app.get("/send", (req, res) => { + res.send(Buffer.from('whoop')); + res.send({ some: 'json' }); + res.send('some html
'); +}); +// Still valid syntax -- END \ No newline at end of file diff --git a/transforms/magic-redirect.ts b/transforms/magic-redirect.ts index 9eefd66..eb988c3 100644 --- a/transforms/magic-redirect.ts +++ b/transforms/magic-redirect.ts @@ -1,11 +1,8 @@ import { type API, - type ASTNode, type ASTPath, - type ArrowFunctionExpression, CallExpression, type FileInfo, - type FunctionExpression, callExpression, identifier, literal, @@ -13,8 +10,7 @@ import { memberExpression, } from 'jscodeshift' import { getParsedFile } from '../utils/parse' - -const parentExpressionType = ['ArrowFunctionExpression', 'FunctionExpression'] as const +import { recursiveParent } from '../utils/recursiveParent' const unifiedMagicString = (path: ASTPath