Closed Bug 1217072 Opened 9 years ago Closed 9 years ago

Remove use of for-each from addon-sdk/.

Categories

(Add-on SDK Graveyard :: General, defect)

defect
Not set
normal

Tracking

(firefox44 fixed)

RESOLVED FIXED
mozilla44
Tracking Status
firefox44 --- fixed

People

(Reporter: arai, Assigned: arai)

References

Details

Attachments

(1 file)

Need to replace non-standard for-each with one of:
  * for-of
  * for-in
  * array.map/array.filter for array-comprehension
as a part of bug 1083470.

converting rules are following:
  * for-each
    * for each (let x in array) { ... }
      -> for (let x of array) { ... }
    * for each (let x in object) { ... }
      -> for (let key in object) { let x = object[key]; ... }
    * for each (let [key, value] in Iterator(object)) { ... }
      -> for (let key in object) { let value = object[key]; ... }
    * for each (let x in array) { ... }
      where array can be null or undefined
      -> if (array) { for (let x of array) { ... } }

  * legacy array comprehension with for-each
    * [EXPR for each (x in array)]
      -> array.map(x => EXPR)
    * [EXPR for each (x in array) if (COND)]
      -> array.filter(x => COND).map(x => EXPR)
    * [x for each (x in array) if (COND)]
      -> array.filter(x => COND)
    * [EXPR for each ([i, x] in Iterator(array)) if (g(x, i)]
      -> array.filter((x, i) => g(x, i)).map((x => EXPR)
    * [EXPR for each (x in arraylike)]
      -> Array.from(arraylike).map(x => EXPR)
    * [EXPR for each (x in string)]
      -> Array.prototype.slice.call(string).map(x => EXPR)
         // Array.from behaves differently for surrogate-pair

I'll post a patch shortly.
I didn't replace this line, as it looks like it's testing behavior for for-each.

https://dxr.mozilla.org/mozilla-central/rev/605de27d4e7f530159842649c02075c578d7a4a5/addon-sdk/source/test/test-api-utils.js#307
>  for each (let val in obj)
>    valsItr.push(val);

I think this should be removed at the same time as removing for-each support from SpiderMonkey.
(In reply to Tooru Fujisawa [:arai] from comment #0)
>   * legacy array comprehension with for-each
>     * [EXPR for each (x in array)]
>       -> array.map(x => EXPR)
>     * [EXPR for each (x in array) if (COND)]
>       -> array.filter(x => COND).map(x => EXPR)
>     * [x for each (x in array) if (COND)]
>       -> array.filter(x => COND)
>     * [EXPR for each ([i, x] in Iterator(array)) if (g(x, i)]
>       -> array.filter((x, i) => g(x, i)).map((x => EXPR)
>     * [EXPR for each (x in arraylike)]
>       -> Array.from(arraylike).map(x => EXPR)
>     * [EXPR for each (x in string)]
>       -> Array.prototype.slice.call(string).map(x => EXPR)
>          // Array.from behaves differently for surrogate-pair

Why aren't we switching to the new form of array comprehensions for these?
Flags: needinfo?(arai.unmht)
because it's removed from the spec (iiuc, even from ES7).
Flags: needinfo?(arai.unmht)
array comprehension and generator comprehension are not likely to be added to the spec, and we'll eventually remove it from SpiderMonkey if we can.
I've just updated documentation on MDN from experimental to non-standard.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Array_comprehensions
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Generator_comprehensions

let's go with map/filter :)
Attachment #8676972 - Flags: review?(dtownsend) → review+
https://hg.mozilla.org/mozilla-central/rev/a2965479d63d
Status: NEW → RESOLVED
Closed: 9 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla44
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: