Skip to content

Commit 4d6cc50

Browse files
feat: isStrictCompilerOptionEnabled should default to strict: true for TS 6 (#873)
<!-- 👋 Hi, thanks for sending a PR to ts-api-utils! 💖. Please fill out all fields below and make sure each item is true and [x] checked. Otherwise we may not be able to review your PR. --> ## PR Checklist - [x] Addresses an existing open issue: fixes #870 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/ts-api-utils/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/ts-api-utils/blob/main/.github/CONTRIBUTING.md) were taken ## Overview Changes the way the options are resolved to make sure the strict options are enabled by default. For the test infra, I've added `typescript@rc` into the test matrix in order to test TS 6. Once TS 6 is actually released, `latest` will cover TS 6 and we can pin TS@5.9.3 to test the latest TS 5 version.
1 parent 16e99b3 commit 4d6cc50

3 files changed

Lines changed: 28 additions & 9 deletions

File tree

.github/workflows/test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ jobs:
2929
ts_version:
3030
- 4.8.4
3131
- latest
32+
# TS 6 rc. Once TS 6 is released, this can be removed, since it will
33+
# be superseded by `latest`
34+
- rc
3235

3336
name: Test
3437

src/compilerOptions.test.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import {
88
isCompilerOptionEnabled,
99
isStrictCompilerOptionEnabled,
1010
} from "./compilerOptions";
11+
import { isTsVersionAtLeast } from "./utils";
1112

1213
describe("isCompilerOptionEnabled", () => {
13-
it("checks if option is enabled", () => {
14+
it("checks if option is enabled explicitly", () => {
1415
expect(isCompilerOptionEnabled({}, "allowJs")).toBe(false);
1516
expect(isCompilerOptionEnabled({ allowJs: undefined }, "allowJs")).toBe(
1617
false,
@@ -134,7 +135,7 @@ describe("isCompilerOptionEnabled", () => {
134135
).toBe(false);
135136
expect(
136137
isCompilerOptionEnabled(
137-
{ suppressImplicitAnyIndexErrors: true },
138+
{ strict: false, suppressImplicitAnyIndexErrors: true },
138139
"suppressImplicitAnyIndexErrors",
139140
),
140141
).toBe(false);
@@ -354,6 +355,16 @@ describe("isStrictCompilerOptionEnabled", () => {
354355
).toBe(true);
355356
});
356357

358+
it("correctly resolves strict option defaults", () => {
359+
expect(isStrictCompilerOptionEnabled({}, "strictNullChecks")).toBe(
360+
isTsVersionAtLeast(6),
361+
);
362+
363+
expect(isStrictCompilerOptionEnabled({}, "noImplicitAny")).toBe(
364+
isTsVersionAtLeast(6),
365+
);
366+
});
367+
357368
it("knows about strictPropertyInitializations dependency on strictNullChecks", () => {
358369
expect(
359370
isStrictCompilerOptionEnabled(
@@ -382,13 +393,13 @@ describe("isStrictCompilerOptionEnabled", () => {
382393
{ strictPropertyInitialization: true },
383394
"strictPropertyInitialization",
384395
),
385-
).toBe(false);
396+
).toBe(isTsVersionAtLeast(6));
386397
expect(
387398
isStrictCompilerOptionEnabled(
388399
{ strictNullChecks: true },
389400
"strictPropertyInitialization",
390401
),
391-
).toBe(false);
402+
).toBe(isTsVersionAtLeast(6));
392403
expect(
393404
isStrictCompilerOptionEnabled(
394405
{ strict: false, strictPropertyInitialization: true },

src/compilerOptions.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import ts from "typescript";
55

6+
import { isTsVersionAtLeast } from "./utils";
7+
68
/* eslint-disable jsdoc/informative-docs */
79
/**
810
* An option that can be tested with {@link isCompilerOptionEnabled}.
@@ -136,9 +138,12 @@ export function isStrictCompilerOptionEnabled(
136138
options: ts.CompilerOptions,
137139
option: StrictCompilerOption,
138140
): boolean {
139-
return (
140-
(options.strict ? options[option] !== false : options[option] === true) &&
141-
(option !== "strictPropertyInitialization" ||
142-
isStrictCompilerOptionEnabled(options, "strictNullChecks"))
143-
);
141+
if (option === "strictPropertyInitialization") {
142+
// strictPropertyInitialization has no effect unless strictNullChecks is also enabled.
143+
if (!isStrictCompilerOptionEnabled(options, "strictNullChecks")) {
144+
return false;
145+
}
146+
}
147+
148+
return options[option] ?? options.strict ?? isTsVersionAtLeast(6);
144149
}

0 commit comments

Comments
 (0)