Skip to content

Commit bacf9e8

Browse files
Working replacements for single line changes
1 parent f1bd492 commit bacf9e8

File tree

1 file changed

+38
-27
lines changed

1 file changed

+38
-27
lines changed

src/rules/convertComments.ts

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ const tslintRegex = new RegExp(/\s*tslint:(enable|disable)(?:-(line|next-line))?
1717

1818
export const convertComments = async (dependencies: ConvertCommentsResultsDependencies) => {
1919
// TODO: Remove console logs
20-
console.log("Started");
2120
const filenames = await dependencies.fileSystem.readDir("./", { withFileTypes: true });
2221
if (!isError(filenames)) {
2322
const filteredFilenames: string[] = filenames
24-
.filter(fileEnt => fileEnt.isFile())
25-
.map(fileEnt => fileEnt.name);
23+
.filter((fileEnt) => fileEnt.isFile())
24+
.map((fileEnt) => fileEnt.name);
2625
// TODO: Remove console logs
27-
console.log("Filenames filtered");
28-
console.log(filteredFilenames);
2926
for (const filename of filteredFilenames) {
27+
// todo: cli should pass glob/wildcard
28+
if (!filename.includes("index")) continue;
29+
3030
const fileContent: string | Error = await dependencies.fileSystem.readFile(filename);
3131
if (!isError(fileContent)) {
3232
const writeFileRes = await replaceComments(dependencies, filename, fileContent);
@@ -40,7 +40,9 @@ export const convertComments = async (dependencies: ConvertCommentsResultsDepend
4040
return Error("Failed to convert file comments");
4141
};
4242

43-
type Modifier = "line" | "next-line" | undefined;
43+
type CommentKind = ts.SyntaxKind.MultiLineCommentTrivia | ts.SyntaxKind.SingleLineCommentTrivia;
44+
45+
type Modifier = "line" | "next-line" | undefined; // todo: split "line" into "block-start" and "block-end"?
4446
function parseComment(
4547
commentText: string,
4648
): { rulesList: string[] | "all"; isEnabled: boolean; modifier: Modifier } | undefined {
@@ -67,7 +69,7 @@ function parseComment(
6769
}
6870

6971
function splitOnSpaces(str: string): string[] {
70-
return str.split(/\s+/).filter(s => s !== "");
72+
return str.split(/\s+/).filter((s) => s !== "");
7173
}
7274

7375
type IReplacement = {
@@ -92,34 +94,34 @@ const replaceComments = async (
9294
// or that's what I think it does.
9395
utils.forEachComment(sourceFile, (fullText, comment) => {
9496
const replacements: IReplacement[] = [];
95-
const commentText =
96-
comment.kind === ts.SyntaxKind.SingleLineCommentTrivia
97-
? fullText.substring(comment.pos + 3, comment.end)
98-
: fullText.substring(comment.pos + 3, comment.end - 2);
97+
const commentText = (comment.kind === ts.SyntaxKind.SingleLineCommentTrivia
98+
? fullText.substring(comment.pos + 2, comment.end)
99+
: fullText.substring(comment.pos + 2, comment.end - 2)
100+
).trim();
99101

100102
const parsed = parseComment(commentText);
101103
if (parsed !== undefined) {
102104
const { rulesList, modifier } = parsed;
103105
const switchRange = getSwitchRange(modifier, comment, sourceFile);
106+
console.log({ modifier, comment });
104107
if (switchRange !== undefined) {
105108
// Extra log to check what is going on
106-
console.log("----------- COMMENT TEXT -----------");
107-
console.log(commentText);
108-
console.log("----------- PARSED DATA -----------");
109-
console.log(parsed);
110-
console.log("----------- SWITCH RANGE -----------");
111-
console.log(switchRange);
112109
const rulesToSwitch =
113110
rulesList === "all"
114111
? Array.from(rulesConverters.keys())
115-
: rulesList.filter(ruleKey => rulesConverters.has(ruleKey));
112+
: rulesList.filter((ruleKey) => rulesConverters.has(ruleKey));
113+
console.log({ rulesToSwitch });
116114
for (const ruleToSwitch of rulesToSwitch) {
117115
const transformedRules = switchRule(ruleToSwitch);
118116
if (transformedRules) {
119117
replacements.push({
120-
start: switchRange.pos,
121-
end: switchRange.end,
122-
replacementText: transformedRules.join(" "),
118+
start: comment.pos,
119+
end: comment.end,
120+
replacementText: createReplacementText(
121+
comment.kind,
122+
modifier,
123+
transformedRules,
124+
),
123125
});
124126
}
125127
}
@@ -130,17 +132,28 @@ const replaceComments = async (
130132

131133
const newText = getNewText(fileContent, replacements);
132134
// Check the output before writing to file.
133-
console.log("");
134-
console.log("************** NEW FILE BEING WRITTEN ! **************");
135-
console.log(newText);
136135
// Write the file with the changes.
137136
// At the moment,
138137
_dependencies.fileSystem.writeFileSync(fileName, newText);
139138
});
140139
return true;
141140
};
142141

142+
function createReplacementText(
143+
commentKind: CommentKind,
144+
_modifier: Modifier,
145+
transformedRules: string[],
146+
) {
147+
const directive = "eslint-disable-next-line"; // todo: map from modifier
148+
const [left, right] =
149+
commentKind === ts.SyntaxKind.SingleLineCommentTrivia ? ["// ", ""] : ["/* ", " */"];
150+
151+
// todo: no transformed rules?
152+
return [left, directive, " ", transformedRules.join(" "), right].join("");
153+
}
154+
143155
function getNewText(sourceText: string, replacementsInReverse: IReplacement[]) {
156+
console.log({ replacementsInReverse });
144157
for (const { start, end, replacementText } of replacementsInReverse) {
145158
sourceText = sourceText.slice(0, start) + replacementText + sourceText.slice(end);
146159
}
@@ -154,9 +167,7 @@ function switchRule(ruleName: string): string[] | null {
154167
const tslintRule = formatRawTslintRule(ruleName, { ruleName });
155168
const conversion = tslintRuleConverter(tslintRule);
156169
if (!(conversion instanceof ConversionError)) {
157-
const eslintRules = conversion.rules.map(r => r.ruleName);
158-
console.log(`Rulename: ${ruleName}`);
159-
console.log(eslintRules);
170+
const eslintRules = conversion.rules.map((r) => r.ruleName);
160171
return eslintRules;
161172
}
162173
}

0 commit comments

Comments
 (0)