Skip to content

Fixed out of spec behavior of array.reduce#731

Merged
Perryvw merged 6 commits into
masterfrom
feature/reduce-undefined-initial-argument
Oct 5, 2019
Merged

Fixed out of spec behavior of array.reduce#731
Perryvw merged 6 commits into
masterfrom
feature/reduce-undefined-initial-argument

Conversation

@Perryvw
Copy link
Copy Markdown
Member

@Perryvw Perryvw commented Oct 3, 2019

Fixes #727

@Perryvw Perryvw requested a review from ark120202 October 3, 2019 20:12
Comment thread src/lualib/ArrayReduce.ts Outdated
@@ -1,20 +1,23 @@
/** @vararg */
interface VarArg<T> extends Array<T> {}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
interface VarArg<T> extends Array<T> {}
interface Vararg<T> extends Array<T> {}

Comment thread src/lualib/ArrayReduce.ts Outdated
let accumulator = initial;
if (initial === undefined) {
let accumulator = select(1, ...initial);
if (accumulator === undefined) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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"

Comment thread src/lualib/ArrayReduce.ts Outdated
const len = arr.length;

if (len === 0 && initial === undefined) {
if (len === 0 && select("#", ...initial) === 0) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That's out of scope of this PR, but we probably want to transform vararg.length to select("#", ...) in general

Comment thread src/lualib/ArrayReduce.ts Outdated
@@ -1,27 +1,35 @@
/** @vararg */
interface Vararg<T> extends Array<T> {}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we should move it to something like declarations/tstl.d.ts

Comment thread src/lualib/ArrayReduce.ts Outdated

if (len === 0 && initial === undefined) {
const initialValuePresent = select("#", ...initial) !== 0;
if (len === 0 && !initialValuePresent) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thoughts on moving that check to a condition below and removing initialValuePresent variable?

Comment thread test/unit/builtins/array.spec.ts Outdated

test("array.reduce undefined returning callback", () => {
util.testFunction`
const calls: {a: void, b: string}[] = [];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
const calls: {a: void, b: string}[] = [];
const calls: Array<{ a: void, b: string }> = [];

@Perryvw Perryvw merged commit b26ec48 into master Oct 5, 2019
@Perryvw Perryvw deleted the feature/reduce-undefined-initial-argument branch October 5, 2019 16:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Array.reduce should accept undefined as an initial value

2 participants