Skip to content

Commit

Permalink
refactor: ImportDeclaration.setDefaultImport should not rename.
Browse files Browse the repository at this point in the history
BREAKING CHANGE: ImportDeclaration.setDefaultImport no longer renames the identifier. Use `.renameDefaultImport` instead.
  • Loading branch information
dsherret committed Sep 20, 2018
1 parent f81f90a commit f425bd8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
3 changes: 2 additions & 1 deletion docs/details/imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Set it:

```ts
importDeclaration.setDefaultImport("MyClass");
importDeclaration.renameDefaultImport("MyClass2");
importDeclaration.removeDefaultImport();
```

Expand All @@ -97,7 +98,7 @@ Doing the following:

```ts
const importDeclaration = sourceFile.getImportDeclarations()[0];
importDeclaration.setDefaultImport("NewName");
importDeclaration.renameDefaultImport("NewName");
````

Will rename the default import and all its usages:
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/file/ImportDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,15 @@ export class ImportDeclaration extends Statement<ts.ImportDeclaration> {
/**
* Sets the default import.
* @param text - Text to set as the default import.
* @remarks Use renameDefaultImport to rename.
*/
setDefaultImport(text: string) {
if (StringUtils.isNullOrWhitespace(text))
return this.removeDefaultImport();

const defaultImport = this.getDefaultImport();
if (defaultImport != null) {
defaultImport.rename(text);
defaultImport.replaceWithText(text);
return this;
}

Expand Down
6 changes: 3 additions & 3 deletions src/tests/compiler/file/importDeclarationTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@ describe(nameof(ImportDeclaration), () => {
}

it("should remove when empty", () => {
doTest(`import identifier from './file'; const t = identifier;`, "", `import './file'; const t = newName;`);
doTest(`import identifier from './file'; const t = identifier;`, "", `import './file'; const t = identifier;`);
});

it("should rename when exists", () => {
doTest(`import identifier from './file'; const t = identifier;`, "newName", `import newName from './file'; const t = newName;`);
it("should not rename when exists", () => {
doTest(`import identifier from './file'; const t = identifier;`, "newName", `import newName from './file'; const t = identifier;`);
});

it("should set the default import when importing for side effects", () => {
Expand Down

0 comments on commit f425bd8

Please sign in to comment.