Skip to content

Commit

Permalink
Merge pull request #947 from sveltejs/gh-934
Browse files Browse the repository at this point in the history
throw error on illegal context
  • Loading branch information
Rich-Harris authored Nov 23, 2017
2 parents 5823150 + c9aa723 commit 14b27b7
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 4 deletions.
14 changes: 13 additions & 1 deletion src/parse/state/mustache.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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(','));
Expand All @@ -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`);
}
Expand Down
3 changes: 0 additions & 3 deletions src/utils/reservedNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,4 @@ const reservedNames = new Set([
'yield',
]);

// prevent e.g. `{{#each states as state}}` breaking
reservedNames.add('state');

export default reservedNames;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[{
"message": "'case' is a reserved word in JavaScript and cannot be used here",
"loc": {
"line": 1,
"column": 18
},
"pos": 18
}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#each cases as [case]}}
{{case.title}}
{{/each}}
8 changes: 8 additions & 0 deletions test/validator/samples/each-block-invalid-context/errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[{
"message": "'case' is a reserved word in JavaScript and cannot be used here",
"loc": {
"line": 1,
"column": 17
},
"pos": 17
}]
3 changes: 3 additions & 0 deletions test/validator/samples/each-block-invalid-context/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#each cases as case}}
{{case.title}}
{{/each}}

0 comments on commit 14b27b7

Please sign in to comment.