From 7c82ab4ff21f92524d3810212bafc87d4f1f6806 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 22 Mar 2023 14:39:56 -0700 Subject: [PATCH] Allow --module node16 (#647) --- .../definitions-parser/src/lib/module-info.ts | 2 +- .../test/module-info.test.ts | 2 +- packages/dtslint/src/checks.ts | 20 +++++++++++++++++-- packages/dtslint/test/index.test.ts | 16 ++++++++++++--- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/packages/definitions-parser/src/lib/module-info.ts b/packages/definitions-parser/src/lib/module-info.ts index 918f20ee..37d59ca0 100644 --- a/packages/definitions-parser/src/lib/module-info.ts +++ b/packages/definitions-parser/src/lib/module-info.ts @@ -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, diff --git a/packages/definitions-parser/test/module-info.test.ts b/packages/definitions-parser/test/module-info.test.ts index 273b7df8..1c19beeb 100644 --- a/packages/definitions-parser/test/module-info.test.ts +++ b/packages/definitions-parser/test/module-info.test.ts @@ -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"], }; diff --git a/packages/dtslint/src/checks.ts b/packages/dtslint/src/checks.ts index 2ad094a7..269f56db 100644 --- a/packages/dtslint/src/checks.ts +++ b/packages/dtslint/src/checks.ts @@ -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`.'); diff --git a/packages/dtslint/test/index.test.ts b/packages/dtslint/test/index.test.ts index 563e7c11..ff855870 100644 --- a/packages/dtslint/test/index.test.ts +++ b/packages/dtslint/test/index.test.ts @@ -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`.'