How to use the @tslab/typescript-for-tslab.isImportDeclaration function in @tslab/typescript-for-tslab

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 / converter.ts View on Github external
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
}
        if (ts.isClassDeclaration(node) || ts.isEnumDeclaration(node)) {
          if (keep.type) {
            if (keep.value) {
              addName(node, key);
            }
            // If !keep.value, forget this class.
            return;
          }
          // keep.value === true
          if (!valueNames.has(node.name.escapedText)) {
            anyVars.add(node.name.escapedText);
          }
          return;
        }
        if (ts.isImportDeclaration(node)) {
          if (keep.type && keep.value) {
            addName(node, key);
            return;
          }
          let aliased = checker.getAliasedSymbol(sym);
          if (!keep.value) {
            // Here, keep.type == true.
            if (aliased.flags & ts.SymbolFlags.Value) {
              // Overwritten with a new value.
              return;
            }
            if (aliased.flags && ts.SymbolFlags.Type) {
              addName(node, key);
            }
            return;
          }
github yunabe / tslab / src / converter.spec.ts View on Github external
it("keep namespace", () => {
    const src = ts.createSourceFile(
      "src.ts",
      'import mydefault, * as ns from "mylib";',
      ts.ScriptTarget.ES2019
    );
    const stmt = src.statements[0];
    if (!ts.isImportDeclaration(stmt)) {
      fail("stmt is not isImportDeclaration");
      return;
    }
    const names = new Set(["ns"]);
    converter.keepNamesInImport(stmt, names as Set);
    let printer = ts.createPrinter();
    expect(printer.printFile(src)).toEqual('import * as ns from "mylib";\n');
  });