Skip to content

Commit 4592143

Browse files
authored
Generator declaration is not allowed in single statement position (#4815)
JerryScript-DCO-1.0-Signed-off-by: Robert Sipka [email protected]
1 parent 8077779 commit 4592143

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
lines changed

jerry-core/parser/js/js-parser-statm.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,9 @@ parser_parse_function_statement (parser_context_t *context_p) /**< context */
600600
JERRY_ASSERT (context_p->token.type == LEXER_KEYW_FUNCTION);
601601

602602
#if JERRY_ESNEXT
603-
if (JERRY_UNLIKELY (parser_statement_flags[context_p->stack_top_uint8] & PARSER_STATM_SINGLE_STATM))
603+
bool is_single_statement = (parser_statement_flags[context_p->stack_top_uint8] & PARSER_STATM_SINGLE_STATM) != 0;
604+
605+
if (JERRY_UNLIKELY (is_single_statement))
604606
{
605607
if (context_p->status_flags & PARSER_IS_STRICT)
606608
{
@@ -667,6 +669,10 @@ parser_parse_function_statement (parser_context_t *context_p) /**< context */
667669

668670
if (lexer_consume_generator (context_p))
669671
{
672+
if (is_single_statement)
673+
{
674+
parser_raise_error (context_p, PARSER_ERR_GENERATOR_IN_SINGLE_STATEMENT_POS);
675+
}
670676
is_generator_function = true;
671677
}
672678
#endif /* JERRY_ESNEXT */

jerry-core/parser/js/js-parser-util.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,10 @@ parser_error_to_string (parser_error_t error) /**< error code */
12431243
{
12441244
return "Lexical declaration cannot appear in a single-statement context";
12451245
}
1246+
case PARSER_ERR_GENERATOR_IN_SINGLE_STATEMENT_POS:
1247+
{
1248+
return "Generator function cannot appear in a single-statement context";
1249+
}
12461250
case PARSER_ERR_LABELLED_FUNC_NOT_IN_BLOCK:
12471251
{
12481252
return "Labelled functions are only allowed inside blocks";

jerry-core/parser/js/js-parser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ typedef enum
141141
#if JERRY_ESNEXT
142142
PARSER_ERR_VARIABLE_REDECLARED, /**< a variable redeclared */
143143
PARSER_ERR_LEXICAL_SINGLE_STATEMENT, /**< lexical declaration in single statement context */
144+
PARSER_ERR_GENERATOR_IN_SINGLE_STATEMENT_POS, /**< generator func not allowed in single statement position */
144145
PARSER_ERR_LABELLED_FUNC_NOT_IN_BLOCK, /**< labelled functions are only allowed inside blocks */
145146
PARSER_ERR_LEXICAL_LET_BINDING, /**< let binding cannot be declared in let/const */
146147
PARSER_ERR_MISSING_ASSIGN_AFTER_CONST, /**< an assignment is required after a const declaration */
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* Copyright JS Foundation and other contributors, http://js.foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
17+
function parse(txt) {
18+
try {
19+
eval(txt)
20+
assert(false)
21+
} catch (e) {
22+
assert(e instanceof SyntaxError)
23+
}
24+
}
25+
26+
parse("if (true) function* g() { }")
27+
parse("if (false) ; else function* g() { }")

tests/test262-esnext-excludelist.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,8 @@
365365
<test id="language/statements/if/cptn-no-else-false.js"><reason></reason></test>
366366
<test id="language/statements/if/cptn-no-else-true-abrupt-empty.js"><reason></reason></test>
367367
<test id="language/statements/if/cptn-no-else-true-nrml.js"><reason></reason></test>
368-
<test id="language/statements/if/if-gen-else-gen.js"><reason></reason></test>
369-
<test id="language/statements/if/if-gen-else-stmt.js"><reason></reason></test>
370-
<test id="language/statements/if/if-gen-no-else.js"><reason></reason></test>
371-
<test id="language/statements/if/if-stmt-else-gen.js"><reason></reason></test>
372368
<test id="language/statements/if/let-block-with-newline.js"><reason></reason></test>
373369
<test id="language/statements/if/let-identifier-with-newline.js"><reason></reason></test>
374-
<test id="language/statements/labeled/decl-gen.js"><reason></reason></test>
375370
<test id="language/statements/labeled/let-block-with-newline.js"><reason></reason></test>
376371
<test id="language/statements/labeled/let-identifier-with-newline.js"><reason></reason></test>
377372
<test id="language/statements/let/syntax/let-newline-yield-in-generator-function.js"><reason></reason></test>

0 commit comments

Comments
 (0)