Skip to content

Commit

Permalink
feat: improve error message for malformed each (#3185)
Browse files Browse the repository at this point in the history
It now includes a description of what `each` should look like. We guide people towards the newer `each ... of ...` syntax, rather than the older `each ... in ...` syntax.
  • Loading branch information
ForbesLindesay authored May 1, 2020
1 parent f97ebdb commit beb3755
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 11 additions & 1 deletion packages/pug-lexer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1051,8 +1051,18 @@ Lexer.prototype = {
this.tokens.push(this.tokEnd(tok));
return true;
}
const name = /^each\b/.exec(this.input) ? 'each' : 'for';
if (this.scan(/^(?:each|for)\b/)) {
this.error('MALFORMED_EACH', 'malformed each');
this.error(
'MALFORMED_EACH',
'This `' +
name +
'` has a syntax error. `' +
name +
'` statements should be of the form: `' +
name +
' VARIABLE_NAME of JS_EXPRESSION`'
);
}
if (
(captures = /^- *(?:each|for) +([a-zA-Z_$][\w$]*)(?: *, *([a-zA-Z_$][\w$]*))? +in +([^\n]+)/.exec(
Expand Down
2 changes: 1 addition & 1 deletion packages/pug-lexer/test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -28680,7 +28680,7 @@ Object {
"code": "PUG:MALFORMED_EACH",
"column": 5,
"line": 1,
"msg": "malformed each",
"msg": "This \`each\` has a syntax error. \`each\` statements should be of the form: \`each VARIABLE_NAME of JS_EXPRESSION\`",
}
`;

Expand Down

0 comments on commit beb3755

Please sign in to comment.