From 0e87d18b016f4f08c95ed0c9e45d677ae0934bc9 Mon Sep 17 00:00:00 2001 From: KHeo Date: Tue, 19 Nov 2019 09:26:46 +0900 Subject: [PATCH 01/18] Added a file that removes comments that start with #. --- package.json | 2 ++ .../decaff/__testfixtures__/remove-comment-sharp.input.js | 6 ++++++ .../decaff/__testfixtures__/remove-comment-sharp.output.js | 6 ++++++ scripts/decaff/__tests__/remove-comment-sharp.test.js | 6 ++++++ scripts/decaff/remove-comment-sharp.js | 4 ++++ 5 files changed, 24 insertions(+) create mode 100644 scripts/decaff/__testfixtures__/remove-comment-sharp.input.js create mode 100644 scripts/decaff/__testfixtures__/remove-comment-sharp.output.js create mode 100644 scripts/decaff/__tests__/remove-comment-sharp.test.js create mode 100644 scripts/decaff/remove-comment-sharp.js diff --git a/package.json b/package.json index 38a527786125..0797b06f1109 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "stop-only-all": "npm run stop-only -- --folder packages", "test": "echo '⚠️ This root monorepo is only for local development and new contributions. There are no tests.'", "test-debug-package": "node ./scripts/test-debug-package.js", + "test-jscodeshift": "jest ./scripts/decaff", "test-mocha": "mocha --reporter spec scripts/spec.js", "test-mocha-snapshot": "mocha scripts/mocha-snapshot-spec.js", "test-s3-api": "node -r ./packages/coffee/register -r ./packages/ts/register scripts/binary/s3-api-demo.ts", @@ -133,6 +134,7 @@ "husky": "2.4.1", "inquirer": "3.3.0", "inquirer-confirm": "2.0.3", + "jest": "24.9.0", "js-codemod": "cpojer/js-codemod#29dafed", "jscodemods": "cypress-io/jscodemods#01b546e", "jscodeshift": "0.6.3", diff --git a/scripts/decaff/__testfixtures__/remove-comment-sharp.input.js b/scripts/decaff/__testfixtures__/remove-comment-sharp.input.js new file mode 100644 index 000000000000..6a2f2a008f44 --- /dev/null +++ b/scripts/decaff/__testfixtures__/remove-comment-sharp.input.js @@ -0,0 +1,6 @@ +//# Testing this +// eslint-disable-next-line +console.log('it works') //# Test + +// This # should not be removed. +/*# it's not wrong */ diff --git a/scripts/decaff/__testfixtures__/remove-comment-sharp.output.js b/scripts/decaff/__testfixtures__/remove-comment-sharp.output.js new file mode 100644 index 000000000000..834260f7bfd0 --- /dev/null +++ b/scripts/decaff/__testfixtures__/remove-comment-sharp.output.js @@ -0,0 +1,6 @@ +// Testing this +// eslint-disable-next-line +console.log('it works') // Test + +// This # should not be removed. +/*# it's not wrong */ diff --git a/scripts/decaff/__tests__/remove-comment-sharp.test.js b/scripts/decaff/__tests__/remove-comment-sharp.test.js new file mode 100644 index 000000000000..352038332f49 --- /dev/null +++ b/scripts/decaff/__tests__/remove-comment-sharp.test.js @@ -0,0 +1,6 @@ +/* global jest */ + +jest.autoMockOff() +const defineTest = require('jscodeshift/dist/testUtils').defineTest + +defineTest(__dirname, 'remove-comment-sharp') diff --git a/scripts/decaff/remove-comment-sharp.js b/scripts/decaff/remove-comment-sharp.js new file mode 100644 index 000000000000..08273d5abd0f --- /dev/null +++ b/scripts/decaff/remove-comment-sharp.js @@ -0,0 +1,4 @@ +// This file doesn't use AST because it makes things too complicated. +module.exports = (fileInfo) => { + return fileInfo.source.replace(/\/\/#/g, '//') +} From 32791f920db2f6d6c9e11f00f3436b2f4732b2c0 Mon Sep 17 00:00:00 2001 From: KHeo Date: Tue, 19 Nov 2019 11:10:06 +0900 Subject: [PATCH 02/18] Added .eslintrc to silence ts errors in codemods. --- scripts/decaff/.eslintrc.json | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 scripts/decaff/.eslintrc.json diff --git a/scripts/decaff/.eslintrc.json b/scripts/decaff/.eslintrc.json new file mode 100644 index 000000000000..db6ab5794049 --- /dev/null +++ b/scripts/decaff/.eslintrc.json @@ -0,0 +1,3 @@ +{ + "parser": "espree" +} From 5387eea64855ec302e326af45293c0d2b26e2fa3 Mon Sep 17 00:00:00 2001 From: KHeo Date: Tue, 19 Nov 2019 11:10:25 +0900 Subject: [PATCH 03/18] Convert switch (false) to if-else. --- .../__testfixtures__/switch-false.input.js | 28 ++++++++ .../__testfixtures__/switch-false.output.js | 22 ++++++ scripts/decaff/__tests__/switch-false.test.js | 6 ++ scripts/decaff/switch-false.js | 67 +++++++++++++++++++ 4 files changed, 123 insertions(+) create mode 100644 scripts/decaff/__testfixtures__/switch-false.input.js create mode 100644 scripts/decaff/__testfixtures__/switch-false.output.js create mode 100644 scripts/decaff/__tests__/switch-false.test.js create mode 100644 scripts/decaff/switch-false.js diff --git a/scripts/decaff/__testfixtures__/switch-false.input.js b/scripts/decaff/__testfixtures__/switch-false.input.js new file mode 100644 index 000000000000..1a635541b867 --- /dev/null +++ b/scripts/decaff/__testfixtures__/switch-false.input.js @@ -0,0 +1,28 @@ +/* global a, b, c, findThings, isBad, isAwesome, hi */ + +function f () { + switch (false) { + case !a.subject(a): + let x = 30 + + b.doSomething(x) + break + case c.isGood: + c.checkThisOut() + findThings() + break + case isBad: + c.neverCheck() + break + case !isAwesome: + a.subject(a) + break + case hi: + return 3 + default: + b.goToNext() + break + } +} + +f() diff --git a/scripts/decaff/__testfixtures__/switch-false.output.js b/scripts/decaff/__testfixtures__/switch-false.output.js new file mode 100644 index 000000000000..1c0d0ebb40cb --- /dev/null +++ b/scripts/decaff/__testfixtures__/switch-false.output.js @@ -0,0 +1,22 @@ +/* global a, b, c, findThings, isBad, isAwesome, hi */ + +function f () { + if (a.subject(a)) { + let x = 30 + + b.doSomething(x) + } else if (!c.isGood) { + c.checkThisOut() + findThings() + } else if (!isBad) { + c.neverCheck() + } else if (isAwesome) { + a.subject(a) + } else if (!hi) { + return 3 + } else { + b.goToNext() + } +} + +f() diff --git a/scripts/decaff/__tests__/switch-false.test.js b/scripts/decaff/__tests__/switch-false.test.js new file mode 100644 index 000000000000..86ebfccf5ec1 --- /dev/null +++ b/scripts/decaff/__tests__/switch-false.test.js @@ -0,0 +1,6 @@ +/* global jest */ + +jest.autoMockOff() +const defineTest = require('jscodeshift/dist/testUtils').defineTest + +defineTest(__dirname, 'switch-false') diff --git a/scripts/decaff/switch-false.js b/scripts/decaff/switch-false.js new file mode 100644 index 000000000000..9bb2e0077449 --- /dev/null +++ b/scripts/decaff/switch-false.js @@ -0,0 +1,67 @@ +module.exports = (fileInfo, api) => { + const j = api.jscodeshift + + return j(fileInfo.source) + .find(j.SwitchStatement, { + discriminant: { + type: 'Literal', + value: false, + }, + }) + .replaceWith((nodePath) => { + const { node } = nodePath + + const cases = node.cases.map((c) => { + const { test, consequent } = c + + return { + test: generateTest(j, test), + content: generateContent(j, consequent), + } + }) + + const ifStatement = generateIfStatement(j, cases) + + ifStatement.comments = node.comments + + return ifStatement + }) + .toSource() +} + +function generateTest (j, test) { + if (test) { + if (test.type === 'UnaryExpression') { + return test.argument + } + + return j.unaryExpression('!', test) + } + + return null +} + +function generateContent (j, consequent) { + return j.blockStatement(consequent.filter((c) => c.type !== 'BreakStatement')) +} + +function generateIfStatement (j, cases) { + const nonDefaultCases = cases.filter((c) => c.test !== null) + const defaultCase = cases.filter((c) => c.test === null)[0] + + let ifStatement = null + + if (defaultCase) { + ifStatement = defaultCase.content + } + + nonDefaultCases.reverse().forEach((c) => { + ifStatement = j.ifStatement( + c.test, + c.content, + ifStatement + ) + }) + + return ifStatement +} From 5c3c623f056756e1214826a059a0e19d4587d29a Mon Sep 17 00:00:00 2001 From: KHeo Date: Tue, 19 Nov 2019 11:37:08 +0900 Subject: [PATCH 04/18] Add disable-eslint no-empty by default after empty catch. --- .../__testfixtures__/empty-catch.input.js | 7 +++++ .../__testfixtures__/empty-catch.output.js | 7 +++++ scripts/decaff/__tests__/empty-catch.test.js | 6 ++++ scripts/decaff/empty-catch.js | 28 +++++++++++++++++++ 4 files changed, 48 insertions(+) create mode 100644 scripts/decaff/__testfixtures__/empty-catch.input.js create mode 100644 scripts/decaff/__testfixtures__/empty-catch.output.js create mode 100644 scripts/decaff/__tests__/empty-catch.test.js create mode 100644 scripts/decaff/empty-catch.js diff --git a/scripts/decaff/__testfixtures__/empty-catch.input.js b/scripts/decaff/__testfixtures__/empty-catch.input.js new file mode 100644 index 000000000000..ea887d4b034c --- /dev/null +++ b/scripts/decaff/__testfixtures__/empty-catch.input.js @@ -0,0 +1,7 @@ +try { + // do something +} catch (e) {} + +try { + // do something +} finally {} diff --git a/scripts/decaff/__testfixtures__/empty-catch.output.js b/scripts/decaff/__testfixtures__/empty-catch.output.js new file mode 100644 index 000000000000..e7ef9ff68c38 --- /dev/null +++ b/scripts/decaff/__testfixtures__/empty-catch.output.js @@ -0,0 +1,7 @@ +try { + // do something +} catch (e) {} // eslint-disable-line no-empty + +try { + // do something +} finally {} diff --git a/scripts/decaff/__tests__/empty-catch.test.js b/scripts/decaff/__tests__/empty-catch.test.js new file mode 100644 index 000000000000..dbd86f548880 --- /dev/null +++ b/scripts/decaff/__tests__/empty-catch.test.js @@ -0,0 +1,6 @@ +/* global jest */ + +jest.autoMockOff() +const defineTest = require('jscodeshift/dist/testUtils').defineTest + +defineTest(__dirname, 'empty-catch') diff --git a/scripts/decaff/empty-catch.js b/scripts/decaff/empty-catch.js new file mode 100644 index 000000000000..815b8395859b --- /dev/null +++ b/scripts/decaff/empty-catch.js @@ -0,0 +1,28 @@ +module.exports = (fileInfo, api) => { + const j = api.jscodeshift + + const source = j(fileInfo.source) + .find(j.TryStatement) + .replaceWith((nodePath) => { + const { node } = nodePath + + // Add trailing eslint-disable-line for empty catch block + if (node.handler && node.handler.body.body.length === 0) { + node.handler.body.comments = [ + { + type: 'Line', + value: ' eslint-disable-line no-empty', + leading: false, + trailing: true, + }, + ] + } + + return node + }) + .toSource() + + // Generated source above creates {}// eslint-disable-line block. + // So, add a space with replace + return source.replace(/\{\}\/\/ eslint-disable-line/g, '{} // eslint-disable-line') +} From e85c6455c0a12a2711293829db494694d9e9c765 Mon Sep 17 00:00:00 2001 From: KHeo Date: Tue, 19 Nov 2019 12:13:38 +0900 Subject: [PATCH 05/18] Reserve comments when converting switch-case. --- .../__testfixtures__/switch-false.input.js | 2 ++ .../__testfixtures__/switch-false.output.js | 14 +++++----- scripts/decaff/switch-false.js | 27 ++++++++++++++++--- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/scripts/decaff/__testfixtures__/switch-false.input.js b/scripts/decaff/__testfixtures__/switch-false.input.js index 1a635541b867..877f73801374 100644 --- a/scripts/decaff/__testfixtures__/switch-false.input.js +++ b/scripts/decaff/__testfixtures__/switch-false.input.js @@ -2,11 +2,13 @@ function f () { switch (false) { + // Comment should be reserved case !a.subject(a): let x = 30 b.doSomething(x) break + // Comment should be reserved 2 case c.isGood: c.checkThisOut() findThings() diff --git a/scripts/decaff/__testfixtures__/switch-false.output.js b/scripts/decaff/__testfixtures__/switch-false.output.js index 1c0d0ebb40cb..07dacb8e264a 100644 --- a/scripts/decaff/__testfixtures__/switch-false.output.js +++ b/scripts/decaff/__testfixtures__/switch-false.output.js @@ -2,20 +2,22 @@ function f () { if (a.subject(a)) { - let x = 30 + // Comment should be reserved + let x = 30; b.doSomething(x) } else if (!c.isGood) { - c.checkThisOut() + // Comment should be reserved 2 + c.checkThisOut(); findThings() } else if (!isBad) { - c.neverCheck() + c.neverCheck(); } else if (isAwesome) { - a.subject(a) + a.subject(a); } else if (!hi) { - return 3 + return 3; } else { - b.goToNext() + b.goToNext(); } } diff --git a/scripts/decaff/switch-false.js b/scripts/decaff/switch-false.js index 9bb2e0077449..9e0ed3a12ba1 100644 --- a/scripts/decaff/switch-false.js +++ b/scripts/decaff/switch-false.js @@ -12,11 +12,12 @@ module.exports = (fileInfo, api) => { const { node } = nodePath const cases = node.cases.map((c) => { - const { test, consequent } = c + const { test, consequent, comments } = c return { test: generateTest(j, test), content: generateContent(j, consequent), + comments, } }) @@ -52,16 +53,36 @@ function generateIfStatement (j, cases) { let ifStatement = null if (defaultCase) { - ifStatement = defaultCase.content + const content = addComment(defaultCase.content, defaultCase.comments) + + ifStatement = content } nonDefaultCases.reverse().forEach((c) => { + const content = addComment(c.content, c.comments) + ifStatement = j.ifStatement( c.test, - c.content, + content, ifStatement ) }) return ifStatement } + +function addComment (content, comments) { + if (content.body.length > 0) { + content.body[0].comments = [...(comments || []), ...(content.comments || [])] + } else { + content.comments = (comments || []).map((co) => { + return { + ...co, + leading: false, + trailing: false, + } + }) + } + + return content +} From 4fa499e1f456221edc196975ebdd028478b3c35f Mon Sep 17 00:00:00 2001 From: KHeo Date: Tue, 19 Nov 2019 12:14:27 +0900 Subject: [PATCH 06/18] Added new codemods to bulk-decaffeinate. --- bulk-decaffeinate.config.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bulk-decaffeinate.config.js b/bulk-decaffeinate.config.js index 7f81e1d7f506..f7c2c068dd61 100644 --- a/bulk-decaffeinate.config.js +++ b/bulk-decaffeinate.config.js @@ -13,5 +13,8 @@ module.exports = { path.resolve('node_modules', 'jscodemods', 'decaffeinate', 'fix-multi-assign-class-export.js'), path.resolve('node_modules', 'jscodemods', 'decaffeinate', 'fix-implicit-return-assignment.js'), path.resolve('node_modules', 'jscodemods', 'decaffeinate', 'fix-existential-conditional-assignment.js'), + './scripts/decaff/remove-comment-sharp.js', + './scripts/decaff/switch-false.js', + './scripts/decaff/empty-catch.js', ], } From 66fc913dec9b23609000b2e78941aa4f06a37271 Mon Sep 17 00:00:00 2001 From: KHeo Date: Tue, 19 Nov 2019 12:42:23 +0900 Subject: [PATCH 07/18] Preserve comments. --- .../decaff/__testfixtures__/switch-false.input.js | 9 +++++++-- .../decaff/__testfixtures__/switch-false.output.js | 11 +++++++---- scripts/decaff/switch-false.js | 14 ++++++++++++-- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/scripts/decaff/__testfixtures__/switch-false.input.js b/scripts/decaff/__testfixtures__/switch-false.input.js index 877f73801374..16247d4f06dc 100644 --- a/scripts/decaff/__testfixtures__/switch-false.input.js +++ b/scripts/decaff/__testfixtures__/switch-false.input.js @@ -1,4 +1,4 @@ -/* global a, b, c, findThings, isBad, isAwesome, hi */ +/* global a, b, c, findThings, isBad, isAwesome, hi, you */ function f () { switch (false) { @@ -8,7 +8,8 @@ function f () { b.doSomething(x) break - // Comment should be reserved 2 + // Multi line comment + // should be reserved case c.isGood: c.checkThisOut() findThings() @@ -20,7 +21,11 @@ function f () { a.subject(a) break case hi: + // This should be reserved, too return 3 + case you: + // This comment is preserved + break default: b.goToNext() break diff --git a/scripts/decaff/__testfixtures__/switch-false.output.js b/scripts/decaff/__testfixtures__/switch-false.output.js index 07dacb8e264a..c787b2d3da64 100644 --- a/scripts/decaff/__testfixtures__/switch-false.output.js +++ b/scripts/decaff/__testfixtures__/switch-false.output.js @@ -1,4 +1,4 @@ -/* global a, b, c, findThings, isBad, isAwesome, hi */ +/* global a, b, c, findThings, isBad, isAwesome, hi, you */ function f () { if (a.subject(a)) { @@ -7,7 +7,8 @@ function f () { b.doSomething(x) } else if (!c.isGood) { - // Comment should be reserved 2 + // Multi line comment + // should be reserved c.checkThisOut(); findThings() } else if (!isBad) { @@ -15,8 +16,10 @@ function f () { } else if (isAwesome) { a.subject(a); } else if (!hi) { - return 3; - } else { + // This should be reserved, too + return 3 + } else if (!you) // This comment is preserved + {} else { b.goToNext(); } } diff --git a/scripts/decaff/switch-false.js b/scripts/decaff/switch-false.js index 9e0ed3a12ba1..850a8fe90495 100644 --- a/scripts/decaff/switch-false.js +++ b/scripts/decaff/switch-false.js @@ -43,6 +43,14 @@ function generateTest (j, test) { } function generateContent (j, consequent) { + if (consequent.length === 1 && consequent[0].type === 'BreakStatement') { + const block = j.blockStatement([]) + + block.comments = consequent[0].comments + + return block + } + return j.blockStatement(consequent.filter((c) => c.type !== 'BreakStatement')) } @@ -73,15 +81,17 @@ function generateIfStatement (j, cases) { function addComment (content, comments) { if (content.body.length > 0) { - content.body[0].comments = [...(comments || []), ...(content.comments || [])] + content.body[0].comments = [...(comments || []), ...(content.body[0].comments || [])] } else { - content.comments = (comments || []).map((co) => { + const newComments = (comments || []).map((co) => { return { ...co, leading: false, trailing: false, } }) + + content.comments = [...newComments, ...(content.comments || [])] } return content From 6e2da0c1ca0ba176aa9d5b7f2412fced21d9c72f Mon Sep 17 00:00:00 2001 From: KHeo Date: Tue, 19 Nov 2019 15:05:06 +0900 Subject: [PATCH 08/18] Ignored lint rules for test fixtures. --- scripts/decaff/__testfixtures__/.eslintrc.json | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 scripts/decaff/__testfixtures__/.eslintrc.json diff --git a/scripts/decaff/__testfixtures__/.eslintrc.json b/scripts/decaff/__testfixtures__/.eslintrc.json new file mode 100644 index 000000000000..bbf3e6188a43 --- /dev/null +++ b/scripts/decaff/__testfixtures__/.eslintrc.json @@ -0,0 +1,9 @@ +{ + "parser": "espree", + "rules": { + "no-empty": "off", + "semi": "off", + "brace-style": "off", + "no-case-declarations": "off" + } +} From b29eab44a5b3c8bac357682528c6b30fd3358a1e Mon Sep 17 00:00:00 2001 From: KHeo Date: Wed, 20 Nov 2019 10:02:21 +0900 Subject: [PATCH 09/18] Move comments between arrow and block to the top. --- .../__testfixtures__/arrow-comment.input.js | 10 +++++++ .../__testfixtures__/arrow-comment.output.js | 10 +++++++ .../decaff/__tests__/arrow-comment.test.js | 6 +++++ scripts/decaff/arrow-comment.js | 27 +++++++++++++++++++ 4 files changed, 53 insertions(+) create mode 100644 scripts/decaff/__testfixtures__/arrow-comment.input.js create mode 100644 scripts/decaff/__testfixtures__/arrow-comment.output.js create mode 100644 scripts/decaff/__tests__/arrow-comment.test.js create mode 100644 scripts/decaff/arrow-comment.js diff --git a/scripts/decaff/__testfixtures__/arrow-comment.input.js b/scripts/decaff/__testfixtures__/arrow-comment.input.js new file mode 100644 index 000000000000..357c42d5825e --- /dev/null +++ b/scripts/decaff/__testfixtures__/arrow-comment.input.js @@ -0,0 +1,10 @@ +// Original comment +const func = (a, b) => +// Multiline comment +// is here +{ + a.works() + b.check.thisOut() +} + +func() diff --git a/scripts/decaff/__testfixtures__/arrow-comment.output.js b/scripts/decaff/__testfixtures__/arrow-comment.output.js new file mode 100644 index 000000000000..1593e89d37d5 --- /dev/null +++ b/scripts/decaff/__testfixtures__/arrow-comment.output.js @@ -0,0 +1,10 @@ +// Original comment +// Multiline comment +// is here +const func = (a, b) => +{ + a.works() + b.check.thisOut() +}; + +func() diff --git a/scripts/decaff/__tests__/arrow-comment.test.js b/scripts/decaff/__tests__/arrow-comment.test.js new file mode 100644 index 000000000000..5d5eaa25577f --- /dev/null +++ b/scripts/decaff/__tests__/arrow-comment.test.js @@ -0,0 +1,6 @@ +/* global jest */ + +jest.autoMockOff() +const defineTest = require('jscodeshift/dist/testUtils').defineTest + +defineTest(__dirname, 'arrow-comment') diff --git a/scripts/decaff/arrow-comment.js b/scripts/decaff/arrow-comment.js new file mode 100644 index 000000000000..044870f9b0e1 --- /dev/null +++ b/scripts/decaff/arrow-comment.js @@ -0,0 +1,27 @@ +module.exports = (fileInfo, api) => { + const j = api.jscodeshift + + return j(fileInfo.source) + .find(j.VariableDeclaration, { + declarations: [{ + type: 'VariableDeclarator', + init: { + type: 'ArrowFunctionExpression', + body: { + type: 'BlockStatement', + }, + }, + }], + }) + .replaceWith((nodePath) => { + const { node } = nodePath + + if (node.declarations[0].init.body.comments.length > 0) { + node.comments = [...node.comments, ...node.declarations[0].init.body.comments] + node.declarations[0].init.body.comments = null + } + + return node + }) + .toSource() +} From 687bc2bf1d18f2a5961463e23f45df40a0ebea9c Mon Sep 17 00:00:00 2001 From: KHeo Date: Wed, 20 Nov 2019 10:04:35 +0900 Subject: [PATCH 10/18] Merged tests into one file. --- scripts/decaff/__tests__/arrow-comment.test.js | 6 ------ .../{remove-comment-sharp.test.js => decaff.test.js} | 3 +++ scripts/decaff/__tests__/empty-catch.test.js | 6 ------ scripts/decaff/__tests__/switch-false.test.js | 6 ------ 4 files changed, 3 insertions(+), 18 deletions(-) delete mode 100644 scripts/decaff/__tests__/arrow-comment.test.js rename scripts/decaff/__tests__/{remove-comment-sharp.test.js => decaff.test.js} (57%) delete mode 100644 scripts/decaff/__tests__/empty-catch.test.js delete mode 100644 scripts/decaff/__tests__/switch-false.test.js diff --git a/scripts/decaff/__tests__/arrow-comment.test.js b/scripts/decaff/__tests__/arrow-comment.test.js deleted file mode 100644 index 5d5eaa25577f..000000000000 --- a/scripts/decaff/__tests__/arrow-comment.test.js +++ /dev/null @@ -1,6 +0,0 @@ -/* global jest */ - -jest.autoMockOff() -const defineTest = require('jscodeshift/dist/testUtils').defineTest - -defineTest(__dirname, 'arrow-comment') diff --git a/scripts/decaff/__tests__/remove-comment-sharp.test.js b/scripts/decaff/__tests__/decaff.test.js similarity index 57% rename from scripts/decaff/__tests__/remove-comment-sharp.test.js rename to scripts/decaff/__tests__/decaff.test.js index 352038332f49..6382628fa29a 100644 --- a/scripts/decaff/__tests__/remove-comment-sharp.test.js +++ b/scripts/decaff/__tests__/decaff.test.js @@ -3,4 +3,7 @@ jest.autoMockOff() const defineTest = require('jscodeshift/dist/testUtils').defineTest +defineTest(__dirname, 'switch-false') +defineTest(__dirname, 'empty-catch') defineTest(__dirname, 'remove-comment-sharp') +defineTest(__dirname, 'arrow-comment') diff --git a/scripts/decaff/__tests__/empty-catch.test.js b/scripts/decaff/__tests__/empty-catch.test.js deleted file mode 100644 index dbd86f548880..000000000000 --- a/scripts/decaff/__tests__/empty-catch.test.js +++ /dev/null @@ -1,6 +0,0 @@ -/* global jest */ - -jest.autoMockOff() -const defineTest = require('jscodeshift/dist/testUtils').defineTest - -defineTest(__dirname, 'empty-catch') diff --git a/scripts/decaff/__tests__/switch-false.test.js b/scripts/decaff/__tests__/switch-false.test.js deleted file mode 100644 index 86ebfccf5ec1..000000000000 --- a/scripts/decaff/__tests__/switch-false.test.js +++ /dev/null @@ -1,6 +0,0 @@ -/* global jest */ - -jest.autoMockOff() -const defineTest = require('jscodeshift/dist/testUtils').defineTest - -defineTest(__dirname, 'switch-false') From 263f6a8cd742b2255e343fcb6365a20affc3b444 Mon Sep 17 00:00:00 2001 From: KHeo Date: Wed, 20 Nov 2019 10:38:23 +0900 Subject: [PATCH 11/18] Turned off no-undef for test fixtures. --- scripts/decaff/__testfixtures__/.eslintrc.json | 1 + scripts/decaff/__testfixtures__/switch-false.input.js | 2 -- scripts/decaff/__testfixtures__/switch-false.output.js | 2 -- 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/scripts/decaff/__testfixtures__/.eslintrc.json b/scripts/decaff/__testfixtures__/.eslintrc.json index bbf3e6188a43..311d18860155 100644 --- a/scripts/decaff/__testfixtures__/.eslintrc.json +++ b/scripts/decaff/__testfixtures__/.eslintrc.json @@ -2,6 +2,7 @@ "parser": "espree", "rules": { "no-empty": "off", + "no-undef": "off", "semi": "off", "brace-style": "off", "no-case-declarations": "off" diff --git a/scripts/decaff/__testfixtures__/switch-false.input.js b/scripts/decaff/__testfixtures__/switch-false.input.js index 16247d4f06dc..78af2d53df24 100644 --- a/scripts/decaff/__testfixtures__/switch-false.input.js +++ b/scripts/decaff/__testfixtures__/switch-false.input.js @@ -1,5 +1,3 @@ -/* global a, b, c, findThings, isBad, isAwesome, hi, you */ - function f () { switch (false) { // Comment should be reserved diff --git a/scripts/decaff/__testfixtures__/switch-false.output.js b/scripts/decaff/__testfixtures__/switch-false.output.js index c787b2d3da64..cb82f14828a0 100644 --- a/scripts/decaff/__testfixtures__/switch-false.output.js +++ b/scripts/decaff/__testfixtures__/switch-false.output.js @@ -1,5 +1,3 @@ -/* global a, b, c, findThings, isBad, isAwesome, hi, you */ - function f () { if (a.subject(a)) { // Comment should be reserved From a90f724408f1c0d642af10a86332fa348db2dc7a Mon Sep 17 00:00:00 2001 From: KHeo Date: Wed, 20 Nov 2019 10:56:24 +0900 Subject: [PATCH 12/18] No-cond-assign --- .../decaff/__testfixtures__/.eslintrc.json | 1 + .../__testfixtures__/no-cond-assign.input.js | 6 ++++ .../__testfixtures__/no-cond-assign.output.js | 8 +++++ scripts/decaff/__tests__/decaff.test.js | 1 + scripts/decaff/no-cond-assign.js | 29 +++++++++++++++++++ 5 files changed, 45 insertions(+) create mode 100644 scripts/decaff/__testfixtures__/no-cond-assign.input.js create mode 100644 scripts/decaff/__testfixtures__/no-cond-assign.output.js create mode 100644 scripts/decaff/no-cond-assign.js diff --git a/scripts/decaff/__testfixtures__/.eslintrc.json b/scripts/decaff/__testfixtures__/.eslintrc.json index 311d18860155..21e7f159b82c 100644 --- a/scripts/decaff/__testfixtures__/.eslintrc.json +++ b/scripts/decaff/__testfixtures__/.eslintrc.json @@ -3,6 +3,7 @@ "rules": { "no-empty": "off", "no-undef": "off", + "no-cond-assign": "off", "semi": "off", "brace-style": "off", "no-case-declarations": "off" diff --git a/scripts/decaff/__testfixtures__/no-cond-assign.input.js b/scripts/decaff/__testfixtures__/no-cond-assign.input.js new file mode 100644 index 000000000000..896e62036218 --- /dev/null +++ b/scripts/decaff/__testfixtures__/no-cond-assign.input.js @@ -0,0 +1,6 @@ +let a + +// Comment +if (a = c()) { + a.fix() +} diff --git a/scripts/decaff/__testfixtures__/no-cond-assign.output.js b/scripts/decaff/__testfixtures__/no-cond-assign.output.js new file mode 100644 index 000000000000..3541fbd343a2 --- /dev/null +++ b/scripts/decaff/__testfixtures__/no-cond-assign.output.js @@ -0,0 +1,8 @@ +let a + +// Comment +a = c(); + +if (a) { + a.fix() +} diff --git a/scripts/decaff/__tests__/decaff.test.js b/scripts/decaff/__tests__/decaff.test.js index 6382628fa29a..4921d9b52b37 100644 --- a/scripts/decaff/__tests__/decaff.test.js +++ b/scripts/decaff/__tests__/decaff.test.js @@ -7,3 +7,4 @@ defineTest(__dirname, 'switch-false') defineTest(__dirname, 'empty-catch') defineTest(__dirname, 'remove-comment-sharp') defineTest(__dirname, 'arrow-comment') +defineTest(__dirname, 'no-cond-assign') diff --git a/scripts/decaff/no-cond-assign.js b/scripts/decaff/no-cond-assign.js new file mode 100644 index 000000000000..3c74a3b1f89d --- /dev/null +++ b/scripts/decaff/no-cond-assign.js @@ -0,0 +1,29 @@ +module.exports = (fileInfo, api) => { + const j = api.jscodeshift + + return j(fileInfo.source) + .find(j.IfStatement, { + test: { + type: 'AssignmentExpression', + }, + }) + .replaceWith((nodePath) => { + const { node } = nodePath + + const assign = j.expressionStatement(node.test) + + assign.comments = node.comments + + const ifStatement = j.ifStatement( + node.test.left, + node.consequent, + node.alternate + ) + + return [ + assign, + ifStatement, + ] + }) + .toSource() +} From ef0c0f8d92540bd71f3c688e48ce219ffda65275 Mon Sep 17 00:00:00 2001 From: KHeo Date: Wed, 20 Nov 2019 10:59:11 +0900 Subject: [PATCH 13/18] Added jscodemods to decaff config. --- bulk-decaffeinate.config.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bulk-decaffeinate.config.js b/bulk-decaffeinate.config.js index f7c2c068dd61..ed68498132ef 100644 --- a/bulk-decaffeinate.config.js +++ b/bulk-decaffeinate.config.js @@ -16,5 +16,7 @@ module.exports = { './scripts/decaff/remove-comment-sharp.js', './scripts/decaff/switch-false.js', './scripts/decaff/empty-catch.js', + './scripts/decaff/no-cond-assing.js', + './scripts/decaff/arrow-comment.js', ], } From 7cccd9c2e7779e4f26a2e7c826abd4df1a30be21 Mon Sep 17 00:00:00 2001 From: KHeo Date: Thu, 21 Nov 2019 09:01:07 +0900 Subject: [PATCH 14/18] Fixed typo. --- bulk-decaffeinate.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bulk-decaffeinate.config.js b/bulk-decaffeinate.config.js index ed68498132ef..e7af5f254a2d 100644 --- a/bulk-decaffeinate.config.js +++ b/bulk-decaffeinate.config.js @@ -16,7 +16,7 @@ module.exports = { './scripts/decaff/remove-comment-sharp.js', './scripts/decaff/switch-false.js', './scripts/decaff/empty-catch.js', - './scripts/decaff/no-cond-assing.js', + './scripts/decaff/no-cond-assign.js', './scripts/decaff/arrow-comment.js', ], } From 1f5ae33bbd60e2daf690a331f0c8e37a2c5fef53 Mon Sep 17 00:00:00 2001 From: KHeo Date: Wed, 27 Nov 2019 15:50:03 +0900 Subject: [PATCH 15/18] Make CI test codemods --- circle.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/circle.yml b/circle.yml index 951cfab046bd..de86112a72f3 100644 --- a/circle.yml +++ b/circle.yml @@ -290,6 +290,8 @@ jobs: - run: npm run test-mocha-snapshot # make sure packages with TypeScript can be transpiled to JS - run: npm run all build-js + # test codemods + - run: npm run test-jscodeshift # run unit tests from individual packages - run: npm run all test -- --package cli - run: npm run all test -- --package electron From 71c4fe25240e321d5b20302d801aacfc9822f832 Mon Sep 17 00:00:00 2001 From: KHeo Date: Mon, 2 Dec 2019 18:19:46 +0900 Subject: [PATCH 16/18] Added test-no-return. --- bulk-decaffeinate.config.js | 1 + .../__testfixtures__/test-no-return.input.js | 24 +++++++++ .../__testfixtures__/test-no-return.output.js | 24 +++++++++ scripts/decaff/__tests__/decaff.test.js | 1 + scripts/decaff/test-no-return.js | 51 +++++++++++++++++++ 5 files changed, 101 insertions(+) create mode 100644 scripts/decaff/__testfixtures__/test-no-return.input.js create mode 100644 scripts/decaff/__testfixtures__/test-no-return.output.js create mode 100644 scripts/decaff/test-no-return.js diff --git a/bulk-decaffeinate.config.js b/bulk-decaffeinate.config.js index e7af5f254a2d..8120ef9cf4b0 100644 --- a/bulk-decaffeinate.config.js +++ b/bulk-decaffeinate.config.js @@ -18,5 +18,6 @@ module.exports = { './scripts/decaff/empty-catch.js', './scripts/decaff/no-cond-assign.js', './scripts/decaff/arrow-comment.js', + './scripts/decaff/test-no-return.js', ], } diff --git a/scripts/decaff/__testfixtures__/test-no-return.input.js b/scripts/decaff/__testfixtures__/test-no-return.input.js new file mode 100644 index 000000000000..07c28501686f --- /dev/null +++ b/scripts/decaff/__testfixtures__/test-no-return.input.js @@ -0,0 +1,24 @@ +context('a', function () { + beforeEach(() => { + doSomething() + + return settings.write('something') + }) + + afterEach(() => { + return settings.reset() + }) + + it('test 1', function () { + cy.visit('hi/world') + + return cy.click('abc') + }) + + it('test 2', () => { + return this.project.saveState() + .then((state) => { + expect(state).to.deep.eq({}) + }) + }) +}) diff --git a/scripts/decaff/__testfixtures__/test-no-return.output.js b/scripts/decaff/__testfixtures__/test-no-return.output.js new file mode 100644 index 000000000000..e157967c67b0 --- /dev/null +++ b/scripts/decaff/__testfixtures__/test-no-return.output.js @@ -0,0 +1,24 @@ +context('a', function () { + beforeEach(() => { + doSomething() + + settings.write('something'); + }) + + afterEach(() => { + settings.reset(); + }) + + it('test 1', function () { + cy.visit('hi/world') + + cy.click('abc'); + }) + + it('test 2', () => { + this.project.saveState() + .then((state) => { + expect(state).to.deep.eq({}) + }); + }) +}) diff --git a/scripts/decaff/__tests__/decaff.test.js b/scripts/decaff/__tests__/decaff.test.js index 4921d9b52b37..c613a1a0582b 100644 --- a/scripts/decaff/__tests__/decaff.test.js +++ b/scripts/decaff/__tests__/decaff.test.js @@ -8,3 +8,4 @@ defineTest(__dirname, 'empty-catch') defineTest(__dirname, 'remove-comment-sharp') defineTest(__dirname, 'arrow-comment') defineTest(__dirname, 'no-cond-assign') +defineTest(__dirname, 'test-no-return') diff --git a/scripts/decaff/test-no-return.js b/scripts/decaff/test-no-return.js new file mode 100644 index 000000000000..f3d39398ddbc --- /dev/null +++ b/scripts/decaff/test-no-return.js @@ -0,0 +1,51 @@ +module.exports = (fileInfo, api) => { + const funcs = [ + { name: 'beforeEach', index: 0 }, + { name: 'afterEach', index: 0 }, + { name: 'it', index: 1 }, + ] + + const j = api.jscodeshift + + const removeReturn = (src, name, index) => { + return j(src) + .find(j.ExpressionStatement, { + expression: { + callee: { + type: 'Identifier', + name, + }, + }, + }) + .replaceWith(({ node }) => { + let block = node.expression.arguments[index].body + + if (block.body && block.body.length > 0) { + block.body = block.body.map((s) => { + if (s.type === 'ReturnStatement') { + const stat = j.expressionStatement(s.argument) + + if (s.comments) { + stat.comments = s.comments + } + + return stat + } + + return s + }) + } + + return node + }) + .toSource() + } + + let src = fileInfo.source + + funcs.forEach(({ name, index }) => { + src = removeReturn(src, name, index) + }) + + return src +} From a3b10696227472da9d9b0df3d5fec37cb7874317 Mon Sep 17 00:00:00 2001 From: KHeo Date: Mon, 2 Dec 2019 18:19:46 +0900 Subject: [PATCH 17/18] Revert "Added test-no-return." This reverts commit d610829e957e38a6d56c19ba2219681e3f6c65ec. --- bulk-decaffeinate.config.js | 1 - .../__testfixtures__/test-no-return.input.js | 24 --------- .../__testfixtures__/test-no-return.output.js | 24 --------- scripts/decaff/__tests__/decaff.test.js | 1 - scripts/decaff/test-no-return.js | 51 ------------------- 5 files changed, 101 deletions(-) delete mode 100644 scripts/decaff/__testfixtures__/test-no-return.input.js delete mode 100644 scripts/decaff/__testfixtures__/test-no-return.output.js delete mode 100644 scripts/decaff/test-no-return.js diff --git a/bulk-decaffeinate.config.js b/bulk-decaffeinate.config.js index 8120ef9cf4b0..e7af5f254a2d 100644 --- a/bulk-decaffeinate.config.js +++ b/bulk-decaffeinate.config.js @@ -18,6 +18,5 @@ module.exports = { './scripts/decaff/empty-catch.js', './scripts/decaff/no-cond-assign.js', './scripts/decaff/arrow-comment.js', - './scripts/decaff/test-no-return.js', ], } diff --git a/scripts/decaff/__testfixtures__/test-no-return.input.js b/scripts/decaff/__testfixtures__/test-no-return.input.js deleted file mode 100644 index 07c28501686f..000000000000 --- a/scripts/decaff/__testfixtures__/test-no-return.input.js +++ /dev/null @@ -1,24 +0,0 @@ -context('a', function () { - beforeEach(() => { - doSomething() - - return settings.write('something') - }) - - afterEach(() => { - return settings.reset() - }) - - it('test 1', function () { - cy.visit('hi/world') - - return cy.click('abc') - }) - - it('test 2', () => { - return this.project.saveState() - .then((state) => { - expect(state).to.deep.eq({}) - }) - }) -}) diff --git a/scripts/decaff/__testfixtures__/test-no-return.output.js b/scripts/decaff/__testfixtures__/test-no-return.output.js deleted file mode 100644 index e157967c67b0..000000000000 --- a/scripts/decaff/__testfixtures__/test-no-return.output.js +++ /dev/null @@ -1,24 +0,0 @@ -context('a', function () { - beforeEach(() => { - doSomething() - - settings.write('something'); - }) - - afterEach(() => { - settings.reset(); - }) - - it('test 1', function () { - cy.visit('hi/world') - - cy.click('abc'); - }) - - it('test 2', () => { - this.project.saveState() - .then((state) => { - expect(state).to.deep.eq({}) - }); - }) -}) diff --git a/scripts/decaff/__tests__/decaff.test.js b/scripts/decaff/__tests__/decaff.test.js index c613a1a0582b..4921d9b52b37 100644 --- a/scripts/decaff/__tests__/decaff.test.js +++ b/scripts/decaff/__tests__/decaff.test.js @@ -8,4 +8,3 @@ defineTest(__dirname, 'empty-catch') defineTest(__dirname, 'remove-comment-sharp') defineTest(__dirname, 'arrow-comment') defineTest(__dirname, 'no-cond-assign') -defineTest(__dirname, 'test-no-return') diff --git a/scripts/decaff/test-no-return.js b/scripts/decaff/test-no-return.js deleted file mode 100644 index f3d39398ddbc..000000000000 --- a/scripts/decaff/test-no-return.js +++ /dev/null @@ -1,51 +0,0 @@ -module.exports = (fileInfo, api) => { - const funcs = [ - { name: 'beforeEach', index: 0 }, - { name: 'afterEach', index: 0 }, - { name: 'it', index: 1 }, - ] - - const j = api.jscodeshift - - const removeReturn = (src, name, index) => { - return j(src) - .find(j.ExpressionStatement, { - expression: { - callee: { - type: 'Identifier', - name, - }, - }, - }) - .replaceWith(({ node }) => { - let block = node.expression.arguments[index].body - - if (block.body && block.body.length > 0) { - block.body = block.body.map((s) => { - if (s.type === 'ReturnStatement') { - const stat = j.expressionStatement(s.argument) - - if (s.comments) { - stat.comments = s.comments - } - - return stat - } - - return s - }) - } - - return node - }) - .toSource() - } - - let src = fileInfo.source - - funcs.forEach(({ name, index }) => { - src = removeReturn(src, name, index) - }) - - return src -} From 0838b7f7f34e18d13837e40d8a1119adfdd7e6c1 Mon Sep 17 00:00:00 2001 From: Ben Kucera <14625260+Bkucera@users.noreply.github.com> Date: Fri, 6 Dec 2019 12:18:28 -0500 Subject: [PATCH 18/18] fix arrow-comment --- scripts/decaff/arrow-comment.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/decaff/arrow-comment.js b/scripts/decaff/arrow-comment.js index 044870f9b0e1..8607e1047cd7 100644 --- a/scripts/decaff/arrow-comment.js +++ b/scripts/decaff/arrow-comment.js @@ -15,9 +15,10 @@ module.exports = (fileInfo, api) => { }) .replaceWith((nodePath) => { const { node } = nodePath + const comments = node.declarations[0].init.body.comments - if (node.declarations[0].init.body.comments.length > 0) { - node.comments = [...node.comments, ...node.declarations[0].init.body.comments] + if (comments && comments.length > 0) { + node.comments = [...node.comments, ...comments] node.declarations[0].init.body.comments = null }