Skip to content

Commit

Permalink
feat: #400 - Symbol - Add global exports.
Browse files Browse the repository at this point in the history
This seems to be used with NamespaceExportDeclarations.
  • Loading branch information
dsherret committed Oct 6, 2018
1 parent 1f8b1cb commit 2bbeff9
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/compiler/symbols/Symbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,35 @@ export class Symbol {
return ArrayUtils.from(this.compilerSymbol.exports.values()).map(symbol => this.context.compilerFactory.getSymbol(symbol));
}

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

/**
* Gets the global export of the symbol by the specified name or returns undefined if not exists.
* @param name - Name of the global export.
*/
getGlobalExportByName(name: string): Symbol | undefined {
if (this.compilerSymbol.globalExports == null)
return undefined;

const tsSymbol = this.compilerSymbol.globalExports.get(name as ts.__String);
return tsSymbol == null ? undefined : this.context.compilerFactory.getSymbol(tsSymbol);
}

/**
* Gets the global exports from the symbol.
*/
getGlobalExports(): Symbol[] {
if (this.compilerSymbol.globalExports == null)
return [];
return ArrayUtils.from(this.compilerSymbol.globalExports.values()).map(symbol => this.context.compilerFactory.getSymbol(symbol));
}

/**
* Gets the member of the symbol by the specified name or throws if not exists.
* @param name - Name of the export.
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 @@ -65,6 +65,67 @@ describe(nameof(Symbol), () => {
});
});

describe(nameof<Symbol>(s => s.getGlobalExports), () => {
function doTest(code: string, exportNames: string[]) {
const { sourceFile } = getInfoFromText(code, { isDefinitionFile: true });
expect(sourceFile.getSymbolOrThrow().getGlobalExports().map(e => e.getName())).to.deep.equal(exportNames);
}

it("should get the global exports when they exist", () => {
doTest("export class MyName {}\nexport as namespace test;", ["test"]);
});

it("should not get the global exports when they don't exist", () => {
doTest("export class Test {}", []);
});
});

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

it("should get the global export when it exists", () => {
doTest("export class MyName {}\nexport as namespace test;", "test", "test");
});

it("should not get the export when it doesn't exist", () => {
doTest("export class MyName {}\nexport as namespace test;", "otherName", undefined);
});

it("should not get the export when not a valid file", () => {
doTest("export class MyName {}", "MyName", undefined);
});
});

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

it("should get the global export when it exists", () => {
doTest("export class MyName {}\nexport as namespace test;", "test", "test");
});

it("should not get the export when it doesn't exist", () => {
doTest("export class MyName {}\nexport as namespace test;", "otherName", undefined);
});

it("should not get the export when not a valid file", () => {
doTest("export class MyName {}", "MyName", undefined);
});
});

describe(nameof<Symbol>(s => s.getMembers), () => {
function doTest(code: string, expectedNames: string[]) {
const { sourceFile } = getInfoFromText(code);
Expand Down

0 comments on commit 2bbeff9

Please sign in to comment.