Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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);
}
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;
}
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');
});