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 path from 'path';
|
||||||
import * as ts from 'typescript';
|
import * as ts from 'typescript';
|
||||||
|
|
||||||
export function makeProgram(files: {name: string, contents: string}[]):
|
export function makeProgram(
|
||||||
{program: ts.Program, host: ts.CompilerHost} {
|
files: {name: string, contents: string}[],
|
||||||
|
options?: ts.CompilerOptions): {program: ts.Program, host: ts.CompilerHost} {
|
||||||
const host = new InMemoryHost();
|
const host = new InMemoryHost();
|
||||||
files.forEach(file => host.writeFile(file.name, file.contents));
|
files.forEach(file => host.writeFile(file.name, file.contents));
|
||||||
|
|
||||||
const rootNames = files.map(file => host.getCanonicalFileName(file.name));
|
const rootNames = files.map(file => host.getCanonicalFileName(file.name));
|
||||||
const program = ts.createProgram(
|
const program = ts.createProgram(
|
||||||
rootNames,
|
rootNames, {
|
||||||
{noLib: true, experimentalDecorators: true, moduleResolution: ts.ModuleResolutionKind.NodeJs},
|
noLib: true,
|
||||||
|
experimentalDecorators: true,
|
||||||
|
moduleResolution: ts.ModuleResolutionKind.NodeJs, ...options
|
||||||
|
},
|
||||||
host);
|
host);
|
||||||
const diags = [...program.getSyntacticDiagnostics(), ...program.getSemanticDiagnostics()];
|
const diags = [...program.getSyntacticDiagnostics(), ...program.getSemanticDiagnostics()];
|
||||||
if (diags.length > 0) {
|
if (diags.length > 0) {
|
||||||
throw new Error(
|
const errors = diags.map(diagnostic => {
|
||||||
`Typescript diagnostics failed! ${diags.map(diag => diag.messageText).join(', ')}`);
|
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};
|
return {program, host};
|
||||||
}
|
}
|
||||||
@ -39,7 +50,7 @@ export class InMemoryHost implements ts.CompilerHost {
|
|||||||
onError && onError(`File does not exist: ${this.getCanonicalFileName(fileName)})`);
|
onError && onError(`File does not exist: ${this.getCanonicalFileName(fileName)})`);
|
||||||
return undefined;
|
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'; }
|
getDefaultLibFileName(options: ts.CompilerOptions): string { return '/lib.d.ts'; }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user