JSC's parser should follow the ES6 spec with respect to parsing Declarations
https://bugs.webkit.org/show_bug.cgi?id=146621
Reviewed by Mark Lam.
Source/JavaScriptCore:
There were a few locations where JSC would allow declaration statements
in incorrect ways. JSC didn't distinguish between 'Statement' and
'StatementListItem' grammar productions. The relevant grammar is here:
http://www.ecma-international.org/ecma-262/6.0/index.html#sec-statements
From the ECMA Script 6.0 spec:
1. Section 13.6 The if Statement (http://www.ecma-international.org/ecma-262/6.0/index.html#sec-if-statement)
says that IfStatements only takes Statements for the "then-else" clauses, not StatementListItems.
(Same with 'while/for/do-while' loop bodies).
2. Section 13 ECMAScript Language: Statements and Declarations
(http://www.ecma-international.org/ecma-262/6.0/index.html#sec-ecmascript-language-statements-and-declarations)
defines the syntax of Statements, and they do not include ClassDeclarations and LexicalDeclarations
(const, let, see 13.3.1 Let and Const Declarations).
Declarations can only be in the “then-else” clauses when embedded in a StatementListItem in a BlockStatement (see 13.2).
Hence, the following style of declarations are no longer allowed:
'if/for/while (condition) const x = 40;'
'if/for/while (condition) class C { }'
Instead, we mandate such declaration constructs are within a StatementList
(which is the production that JSC's Parser::parseSourceElements function parses):
'if/for/while (condition) { const x = 40; }'
'if/for/while (condition) { class C { } }'
* parser/Parser.cpp:
(JSC::Parser<LexerType>::parseSourceElements):
(JSC::Parser<LexerType>::parseStatementListItem):
(JSC::Parser<LexerType>::parseVarDeclaration):
(JSC::Parser<LexerType>::parseStatement):
(JSC::Parser<LexerType>::parseExpressionStatement):
* parser/Parser.h:
(JSC::Parser::getLabel):
LayoutTests:
* js/parser-syntax-check-expected.txt:
* js/script-tests/const.js:
(with1):
(with2):
* js/script-tests/parser-syntax-check.js:
* js/script-tests/statement-list-item-syntax-errors.js: Added.
(testSyntax):
(runTests):
* js/statement-list-item-syntax-errors-expected.txt: Added.
* js/statement-list-item-syntax-errors.html: Added.
* sputnik/Conformance/07_Lexical_Conventions/7.5_Tokens/7.5.3_Future_Reserved_Words/S7.5.3_A1.5-expected.txt:
git-svn-id: http://svn.webkit.org/repository/webkit/trunk@186379 268f45cc-cd09-0410-ab3c-d52691b4dbfc
11 files changed