Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename 'find' functions
  • Loading branch information
Andy Hanson committed Aug 15, 2016
commit 2eb159e269f79f1c7d86723b2a0d9c44739627fd
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,7 @@ namespace ts {
}

function getDeclarationOfAliasSymbol(symbol: Symbol): Declaration {
return find(symbol.declarations, d => isAliasSymbolDeclaration(d) ? d : undefined);
return findMap(symbol.declarations, d => isAliasSymbolDeclaration(d) ? d : undefined);
}

function getTargetOfImportEqualsDeclaration(node: ImportEqualsDeclaration): Symbol {
Expand Down
9 changes: 6 additions & 3 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ namespace ts {
}

/** Works like Array.prototype.find, returning `undefined` if no element satisfying the predicate is found. */
export function tryFind<T>(array: T[], predicate: (element: T, index: number) => boolean): T | undefined {
export function find<T>(array: T[], predicate: (element: T, index: number) => boolean): T | undefined {
for (let i = 0, len = array.length; i < len; i++) {
const value = array[i];
if (predicate(value, i)) {
Expand All @@ -120,8 +120,11 @@ namespace ts {
return undefined;
}

/** Like `forEach`, but assumes existence of array and fails if no truthy value is found. */
export function find<T, U>(array: T[], callback: (element: T, index: number) => U | undefined): U {
/**
* Returns the first truthy result of `callback`, or else fails.
* This is like `forEach`, but never returns undefined.
*/
export function findMap<T, U>(array: T[], callback: (element: T, index: number) => U | undefined): U {
for (let i = 0, len = array.length; i < len; i++) {
const result = callback(array[i], i);
if (result) {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2715,7 +2715,7 @@ namespace ts {

/** Return ".ts" or ".tsx" if that is the extension. */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

".d.ts"?

export function tryExtractTypeScriptExtension(fileName: string): string | undefined {
return tryFind(supportedTypescriptExtensionsWithDtsFirst, extension => fileExtensionIs(fileName, extension));
return find(supportedTypescriptExtensionsWithDtsFirst, extension => fileExtensionIs(fileName, extension));
}
// Must have '.d.ts' first because if '.ts' goes first, that will be detected as the extension instead of '.d.ts'.
const supportedTypescriptExtensionsWithDtsFirst = supportedTypeScriptExtensions.slice().reverse();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just change supportedTypeScriptExtensions ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

supportedTypeScriptExtensions is in order of file resolution precedence, meaning we want .d.ts to be tried last.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like something you probably want to make explicit in case anyone relies on that behavior.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

supportedTypescriptExtensions currently has a comment on it saying that it's in file resolution precedence. What else could we add?
I might want to change this code to just duplicate the list, though, because if anyone ever changes the order of supportedTypescriptExtensions, this one will no longer have .d.ts first.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that the list is in resolution order is independent of the fact that you're trying to give the longer extension a higher priority. Does that make sense?

Expand Down