test(ivy): allow `makeProgram` to be more configurable (#24897)
This supports use cases needed by ngcc, where the compilation needs to be configured for JavaScript differently to normal TypeScript. PR Close #24897
This commit is contained in:
parent
6f1685ab98
commit
07e6de5788
|
@ -9,20 +9,31 @@
|
|||
import * as path from 'path';
|
||||
import * as ts from 'typescript';
|
||||
|
||||
export function makeProgram(files: {name: string, contents: string}[]):
|
||||
{program: ts.Program, host: ts.CompilerHost} {
|
||||
export function makeProgram(
|
||||
files: {name: string, contents: string}[],
|
||||
options?: ts.CompilerOptions): {program: ts.Program, host: ts.CompilerHost} {
|
||||
const host = new InMemoryHost();
|
||||
files.forEach(file => host.writeFile(file.name, file.contents));
|
||||
|
||||
const rootNames = files.map(file => host.getCanonicalFileName(file.name));
|
||||
const program = ts.createProgram(
|
||||
rootNames,
|
||||
{noLib: true, experimentalDecorators: true, moduleResolution: ts.ModuleResolutionKind.NodeJs},
|
||||
rootNames, {
|
||||
noLib: true,
|
||||
experimentalDecorators: true,
|
||||
moduleResolution: ts.ModuleResolutionKind.NodeJs, ...options
|
||||
},
|
||||
host);
|
||||
const diags = [...program.getSyntacticDiagnostics(), ...program.getSemanticDiagnostics()];
|
||||
if (diags.length > 0) {
|
||||
throw new Error(
|
||||
`Typescript diagnostics failed! ${diags.map(diag => diag.messageText).join(', ')}`);
|
||||
const errors = diags.map(diagnostic => {
|
||||
let message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
|
||||
if (diagnostic.file) {
|
||||
const {line, character} = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start !);
|
||||
message = `${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`;
|
||||
}
|
||||
return `Error: ${message}`;
|
||||
});
|
||||
throw new Error(`Typescript diagnostics failed! ${errors.join(', ')}`);
|
||||
}
|
||||
return {program, host};
|
||||
}
|
||||
|
@ -39,7 +50,7 @@ export class InMemoryHost implements ts.CompilerHost {
|
|||
onError && onError(`File does not exist: ${this.getCanonicalFileName(fileName)})`);
|
||||
return undefined;
|
||||
}
|
||||
return ts.createSourceFile(fileName, contents, languageVersion, undefined, ts.ScriptKind.TS);
|
||||
return ts.createSourceFile(fileName, contents, languageVersion);
|
||||
}
|
||||
|
||||
getDefaultLibFileName(options: ts.CompilerOptions): string { return '/lib.d.ts'; }
|
||||
|
|
Loading…
Reference in New Issue