From b7d1bd4a5b9e23ed856b9ba2d607c55fdc0d7de3 Mon Sep 17 00:00:00 2001 From: Anton Gilgur Date: Sat, 30 Jul 2022 15:49:02 -0400 Subject: [PATCH] refactor: combine `check-tsconfig` with `parse-tsconfig` - `checkTsConfig` is literally only used by `parseTsConfig` - I first just moved the function over, but it's a two-liner, so thought it made more sense to just in-line it - merge the unit tests as well - we already check the non-error case in `parse-tsconfig.spec`, so only add the error case - `check-tsconfig` was longer in the past and more files were being created then for separation, so this may have made more sense then, but now it is unnecessary - c.f. 733207791800ffe15672554d2a6337c73c65cf74 --- CONTRIBUTING.md | 2 +- __tests__/check-tsconfig.spec.ts | 33 -------------------------------- __tests__/parse-tsconfig.spec.ts | 7 +++++++ src/check-tsconfig.ts | 11 ----------- src/parse-tsconfig.ts | 6 ++++-- 5 files changed, 12 insertions(+), 47 deletions(-) delete mode 100644 __tests__/check-tsconfig.spec.ts delete mode 100644 src/check-tsconfig.ts diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 37b61305..9f46c87e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -66,7 +66,7 @@ It can also be useful to review some issues and have a "goal" in mind (especiall Once you have some understanding of the codebase's main workflow, you can start to dive deeper into pieces that require more domain knowledge.
A useful resource as you dive deeper are the [unit tests](__tests__/). They're good to look through as you dig into a module to understand how it's used. -1. From here, you can start to read more of the modules that integrate with the TypeScript API, such as [`host`](src/host.ts), [`parse-tsconfig`](src/parse-tsconfig.ts), [`check-tsconfig`](src/check-tsconfig.ts), and maybe how TS is imported in [`tsproxy`](src/tsproxy.ts) and [`tslib`](src/tslib.ts) +1. From here, you can start to read more of the modules that integrate with the TypeScript API, such as [`host`](src/host.ts) and [`parse-tsconfig`](src/parse-tsconfig.ts), and maybe how TS is imported in [`tsproxy`](src/tsproxy.ts) and [`tslib`](src/tslib.ts) - A _very_ useful reference here is the [TypeScript Wiki](https://github.com/microsoft/TypeScript/wiki), which has two main articles that are the basis for most Compiler integrations: - [Using the Compiler API](https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API) - [Using the Language Service API](https://github.com/microsoft/TypeScript/wiki/Using-the-Language-Service-API) diff --git a/__tests__/check-tsconfig.spec.ts b/__tests__/check-tsconfig.spec.ts deleted file mode 100644 index 3e77e392..00000000 --- a/__tests__/check-tsconfig.spec.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { test, expect } from "@jest/globals"; -import * as ts from "typescript"; - -import { setTypescriptModule } from "../src/tsproxy"; -import { checkTsConfig } from "../src/check-tsconfig"; - -setTypescriptModule(ts); - -const defaultConfig = { fileNames: [], errors: [], options: {} }; - -test("checkTsConfig", () => { - expect(() => checkTsConfig({ - ...defaultConfig, - options: { module: ts.ModuleKind.ES2015 }, - })).not.toThrow(); - - expect(() => checkTsConfig({ - ...defaultConfig, - options: { module: ts.ModuleKind.ES2020 }, - })).not.toThrow(); - - expect(() => checkTsConfig({ - ...defaultConfig, - options: { module: ts.ModuleKind.ESNext }, - })).not.toThrow(); -}); - -test("checkTsConfig - errors", () => { - expect(() => checkTsConfig({ - ...defaultConfig, - options: { module: ts.ModuleKind.None }, - })).toThrow("Incompatible tsconfig option. Module resolves to 'None'. This is incompatible with Rollup, please use"); -}); diff --git a/__tests__/parse-tsconfig.spec.ts b/__tests__/parse-tsconfig.spec.ts index b7014709..46a5a1c6 100644 --- a/__tests__/parse-tsconfig.spec.ts +++ b/__tests__/parse-tsconfig.spec.ts @@ -14,6 +14,13 @@ test("parseTsConfig", () => { expect(() => parseTsConfig(makeContext(), defaultOpts)).not.toThrow(); }); +test("parseTsConfig - incompatible module", () => { + expect(() => parseTsConfig(makeContext(), { + ...defaultOpts, + tsconfigOverride: { compilerOptions: { module: "none" } }, + })).toThrow("Incompatible tsconfig option. Module resolves to 'None'. This is incompatible with Rollup, please use"); +}); + test("parseTsConfig - tsconfig errors", () => { const context = makeContext(); diff --git a/src/check-tsconfig.ts b/src/check-tsconfig.ts deleted file mode 100644 index 033742fe..00000000 --- a/src/check-tsconfig.ts +++ /dev/null @@ -1,11 +0,0 @@ -import * as tsTypes from "typescript"; - -import { tsModule } from "./tsproxy"; - -export function checkTsConfig(parsedConfig: tsTypes.ParsedCommandLine): void -{ - const module = parsedConfig.options.module!; - - if (module !== tsModule.ModuleKind.ES2015 && module !== tsModule.ModuleKind.ES2020 && module !== tsModule.ModuleKind.ESNext) - throw new Error(`rpt2: Incompatible tsconfig option. Module resolves to '${tsModule.ModuleKind[module]}'. This is incompatible with Rollup, please use 'module: "ES2015"', 'module: "ES2020"', or 'module: "ESNext"'.`); -} diff --git a/src/parse-tsconfig.ts b/src/parse-tsconfig.ts index 45380ce9..dd41bffe 100644 --- a/src/parse-tsconfig.ts +++ b/src/parse-tsconfig.ts @@ -7,7 +7,6 @@ import { printDiagnostics } from "./print-diagnostics"; import { convertDiagnostic } from "./tscache"; import { getOptionsOverrides } from "./get-options-overrides"; import { IOptions } from "./ioptions"; -import { checkTsConfig } from "./check-tsconfig"; export function parseTsConfig(context: IContext, pluginOptions: IOptions) { @@ -45,7 +44,10 @@ export function parseTsConfig(context: IContext, pluginOptions: IOptions) const compilerOptionsOverride = getOptionsOverrides(pluginOptions, preParsedTsConfig); const parsedTsConfig = tsModule.parseJsonConfigFileContent(mergedConfig, tsModule.sys, baseDir, compilerOptionsOverride, configFileName); - checkTsConfig(parsedTsConfig); + const module = parsedTsConfig.options.module!; + if (module !== tsModule.ModuleKind.ES2015 && module !== tsModule.ModuleKind.ES2020 && module !== tsModule.ModuleKind.ESNext) + throw new Error(`rpt2: Incompatible tsconfig option. Module resolves to '${tsModule.ModuleKind[module]}'. This is incompatible with Rollup, please use 'module: "ES2015"', 'module: "ES2020"', or 'module: "ESNext"'.`); + printDiagnostics(context, convertDiagnostic("config", parsedTsConfig.errors), pretty); context.debug(`built-in options overrides: ${JSON.stringify(compilerOptionsOverride, undefined, 4)}`);