Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it("wrong names", () => {
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(["wrong"]);
try {
converter.keepNamesInImport(stmt, names as Set);
fail("keepNamesInImport must fail.");
} catch (e) {
expect(String(e)).toEqual("Error: no symbol is included in names");
}
it("keep default remove 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(["mydefault"]);
converter.keepNamesInImport(stmt, names as Set);
let printer = ts.createPrinter();
expect(printer.printFile(src)).toEqual('import mydefault from "mylib";\n');
});
it("keep default and named import", () => {
const src = ts.createSourceFile(
"src.ts",
'import mydefault, {foo, bar as baz} from "mylib";',
ts.ScriptTarget.ES2019
);
const stmt = src.statements[0];
if (!ts.isImportDeclaration(stmt)) {
fail("stmt is not isImportDeclaration");
return;
}
const names = new Set(["mydefault", "baz"]);
converter.keepNamesInImport(stmt, names as Set);
let printer = ts.createPrinter();
expect(printer.printFile(src)).toEqual(
'import mydefault, { bar as baz } from "mylib";\n'
);
});