Skip to content

Commit 7e96af9

Browse files
fix: simplify collecting strict TS options (#2196)
## PR Checklist - [ ] Addresses an existing open issue: fixes #000 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/TypeStat/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/TypeStat/blob/main/.github/CONTRIBUTING.md) were taken ## Overview I forgot this from #2192 , though maybe it's better to have it separately. This simplifies and IMO corrects the logic for some of these options. Before, it was possible that if you have `noImplicitAny` set as false in `typestat.json` file for the fixes, it would change the typescript compiler options to be also false. This could lead to weird behavior. New logic set `noImplicitAny` as true in the collected compiler options if it's true either in the fixes section or in the original ts config file. I think the `types` section in `BaseTypeStatOptions` is unnecessary. It's only used in `findComplaintForOptions` to check options and print "fixes.strictNonNullAssertions specified but not strictNullChecks." if there is mismatch. That check could use the actual collected compiler options. However, it's not harmful to keep it.
1 parent b5983d8 commit 7e96af9

File tree

5 files changed

+30
-59
lines changed

5 files changed

+30
-59
lines changed

src/options/fillOutRawOptions.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export const fillOutRawOptions = ({
3030
projectPath,
3131
rawOptions,
3232
}: OptionsFromRawOptionsSettings): PendingTypeStatOptions => {
33-
const rawOptionTypes = rawOptions.types ?? {};
3433
const noImplicitAny = collectNoImplicitAny(
3534
parsedTsConfig.options,
3635
rawOptions,
@@ -39,8 +38,10 @@ export const fillOutRawOptions = ({
3938
parsedTsConfig.options,
4039
rawOptions,
4140
);
42-
const { compilerStrictNullChecks, typeStrictNullChecks } =
43-
collectStrictNullChecks(parsedTsConfig.options, rawOptionTypes);
41+
const strictNullChecks = collectStrictNullChecks(
42+
parsedTsConfig.options,
43+
rawOptions,
44+
);
4445

4546
const packageOptions = collectPackageOptions(cwd, rawOptions);
4647

@@ -92,13 +93,13 @@ export const fillOutRawOptions = ({
9293
noEmit: true,
9394
noImplicitAny,
9495
noImplicitThis,
95-
strictNullChecks: compilerStrictNullChecks,
96+
strictNullChecks,
9697
},
9798
},
9899
postProcess: { shell },
99100
projectPath,
100101
types: {
101-
strictNullChecks: typeStrictNullChecks,
102+
strictNullChecks: strictNullChecks || undefined,
102103
},
103104
};
104105
};

src/options/parsing/collectNoImplicitAny.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,8 @@ import { RawTypeStatOptions } from "../types.js";
55
export const collectNoImplicitAny = (
66
compilerOptions: Readonly<ts.CompilerOptions>,
77
rawOptions: RawTypeStatOptions,
8-
): boolean => {
9-
if (rawOptions.fixes?.noImplicitAny !== undefined) {
10-
return rawOptions.fixes.noImplicitAny;
11-
}
12-
13-
if (compilerOptions.noImplicitAny !== undefined) {
14-
return compilerOptions.noImplicitAny;
15-
}
16-
17-
return false;
18-
};
8+
): boolean =>
9+
rawOptions.fixes?.noImplicitAny ||
10+
compilerOptions.noImplicitAny ||
11+
compilerOptions.strict ||
12+
false;

src/options/parsing/collectNoImplicitThis.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,8 @@ import { RawTypeStatOptions } from "../types.js";
55
export const collectNoImplicitThis = (
66
compilerOptions: Readonly<ts.CompilerOptions>,
77
rawOptions: RawTypeStatOptions,
8-
): boolean => {
9-
if (rawOptions.fixes?.noImplicitThis !== undefined) {
10-
return rawOptions.fixes.noImplicitThis;
11-
}
12-
13-
if (compilerOptions.noImplicitThis !== undefined) {
14-
return compilerOptions.noImplicitThis;
15-
}
16-
17-
return false;
18-
};
8+
): boolean =>
9+
rawOptions.fixes?.noImplicitThis ||
10+
compilerOptions.noImplicitThis ||
11+
compilerOptions.strict ||
12+
false;
Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,13 @@
11
import ts from "typescript";
22

3-
import { RawTypeStatTypeOptions } from "../types.js";
3+
import { RawTypeStatOptions } from "../types.js";
44

5+
/** If strictNullChecks enabled either in typestat config or in tsconfig. */
56
export const collectStrictNullChecks = (
67
compilerOptions: Readonly<ts.CompilerOptions>,
7-
rawOptionTypes: RawTypeStatTypeOptions,
8-
) => {
9-
const typeStrictNullChecks = rawOptionTypes.strictNullChecks;
10-
const compilerStrictNullChecks = collectCompilerStrictNullChecks(
11-
compilerOptions,
12-
typeStrictNullChecks,
13-
);
14-
15-
return { compilerStrictNullChecks, typeStrictNullChecks };
16-
};
17-
18-
const collectCompilerStrictNullChecks = (
19-
compilerOptions: Readonly<ts.CompilerOptions>,
20-
typeStrictNullChecks: boolean | undefined,
21-
): boolean => {
22-
if (typeStrictNullChecks !== undefined) {
23-
return typeStrictNullChecks;
24-
}
25-
26-
if (compilerOptions.strictNullChecks !== undefined) {
27-
return compilerOptions.strictNullChecks;
28-
}
29-
30-
if (compilerOptions.strict !== undefined) {
31-
return compilerOptions.strict;
32-
}
33-
34-
return false;
35-
};
8+
rawOptions: RawTypeStatOptions,
9+
): boolean =>
10+
rawOptions.types?.strictNullChecks ||
11+
compilerOptions.strictNullChecks ||
12+
compilerOptions.strict ||
13+
false;

test/__snapshots__/cleanups.test.ts.snap

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ exports[`Cleanups > non-TypeErrors > options 1`] = `
101101
"shell": []
102102
},
103103
"projectPath": "<rootDir>/tsconfig.json",
104-
"types": {}
104+
"types": {
105+
"strictNullChecks": true
106+
}
105107
}
106108
]"
107109
`;
@@ -203,7 +205,9 @@ exports[`Cleanups > suppressTypeErrors > options 1`] = `
203205
"shell": []
204206
},
205207
"projectPath": "<rootDir>/tsconfig.json",
206-
"types": {}
208+
"types": {
209+
"strictNullChecks": true
210+
}
207211
}
208212
]"
209213
`;

0 commit comments

Comments
 (0)