diff --git a/src/polymer-expressions.js b/src/polymer-expressions.js index 953ce65..11448d3 100644 --- a/src/polymer-expressions.js +++ b/src/polymer-expressions.js @@ -194,6 +194,7 @@ this.depsList = []; this.currentPath = undefined; this.ident = undefined; + this.indexName = undefined; } ASTDelegate.prototype = { @@ -314,9 +315,10 @@ this.ident = ident; }, - createInExpression: function(ident, expression) { + createInExpression: function(ident, indexName, expression) { this.expression = expression; this.ident = ident; + this.indexName = indexName; }, createTopLevel: function(expression) { diff --git a/tests/tests.js b/tests/tests.js index ba6a5f5..831962a 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -862,4 +862,29 @@ suite('PolymerExpressions', function() { Platform.performMicrotaskCheckpoint(); assert.equal('B', div.childNodes[1].textContent); }); + + test('in expression with index scope', function() { + var div = createTestHtml( + ''); + + var model = { + array: ['a', 'b'] + }; + + recursivelySetTemplateModel(div, model); + Platform.performMicrotaskCheckpoint(); + + // TODO(arv): Enable the rest of this test when we have hooked up the + // binding for the index. + // assert.strictEqual('0. a', div.childNodes[1].textContent); + // assert.strictEqual('1. b', div.childNodes[2].textContent); + + // model.array.splice(1, 0, 'c'); + // Platform.performMicrotaskCheckpoint(); + // assert.strictEqual('0. a', div.childNodes[1].textContent); + // assert.strictEqual('1. c', div.childNodes[2].textContent); + // assert.strictEqual('2. b', div.childNodes[3].textContent); + }); }); diff --git a/third_party/esprima/esprima.js b/third_party/esprima/esprima.js index b113fe8..e8df13d 100644 --- a/third_party/esprima/esprima.js +++ b/third_party/esprima/esprima.js @@ -971,6 +971,7 @@ // Expression as Identifier // InExpression :: + // Identifier, Identifier in Expression // Identifier in Expression function parseTopLevel() { @@ -981,7 +982,7 @@ if (expr) { if (lookahead.value === 'as') { parseAsExpression(expr); - } else if (lookahead.value === 'in' && + } else if (lookahead.value === ',' || lookahead.value == 'in' && expr.type === Syntax.Identifier) { parseInExpression(expr); } else if (match('|')) { @@ -1034,9 +1035,17 @@ } function parseInExpression(identifier) { + var indexName; + if (lookahead.value === ',') { + lex(); + if (lookahead.type !== Token.Identifier) + throwUnexpected(lookahead); + indexName = lex().value; + } + lex(); // in var expr = parseExpression(); - delegate.createInExpression(identifier.name, expr); + delegate.createInExpression(identifier.name, indexName, expr); } function parse(code, inDelegate) {