Allow --module node16 (#647)

This commit is contained in:
Andrew Branch
2023-03-22 14:39:56 -07:00
committed by GitHub
parent d57e5435fe
commit 7c82ab4ff2
4 changed files with 33 additions and 7 deletions

View File

@@ -427,7 +427,7 @@ export function createSourceFile(
moduleResolutionHost: ts.ModuleResolutionHost,
compilerOptions: ts.CompilerOptions
): ts.SourceFile {
const file = ts.createSourceFile(filename, content, ts.ScriptTarget.Latest, /*setParentNodes*/ false);
const file = ts.createSourceFile(filename, content, ts.ScriptTarget.Latest, /*setParentNodes*/ true);
file.impliedNodeFormat = ts.getImpliedNodeFormatForFile(
filename as ts.Path,
/*packageJsonInfoCache*/ undefined,

View File

@@ -7,7 +7,7 @@ import { allReferencedFiles, getModuleInfo, getTestDependencies } from "../src/l
const fs = createMockDT().fs;
const moduleResolutionHost = createModuleResolutionHost(fs);
const compilerOptions = {
module: ts.ModuleKind.CommonJS,
module: ts.ModuleKind.Node16,
baseUrl: "/DefinitelyTyped/types",
typeRoots: ["/DefinitelyTyped/types"],
};

View File

@@ -56,16 +56,24 @@ export async function checkPackageJson(dirPath: string, typesVersions: readonly
}
}
/**
* numbers in `CompilerOptions` might be enum values mapped from strings
*/
export type CompilerOptionsRaw = {
[K in keyof CompilerOptions]?: CompilerOptions[K] extends number | undefined
? string | number | undefined
: CompilerOptions[K];
}
export interface DefinitelyTypedInfo {
/** "../" or "../../" or "../../../". This should use '/' even on windows. */
readonly relativeBaseUrl: string;
}
export function checkTsconfig(options: CompilerOptions, dt: DefinitelyTypedInfo | undefined): void {
export function checkTsconfig(options: CompilerOptionsRaw, dt: DefinitelyTypedInfo | undefined): void {
if (dt) {
const { relativeBaseUrl } = dt;
const mustHave = {
module: "commonjs",
noEmit: true,
forceConsistentCasingInFileNames: true,
baseUrl: relativeBaseUrl,
@@ -104,6 +112,7 @@ export function checkTsconfig(options: CompilerOptions, dt: DefinitelyTypedInfo
case "noUnusedLocals":
case "noUnusedParameters":
case "exactOptionalPropertyTypes":
case "module":
break;
default:
if (!(key in mustHave)) {
@@ -117,6 +126,13 @@ export function checkTsconfig(options: CompilerOptions, dt: DefinitelyTypedInfo
throw new Error('Must specify "lib", usually to `"lib": ["es6"]` or `"lib": ["es6", "dom"]`.');
}
if (!("module" in options)) {
throw new Error('Must specify "module" to `"module": "commonjs"` or `"module": "node16"`.');
}
if (options.module?.toString().toLowerCase() !== "commonjs" && options.module?.toString().toLowerCase() !== "node16") {
throw new Error(`When "module" is present, it must be set to "commonjs" or "node16".`);
}
if ("strict" in options) {
if (options.strict !== true) {
throw new Error('When "strict" is present, it must be set to `true`.');

View File

@@ -2,7 +2,7 @@
import { join } from "path";
import { consoleTestResultHandler, runTest } from "tslint/lib/test";
import { existsSync, readdirSync, statSync } from "fs";
import { checkTsconfig } from "../src/checks";
import { CompilerOptionsRaw, checkTsconfig } from "../src/checks";
import { assertPackageIsNotDeprecated } from "../src/index";
const testDir = __dirname;
@@ -30,8 +30,8 @@ function testSingle(testDirectory: string) {
}
describe("dtslint", () => {
const base = {
module: "commonjs" as any,
const base: CompilerOptionsRaw = {
module: "commonjs",
lib: ["es6"],
noImplicitAny: true,
noImplicitThis: true,
@@ -52,6 +52,16 @@ describe("dtslint", () => {
it("allows exactOptionalPropertyTypes: true", () => {
expect(checkTsconfig({ ...base, exactOptionalPropertyTypes: true }, { relativeBaseUrl: "../" })).toBeFalsy();
});
it("allows module: node16", () => {
expect(checkTsconfig({ ...base, module: "node16" }, { relativeBaseUrl: "../" })).toBeFalsy();
});
it("disallows missing `module`", () => {
const options = { ...base };
delete options.module;
expect(() => checkTsconfig(options, { relativeBaseUrl: "../" })).toThrow(
'Must specify "module" to `"module": "commonjs"` or `"module": "node16"`.'
);
});
it("disallows exactOptionalPropertyTypes: false", () => {
expect(() => checkTsconfig({ ...base, exactOptionalPropertyTypes: false }, { relativeBaseUrl: "../" })).toThrow(
'When "exactOptionalPropertyTypes" is present, it must be set to `true`.'