Skip to content

Commit

Permalink
feat: Add Symbol#getAliasedSymbolOrThrow() and Symbol#getExportByName…
Browse files Browse the repository at this point in the history
…OrThrow(name: string).
  • Loading branch information
dsherret committed Jul 2, 2018
1 parent 0c868e9 commit 78c7ea3
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/compiler/common/Symbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,14 @@ export class Symbol {
}

/**
* Gets the aliased symbol.
* Gets the aliased symbol or throws if it doesn't exist.
*/
getAliasedSymbolOrThrow(): Symbol {
return errors.throwIfNullOrUndefined(this.getAliasedSymbol(), "Expected to find an aliased symbol.");
}

/**
* Gets the aliased symbol or returns undefined if it doesn't exist.
*/
getAliasedSymbol(): Symbol | undefined {
return this.global.typeChecker.getAliasedSymbol(this);
Expand Down Expand Up @@ -102,7 +109,15 @@ export class Symbol {
}

/**
* Get the exports of the symbol.
* Get the export of the symbol by the specified name or throws if not exists.
* @param name - Name of the export.
*/
getExportByNameOrThrow(name: string): Symbol {
return errors.throwIfNullOrUndefined(this.getExportByName(name), `Expected to find export with name: ${name}`);
}

/**
* Get the export of the symbol by the specified name or returns undefined if not exists.
* @param name - Name of the export.
*/
getExportByName(name: string): Symbol | undefined {
Expand Down
61 changes: 61 additions & 0 deletions src/tests/compiler/symbol/symbolTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,65 @@ describe(nameof(Symbol), () => {
expect(enumNameNodeSymbol.getFlags()).to.equal(SymbolFlags.RegularEnum);
});
});

describe(nameof<Symbol>(s => s.getExportByName), () => {
function doTest(code: string, exportName: string, expectedName: string | undefined) {
const { sourceFile } = getInfoFromText(code);
const exportSymbol = sourceFile.getSymbolOrThrow().getExportByName(exportName);
if (expectedName == null)
expect(exportSymbol).to.be.undefined;
else
expect(exportSymbol!.getName()).to.equal(expectedName);
}

it("should get the export when it exists", () => {
doTest("export class MyName {}", "MyName", "MyName");
});

it("should not get the export when it doesn't exist", () => {
doTest("export class MyName {}", "MyName2", undefined);
});
});

describe(nameof<Symbol>(s => s.getExportByNameOrThrow), () => {
function doTest(code: string, exportName: string, expectedName: string | undefined) {
const { sourceFile } = getInfoFromText(code);
if (expectedName == null)
expect(() => sourceFile.getSymbolOrThrow().getExportByNameOrThrow(exportName)).to.throw();
else
expect(sourceFile.getSymbolOrThrow().getExportByNameOrThrow(exportName).getName()).to.equal(expectedName);
}

it("should get the export when it exists", () => {
doTest("export class MyName {}", "MyName", "MyName");
});

it("should not get the export when it doesn't exist", () => {
doTest("export class MyName {}", "MyName2", undefined);
});
});

describe(nameof<Symbol>(s => s.getAliasedSymbol), () => {
it("should get the aliased symbol when it exists", () => {
const { sourceFile } = getInfoFromText("class MyTest {}\nexport default MyTest;");
expect(sourceFile.getDefaultExportSymbolOrThrow().getAliasedSymbol()!.getName()).to.equal("MyTest");
});

it("should return undefined when it doesn't exist", () => {
const { sourceFile } = getInfoFromText("class MyTest {}\nexport default MyTest;");
expect(sourceFile.getClassOrThrow("MyTest").getSymbolOrThrow().getAliasedSymbol()).to.be.undefined;
});
});

describe(nameof<Symbol>(s => s.getAliasedSymbolOrThrow), () => {
it("should get the aliased symbol when it exists", () => {
const { sourceFile } = getInfoFromText("class MyTest {}\nexport default MyTest;");
expect(sourceFile.getDefaultExportSymbolOrThrow().getAliasedSymbolOrThrow().getName()).to.equal("MyTest");
});

it("should throw when it doesn't exist", () => {
const { sourceFile } = getInfoFromText("class MyTest {}\nexport default MyTest;");
expect(() => sourceFile.getClassOrThrow("MyTest").getSymbolOrThrow().getAliasedSymbolOrThrow()).to.throw();
});
});
});

0 comments on commit 78c7ea3

Please sign in to comment.