Last active
October 21, 2022 00:11
-
-
Save jonesnxt/0bc9e4b2ef18ddbe3f9282db8ceaa66d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { diffLines } from 'diff'; | |
import { writeFileSync } from 'fs'; | |
import { readFiles } from 'node-dir'; | |
import { format as prettierFormat } from 'prettier'; | |
import prettierTypescript from 'prettier/parser-typescript.js'; | |
import S from 'subsecond'; | |
const TARGET_DIR = '../src'; | |
const contents = []; | |
readFiles( | |
TARGET_DIR, | |
{ | |
match: /\.[jt]sx?$/, | |
exclude: /^\./, | |
}, | |
function (err, content, next) { | |
if (err) throw err; | |
contents.push(content); | |
next(); | |
}, | |
function (err, files) { | |
if (err) throw err; | |
runSubsecond( | |
Object.fromEntries(contents.map((content, i) => [files[i], content])) | |
); | |
} | |
); | |
function runSubsecond(files) { | |
const filesCopy = JSON.parse(JSON.stringify(files)); | |
S.load(files); | |
S('MemberExpression').each((expression) => { | |
const [name, index] = expression.children().map((child) => child.text()); | |
const nameDotLength = `${name}.length`; | |
if (!index.startsWith(nameDotLength)) return; | |
expression.text(`${name}.at(${index.slice(nameDotLength.length)})`); | |
}); | |
Object.entries(S.print()).forEach(([name, content]) => { | |
// (optional), if you want nice formatting | |
const formatted = prettierFormat(content, { | |
parser: 'typescript', | |
plugins: [prettierTypescript], | |
printWidth: 80, | |
singleQuote: true, | |
trailingComma: 'es5', | |
tabWidth: 2, | |
useTabs: false, | |
}); | |
diffLines(filesCopy[name], formatted) | |
.filter((line) => line.added || line.removed) | |
.forEach((line) => | |
console.log( | |
line.added ? '+ ' : '- ', | |
line.value.slice(0, line.value.length - 1) | |
) | |
); | |
// Uncomment the following line when you are ready to write the changes | |
// writeFileSync(name, formatted, 'utf8'); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment