-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a modernize-js command to update existing JS code (#126)
This has a few pieces: * We now need the file discovery process to be flexible enough to discover either CoffeeScript or JavaScript. * In order to avoid duplicate comments, we now have a step to carefully remove any comments that look like they were previously auto-generated with bulk-decaffeinate. This is nice because it also means you can run modernize to get a fresh list of the lint failures. * The command itself mostly just needs to call functions previously extracted from the convert command, and also specify `--modernize-js` when running decaffeinate.
- Loading branch information
1 parent
41dd2de
commit 7abea82
Showing
20 changed files
with
273 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
test/tmp-projects/**/*.js | ||
test/examples/**/*.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { readFile, writeFile } from 'fs-promise'; | ||
|
||
import runWithProgressBar from '../runner/runWithProgressBar'; | ||
import { HEADER_COMMENT_LINES } from './runEslintFix'; | ||
|
||
export default async function removeAutogeneratedHeader(jsFiles) { | ||
await runWithProgressBar( | ||
'Removing any existing autogenerated headers...', jsFiles, removeHeadersFromFile); | ||
} | ||
|
||
async function removeHeadersFromFile(path) { | ||
let contents = await readFile(path); | ||
let newContents = removeHeadersFromCode(contents); | ||
if (newContents !== contents) { | ||
await writeFile(path, newContents); | ||
} | ||
} | ||
|
||
export function removeHeadersFromCode(code) { | ||
let lines = code.toString().split('\n'); | ||
|
||
let resultLines = []; | ||
for (let i = 0; i < lines.length; i++) { | ||
let line = lines[i]; | ||
// Remove lines exactly matching a line we auto-generate. | ||
if (Object.values(HEADER_COMMENT_LINES).includes(line)) { | ||
continue; | ||
} | ||
|
||
// Remove regions of lines exactly matching our eslint-disable format. | ||
if (line === '/* eslint-disable') { | ||
let j = i + 1; | ||
let foundMatch = false; | ||
while (j < lines.length) { | ||
if (lines[j] === '*/') { | ||
foundMatch = true; | ||
break; | ||
} | ||
if (!lines[j].startsWith(' ') || !lines[j].endsWith(',')) { | ||
break; | ||
} | ||
j++; | ||
} | ||
if (foundMatch) { | ||
// Skip forward to j, the "*/" line, so the next considered line will be | ||
// the one after. | ||
i = j; | ||
continue; | ||
} | ||
} | ||
|
||
// Everything else gets added to the file. | ||
resultLines.push(line); | ||
} | ||
return resultLines.join('\n'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import getFilesToProcess from './config/getFilesToProcess'; | ||
import removeAutogeneratedHeader from './modernize/removeAutogeneratedHeader'; | ||
import runEslintFix from './modernize/runEslintFix'; | ||
import runFixImports from './modernize/runFixImports'; | ||
import runJscodeshiftScripts from './modernize/runJscodeshiftScripts'; | ||
import makeCLIFn from './runner/makeCLIFn'; | ||
import runWithProgressBar from './runner/runWithProgressBar'; | ||
import { JS_FILE_RECOGNIZER } from './util/FilePaths'; | ||
import pluralize from './util/pluralize'; | ||
|
||
export default async function modernizeJS(config) { | ||
let {decaffeinateArgs = [], decaffeinatePath} = config; | ||
|
||
let jsFiles = await getFilesToProcess(config, JS_FILE_RECOGNIZER); | ||
if (jsFiles.length === 0) { | ||
console.log('There were no JavaScript files to convert.'); | ||
return; | ||
} | ||
|
||
await removeAutogeneratedHeader(jsFiles); | ||
await runWithProgressBar( | ||
'Running decaffeinate --modernize-js on all files...', | ||
jsFiles, | ||
makeCLIFn(path => `${decaffeinatePath} --modernize-js ${decaffeinateArgs.join(' ')} ${path}`) | ||
); | ||
if (config.jscodeshiftScripts) { | ||
await runJscodeshiftScripts(jsFiles, config); | ||
} | ||
if (config.fixImportsConfig) { | ||
await runFixImports(jsFiles, config); | ||
} | ||
await runEslintFix(jsFiles, config, {isUpdate: true}); | ||
|
||
console.log(`Successfully modernized ${pluralize(jsFiles.length, 'file')}.`); | ||
console.log('You should now fix lint issues in any affected files.'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
var nameBefore = 1; | ||
var notChanged = 2; |
5 changes: 5 additions & 0 deletions
5
test/examples/modernize-jscodeshift-test/bulk-decaffeinate.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
jscodeshiftScripts: [ | ||
'./change-name.js', | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export default function transformer(file, api) { | ||
const j = api.jscodeshift; | ||
return j(file.source) | ||
.find(j.Identifier) | ||
.replaceWith( | ||
p => { | ||
if (p.node.name === 'nameBefore') { | ||
return j.identifier('nameAfter'); | ||
} else { | ||
return p.node; | ||
} | ||
} | ||
) | ||
.toSource(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
var path = require('path'); | ||
path.resolve(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
var a = 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
--require babel-polyfill | ||
--compilers js:babel-register | ||
--timeout 60000 |
Oops, something went wrong.