Skip to content

Commit

Permalink
fix(server): cannot access 'formatHost' before initialization (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeraS authored Feb 3, 2025
1 parent 333ae77 commit dae373e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
19 changes: 10 additions & 9 deletions src/common/typescript/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function compile(
const program = ts.createProgram(parsedConfig.fileNames, parsedConfig.options, compilerHost);
// @ts-expect-error
const filesCount = compilerHost.readFile.disableDisplay();
const allDiagnostics = ts.getPreEmitDiagnostics(program).slice();
let allDiagnostics = ts.getPreEmitDiagnostics(program);
logger.verbose(`Program created, read ${filesCount} files`);

if (!hasErrors(allDiagnostics)) {
Expand All @@ -49,7 +49,9 @@ export function compile(
});
logger.verbose('Emit complete!');

allDiagnostics.push(...emitResult.diagnostics);
allDiagnostics = ts.sortAndDeduplicateDiagnostics(
allDiagnostics.concat(emitResult.diagnostics),
);
}

allDiagnostics.forEach(reportDiagnostic);
Expand All @@ -61,21 +63,20 @@ export function compile(
logger.success(`Compiled successfully in ${elapsedTime(start)}`);
}

const formatHost = {
getCanonicalFileName: (path: string) => path,
getCurrentDirectory: ts.sys.getCurrentDirectory,
getNewLine: () => ts.sys.newLine,
};

function reportDiagnostic(diagnostic: Typescript.Diagnostic) {
const formatHost = {
getCanonicalFileName: (path: string) => path,
getCurrentDirectory: ts.sys.getCurrentDirectory,
getNewLine: () => ts.sys.newLine,
};
if (logger.isVerbose) {
logger.message(ts.formatDiagnosticsWithColorAndContext([diagnostic], formatHost));
} else {
logger.message(formatDiagnosticBrief(ts, diagnostic, formatHost));
}
}

function hasErrors(diagnostics: Typescript.Diagnostic[]) {
function hasErrors(diagnostics: readonly Typescript.Diagnostic[]) {
return diagnostics.some(({category}) => category === ts.DiagnosticCategory.Error);
}
}
15 changes: 6 additions & 9 deletions src/common/typescript/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ export function watch(

const createProgram = ts.createEmitAndSemanticDiagnosticsBuilderProgram;

const formatHost = {
getCanonicalFileName: (path: string) => path,
getCurrentDirectory: ts.sys.getCurrentDirectory,
getNewLine: () => ts.sys.newLine,
};

const host = ts.createWatchCompilerHost(
configPath,
{
Expand Down Expand Up @@ -79,6 +73,11 @@ export function watch(
ts.createWatchProgram(host);

function reportDiagnostic(diagnostic: Typescript.Diagnostic) {
const formatHost = {
getCanonicalFileName: (path: string) => path,
getCurrentDirectory: ts.sys.getCurrentDirectory,
getNewLine: () => ts.sys.newLine,
};
if (logger.isVerbose) {
logger.message(ts.formatDiagnosticsWithColorAndContext([diagnostic], formatHost));
} else {
Expand All @@ -92,9 +91,7 @@ export function watch(
*/
function reportWatchStatusChanged(diagnostic: Typescript.Diagnostic) {
if (diagnostic.messageText) {
logger.message(
ts.flattenDiagnosticMessageText(diagnostic.messageText, formatHost.getNewLine()),
);
logger.message(ts.flattenDiagnosticMessageText(diagnostic.messageText, ts.sys.newLine));
}
}
}

0 comments on commit dae373e

Please sign in to comment.