From 4258895c1d04ca3f8eebb7b722914518de94c68a Mon Sep 17 00:00:00 2001 From: Anton Gilgur Date: Sat, 4 Jun 2022 18:52:24 -0400 Subject: [PATCH 1/2] refactor: split out a common typecheckFile func - this is used in 3 places and going to be more for the code I'm adding to fix type-only imports - so DRY it up and standardize the functionality too - some places had `noErrors = false` in one place while others had it in another - same for `printDiagnostics` - but the ordering actually doesn't matter, so just keep it consistent and the same - and then can split a common function that does both out - technically, now getDiagnostics is _only_ used in typecheckFile, so I could link to the two together, but I'm refactoring that one up a little - but this a good, small example of how refactoring one part of a codebase can make it easier to identify more similar pieces and then refactor even more --- src/index.ts | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/src/index.ts b/src/index.ts index f0960108..fd4f511d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,7 +8,7 @@ import * as resolve from "resolve"; import findCacheDir from "find-cache-dir"; import { RollupContext } from "./rollupcontext"; -import { ConsoleContext, VerbosityLevel } from "./context"; +import { ConsoleContext, IContext, VerbosityLevel } from "./context"; import { LanguageServiceHost } from "./host"; import { TsCache, convertDiagnostic, convertEmitOutput, getAllReferences } from "./tscache"; import { tsModule, setTypescriptModule } from "./tsproxy"; @@ -56,6 +56,15 @@ const typescript: PluginImpl = (options) => })); } + const typecheckFile = (id: string, snapshot: tsTypes.IScriptSnapshot, context: IContext) => + { + const diagnostics = getDiagnostics(id, snapshot); + printDiagnostics(context, diagnostics, parsedConfig.options.pretty === true); + + if (diagnostics.length > 0) + noErrors = false; + } + const pluginOptions: IOptions = Object.assign({}, { check: true, @@ -201,11 +210,8 @@ const typescript: PluginImpl = (options) => if (output.emitSkipped) { - noErrors = false; - // always checking on fatal errors, even if options.check is set to false - const diagnostics = getDiagnostics(id, snapshot); - printDiagnostics(contextWrapper, diagnostics, parsedConfig.options.pretty === true); + typecheckFile(id, snapshot, contextWrapper); // since no output was generated, aborting compilation cache().done(); @@ -218,13 +224,7 @@ const typescript: PluginImpl = (options) => }); if (pluginOptions.check) - { - const diagnostics = getDiagnostics(id, snapshot); - if (diagnostics.length > 0) - noErrors = false; - - printDiagnostics(contextWrapper, diagnostics, parsedConfig.options.pretty === true); - } + typecheckFile(id, snapshot, contextWrapper); if (!result) return undefined; @@ -277,11 +277,8 @@ const typescript: PluginImpl = (options) => return; const snapshot = servicesHost.getScriptSnapshot(id); - if (!snapshot) - return; - - const diagnostics = getDiagnostics(id, snapshot); - printDiagnostics(context, diagnostics, parsedConfig.options.pretty === true); + if (snapshot) + typecheckFile(id, snapshot, context); }); } @@ -289,7 +286,6 @@ const typescript: PluginImpl = (options) => context.info(yellow("there were errors or warnings.")); cache().done(); - generateRound++; }, From 8b5016be12469c564d26aff12374a27e6e882f07 Mon Sep 17 00:00:00 2001 From: Anton Gilgur Date: Sat, 4 Jun 2022 19:08:59 -0400 Subject: [PATCH 2/2] fix lint error on shadowed name --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index fd4f511d..e94b4e97 100644 --- a/src/index.ts +++ b/src/index.ts @@ -56,10 +56,10 @@ const typescript: PluginImpl = (options) => })); } - const typecheckFile = (id: string, snapshot: tsTypes.IScriptSnapshot, context: IContext) => + const typecheckFile = (id: string, snapshot: tsTypes.IScriptSnapshot, tcContext: IContext) => { const diagnostics = getDiagnostics(id, snapshot); - printDiagnostics(context, diagnostics, parsedConfig.options.pretty === true); + printDiagnostics(tcContext, diagnostics, parsedConfig.options.pretty === true); if (diagnostics.length > 0) noErrors = false;