Skip to content

Commit 8cb8c06

Browse files
author
KingDarBoja
committed
Write file after switching rule comments
This is still a work in progress, need to check the start and end position of rule comments to avoid overwriting the wrong lines.
1 parent 5dbd345 commit 8cb8c06

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

src/adapters/fileSystem.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ export type FileSystem = {
66
fileExists: (filePath: string) => Promise<boolean>;
77
readFile: (filePath: string) => Promise<Error | string>;
88
writeFile: (filePath: string, contents: string) => Promise<Error | undefined>;
9+
writeFileSync: (filePath: string, contents: string) => undefined;
910
readDir: (dirPath: string, options: ReadDirOptions) => Promise<Error | fs.Dirent[]>;
1011
};

src/adapters/fsFileSystem.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { FileSystem, ReadDirOptions } from "./fileSystem";
66
const readFile = promisify(fs.readFile);
77
const readDir = promisify(fs.readdir);
88
const writeFile = promisify(fs.writeFile);
9+
const writeFileSync = fs.writeFileSync;
910

1011
export const fsFileSystem: FileSystem = {
1112
fileExists: async (filePath: string) => {
@@ -29,6 +30,13 @@ export const fsFileSystem: FileSystem = {
2930
return error;
3031
}
3132
},
33+
writeFileSync: (filePath: string, contents: string) => {
34+
try {
35+
return writeFileSync(filePath, contents);
36+
} catch (error) {
37+
return error;
38+
}
39+
},
3240
readDir: async (dirPath: string, options: ReadDirOptions) => {
3341
try {
3442
return readDir(dirPath, options);

src/rules/convertComments.ts

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { FileSystem } from "../adapters/fileSystem";
22
import { isError } from "../utils";
33
import * as utils from "tsutils";
4-
import * as ts from "typescript";
4+
import ts from "typescript";
55
import { converters } from "./converters";
66
import { formatRawTslintRule } from "./formatRawTslintRule";
77
import { ConversionError } from "../errors/conversionError";
@@ -10,7 +10,7 @@ import { ConversionError } from "../errors/conversionError";
1010
// - https://github.com/Microsoft/TypeScript/issues/21049
1111
// - https://github.com/palantir/tslint/blob/master/src/enableDisableRules.ts
1212
export type ConvertCommentsResultsDependencies = {
13-
fileSystem: Pick<FileSystem, "readDir" | "readFile" | "writeFile">;
13+
fileSystem: Pick<FileSystem, "readDir" | "readFile" | "writeFile" | "writeFileSync">;
1414
};
1515

1616
const tslintRegex: RegExp = new RegExp(/\s*tslint:(enable|disable)(?:-(line|next-line))?(:|\s|$)/g);
@@ -71,8 +71,14 @@ function splitOnSpaces(str: string): string[] {
7171
return str.split(/\s+/).filter(s => s !== "");
7272
}
7373

74+
interface IReplacement {
75+
start: number;
76+
end: number;
77+
replacementText: string;
78+
}
79+
7480
const replaceComments = async (
75-
dependencies: ConvertCommentsResultsDependencies,
81+
_dependencies: ConvertCommentsResultsDependencies,
7682
fileName: string,
7783
fileContent: string,
7884
) => {
@@ -86,14 +92,15 @@ const replaceComments = async (
8692
// If not, it is `MultiLineCommentTrivia` as this method does check if it is a comment...
8793
// or that's what I think it does.
8894
utils.forEachComment(sourceFile, (fullText, comment) => {
95+
const replacements: IReplacement[] = [];
8996
const commentText =
9097
comment.kind === ts.SyntaxKind.SingleLineCommentTrivia
91-
? fullText.substring(comment.pos + 2, comment.end)
92-
: fullText.substring(comment.pos + 2, comment.end - 2);
98+
? fullText.substring(comment.pos + 3, comment.end)
99+
: fullText.substring(comment.pos + 3, comment.end - 2);
93100

94101
const parsed = parseComment(commentText);
95102
if (parsed !== undefined) {
96-
const { rulesList, isEnabled, modifier } = parsed;
103+
const { rulesList, modifier } = parsed;
97104
const switchRange = getSwitchRange(modifier, comment, sourceFile);
98105
if (switchRange !== undefined) {
99106
console.log("---------- COMMENT TEXT -----------");
@@ -107,15 +114,37 @@ const replaceComments = async (
107114
? Array.from(converters.keys())
108115
: rulesList.filter(ruleKey => converters.has(ruleKey));
109116
for (const ruleToSwitch of rulesToSwitch) {
110-
switchRulename(ruleToSwitch, isEnabled, switchRange.pos, switchRange.end);
117+
const transformedRules = switchRule(ruleToSwitch);
118+
if (transformedRules) {
119+
replacements.push({
120+
start: switchRange.pos,
121+
end: switchRange.end,
122+
replacementText: transformedRules.join(" "),
123+
});
124+
}
111125
}
112126
}
113127
}
128+
// Reverse the replacement list
129+
replacements.reverse();
130+
131+
const newText = getNewText(fileContent, replacements);
132+
console.log("************** NEW FILE BEING WRITTEN ! **************");
133+
console.log(newText);
134+
// dependencies.fileSystem.writeFileSync(fileName, newText);
114135
});
115-
return await dependencies.fileSystem.writeFile(fileName, fileContent);
136+
return true;
116137
};
117138

118-
function switchRulename(ruleName: string, _isEnable: boolean, _start: number, _end: number): void {
139+
function getNewText(sourceText: string, replacementsInReverse: IReplacement[]) {
140+
for (const { start, end, replacementText } of replacementsInReverse) {
141+
sourceText = sourceText.slice(0, start) + replacementText + sourceText.slice(end);
142+
}
143+
144+
return sourceText;
145+
}
146+
147+
function switchRule(ruleName: string): string[] | null {
119148
const tslintRuleConverter = converters.get(ruleName);
120149
if (tslintRuleConverter) {
121150
const tslintRule = formatRawTslintRule(ruleName, { ruleName });
@@ -124,8 +153,10 @@ function switchRulename(ruleName: string, _isEnable: boolean, _start: number, _e
124153
const eslintRules = conversion.rules.map(r => r.ruleName);
125154
console.log(`Rulename: ${ruleName}`);
126155
console.log(eslintRules);
156+
return eslintRules;
127157
}
128158
}
159+
return null;
129160
}
130161

131162
function getSwitchRange(

0 commit comments

Comments
 (0)