Merge pull request #947 from sveltejs/gh-934

throw error on illegal context
pull/948/head
Rich Harris 8 years ago committed by GitHub
commit 14b27b71e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,6 +1,7 @@
import readExpression from '../read/expression'; import readExpression from '../read/expression';
import { whitespace } from '../../utils/patterns'; import { whitespace } from '../../utils/patterns';
import { trimStart, trimEnd } from '../../utils/trim'; import { trimStart, trimEnd } from '../../utils/trim';
import reservedNames from '../../utils/reservedNames';
import { Parser } from '../index'; import { Parser } from '../index';
import { Node } from '../../interfaces'; import { Node } from '../../interfaces';
@ -168,8 +169,15 @@ export default function mustache(parser: Parser) {
do { do {
parser.allowWhitespace(); parser.allowWhitespace();
const start = parser.index;
const destructuredContext = parser.read(validIdentifier); const destructuredContext = parser.read(validIdentifier);
if (!destructuredContext) parser.error(`Expected name`); 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); block.destructuredContexts.push(destructuredContext);
parser.allowWhitespace(); parser.allowWhitespace();
} while (parser.eat(',')); } while (parser.eat(','));
@ -180,7 +188,11 @@ export default function mustache(parser: Parser) {
parser.allowWhitespace(); parser.allowWhitespace();
parser.eat(']', true); parser.eat(']', true);
} else { } 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`); if (!block.context) parser.error(`Expected name`);
} }

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