Skip to content

Commit b6a7339

Browse files
Merge branch 'main'
2 parents df56739 + 6c72e6f commit b6a7339

13 files changed

+136
-82
lines changed

docs/Architecture.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Within `src/conversion/convertLintConfig.ts`, the following steps occur:
1717
- 3a. If no output rules conflict with `eslint-config-prettier`, it's added in
1818
- 3b. Any ESLint rules that are configured the same as an extended preset are trimmed
1919
4. The summarized configuration is written to the output config file
20-
5. Files to transform comments in have source text rewritten using the same rule conversion logic
20+
5. Files to transform comments in have source text rewritten using the cached rule conversion results
2121
6. A summary of the results is printed to the user's console
2222

2323
### Conversion Results
@@ -51,6 +51,17 @@ These are located in `src/rules/mergers/`, and keyed under their names by the ma
5151

5252
For example, `@typescript-eslint/ban-types` spreads both arguments' `types` members into one large `types` object.
5353

54+
## Comment Conversion
55+
56+
Comments are converted after rule conversion by `src/comments/convertComments.ts`.
57+
Source files are parsed into TypeScript files by `src/comments/parseFileComments.ts`, which then extracts their comment nodes.
58+
Those comments are parsed for TSLint rule disable or enable comments.
59+
60+
Comments that match will be rewritten in their their file to their new ESLint rule equivalent in `src/comments/replaceFileComments.ts`, as determined by:
61+
62+
1. First, if the `ruleEquivalents` cache received from configuration convertion has the TSLint rule's ESLint equivalents listed, those are used.
63+
2. Failing that, a comment-specific `ruleCommentsCache` is populated with rules converted ad-hoc with no arguments.
64+
5465
## Editor Configuration Conversion
5566

5667
Editor lint configurations are converted by `src/editorSettings/convertEditorSettings.ts`.

package-lock.json

Lines changed: 16 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"minimatch": "3.0.4",
1818
"strip-json-comments": "3.1.1",
1919
"tslint": "6.1.3",
20-
"typescript": "4.0.2"
20+
"typescript": "4.0.3"
2121
},
2222
"devDependencies": {
2323
"@babel/core": "7.11.6",
@@ -27,7 +27,7 @@
2727
"@babel/preset-typescript": "7.10.4",
2828
"@types/eslint-config-prettier": "6.11.0",
2929
"@types/glob": "7.1.3",
30-
"@types/jest": "26.0.13",
30+
"@types/jest": "26.0.14",
3131
"@types/minimatch": "3.0.3",
3232
"@types/node": "12.12.21",
3333
"@typescript-eslint/eslint-plugin": "4.1.1",
@@ -36,8 +36,8 @@
3636
"eslint": "7.9.0",
3737
"husky": "4.3.0",
3838
"jest": "26.4.2",
39-
"lint-staged": "10.3.0",
40-
"prettier": "2.1.1",
39+
"lint-staged": "10.4.0",
40+
"prettier": "2.1.2",
4141
"strip-ansi": "6.0.0"
4242
},
4343
"homepage": "https://github.com/typescript-eslint/tslint-to-eslint-config#readme",
@@ -71,5 +71,5 @@
7171
"test:ci": "jest --coverage --maxWorkers=2",
7272
"tsc": "tsc"
7373
},
74-
"version": "1.3.0"
74+
"version": "1.5.0"
7575
}

