Fixed out of spec behavior of array.reduce#731
Merged
Conversation
ark120202
reviewed
Oct 3, 2019
src/lualib/ArrayReduce.ts
Outdated
| @@ -1,20 +1,23 @@ | |||
| /** @vararg */ | |||
| interface VarArg<T> extends Array<T> {} | |||
Contributor
There was a problem hiding this comment.
Suggested change
| interface VarArg<T> extends Array<T> {} | |
| interface Vararg<T> extends Array<T> {} |
src/lualib/ArrayReduce.ts
Outdated
| let accumulator = initial; | ||
| if (initial === undefined) { | ||
| let accumulator = select(1, ...initial); | ||
| if (accumulator === undefined) { |
Contributor
There was a problem hiding this comment.
It doesn't look like that would fix that issue. The test case doesn't cover it, but:
['a', 'b'].reduce((a, b) => console.log(a, b));
// "a" "b"
['a', 'b'].reduce((a, b) => console.log(a, b), undefined);
// undefined "a"
// undefined "b"
src/lualib/ArrayReduce.ts
Outdated
| const len = arr.length; | ||
|
|
||
| if (len === 0 && initial === undefined) { | ||
| if (len === 0 && select("#", ...initial) === 0) { |
Contributor
There was a problem hiding this comment.
That's out of scope of this PR, but we probably want to transform vararg.length to select("#", ...) in general
ark120202
reviewed
Oct 5, 2019
src/lualib/ArrayReduce.ts
Outdated
| @@ -1,27 +1,35 @@ | |||
| /** @vararg */ | |||
| interface Vararg<T> extends Array<T> {} | |||
Contributor
There was a problem hiding this comment.
I think we should move it to something like declarations/tstl.d.ts
src/lualib/ArrayReduce.ts
Outdated
|
|
||
| if (len === 0 && initial === undefined) { | ||
| const initialValuePresent = select("#", ...initial) !== 0; | ||
| if (len === 0 && !initialValuePresent) { |
Contributor
There was a problem hiding this comment.
Thoughts on moving that check to a condition below and removing initialValuePresent variable?
test/unit/builtins/array.spec.ts
Outdated
|
|
||
| test("array.reduce undefined returning callback", () => { | ||
| util.testFunction` | ||
| const calls: {a: void, b: string}[] = []; |
Contributor
There was a problem hiding this comment.
Suggested change
| const calls: {a: void, b: string}[] = []; | |
| const calls: Array<{ a: void, b: string }> = []; |
ark120202
approved these changes
Oct 5, 2019
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #727