Skip to content

Commit b9dce9d

Browse files
authored
refactor: split out a common typecheckFile func (#344)
* 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 * fix lint error on shadowed name
1 parent fee9547 commit b9dce9d

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

src/index.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as resolve from "resolve";
88
import findCacheDir from "find-cache-dir";
99

1010
import { RollupContext } from "./rollupcontext";
11-
import { ConsoleContext, VerbosityLevel } from "./context";
11+
import { ConsoleContext, IContext, VerbosityLevel } from "./context";
1212
import { LanguageServiceHost } from "./host";
1313
import { TsCache, convertDiagnostic, convertEmitOutput, getAllReferences } from "./tscache";
1414
import { tsModule, setTypescriptModule } from "./tsproxy";
@@ -56,6 +56,15 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
5656
}));
5757
}
5858

59+
const typecheckFile = (id: string, snapshot: tsTypes.IScriptSnapshot, tcContext: IContext) =>
60+
{
61+
const diagnostics = getDiagnostics(id, snapshot);
62+
printDiagnostics(tcContext, diagnostics, parsedConfig.options.pretty === true);
63+
64+
if (diagnostics.length > 0)
65+
noErrors = false;
66+
}
67+
5968
const pluginOptions: IOptions = Object.assign({},
6069
{
6170
check: true,
@@ -201,11 +210,8 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
201210

202211
if (output.emitSkipped)
203212
{
204-
noErrors = false;
205-
206213
// always checking on fatal errors, even if options.check is set to false
207-
const diagnostics = getDiagnostics(id, snapshot);
208-
printDiagnostics(contextWrapper, diagnostics, parsedConfig.options.pretty === true);
214+
typecheckFile(id, snapshot, contextWrapper);
209215

210216
// since no output was generated, aborting compilation
211217
cache().done();
@@ -218,13 +224,7 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
218224
});
219225

220226
if (pluginOptions.check)
221-
{
222-
const diagnostics = getDiagnostics(id, snapshot);
223-
if (diagnostics.length > 0)
224-
noErrors = false;
225-
226-
printDiagnostics(contextWrapper, diagnostics, parsedConfig.options.pretty === true);
227-
}
227+
typecheckFile(id, snapshot, contextWrapper);
228228

229229
if (!result)
230230
return undefined;
@@ -277,19 +277,15 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
277277
return;
278278

279279
const snapshot = servicesHost.getScriptSnapshot(id);
280-
if (!snapshot)
281-
return;
282-
283-
const diagnostics = getDiagnostics(id, snapshot);
284-
printDiagnostics(context, diagnostics, parsedConfig.options.pretty === true);
280+
if (snapshot)
281+
typecheckFile(id, snapshot, context);
285282
});
286283
}
287284

288285
if (!watchMode && !noErrors)
289286
context.info(yellow("there were errors or warnings."));
290287

291288
cache().done();
292-
293289
generateRound++;
294290
},
295291

0 commit comments

Comments
 (0)