src/converters/comments/convertComments.test.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe("convertComments", () => {
2424
dependencies,
2525
{ comments: undefined },
2626
createStubOriginalConfigurationsData(),
27+
new Map(),
2728
);
2829

2930
// Assert
@@ -45,6 +46,7 @@ describe("convertComments", () => {
4546
dependencies,
4647
{ comments: true },
4748
createStubOriginalConfigurationsData(),
49+
new Map(),
4850
);
4951

5052
// Assert
@@ -64,8 +66,13 @@ describe("convertComments", () => {
6466
// Act
6567
const result = await convertComments(
6668
dependencies,
67-
{ comments: ["*.ts"] },
68-
createStubOriginalConfigurationsData(),
69+
{ comments: true },
70+
createStubOriginalConfigurationsData({
71+
typescript: {
72+
include: ["src/*.ts"],
73+
},
74+
}),
75+
new Map(),
6976
);
7077

7178
// Assert
@@ -89,6 +96,7 @@ describe("convertComments", () => {
8996
dependencies,
9097
{ comments: [] },
9198
createStubOriginalConfigurationsData(),
99+
new Map(),
92100
);
93101

94102
// Assert
@@ -111,8 +119,9 @@ describe("convertComments", () => {
111119
// Act
112120
const result = await convertComments(
113121
dependencies,
114-
{ comments: [] },
122+
{ comments: ["*.ts"] },
115123
createStubOriginalConfigurationsData(),
124+
new Map(),
116125
);
117126

118127
// Assert
@@ -134,6 +143,7 @@ describe("convertComments", () => {
134143
dependencies,
135144
{ comments: ["*.ts"] },
136145
createStubOriginalConfigurationsData(),
146+
new Map(),
137147
);
138148

139149
// Assert
@@ -152,6 +162,7 @@ describe("convertComments", () => {
152162
dependencies,
153163
{ comments: ["*.ts"] },
154164
createStubOriginalConfigurationsData(),
165+
new Map(),
155166
);
156167

157168
// Assert

src/converters/comments/convertComments.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const convertComments = async (
2020
dependencies: ConvertCommentsDependencies,
2121
{ comments }: Pick<TSLintToESLintSettings, "comments">,
2222
{ typescript }: Pick<AllOriginalConfigurations, "typescript">,
23+
ruleEquivalents: Map<string, string[]>,
2324
): Promise<ResultWithDataStatus<string[] | undefined>> => {
2425
if (comments === undefined) {
2526
dependencies.reportCommentResults();
@@ -76,11 +77,11 @@ export const convertComments = async (
7677
};
7778
}
7879

79-
const ruleConversionCache = new Map<string, string | undefined>();
80+
const ruleCommentsCache = new Map<string, string[]>();
8081
const fileFailures = (
8182
await Promise.all(
8283
uniqueGlobbedFilePaths.map(async (filePath) =>
83-
dependencies.convertFileComments(filePath, ruleConversionCache),
84+
dependencies.convertFileComments(filePath, ruleCommentsCache, ruleEquivalents),
8485
),
8586
)
8687
).filter(isError);

src/converters/comments/convertFileComments.test.ts

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe("convertFileComments", () => {
2626
const dependencies = createStubDependencies(readFileError);
2727

2828
// Act
29-
const result = await convertFileComments(dependencies, stubFileName, new Map());
29+
const result = await convertFileComments(dependencies, stubFileName, new Map(), new Map());
3030

3131
// Assert
3232
expect(result).toBe(readFileError);
@@ -39,7 +39,7 @@ describe("convertFileComments", () => {
3939
`);
4040

4141
// Act
42-
await convertFileComments(dependencies, stubFileName, new Map());
42+
await convertFileComments(dependencies, stubFileName, new Map(), new Map());
4343

4444
// Assert
4545
expect(dependencies.fileSystem.writeFile).not.toHaveBeenCalled();
@@ -70,7 +70,7 @@ export const g = true;
7070
`);
7171

7272
// Act
73-
await convertFileComments(dependencies, stubFileName, new Map());
73+
await convertFileComments(dependencies, stubFileName, new Map(), new Map());
7474

7575
// Assert
7676
expect(dependencies.fileSystem.writeFile).toHaveBeenCalledWith(
@@ -110,7 +110,7 @@ export const b = true;
110110
`);
111111

112112
// Act
113-
await convertFileComments(dependencies, stubFileName, new Map());
113+
await convertFileComments(dependencies, stubFileName, new Map(), new Map());
114114

115115
// Assert
116116
expect(dependencies.fileSystem.writeFile).toHaveBeenCalledWith(
@@ -125,15 +125,45 @@ export const b = true;
125125
);
126126
});
127127

128-
it("re-uses a rule conversion from cache when it was already converted", async () => {
128+
it("re-uses a rule conversion from conversion cache when it was already converted", async () => {
129129
// Arrange
130130
const dependencies = createStubDependencies(`
131131
/* tslint:disable:ts-a */
132132
export const a = true;
133133
`);
134134

135135
// Act
136-
await convertFileComments(dependencies, stubFileName, new Map([["ts-a", "es-cached"]]));
136+
await convertFileComments(
137+
dependencies,
138+
stubFileName,
139+
new Map(),
140+
new Map([["ts-a", ["es-cached"]]]),
141+
);
142+
143+
// Assert
144+
expect(dependencies.fileSystem.writeFile).toHaveBeenCalledWith(
145+
stubFileName,
146+
`
147+
/* eslint-disable es-cached */
148+
export const a = true;
149+
`,
150+
);
151+
});
152+
153+
it("re-uses a rule conversion from comments cache when it was already converted", async () => {
154+
// Arrange
155+
const dependencies = createStubDependencies(`
156+
/* tslint:disable:ts-a */
157+
export const a = true;
158+
`);
159+
160+
// Act
161+
await convertFileComments(
162+
dependencies,
163+
stubFileName,
164+
new Map([["ts-a", ["es-cached"]]]),
165+
new Map(),
166+
);
137167

138168
// Assert
139169
expect(dependencies.fileSystem.writeFile).toHaveBeenCalledWith(
@@ -153,7 +183,7 @@ export const a = true;
153183
`);
154184

155185
// Act
156-
await convertFileComments(dependencies, stubFileName, new Map());
186+
await convertFileComments(dependencies, stubFileName, new Map(), new Map());
157187

158188
// Assert
159189
expect(dependencies.fileSystem.writeFile).toHaveBeenCalledWith(
@@ -173,7 +203,7 @@ export const a = true;
173203
`);
174204

175205
// Act
176-
await convertFileComments(dependencies, stubFileName, new Map());
206+
await convertFileComments(dependencies, stubFileName, new Map(), new Map());
177207

178208
// Assert
179209
expect(dependencies.fileSystem.writeFile).toHaveBeenCalledWith(

src/converters/comments/convertFileComments.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ export type ConvertFileCommentsDependencies = {
1111
export const convertFileComments = async (
1212
dependencies: ConvertFileCommentsDependencies,
1313
filePath: string,
14-
ruleConversionCache: Map<string, string | undefined>,
14+
ruleCommentsCache: Map<string, string[]>,
15+
ruleEquivalents: Map<string, string[]>,
1516
) => {
1617
const fileContent = await dependencies.fileSystem.readFile(filePath);
1718
if (fileContent instanceof Error) {
@@ -23,7 +24,8 @@ export const convertFileComments = async (
2324
fileContent,
2425
comments,
2526
dependencies.converters,
26-
ruleConversionCache,
27+
ruleCommentsCache,
28+
ruleEquivalents,
2729
);
2830

2931
return fileContent === newFileContent

0 commit comments

Comments
 (0)