Skip to content

Commit

Permalink
feat: ImportSpecifier and ExportSpecifier - Add removeAliasWithRename()
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Sep 18, 2018
1 parent b594113 commit 576db34
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/compiler/file/ExportSpecifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class ExportSpecifier extends Node<ts.ExportSpecifier> {

/**
* Removes the alias without renaming.
* @remarks Use removeAliasWithRename() if you want it to rename any usages to the name of the export specifier.
*/
removeAlias() {
const aliasIdentifier = this.getAliasNode();
Expand All @@ -79,6 +80,20 @@ export class ExportSpecifier extends Node<ts.ExportSpecifier> {
return this;
}

/**
* Removes the alias and renames any usages to the name of the export specifier.
*/
removeAliasWithRename() {
const aliasIdentifier = this.getAliasNode();
if (aliasIdentifier == null)
return this;

aliasIdentifier.rename(this.getName());
this.removeAlias();

return this;
}

/**
* Gets the alias identifier, if it exists.
*/
Expand Down
15 changes: 15 additions & 0 deletions src/compiler/file/ImportSpecifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class ImportSpecifier extends Node<ts.ImportSpecifier> {

/**
* Removes the alias without renaming.
* @remarks Use removeAliasWithRename() if you want it to rename any usages to the name of the import specifier.
*/
removeAlias() {
const aliasIdentifier = this.getAliasNode();
Expand All @@ -79,6 +80,20 @@ export class ImportSpecifier extends Node<ts.ImportSpecifier> {
return this;
}

/**
* Removes the alias and renames any usages to the name of the import specifier.
*/
removeAliasWithRename() {
const aliasIdentifier = this.getAliasNode();
if (aliasIdentifier == null)
return this;

aliasIdentifier.rename(this.getName());
this.removeAlias();

return this;
}

/**
* Gets the alias identifier, if it exists.
*/
Expand Down
22 changes: 22 additions & 0 deletions src/tests/compiler/file/exportSpecifierTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,28 @@ describe(nameof(ExportSpecifier), () => {
});
});

describe(nameof<ExportSpecifier>(n => n.removeAliasWithRename), () => {
function doTest(text: string, expected: string, expectedImportName: string) {
const { sourceFile, project } = getInfoFromText<ExportDeclaration>(text);
const otherSourceFile = project.createSourceFile("file.ts", "export class name {}");
const importingFile = project.createSourceFile("importingFile.ts", `import { name } from './testFile';`);
const namedImport = sourceFile.getExportDeclarations()[0].getNamedExports()[0];
namedImport.removeAliasWithRename();
expect(sourceFile.getText()).to.equal(expected);
expect(importingFile.getImportDeclarations()[0].getNamedImports()[0].getName()).to.equal(expectedImportName);
expect(otherSourceFile.getText()).to.equal("export class name {}");
}

it("should do nothing when there is no alias", () => {
doTest("export {name} from './file';", "export {name} from './file';", "name");
});

it("should be remove and update the current file when there is an alias", () => {
doTest("import {name as alias} from './file'; export { alias as name};",
"import {name as alias} from './file'; export { alias};", "alias");
});
});

function setupLocalTargetSymbolTest() {
const project = getProject();
const mainFile = project.createSourceFile("main.ts", `export { MyClass, OtherClass } from "./MyClass";`);
Expand Down
19 changes: 19 additions & 0 deletions src/tests/compiler/file/importSpecifierTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,25 @@ describe(nameof(ImportSpecifier), () => {
});
});

describe(nameof<ImportSpecifier>(n => n.removeAliasWithRename), () => {
function doTest(text: string, expected: string) {
const { firstChild, sourceFile, project } = getInfoFromText<ImportDeclaration>(text);
const otherSourceFile = project.createSourceFile("file.ts", "export class name {}");
const namedImport = firstChild.getNamedImports()[0];
namedImport.removeAliasWithRename();
expect(sourceFile.getText()).to.equal(expected);
expect(otherSourceFile.getText()).to.equal("export class name {}");
}

it("should do nothing when there is no alias", () => {
doTest("import {name} from './file';", "import {name} from './file';");
});

it("should be remove and update the current file when there is an alias", () => {
doTest("import {name as alias} from './file'; const t = alias;", "import {name} from './file'; const t = name;");
});
});

describe(nameof<ImportSpecifier>(n => n.getAliasNode), () => {
function doTest(text: string, alias: string | undefined) {
const {firstChild} = getInfoFromText<ImportDeclaration>(text);
Expand Down

0 comments on commit 576db34

Please sign in to comment.