Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export async function generateConfig(inputFile: string): Promise {
const program: ts.Program = ts.createProgram([inputFile], {
target: ts.ScriptTarget.ES5,
downlevelIteration: true,
allowSyntheticDefaultImports: true,
lib: ['es6', 'esnext', 'es2015', 'dom'],
moduleResolution: ts.ModuleResolutionKind.NodeJs,
module: ts.ModuleKind.CommonJS,
});
const compilerDiagnostics: ReadonlyArray = program.getSemanticDiagnostics();
if (compilerDiagnostics.length > 0) {
for (const diagnostic of compilerDiagnostics) {
const message: string = ts.flattenDiagnosticMessageText(diagnostic.messageText, os.EOL);
if (diagnostic.file) {
const location: ts.LineAndCharacter = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start!);
const formattedMessage: string = `${diagnostic.file.fileName}(${location.line + 1},${location.character + 1}):` + ` [TypeScript] ${message}`;
// tslint:disable-next-line:no-console
console.error(formattedMessage);
reference(ref: Reference>): ts.Expression {
const ngExpr = this.refEmitter.emit(ref, this.contextFile);
// Use `translateExpression` to convert the `Expression` into a `ts.Expression`.
return translateExpression(
ngExpr, this.importManager, NOOP_DEFAULT_IMPORT_RECORDER, ts.ScriptTarget.ES2015);
}
transform(emitImport?: boolean): ts.SourceFile {
let file = ts.createSourceFile(this.owner.fileName, '', ts.ScriptTarget.Latest /*, false, ts.ScriptKind.TS */);
let templateCode = new TemplateTransformer(this, emitImport).code;
// let mainFile = new TemplateTransformer(this, emitImport).toSourceFile();
return ts.updateSourceFileNode(file, [
...templateCode.imports,
...this.dependencies.reduce((statements: ts.Statement[], dep) => {
return statements.concat(dep.toSourceFile().statements);
}, []),
templateCode.view
]);
}
function createTypescriptContext(content) {
// Set compiler options.
const compilerOptions = {
noEmitOnError: false,
allowJs: true,
newLine: ts.NewLineKind.LineFeed,
target: ts.ScriptTarget.ESNext,
skipLibCheck: true,
sourceMap: false,
importHelpers: true,
};
// Create compiler host.
const compilerHost = new compiler_host_1.WebpackCompilerHost(compilerOptions, basePath);
// Add a dummy file to host content.
compilerHost.writeFile(fileName, content, false);
// Create the TypeScript program.
const program = ts.createProgram([fileName], compilerOptions, compilerHost);
return { compilerHost, program };
}
exports.createTypescriptContext = createTypescriptContext;
export async function transpileToEs5Worker(_cwd: string, input: string, inlineHelpers: boolean) {
const results: d.TranspileResults = {
sourceFilePath: null,
code: input,
map: null,
diagnostics: [],
moduleFile: null,
build: {}
};
const transpileOpts: ts.TranspileOptions = {
compilerOptions: {
sourceMap: false,
allowJs: true,
declaration: false,
target: ts.ScriptTarget.ES5,
module: ts.ModuleKind.ESNext,
removeComments: false,
isolatedModules: true,
skipLibCheck: true,
noEmitHelpers: !inlineHelpers,
importHelpers: !inlineHelpers
}
};
const tsResults = ts.transpileModule(input, transpileOpts);
results.diagnostics.push(
...loadTypeScriptDiagnostics(tsResults.diagnostics)
);
if (results.diagnostics.length === 0) {
function Dependencies(files, options) {
this.__cache = {};
this.__nsModule = {};
this.unknown = '???';
this.files = files;
var transpileOptions = {
target: ts.ScriptTarget.ES5,
module: ts.ModuleKind.CommonJS,
tsconfigDirectory: options.tsconfigDirectory
};
this.program = ts.createProgram(this.files, transpileOptions, utilities_1.compilerHost(transpileOptions));
}
Dependencies.prototype.getDependencies = function () {
function compileTscriptCode(typescriptFilename: string): string {
const content = fs.readFileSync(typescriptFilename, "ascii");
const compilerOptions = {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2016,
skipLibCheck: true,
declaration: true,
sourceMap: true,
strict: true,
noImplicitAny: true,
noImplicitReturns: true
};
const res1 = ts.transpileModule(content, { compilerOptions, moduleName: "myModule2" });
const javascriptFilename = typescriptFilename.replace(/\.ts$/, ".js");
const sourcemapFilename = typescriptFilename.replace(/\.ts$/, ".js.map");
generateFileId(ctx);
lookupNode(ctx, ctx.file)
.getNestedNodes()
.map(n => lookupNode(ctx, n))
.forEach(n => generateNode(ctx, n));
ctx.concreteLists.forEach(([fullClassName, field]) =>
generateConcreteListInitializer(ctx, fullClassName, field)
);
const sourceFile = ts.createSourceFile(
ctx.tsPath,
"",
ts.ScriptTarget.Latest,
false,
ts.ScriptKind.TS
);
const printer = ts.createPrinter();
const source =
ctx.statements
.map(s => printer.printNode(ts.EmitHint.Unspecified, s, sourceFile))
.join("\n") + "\n";
return SOURCE_COMMENT + source;
}
function startFileWatcher(cliArgs: CliArgs) {
const fileSearch = new FileSearch(cliArgs.includes, cliArgs.excludes);
const allFilesInDirectory = fileSearch.getFiles(cliArgs.directory);
const program: ts.Program = ts.createProgram([...allFilesInDirectory],
{ target: ts.ScriptTarget.ES5, module: ts.ModuleKind.CommonJS });
const testFiles = fileSearch.getTestFiles(allFilesInDirectory, program);
const fileWatcherOptions: FileWatcherOptions = {
directory: cliArgs.directory,
files: testFiles
};
const fileWatcher = new FileWatcher(fileWatcherOptions);
fileWatcher.start();
fileWatcher.addListener(FileWatcherEvent.REBUILD, (fileName: string) => {
generateSingleFile({
directory: cliArgs.directory,
includes: cliArgs.includes,
excludes: cliArgs.excludes
}, fileName);
});
protected parse(source: string, fileName: string, options: IJsParseOptions = {}): IMessage[] {
let sourceFile = ts.createSourceFile(fileName, source, ts.ScriptTarget.Latest, true, options.scriptKind);
return this.parseNode(sourceFile, sourceFile, options.lineNumberStart || 1);
}