mjs
mjs copied to clipboard
Let without a semicolon produces syntax error
The following code produces a syntax error:
function foo() {
let x = 0
}
Whereas:
function foo() {
let x = 0;
}
Does not. ASI seems to work correctly under other circumstances. Since there is no documentation saying the semicolon is needed in the situation; I feel we can assume this is a bug
This causes problems with minifyers, out of spec, i think.
@marciopamplona I am not sure if you are addressing me or not, but just in case: A good minifier would insert the semicolons; however it is trivially easy to omit a semicolon and then all you get is the cryptic "syntax error". Additionally semicolons can be omitted in other places and mjs doesn't object. Nor is there any documentation on semicolon requirements. So no matter how you slice it, this is a bug in one form or another
@jockm Yes, i agree with you, this bug cause problems with minifyers. I referred to the bug and not to your comment.
@marciopamplona Gotcha, mea culpa
I'm not part of the development team, but it looks like the code that might be causing this bug is this:
https://github.com/cesanta/mjs/blob/8d847f2573f3fe85583986eae73b67818b6c5846/mjs/src/mjs_parser.c
static mjs_err_t parse_statement_list(struct pstate *p, int et) {
mjs_err_t res = MJS_OK;
int drop = 0;
pnext1(p);
while (res == MJS_OK && p->tok.tok != TOK_EOF && p->tok.tok != et) {
if (drop) emit_byte(p, OP_DROP);
res = parse_statement(p);
drop = 1;
while (p->tok.tok == TOK_SEMICOLON) pnext1(p);
}
/*
* Client code expects statement list to contain a value, so if the statement
* list was empty, push `undefined`.
*/
if (!drop) {
emit_byte(p, OP_PUSH_UNDEF);
}
return res;
}
static mjs_err_t parse_block(struct pstate *p, int mkscope) {
mjs_err_t res = MJS_OK;
p->depth++;
if (p->depth > (STACK_LIMIT / BINOP_STACK_FRAME_SIZE)) {
mjs_set_errorf(p->mjs, MJS_SYNTAX_ERROR, "parser stack overflow");
res = MJS_SYNTAX_ERROR;
return res;
}
LOG(LL_VERBOSE_DEBUG, ("[%.*s]", 10, p->tok.ptr));
if (mkscope) emit_byte(p, OP_NEW_SCOPE);
res = parse_statement_list(p, TOK_CLOSE_CURLY);
EXPECT(p, TOK_CLOSE_CURLY);
if (mkscope) emit_byte(p, OP_DEL_SCOPE);
return res;
}
And you know wouldn't it be great if cesanta actually commented on any of this. They have been suspiciously silent on all mJS for some time now.