Fix #1726: expression in property access causes unexpected results (#4851)

* fix #1726

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

* Updated output

* Optimization
This commit is contained in:
zdenko 2018-01-16 05:03:01 +01:00 committed by Geoffrey Booth
parent e53307b46b
commit 0217ed5505
3 changed files with 22 additions and 6 deletions

View File

@ -849,7 +849,7 @@
// generates output indented by two spaces; so all we need to do is
// search for a `code` property that begins with at least two spaces.
fragmentIndent = '';
ref1 = fragments.slice(0, fragmentIndex + 1);
ref1 = fragments.slice(0, (fragmentIndex + 1));
for (k = ref1.length - 1; k >= 0; k += -1) {
pastFragment = ref1[k];
indent = /^ {2,}/m.exec(pastFragment.code);
@ -874,7 +874,7 @@
}
return results;
})()).join(`\n${fragmentIndent}`).replace(/^(\s*)$/gm, '');
ref2 = fragments.slice(0, fragmentIndex + 1);
ref2 = fragments.slice(0, (fragmentIndex + 1));
for (pastFragmentIndex = l = ref2.length - 1; l >= 0; pastFragmentIndex = l += -1) {
pastFragment = ref2[pastFragmentIndex];
newLineIndex = pastFragment.code.lastIndexOf('\n');
@ -2097,8 +2097,14 @@
compileNode(o) {
var compiled, compiledText, from, fromCompiled, to, toStr;
({to, from} = this.range);
fromCompiled = from && from.compileToFragments(o, LEVEL_PAREN) || [this.makeCode('0')];
// TODO: jwalton - move this into the 'if'?
// Handle an expression in the property access, e.g. `a[!b in c..]`.
if (from != null ? from.shouldCache() : void 0) {
from = new Value(new Parens(from));
}
if (to != null ? to.shouldCache() : void 0) {
to = new Value(new Parens(to));
}
fromCompiled = (from != null ? from.compileToFragments(o, LEVEL_PAREN) : void 0) || [this.makeCode('0')];
if (to) {
compiled = to.compileToFragments(o, LEVEL_PAREN);
compiledText = fragmentsToText(compiled);

View File

@ -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

View File

@ -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)..]