How to use @tslab/typescript-for-tslab - 10 common examples

To help you get started, we’ve selected a few @tslab/typescript-for-tslab examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github yunabe / tslab / src / tsapi.spec.ts View on Github external
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();
  });
github yunabe / tslab / src / converter.ts View on Github external
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;
  }
github yunabe / tslab / src / converter.ts View on Github external
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;
  }
github yunabe / tslab / src / converter.ts View on Github external
}
    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;
  }
github yunabe / tslab / src / converter.ts View on Github external
// 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;
  }
github yunabe / tslab / src / converter.ts View on Github external
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;
github yunabe / tslab / src / converter.ts View on Github external
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;
  }
github yunabe / tslab / src / converter.ts View on Github external
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("");
  }
github yunabe / tslab / src / converter.ts View on Github external
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);
github yunabe / tslab / src / converter.ts View on Github external
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;
}