Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow exactOptionalPropertyTypes in DT tsconfigs #366

Merged
merged 2 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions packages/dtslint/src/checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { TypeScriptVersion } from "@definitelytyped/typescript-versions";
import assert = require("assert");
import { pathExists } from "fs-extra";
import { join as joinPaths } from "path";
import { CompilerOptions } from "typescript";

import { getCompilerOptions, readJson } from "./util";
import { readJson } from "./util";

export async function checkPackageJson(dirPath: string, typesVersions: readonly TypeScriptVersion[]): Promise<void> {
const pkgJsonPath = joinPaths(dirPath, "package.json");
Expand Down Expand Up @@ -59,9 +60,7 @@ export interface DefinitelyTypedInfo {
/** "../" or "../../" or "../../../". This should use '/' even on windows. */
readonly relativeBaseUrl: string;
}
export async function checkTsconfig(dirPath: string, dt: DefinitelyTypedInfo | undefined): Promise<void> {
const options = await getCompilerOptions(dirPath);

export function checkTsconfig(options: CompilerOptions, dt: DefinitelyTypedInfo | undefined): void {
if (dt) {
const { relativeBaseUrl } = dt;

Expand All @@ -78,12 +77,11 @@ export async function checkTsconfig(dirPath: string, dt: DefinitelyTypedInfo | u
const expected = mustHave[key];
const actual = options[key];
if (!deepEquals(expected, actual)) {
throw new Error(`Expected compilerOptions[${JSON.stringify(key)}] === ${JSON.stringify(expected)}`);
throw new Error(`Expected compilerOptions[${JSON.stringify(key)}] === ${JSON.stringify(expected)}, but got ${JSON.stringify(actual)}`);
}
}

for (const key in options) {
// tslint:disable-line forin
switch (key) {
case "lib":
case "noImplicitAny":
Expand All @@ -94,16 +92,14 @@ export async function checkTsconfig(dirPath: string, dt: DefinitelyTypedInfo | u
case "strictFunctionTypes":
case "esModuleInterop":
case "allowSyntheticDefaultImports":
// Allow any value
break;
case "target":
case "paths":
case "target":
case "jsx":
case "jsxFactory":
case "experimentalDecorators":
case "noUnusedLocals":
case "noUnusedParameters":
// OK. "paths" checked further by types-publisher
case "exactOptionalPropertyTypes":
break;
default:
if (!(key in mustHave)) {
Expand Down Expand Up @@ -134,6 +130,11 @@ export async function checkTsconfig(dirPath: string, dt: DefinitelyTypedInfo | u
}
}
}
if ("exactOptionalPropertyTypes" in options) {
if (options.exactOptionalPropertyTypes !== true) {
throw new Error('When "exactOptionalPropertyTypes" is present, it must be set to `true`.');
}
}

if (options.types && options.types.length) {
throw new Error(
Expand Down
6 changes: 3 additions & 3 deletions packages/dtslint/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { basename, dirname, join as joinPaths, resolve } from "path";
import { cleanTypeScriptInstalls, installAllTypeScriptVersions, installTypeScriptNext } from "@definitelytyped/utils";
import { checkPackageJson, checkTsconfig } from "./checks";
import { checkTslintJson, lint, TsVersion } from "./lint";
import { mapDefinedAsync, withoutPrefix } from "./util";
import { getCompilerOptions, mapDefinedAsync, withoutPrefix } from "./util";

async function main(): Promise<void> {
const args = process.argv.slice(2);
Expand Down Expand Up @@ -221,8 +221,8 @@ async function testTypesVersion(
isLatest: boolean
): Promise<void> {
await checkTslintJson(dirPath, dt);
await checkTsconfig(
dirPath,
checkTsconfig(
await getCompilerOptions(dirPath),
dt ? { relativeBaseUrl: ".." + (isOlderVersion ? "/.." : "") + (isLatest ? "" : "/..") + "/" } : undefined
);
const err = await lint(dirPath, lowVersion, hiVersion, isLatest, expectOnly, tsLocal);
Expand Down
53 changes: 44 additions & 9 deletions packages/dtslint/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { join } from "path";
import { consoleTestResultHandler, runTest } from "tslint/lib/test";
import { existsSync, readdirSync } from "fs";
import { checkTsconfig } from "../src/checks";

const testDir = __dirname;

Expand All @@ -28,15 +29,49 @@ function testSingle(testDirectory: string) {
}

describe("dtslint", () => {
const tests = readdirSync(testDir).filter(x => x !== "index.test.ts");
for (const testName of tests) {
const testDirectory = join(testDir, testName);
if (existsSync(join(testDirectory, "tslint.json"))) {
testSingle(testDirectory);
} else {
for (const subTestName of readdirSync(testDirectory)) {
testSingle(join(testDirectory, subTestName));
const base = {
"module": "commonjs" as any,
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true,
};
describe("checks", () => {
it("disallows unknown compiler options", () => {
expect(() => checkTsconfig({ ...base, completelyInvented: true }, { relativeBaseUrl: "../" }))
.toThrow("Unexpected compiler option completelyInvented");
});
it("allows exactOptionalPropertyTypes: true", () => {
expect(checkTsconfig({ ...base, exactOptionalPropertyTypes: true }, { relativeBaseUrl: "../" }))
.toBeFalsy();
});
it("disallows exactOptionalPropertyTypes: false", () => {
expect(() => checkTsconfig({ ...base, exactOptionalPropertyTypes: false }, { relativeBaseUrl: "../" }))
.toThrow("When \"exactOptionalPropertyTypes\" is present, it must be set to `true`.");
});
});
describe("rules", () => {
const tests = readdirSync(testDir).filter(x => x !== "index.test.ts");
for (const testName of tests) {
const testDirectory = join(testDir, testName);
if (existsSync(join(testDirectory, "tslint.json"))) {
testSingle(testDirectory);
} else {
for (const subTestName of readdirSync(testDirectory)) {
testSingle(join(testDirectory, subTestName));
}
}
}
}
});
});