Skip to content

Commit

Permalink
Require tags in comments
Browse files Browse the repository at this point in the history
  • Loading branch information
xinhaoz committed Mar 26, 2021
1 parent 396946d commit dc9fbe8
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 31 deletions.
75 changes: 49 additions & 26 deletions scrubber/scrubber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const TAG_END_CHAR = "}";

const FILE_TYPE_COMMENT: { [key: string]: string } = {
js: "//",
json: "//",
ts: "//",
py: "#",
};
Expand Down Expand Up @@ -45,12 +46,18 @@ async function scrubFile(
if (err) {
reject(err);
}
const lines: string[] = text.split("\n");

const ext = filePath.split(".").pop();
const commentType = ext && FILE_TYPE_COMMENT[ext];
const scrubbedLines: string[] = [];
let skip = false;

const lines: string[] = text.split("\n");

for (let i = 0; i < lines.length; ++i) {
const line = lines[i];
let tryProcessTag = true;

if (line.length === 0) {
scrubbedLines.push(line);
continue;
Expand All @@ -59,37 +66,53 @@ async function scrubFile(
// Split on whitespace
const tokens = line.trim().split(/[ ]+/);

if (tokens[0] in tags && tokens.length !== 2) {
console.warn(
`WARNING line ${
i + 1
}: possible malformed tag; tags must be on their own line preceded by '}' or followed by '{'`,
);
continue;
}

if (tokens[0] in tags || tokens[1] in tags) {
const tag = tokens[0] in tags ? tokens[0] : tokens[1];
const brace = tag === tokens[0] ? tokens[1] : tokens[0];

if (brace === tokens[1] && brace !== TAG_START_CHAR) {
throw new Error("Malformed tag line: expected '{' after tag name'");
if (commentType) {
if (tokens[0] !== commentType) {
tryProcessTag = false;
}
tokens.shift();
}

if (brace === tokens[0] && brace !== TAG_END_CHAR) {
throw new Error(
"Malformed tag line: expected '}' before tag name'",
if (tryProcessTag) {
if (tokens[0] in tags && tokens.length !== 2) {
console.warn(
`WARNING line ${
i + 1
}: possible malformed tag; tags must be on their own line preceded by '}' or followed by '{'`,
);
scrubbedLines.push(line);
continue;
}

// NOTE: nested tagging is not currently expected and will lead to unexpected behaviour.

if (tags[tag] === "remove") {
skip = brace === TAG_START_CHAR;
if (tokens[0] in tags || tokens[1] in tags) {
const tag = tokens[0] in tags ? tokens[0] : tokens[1];
const brace = tag === tokens[0] ? tokens[1] : tokens[0];

if (brace === tokens[1] && brace !== TAG_START_CHAR) {
throw new Error(
`Malformed tag ${filePath}:line ${
i + 1
}: expected '{' after tag name`,
);
}

if (brace === tokens[0] && brace !== TAG_END_CHAR) {
throw new Error(
`Malformed tag ${filePath}:line ${
i + 1
}: expected '}' before tag name`,
);
}

// NOTE: nested tagging is not currently expected and will lead to unexpected behaviour.

if (tags[tag] === "remove") {
skip = brace === TAG_START_CHAR;
}

// We always scrub tags from the final file.
continue;
}

// We always scrub tags from the final file.
continue;
}

if (skip) {
Expand Down
5 changes: 5 additions & 0 deletions test_dir/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @remove {
console.log("this should be gone");
// } @remove

console.log("hello world");
5 changes: 5 additions & 0 deletions test_dir/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @remove {
print('this should be gone')
# } @remove

print('hello world')
5 changes: 0 additions & 5 deletions test_dir/test1

This file was deleted.

0 comments on commit dc9fbe8

Please sign in to comment.