Skip to content

Commit 01df1eb

Browse files
Merge branch 'main'
2 parents 22170eb + 0d2a9ea commit 01df1eb

10 files changed

+269
-149
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ We **strongly** advise reading [docs/FAQs.md](./docs/FAQs.md) before planning yo
4242

4343
Each of these flags is optional:
4444

45-
- **[`comments`](#comments)**: File glob path(s) to convert TSLint rule flags to ESLint within.
45+
- **[`comments`](#comments)**: TypeScript configuration or file glob path(s) to convert TSLint rule flags to ESLint within.
4646
- **[`config`](#config)**: Path to print the generated ESLint configuration file to.
4747
- **[`editor`](#editor)**: Path to an editor configuration file to convert linter settings within.
4848
- **[`eslint`](#eslint)**: Path to an ESLint configuration file to read settings from.
@@ -64,7 +64,13 @@ Comments such as `// tslint:disable: tslint-rule-name` will be converted to equi
6464

6565
If passed without arguments, respects the `excludes`, `files`, and `includes` in your TypeScript configuration.
6666

67-
Alternately, you can specify which files to convert comments in as globs:
67+
If passed a single file path ending with `.json`, that is treated as a TypeScript configuration file describing with files to convert.
68+
69+
```shell
70+
npx tslint-to-eslint-config --comments tsconfig.json
71+
```
72+
73+
If passed any other arguments, those are treated as glob paths for file paths to convert:
6874

6975
```shell
7076
npx tslint-to-eslint-config --comments 'src/**/*.ts'

package-lock.json

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

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
"@types/jest": "26.0.13",
3131
"@types/minimatch": "3.0.3",
3232
"@types/node": "12.12.21",
33-
"@typescript-eslint/eslint-plugin": "4.1.0",
34-
"@typescript-eslint/parser": "4.1.0",
33+
"@typescript-eslint/eslint-plugin": "4.1.1",
34+
"@typescript-eslint/parser": "4.1.1",
3535
"babel-jest": "26.3.0",
36-
"eslint": "7.8.1",
36+
"eslint": "7.9.0",
3737
"husky": "4.3.0",
3838
"jest": "26.4.2",
3939
"lint-staged": "10.3.0",

src/cli/main.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ import { ruleMergers } from "../converters/lintConfigs/rules/ruleMergers";
8080
import { writeEditorConfigConversionResults } from "../converters/lintConfigs/writeEditorConfigConversionResults";
8181
import { addPrettierExtensions } from "../converters/lintConfigs/summarization/prettier/addPrettierExtensions";
8282
import { removeExtendsDuplicatedRules } from "../converters/lintConfigs/pruning/removeExtendsDuplicatedRules";
83+
import {
84+
collectCommentFileNames,
85+
CollectCommentFileNamesDependencies,
86+
} from "../comments/collectCommentFileNames";
8387

8488
const convertFileCommentsDependencies: ConvertFileCommentsDependencies = {
8589
converters: ruleConverters,
@@ -90,12 +94,6 @@ const reportCommentResultsDependencies: ReportCommentResultsDependencies = {
9094
logger: processLogger,
9195
};
9296

93-
const convertCommentsDependencies: ConvertCommentsDependencies = {
94-
convertFileComments: bind(convertFileComments, convertFileCommentsDependencies),
95-
globAsync,
96-
reportCommentResults: bind(reportCommentResults, reportCommentResultsDependencies),
97-
};
98-
9997
const convertRulesDependencies: ConvertRulesDependencies = {
10098
ruleConverters,
10199
ruleMergers,
@@ -131,6 +129,17 @@ const findOriginalConfigurationsDependencies: FindOriginalConfigurationsDependen
131129
mergeLintConfigurations,
132130
};
133131

132+
const collectCommentFileNamesDependencies: CollectCommentFileNamesDependencies = {
133+
findTypeScriptConfiguration: bind(findTypeScriptConfiguration, findConfigurationDependencies),
134+
};
135+
136+
const convertCommentsDependencies: ConvertCommentsDependencies = {
137+
collectCommentFileNames: bind(collectCommentFileNames, collectCommentFileNamesDependencies),
138+
convertFileComments: bind(convertFileComments, convertFileCommentsDependencies),
139+
globAsync,
140+
reportCommentResults: bind(reportCommentResults, reportCommentResultsDependencies),
141+
};
142+
134143
const choosePackageManagerDependencies: ChoosePackageManagerDependencies = {
135144
fileSystem: fsFileSystem,
136145
};
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { collectCommentFileNames } from "./collectCommentFileNames";
2+
3+
describe("collectCommentFileNames", () => {
4+
it("returns an error result when filePathGlobs is true and typescriptConfiguration is undefined", async () => {
5+
const findTypeScriptConfiguration = jest.fn();
6+
7+
const result = await collectCommentFileNames({ findTypeScriptConfiguration }, true);
8+
9+
expect(result).toEqual(expect.any(Error));
10+
});
11+
12+
it("returns the typescript configuration when filePathGlobs is true and typescriptConfiguration exists", async () => {
13+
const findTypeScriptConfiguration = jest.fn();
14+
const typescriptConfiguration = {
15+
include: ["a.ts"],
16+
};
17+
18+
const result = await collectCommentFileNames(
19+
{ findTypeScriptConfiguration },
20+
true,
21+
typescriptConfiguration,
22+
);
23+
24+
expect(result).toEqual(typescriptConfiguration);
25+
});
26+
27+
it("returns the input file paths when filePathGlobs is an array", async () => {
28+
const findTypeScriptConfiguration = jest.fn();
29+
const filePathGlobs = ["a.ts"];
30+
31+
const result = await collectCommentFileNames(
32+
{ findTypeScriptConfiguration },
33+
filePathGlobs,
34+
);
35+
36+
expect(result).toEqual({
37+
include: filePathGlobs,
38+
});
39+
});
40+
41+
it("returns the input file path when filePathGlobs is a source file path string", async () => {
42+
const findTypeScriptConfiguration = jest.fn();
43+
const filePathGlobs = "a.ts";
44+
45+
const result = await collectCommentFileNames(
46+
{ findTypeScriptConfiguration },
47+
filePathGlobs,
48+
);
49+
50+
expect(result).toEqual({
51+
include: [filePathGlobs],
52+
});
53+
});
54+
55+
it("returns the failure when filePathGlobs is a config file path string and reading it fails", async () => {
56+
const error = new Error("Failure!");
57+
const findTypeScriptConfiguration = jest.fn().mockResolvedValue(error);
58+
59+
const result = await collectCommentFileNames(
60+
{ findTypeScriptConfiguration },
61+
"tsconfig.json",
62+
);
63+
64+
expect(result).toEqual(error);
65+
});
66+
67+
it("returns the typescript configuration from disk when filePathGlobs is a config path string and reading it succeeds", async () => {
68+
const findTypeScriptConfiguration = jest.fn().mockResolvedValue({
69+
include: ["a.ts"],
70+
});
71+
72+
const result = await collectCommentFileNames(
73+
{ findTypeScriptConfiguration },
74+
"tsconfig.json",
75+
);
76+
77+
expect(result).toEqual({
78+
include: ["a.ts"],
79+
});
80+
});
81+
});
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { SansDependencies } from "../binding";
2+
import {
3+
findTypeScriptConfiguration,
4+
TypeScriptConfiguration,
5+
} from "../input/findTypeScriptConfiguration";
6+
import { uniqueFromSources } from "../utils";
7+
8+
export type CollectCommentFileNamesDependencies = {
9+
findTypeScriptConfiguration: SansDependencies<typeof findTypeScriptConfiguration>;
10+
};
11+
12+
export type CommentFileNames = {
13+
exclude?: string[];
14+
include: string[];
15+
};
16+
17+
export const collectCommentFileNames = async (
18+
dependencies: CollectCommentFileNamesDependencies,
19+
filePathGlobs: true | string | string[],
20+
typescriptConfiguration?: TypeScriptConfiguration,
21+
): Promise<CommentFileNames | Error> => {
22+
if (filePathGlobs === true) {
23+
if (!typescriptConfiguration) {
24+
return new Error(
25+
"--comments indicated to convert files listed in a tsconfig.json, but one was not found on disk or specified by with --typescript.",
26+
);
27+
}
28+
29+
return {
30+
exclude: typescriptConfiguration.exclude,
31+
include: uniqueFromSources(
32+
typescriptConfiguration.files,
33+
typescriptConfiguration.include,
34+
),
35+
};
36+
}
37+
38+
if (typeof filePathGlobs === "string" && filePathGlobs.endsWith(".json")) {
39+
const findResult = await dependencies.findTypeScriptConfiguration(filePathGlobs);
40+
if (findResult instanceof Error) {
41+
return findResult;
42+
}
43+
44+
return await collectCommentFileNames(dependencies, true, findResult);
45+
}
46+
47+
return {
48+
include: uniqueFromSources(filePathGlobs),
49+
};
50+
};

0 commit comments

Comments
 (0)