allow exactOptionalPropertyTypes in DT tsconfigs (#366)

* allow exactOptionalPropertyTypes in DT tsconfigs

Also add unit tests to dtslint for the first time ever.

I'm not sure it makes sense to allow any other flags, and eventually
exactOptionalPropertyTypes should be required.

* remove confusing comments to satisfy lint
This commit is contained in:
Nathan Shively-Sanders
2021-12-07 07:57:35 -08:00
committed by GitHub
parent 2ac54be2aa
commit b62202fd20
3 changed files with 58 additions and 22 deletions

View File

@@ -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");
@@ -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;
@@ -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":
@@ -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)) {
@@ -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(

View File

@@ -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);
@@ -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);

View File

@@ -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;
@@ -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));
}
}
}
}
});
});