@@ -354,6 +354,8 @@ export default abstract class StatementParser extends ExpressionParser {
354354 ParseStatementFlag . AllowImportExport |
355355 ParseStatementFlag . AllowDeclaration |
356356 ParseStatementFlag . AllowFunctionDeclaration |
357+ // This function is actually also used to parse StatementItems,
358+ // which with Annex B enabled allows labeled functions.
357359 ParseStatementFlag . AllowLabeledFunction ,
358360 ) ;
359361 }
@@ -363,18 +365,24 @@ export default abstract class StatementParser extends ExpressionParser {
363365 return this . parseStatementLike (
364366 ParseStatementFlag . AllowDeclaration |
365367 ParseStatementFlag . AllowFunctionDeclaration |
366- ParseStatementFlag . AllowLabeledFunction ,
368+ ( ! this . options . annexB || this . state . strict
369+ ? 0
370+ : ParseStatementFlag . AllowLabeledFunction ) ,
367371 ) ;
368372 }
369373
370- parseStatementOrFunctionDeclaration (
374+ parseStatementOrSloppyAnnexBFunctionDeclaration (
371375 this : Parser ,
372- disallowLabeledFunction : boolean ,
376+ allowLabeledFunction : boolean = false ,
373377 ) {
374- return this . parseStatementLike (
375- ParseStatementFlag . AllowFunctionDeclaration |
376- ( disallowLabeledFunction ? 0 : ParseStatementFlag . AllowLabeledFunction ) ,
377- ) ;
378+ let flags : ParseStatementFlag = ParseStatementFlag . StatementOnly ;
379+ if ( this . options . annexB && ! this . state . strict ) {
380+ flags |= ParseStatementFlag . AllowFunctionDeclaration ;
381+ if ( allowLabeledFunction ) {
382+ flags |= ParseStatementFlag . AllowLabeledFunction ;
383+ }
384+ }
385+ return this . parseStatementLike ( flags ) ;
378386 }
379387
380388 // Parse a single statement.
@@ -438,12 +446,15 @@ export default abstract class StatementParser extends ExpressionParser {
438446 return this . parseForStatement ( node as Undone < N . ForStatement > ) ;
439447 case tt . _function :
440448 if ( this . lookaheadCharCode ( ) === charCodes . dot ) break ;
441- if ( ! allowDeclaration ) {
442- if ( this . state . strict ) {
443- this . raise ( Errors . StrictFunction , { at : this . state . startLoc } ) ;
444- } else if ( ! allowFunctionDeclaration ) {
445- this . raise ( Errors . SloppyFunction , { at : this . state . startLoc } ) ;
446- }
449+ if ( ! allowFunctionDeclaration ) {
450+ this . raise (
451+ this . state . strict
452+ ? Errors . StrictFunction
453+ : this . options . annexB
454+ ? Errors . SloppyFunctionAnnexB
455+ : Errors . SloppyFunction ,
456+ { at : this . state . startLoc } ,
457+ ) ;
447458 }
448459 return this . parseFunctionStatement (
449460 node as Undone < N . FunctionDeclaration > ,
@@ -981,12 +992,9 @@ export default abstract class StatementParser extends ExpressionParser {
981992 node . test = this . parseHeaderExpression ( ) ;
982993 // Annex B.3.3
983994 // https://tc39.es/ecma262/#sec-functiondeclarations-in-ifstatement-statement-clauses
984- node . consequent = this . parseStatementOrFunctionDeclaration (
985- // https://tc39.es/ecma262/#sec-if-statement-static-semantics-early-errors
986- true ,
987- ) ;
995+ node . consequent = this . parseStatementOrSloppyAnnexBFunctionDeclaration ( ) ;
988996 node . alternate = this . eat ( tt . _else )
989- ? this . parseStatementOrFunctionDeclaration ( true )
997+ ? this . parseStatementOrSloppyAnnexBFunctionDeclaration ( )
990998 : null ;
991999 return this . finishNode ( node , "IfStatement" ) ;
9921000 }
@@ -1074,8 +1082,11 @@ export default abstract class StatementParser extends ExpressionParser {
10741082 parseCatchClauseParam ( this : Parser ) : N . Pattern {
10751083 const param = this . parseBindingAtom ( ) ;
10761084
1077- const simple = param . type === "Identifier" ;
1078- this . scope . enter ( simple ? SCOPE_SIMPLE_CATCH : 0 ) ;
1085+ this . scope . enter (
1086+ this . options . annexB && param . type === "Identifier"
1087+ ? SCOPE_SIMPLE_CATCH
1088+ : 0 ,
1089+ ) ;
10791090 this . checkLVal ( param , {
10801091 in : { type : "CatchClause" } ,
10811092 binding : BIND_CATCH_PARAM ,
@@ -1234,7 +1245,7 @@ export default abstract class StatementParser extends ExpressionParser {
12341245 // https://tc39.es/ecma262/#prod-LabelledItem
12351246 node . body =
12361247 flags & ParseStatementFlag . AllowLabeledFunction
1237- ? this . parseStatementOrFunctionDeclaration ( false )
1248+ ? this . parseStatementOrSloppyAnnexBFunctionDeclaration ( true )
12381249 : this . parseStatement ( ) ;
12391250
12401251 this . state . labels . pop ( ) ;
@@ -1419,6 +1430,7 @@ export default abstract class StatementParser extends ExpressionParser {
14191430 init . type === "VariableDeclaration" &&
14201431 init . declarations [ 0 ] . init != null &&
14211432 ( ! isForIn ||
1433+ ! this . options . annexB ||
14221434 this . state . strict ||
14231435 init . kind !== "var" ||
14241436 init . declarations [ 0 ] . id . type !== "Identifier" )
@@ -1626,7 +1638,7 @@ export default abstract class StatementParser extends ExpressionParser {
16261638 // treatFunctionsAsVar).
16271639 this . scope . declareName (
16281640 node . id . name ,
1629- this . state . strict || node . generator || node . async
1641+ ! this . options . annexB || this . state . strict || node . generator || node . async
16301642 ? this . scope . treatFunctionsAsVar
16311643 ? BIND_VAR
16321644 : BIND_LEXICAL
0 commit comments