throw error on illegal context (#934)

pull/947/head
Rich Harris 7 years ago
parent 582315086f
commit c9aa72313e

@ -1,6 +1,7 @@
import readExpression from '../read/expression';
import { whitespace } from '../../utils/patterns';
import { trimStart, trimEnd } from '../../utils/trim';
import reservedNames from '../../utils/reservedNames';
import { Parser } from '../index';
import { Node } from '../../interfaces';
@ -168,8 +169,15 @@ export default function mustache(parser: Parser) {
do {
parser.allowWhitespace();
const start = parser.index;
const destructuredContext = parser.read(validIdentifier);
if (!destructuredContext) parser.error(`Expected name`);
if (reservedNames.has(destructuredContext)) {
parser.error(`'${destructuredContext}' is a reserved word in JavaScript and cannot be used here`, start);
}
block.destructuredContexts.push(destructuredContext);
parser.allowWhitespace();
} while (parser.eat(','));
@ -180,7 +188,11 @@ export default function mustache(parser: Parser) {
parser.allowWhitespace();
parser.eat(']', true);
} else {
block.context = parser.read(validIdentifier); // TODO check it's not a keyword
const start = parser.index;
block.context = parser.read(validIdentifier);
if (reservedNames.has(block.context)) {
parser.error(`'${block.context}' is a reserved word in JavaScript and cannot be used here`, start);
}
if (!block.context) parser.error(`Expected name`);
}

@ -49,7 +49,4 @@ const reservedNames = new Set([
'yield',
]);
// prevent e.g. `{{#each states as state}}` breaking
reservedNames.add('state');
export default reservedNames;

@ -0,0 +1,8 @@
[{
"message": "'case' is a reserved word in JavaScript and cannot be used here",
"loc": {
"line": 1,
"column": 18
},
"pos": 18
}]

@ -0,0 +1,3 @@
{{#each cases as [case]}}
{{case.title}}
{{/each}}

@ -0,0 +1,8 @@
[{
"message": "'case' is a reserved word in JavaScript and cannot be used here",
"loc": {
"line": 1,
"column": 17
},
"pos": 17
}]

@ -0,0 +1,3 @@
{{#each cases as case}}
{{case.title}}
{{/each}}
Loading…
Cancel
Save