Skip to content

Commit

Permalink
feat: Add ImportDeclaration.getImportClause() and .getImportClauseOrT…
Browse files Browse the repository at this point in the history
…hrow()

These methods aren't so useful, but adding them anyway.
  • Loading branch information
dsherret committed May 8, 2018
1 parent 0250138 commit 8d99c27
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/compiler/file/ImportDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as errors from "../../errors";
import { ImportSpecifierStructure } from "../../structures";
import { insertIntoParentTextRange, verifyAndGetIndex, insertIntoCommaSeparatedNodes, removeChildren, getNodesToReturn } from "../../manipulation";
import { ArrayUtils, TypeGuards, StringUtils, ModuleUtils } from "../../utils";
import { Identifier } from "../common";
import { Node, Identifier } from "../common";
import { Statement } from "../statement";
import { StringLiteral } from "../literal";
import { ImportSpecifier } from "./ImportSpecifier";
Expand Down Expand Up @@ -333,13 +333,17 @@ export class ImportDeclaration extends Statement<ts.ImportDeclaration> {
return this;
}

private getImportClauseOrThrow() {
// todo: make public?
/**
* Gets the import clause or throws if it doesn't exist.
*/
getImportClauseOrThrow(): Node {
return errors.throwIfNullOrUndefined(this.getImportClause(), "Expected to find an import clause.");
}

private getImportClause() {
// todo: make public?
/**
* Gets the import clause or returns undefined if it doesn't exist.
*/
getImportClause(): Node | undefined {
return this.getNodeFromCompilerNodeIfExists(this.compilerNode.importClause);
}
}
36 changes: 36 additions & 0 deletions src/tests/compiler/file/importDeclarationTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,4 +475,40 @@ describe(nameof(ImportDeclaration), () => {
doTest(`import defaultExport, {Name1, Name2} from "module-name";`, `import defaultExport from "module-name";`);
});
});

describe(nameof<ImportDeclaration>(n => n.getImportClauseOrThrow), () => {
function doTest(text: string, expectedName: string | undefined) {
const { firstChild } = getInfoFromText<ImportDeclaration>(text);
if (expectedName == null)
expect(() => firstChild.getImportClauseOrThrow()).to.throw();
else
expect(firstChild.getImportClauseOrThrow().getText()).to.equal(expectedName);
}

it("should get the import clause when it exists", () => {
doTest(`import * as name from "./test";`, "* as name");
});

it("should throw when the import clause doesn't exist", () => {
doTest(`import "./test";`, undefined);
});
});

describe(nameof<ImportDeclaration>(n => n.getImportClause), () => {
function doTest(text: string, expectedName: string | undefined) {
const { firstChild } = getInfoFromText<ImportDeclaration>(text);
if (expectedName == null)
expect(firstChild.getImportClause()).to.be.undefined;
else
expect(firstChild.getImportClause()!.getText()).to.equal(expectedName);
}

it("should get the import clause when it exists", () => {
doTest(`import * as name from "./test";`, "* as name");
});

it("should return undefined when the import clause doesn't exist", () => {
doTest(`import "./test";`, undefined);
});
});
});

0 comments on commit 8d99c27

Please sign in to comment.