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
Merge branch 'master' into no_ts_extension
  • Loading branch information
Andy Hanson committed Aug 15, 2016
commit 3de8c221969a00cfcfae952cdc056eaf91311c9e
15 changes: 13 additions & 2 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ namespace ts {
return undefined;
}

/** Works like Array.prototype.find. */
export function find<T>(array: T[], predicate: (element: T, index: number) => boolean): T | undefined {
/** 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 {
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I expected find to have the type that tryFind has right now. Why does find take callback and not predicate?
I forgot to mention it when I ran into it last week while trying to use find.

Copy link
Author

Choose a reason for hiding this comment

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

If we rename tryFind to find, what should we call find? mustFind?

Copy link
Member

Choose a reason for hiding this comment

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

findMap? Let me go see if this function is in hoogle...

Copy link
Member

Choose a reason for hiding this comment

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

It's the equivalent of bind (>>=) or something like Data.Foldable.concatMap except that it takes Array<T> to Exception<T>. So, yeah, findMap is not bad, or mapFirst or mapSingle.

for (let i = 0, len = array.length; i < len; i++) {
const value = array[i];
if (predicate(value, i)) {
Expand All @@ -120,6 +120,17 @@ 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 {
for (let i = 0, len = array.length; i < len; i++) {
const result = callback(array[i], i);
if (result) {
return result;
}
}
Debug.fail();
}

export function contains<T>(array: T[], value: T): boolean {
if (array) {
for (const v of array) {
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 find(supportedTypescriptExtensionsWithDtsFirst, extension => fileExtensionIs(fileName, extension));
return tryFind(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
2 changes: 1 addition & 1 deletion src/harness/compilerRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class CompilerBaselineRunner extends RunnerBase {
private makeUnitName(name: string, root: string) {
const path = ts.toPath(name, root, (fileName) => Harness.Compiler.getCanonicalFileName(fileName));
const pathStart = ts.toPath(Harness.IO.getCurrentDirectory(), "", (fileName) => Harness.Compiler.getCanonicalFileName(fileName));
return pathStart === "" ? path : path.replace(pathStart, "/");
return pathStart ? path.replace(pathStart, "/") : path;
};

public checkTestCodeOutput(fileName: string) {
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.