Skip to content

Commit

Permalink
feat: Add ImportDeclaration.removeDefaultImport()
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Sep 20, 2018
1 parent 51fc63d commit 87dd9cf
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
1 change: 1 addition & 0 deletions 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.removeDefaultImport();
```

#### Example
Expand Down
37 changes: 31 additions & 6 deletions src/compiler/file/ImportDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,7 @@ export class ImportDeclaration extends Statement<ts.ImportDeclaration> {
const importClause = this.getImportClause();
if (importClause == null)
return undefined;
const firstChild = importClause.getFirstChild();
if (firstChild == null || firstChild.getKind() !== SyntaxKind.Identifier)
return undefined;
return firstChild as Identifier;
return importClause.getNodeProperty("name");
}

/**
Expand Down Expand Up @@ -184,6 +181,34 @@ export class ImportDeclaration extends Statement<ts.ImportDeclaration> {
}
}

/**
* Removes the default import.
*/
removeDefaultImport() {
const importClause = this.getImportClause();
if (importClause == null)
return this;
const defaultImport = importClause.getNodeProperty("name");
if (defaultImport == null)
return;

const hasOnlyDefaultImport = importClause.getChildCount() === 1;
if (hasOnlyDefaultImport)
removeChildren({
children: [importClause, importClause.getNextSiblingIfKindOrThrow(SyntaxKind.FromKeyword)],
removePrecedingSpaces: true,
removePrecedingNewLines: true
});
else
removeChildren({
children: [defaultImport, defaultImport.getNextSiblingIfKindOrThrow(SyntaxKind.CommaToken)],
removePrecedingSpaces: true,
removePrecedingNewLines: true
});

return this;
}

/**
* Gets the namespace import if it exists or throws.
*/
Expand Down Expand Up @@ -337,14 +362,14 @@ export class ImportDeclaration extends Statement<ts.ImportDeclaration> {
/**
* Gets the import clause or throws if it doesn't exist.
*/
getImportClauseOrThrow(): Node {
getImportClauseOrThrow() {
return errors.throwIfNullOrUndefined(this.getImportClause(), "Expected to find an import clause.");
}

/**
* Gets the import clause or returns undefined if it doesn't exist.
*/
getImportClause(): Node | undefined {
getImportClause() {
return this.getNodeFromCompilerNodeIfExists(this.compilerNode.importClause);
}

Expand Down
26 changes: 25 additions & 1 deletion src/tests/compiler/file/importDeclarationTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,31 @@ describe(nameof(ImportDeclaration), () => {
});
});

describe(nameof<ImportDeclaration>(n => n.getNamespaceImportOrThrow), () => {
describe(nameof<ImportDeclaration>(n => n.removeDefaultImport), () => {
function doTest(text: string, expected: string) {
const { firstChild, sourceFile } = getInfoFromText<ImportDeclaration>(text);
firstChild.removeDefaultImport();
expect(sourceFile.getText()).to.equal(expected);
}

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

it("should do nothing when it doesn't exists", () => {
doTest(`import './file';`, `import './file';`);
});

it("should remove it when a namespace import exists", () => {
doTest(`import name, * as identifier from './file';`, `import * as identifier from './file';`);
});

it("should remove it when named imports exists", () => {
doTest(`import name, { test } from './file';`, `import { test } from './file';`);
});
});

describe(nameof<ImportDeclaration>(n => n.removeNamespaceImport), () => {
function doTest(text: string, expected: string) {
const { firstChild, sourceFile } = getInfoFromText<ImportDeclaration>(text);
firstChild.removeNamespaceImport();
Expand Down

0 comments on commit 87dd9cf

Please sign in to comment.