Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it("jsfile", () => {
// With .js suffix in fileName, ts.transpileModule handle the input as JS, not TS.
let out = ts.transpileModule('let x: string = "hello"', {
fileName: "src.js",
reportDiagnostics: true
});
expect(out.diagnostics.length).toEqual(1);
expect(out.diagnostics[0].messageText).toEqual(
"'types' can only be used in a .ts file."
);
expect(out.outputText).toEqual('var x = "hello";\n');
expect(out.sourceMapText).toBeUndefined();
});
const await: ts.Node = ts.tslab.findPrecedingToken(
d.start + d.length,
srcFile
);
if (await.kind !== ts.SyntaxKind.AwaitKeyword) {
// This must not happen, though.
return false;
}
let isTop = true;
let parent = await.parent;
while (isTop && parent && parent !== srcFile) {
switch (parent.kind) {
case ts.SyntaxKind.ArrowFunction:
case ts.SyntaxKind.FunctionExpression:
case ts.SyntaxKind.FunctionDeclaration:
case ts.SyntaxKind.ClassDeclaration:
case ts.SyntaxKind.ModuleDeclaration:
// await is not top-level. This is invalid in tslab.
return false;
}
parent = parent.parent;
}
return true;
}
return false;
}
const await: ts.Node = ts.tslab.findPrecedingToken(
d.start + d.length,
srcFile
);
if (await.kind !== ts.SyntaxKind.AwaitKeyword) {
// This must not happen, though.
return false;
}
let isTop = true;
let parent = await.parent;
while (isTop && parent && parent !== srcFile) {
switch (parent.kind) {
case ts.SyntaxKind.ArrowFunction:
case ts.SyntaxKind.FunctionExpression:
case ts.SyntaxKind.FunctionDeclaration:
case ts.SyntaxKind.ClassDeclaration:
case ts.SyntaxKind.ModuleDeclaration:
// await is not top-level. This is invalid in tslab.
return false;
}
parent = parent.parent;
}
return true;
}
}
const await: ts.Node = ts.tslab.findPrecedingToken(
d.start + d.length,
srcFile
);
if (await.kind !== ts.SyntaxKind.AwaitKeyword) {
// This must not happen, though.
return false;
}
let isTop = true;
let parent = await.parent;
while (isTop && parent && parent !== srcFile) {
switch (parent.kind) {
case ts.SyntaxKind.ArrowFunction:
case ts.SyntaxKind.FunctionExpression:
case ts.SyntaxKind.FunctionDeclaration:
case ts.SyntaxKind.ClassDeclaration:
case ts.SyntaxKind.ModuleDeclaration:
// await is not top-level. This is invalid in tslab.
return false;
}
parent = parent.parent;
}
return true;
}
// https://github.com/microsoft/TypeScript/search?q=await_expression_is_only_allowed_within_an_async_function_1308
return false;
}
const await: ts.Node = ts.tslab.findPrecedingToken(
d.start + d.length,
srcFile
);
if (await.kind !== ts.SyntaxKind.AwaitKeyword) {
// This must not happen, though.
return false;
}
let isTop = true;
let parent = await.parent;
while (isTop && parent && parent !== srcFile) {
switch (parent.kind) {
case ts.SyntaxKind.ArrowFunction:
case ts.SyntaxKind.FunctionExpression:
case ts.SyntaxKind.FunctionDeclaration:
case ts.SyntaxKind.ClassDeclaration:
case ts.SyntaxKind.ModuleDeclaration:
// await is not top-level. This is invalid in tslab.
return false;
}
parent = parent.parent;
}
return true;
}
function isTopLevelAwaitDiagnostic(
srcFile: ts.SourceFile,
d: ts.Diagnostic
): boolean {
if (d.code !== 1308) {
// https://github.com/microsoft/TypeScript/search?q=await_expression_is_only_allowed_within_an_async_function_1308
return false;
}
const await: ts.Node = ts.tslab.findPrecedingToken(
d.start + d.length,
srcFile
);
if (await.kind !== ts.SyntaxKind.AwaitKeyword) {
// This must not happen, though.
return false;
}
let isTop = true;
let parent = await.parent;
while (isTop && parent && parent !== srcFile) {
switch (parent.kind) {
case ts.SyntaxKind.ArrowFunction:
case ts.SyntaxKind.FunctionExpression:
case ts.SyntaxKind.FunctionDeclaration:
case ts.SyntaxKind.ClassDeclaration:
case ts.SyntaxKind.ModuleDeclaration:
// await is not top-level. This is invalid in tslab.
return false;
}
parent = parent.parent;
d.start + d.length,
srcFile
);
if (await.kind !== ts.SyntaxKind.AwaitKeyword) {
// This must not happen, though.
return false;
}
let isTop = true;
let parent = await.parent;
while (isTop && parent && parent !== srcFile) {
switch (parent.kind) {
case ts.SyntaxKind.ArrowFunction:
case ts.SyntaxKind.FunctionExpression:
case ts.SyntaxKind.FunctionDeclaration:
case ts.SyntaxKind.ClassDeclaration:
case ts.SyntaxKind.ModuleDeclaration:
// await is not top-level. This is invalid in tslab.
return false;
}
parent = parent.parent;
}
return true;
}
if (!names.has(decl.name.escapedText)) {
return;
}
decls.push(decl);
});
stmt.declarationList.declarations = ts.createNodeArray(decls);
}
if (ts.isImportDeclaration(stmt)) {
keepNamesInImport(stmt, names);
}
// Do nothing for
// - TypeAliasDeclaration (No multiple specs)
// - FunctionDeclaration (ditto)
// - InterfaceDeclaration (ditto)
});
declsSF.statements = ts.createNodeArray(statements);
let printer = ts.createPrinter();
let anyVarsDecls: string[] = [];
anyVars.forEach(name => {
anyVarsDecls.push(`let ${name}: any;\n`);
});
return printer.printFile(declsSF) + anyVarsDecls.join("");
}
return;
}
statements.push(stmt);
if (ts.isVariableStatement(stmt)) {
const decls: ts.VariableDeclaration[] = [];
stmt.declarationList.declarations.forEach(decl => {
if (!ts.isIdentifier(decl.name)) {
// This must not happen.
return;
}
if (!names.has(decl.name.escapedText)) {
return;
}
decls.push(decl);
});
stmt.declarationList.declarations = ts.createNodeArray(decls);
}
if (ts.isImportDeclaration(stmt)) {
keepNamesInImport(stmt, names);
}
// Do nothing for
// - TypeAliasDeclaration (No multiple specs)
// - FunctionDeclaration (ditto)
// - InterfaceDeclaration (ditto)
});
declsSF.statements = ts.createNodeArray(statements);
export function esModuleToCommonJSModule(
js: string,
target: ts.ScriptTarget
): string {
let out = ts.transpileModule(js, {
fileName: "custom.js",
compilerOptions: {
module: ts.ModuleKind.CommonJS,
esModuleInterop: true,
target,
newLine: ts.NewLineKind.LineFeed,
// Remove 'use strict' from outputs.
noImplicitUseStrict: true
}
}).outputText;
return out;
}