Skip to content

Commit

Permalink
Fix #1726: expression in property access causes unexpected results (#…
Browse files Browse the repository at this point in the history
…4851)

* fix #1726

* Explain what's happening, rather than just linking to an issue

* Updated output

* Optimization
  • Loading branch information
zdenko authored and GeoffreyBooth committed Jan 16, 2018
1 parent e53307b commit 0217ed5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
14 changes: 10 additions & 4 deletions lib/coffeescript/nodes.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions src/nodes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1406,8 +1406,12 @@ exports.Slice = class Slice extends Base
# `9e9` should be safe because `9e9` > `2**32`, the max array length.
compileNode: (o) ->
{to, from} = @range
fromCompiled = from and from.compileToFragments(o, LEVEL_PAREN) or [@makeCode '0']
# TODO: jwalton - move this into the 'if'?
# Handle an expression in the property access, e.g. `a[!b in c..]`.
if from?.shouldCache()
from = new Value new Parens from
if to?.shouldCache()
to = new Value new Parens to
fromCompiled = from?.compileToFragments(o, LEVEL_PAREN) or [@makeCode '0']
if to
compiled = to.compileToFragments o, LEVEL_PAREN
compiledText = fragmentsToText compiled
Expand Down
6 changes: 6 additions & 0 deletions test/slicing_and_splicing.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,9 @@ test "#2953: methods on endpoints in assignment from array splice literal", ->
delete Number.prototype.same

arrayEq [0, 5, 9], list

test "#1726: `Op` expression in property access causes unexpected results", ->
a = [0..2]
arrayEq a, a[(!1 in a)..]
arrayEq a, a[!1 in a..]
arrayEq a[(!1 in a)..], a[(!1 in a)..]

0 comments on commit 0217ed5

Please sign in to comment.