Skip to content

Commit

Permalink
strip comments improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
rgoldfinger-quizlet committed May 24, 2019
1 parent c861406 commit 3e48f1c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
16 changes: 12 additions & 4 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,18 @@ const filePaths: FilePaths = {
program
.command("strip-comments")
.option("-c, --commit")
.action((cmd: { commit: boolean | undefined }) => {
console.log("Stripping comments from files...");
stripComments(filePaths, !!cmd.commit);
});
.option(
"--comments <list>",
"A comma-seperated list of comments to strip. Must start with `//`",
(f: string) => f.split(",")
)
.action(
(cmd: { commit: boolean | undefined; comments: string[] | undefined }) => {
console.log("Stripping comments from files...");
if (cmd.comments) console.log("Removing comments: ", cmd.comments);
stripComments(filePaths, cmd.comments, !!cmd.commit);
}
);

program
.command("convert-codebase")
Expand Down
2 changes: 1 addition & 1 deletion src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default async function convert(files: string[], rootDir: string) {
let res;
try {
res = await babel.transformFileAsync(path, babelOptions(rootDir));
res!.code = stripComments(res!.code!, ["// @flow", "// @noflow"]);
res!.code = stripComments(res!.code!, ["// @flow", "// @noflow"])[0];
} catch (err) {
console.log(err);
errorFiles.push(path);
Expand Down
9 changes: 7 additions & 2 deletions src/stripComments.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export function stripComments(code: string, comments: string[]) {
export function stripComments(
code: string,
comments: string[]
): [string, number] {
const codeSplitByLine = code.split("\n");
let count = 0;

const res = codeSplitByLine.reduce(
(acc, line: string) => {
Expand All @@ -8,6 +12,7 @@ export function stripComments(code: string, comments: string[]) {
const matchedIndex = line.indexOf(matchedComment);
if (matchedIndex > 0) {
acc.push(line.slice(0, matchedIndex));
count = count + 1;
}
} else {
acc.push(line);
Expand All @@ -17,5 +22,5 @@ export function stripComments(code: string, comments: string[]) {
[] as string[]
);

return res.join("\n");
return [res.join("\n"), count];
}
14 changes: 9 additions & 5 deletions src/stripCommentsRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import commit from "./commitAll";
import prettierFormat from "./prettierFormat";
import { FilePaths } from "./cli";

const argv = require("minimist")(global.process.argv.slice(2));

const successFiles: string[] = [];
const errorFiles: string[] = [];

Expand All @@ -19,15 +17,20 @@ const flowComments = [

export default async function run(
paths: FilePaths,
comments: string[] | undefined,
shouldComit: boolean
): Promise<void> {
const files = await collectFiles(paths);

let count = 0;
files.forEach(filePath => {
try {
const code = readFileSync(filePath, "utf8");

const fileData = stripComments(code, argv.comments || flowComments);
const [fileData, countRemoved] = stripComments(
code,
comments || flowComments
);
count = count + countRemoved;
const formattedFileData = prettierFormat(fileData, paths.rootDir);
writeFileSync(filePath, formattedFileData);
successFiles.push(filePath);
Expand All @@ -42,8 +45,9 @@ export default async function run(
}

console.log(
`${successFiles.length} files with comments stripped successfully.`
`${count} comments in ${successFiles.length} files stripped successfully.`
);

if (errorFiles.length) {
console.log(`Error stripping comments in ${errorFiles.length} files:`);
console.log(errorFiles);
Expand Down
1 change: 1 addition & 0 deletions test/insertIgnore.fixture.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function MultiLine() {
style={{}}
className="long class name so the expression is on the next line"
>
<div bar={foo} className="test" />
{Quizlet.doesNotExist && <div bar={foo} className="test" />}
</div>
);
Expand Down

0 comments on commit 3e48f1c

Please sign in to comment.