Skip to content

Commit

Permalink
feat: #606 - Symbol#getXByName-like methods are now Symbol#getX.
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `Symbol#getXByName`-like methods were renamed to `Symbol#getX`.
  • Loading branch information
dsherret committed Apr 10, 2019
1 parent d1c361a commit 08f0710
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 33 deletions.
11 changes: 11 additions & 0 deletions breaking-changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ Methods like `project.emit()` are now async. Corresponding `emitSync` methods ha

The new async emit methods should be much faster than the previous synchronous methods.

### Renamed `Symbol#getXByName`-like methods to `#getX`

The `ByName` suffix was overly verbose, so I removed it. The following methods have been renamed:

* `Symbol#getExportByName(name: string)` -> `getExport`
* `Symbol#getExportByNameOrThrow(name: string)` -> `getExportOrThrow`
* `Symbol#getGlobalExportByName(name: string)` -> `getGlobalExport`
* `Symbol#getGlobalExportByNameOrThrow(name: string)` -> `getGlobalExportOrThrow`
* `Symbol#getMemberByName(name: string)` -> `getMember`
* `Symbol#getMemberByNameOrThrow(name: string)` -> `getMemberOrThrow`

### FileSystemHost changes

The `FileSystemHost` interface now requires implementing `realpathSync()` and `isCaseSensitive()`.
Expand Down
12 changes: 6 additions & 6 deletions lib/ts-morph.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8830,12 +8830,12 @@ export declare class Symbol {
* Gets the export of the symbol by the specified name or throws if not exists.
* @param name - Name of the export.
*/
getExportByNameOrThrow(name: string): Symbol;
getExportOrThrow(name: string): Symbol;
/**
* Gets 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;
getExport(name: string): Symbol | undefined;
/**
* Gets the exports from the symbol.
*/
Expand All @@ -8844,12 +8844,12 @@ export declare class 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;
getGlobalExportOrThrow(name: string): Symbol;
/**
* 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;
getGlobalExport(name: string): Symbol | undefined;
/**
* Gets the global exports from the symbol.
*/
Expand All @@ -8858,12 +8858,12 @@ export declare class Symbol {
* Gets the member of the symbol by the specified name or throws if not exists.
* @param name - Name of the export.
*/
getMemberByNameOrThrow(name: string): Symbol;
getMemberOrThrow(name: string): Symbol;
/**
* Gets the member of the symbol by the specified name or returns undefined if not exists.
* @param name - Name of the member.
*/
getMemberByName(name: string): Symbol | undefined;
getMember(name: string): Symbol | undefined;
/**
* Gets the members of the symbol
*/
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/ast/base/ModuledNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ export function ModuledNode<T extends Constructor<ModuledNodeExtensionType>>(Bas
if (sourceFileSymbol == null)
return undefined;

return sourceFileSymbol.getExportByName("default");
return sourceFileSymbol.getExport("default");
}

getDefaultExportSymbolOrThrow(): Symbol {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/ast/base/export/ExportGetableNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export function ExportGetableNode<T extends Constructor<ExportGetableNodeExtensi
return !isDefaultExport() && sourceFileSymbol.getExports().some(e => e === thisSymbol || e.getAliasedSymbol() === thisSymbol);

function isDefaultExport() {
const defaultExportSymbol = sourceFileSymbol!.getExportByName("default");
const defaultExportSymbol = sourceFileSymbol!.getExport("default");
if (defaultExportSymbol == null)
return false;
return thisSymbol === defaultExportSymbol || thisSymbol === defaultExportSymbol.getAliasedSymbol();
Expand Down
18 changes: 9 additions & 9 deletions src/compiler/symbols/Symbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ export class Symbol {
* Gets 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}`);
getExportOrThrow(name: string): Symbol {
return errors.throwIfNullOrUndefined(this.getExport(name), `Expected to find export with name: ${name}`);
}

/**
* Gets 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 {
getExport(name: string): Symbol | undefined {
if (this.compilerSymbol.exports == null)
return undefined;

Expand All @@ -141,15 +141,15 @@ export class 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}`);
getGlobalExportOrThrow(name: string): Symbol {
return errors.throwIfNullOrUndefined(this.getGlobalExport(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 {
getGlobalExport(name: string): Symbol | undefined {
if (this.compilerSymbol.globalExports == null)
return undefined;

Expand All @@ -170,15 +170,15 @@ export class Symbol {
* Gets the member of the symbol by the specified name or throws if not exists.
* @param name - Name of the export.
*/
getMemberByNameOrThrow(name: string): Symbol {
return errors.throwIfNullOrUndefined(this.getMemberByName(name), `Expected to find member with name: ${name}`);
getMemberOrThrow(name: string): Symbol {
return errors.throwIfNullOrUndefined(this.getMember(name), `Expected to find member with name: ${name}`);
}

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

Expand Down
32 changes: 16 additions & 16 deletions src/tests/compiler/symbols/symbolTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ describe(nameof(Symbol), () => {
});
});

describe(nameof<Symbol>(s => s.getExportByName), () => {
describe(nameof<Symbol>(s => s.getExport), () => {
function doTest(code: string, exportName: string, expectedName: string | undefined) {
const { sourceFile } = getInfoFromText(code);
const exportSymbol = sourceFile.getSymbolOrThrow().getExportByName(exportName);
const exportSymbol = sourceFile.getSymbolOrThrow().getExport(exportName);
if (expectedName == null)
expect(exportSymbol).to.be.undefined;
else
Expand All @@ -47,13 +47,13 @@ describe(nameof(Symbol), () => {
});
});

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

it("should get the export when it exists", () => {
Expand All @@ -80,10 +80,10 @@ describe(nameof(Symbol), () => {
});
});

describe(nameof<Symbol>(s => s.getGlobalExportByName), () => {
describe(nameof<Symbol>(s => s.getGlobalExport), () => {
function doTest(code: string, exportName: string, expectedName: string | undefined) {
const { sourceFile } = getInfoFromText(code, { isDefinitionFile: true });
const exportSymbol = sourceFile.getSymbolOrThrow().getGlobalExportByName(exportName);
const exportSymbol = sourceFile.getSymbolOrThrow().getGlobalExport(exportName);
if (expectedName == null)
expect(exportSymbol).to.be.undefined;
else
Expand All @@ -103,14 +103,14 @@ describe(nameof(Symbol), () => {
});
});

describe(nameof<Symbol>(s => s.getGlobalExportByNameOrThrow), () => {
describe(nameof<Symbol>(s => s.getGlobalExportOrThrow), () => {
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();
expect(() => symbol.getGlobalExportOrThrow(exportName)).to.throw();
else
expect(symbol.getGlobalExportByNameOrThrow(exportName).getName()).to.equal(expectedName);
expect(symbol.getGlobalExportOrThrow(exportName).getName()).to.equal(expectedName);
}

it("should get the global export when it exists", () => {
Expand Down Expand Up @@ -139,15 +139,15 @@ describe(nameof(Symbol), () => {
});
});

describe(nameof<Symbol>(s => s.getMemberByName), () => {
describe(nameof<Symbol>(s => s.getMember), () => {
function doTest(code: string, name: string, expectedName: string | undefined) {
const { sourceFile } = getInfoFromText(code);
const typeAlias = sourceFile.getTypeAliasOrThrow("myType");
const symbol = typeAlias.getType().getSymbolOrThrow();
if (expectedName == null)
expect(symbol.getMemberByName(name)).to.be.undefined;
expect(symbol.getMember(name)).to.be.undefined;
else
expect(symbol.getMemberByName(name)!.getName()).to.equal(expectedName);
expect(symbol.getMember(name)!.getName()).to.equal(expectedName);
}

it("should get the member when it exists", () => {
Expand All @@ -159,15 +159,15 @@ describe(nameof(Symbol), () => {
});
});

describe(nameof<Symbol>(s => s.getMemberByNameOrThrow), () => {
describe(nameof<Symbol>(s => s.getMemberOrThrow), () => {
function doTest(code: string, name: string, expectedName: string | undefined) {
const { sourceFile } = getInfoFromText(code);
const typeAlias = sourceFile.getTypeAliasOrThrow("myType");
const symbol = typeAlias.getType().getSymbolOrThrow();
if (expectedName == null)
expect(() => symbol.getMemberByNameOrThrow(name)).to.throw();
expect(() => symbol.getMemberOrThrow(name)).to.throw();
else
expect(symbol.getMemberByNameOrThrow(name).getName()).to.equal(expectedName);
expect(symbol.getMemberOrThrow(name).getName()).to.equal(expectedName);
}

it("should get the member when it exists", () => {
Expand Down

0 comments on commit 08f0710

Please sign in to comment.