Example #1
0
        AstExpansion ParseSpread(DestructuringErrors refDestructuringErrors)
        {
            var startLoc = Start;

            Next();
            var argument = ParseMaybeAssign(false, refDestructuringErrors);

            return(new AstExpansion(this, startLoc, _lastTokEnd, argument));
        }
Example #2
0
        AstNode ParseForStatement(Position nodeStart)
        {
            Next();
            EnterLexicalScope();
            Expect(TokenType.ParenL);
            if (Type == TokenType.Semi)
            {
                return(ParseFor(nodeStart, null));
            }
            var isLet = IsLet();

            if (Type == TokenType.Var || Type == TokenType.Const || isLet)
            {
                var startLoc = Start;
                var kind     = isLet ? VariableKind.Let : ToVariableKind((string)Value);
                Next();
                var declarations = new StructList <AstVarDef>();
                ParseVar(ref declarations, true, kind);
                AstDefinitions init;
                if (kind == VariableKind.Let)
                {
                    init = new AstLet(this, startLoc, _lastTokEnd, ref declarations);
                }
                else if (kind == VariableKind.Const)
                {
                    init = new AstConst(this, startLoc, _lastTokEnd, ref declarations);
                }
                else
                {
                    init = new AstVar(this, startLoc, _lastTokEnd, ref declarations);
                }

                if ((Type == TokenType.In || Options.EcmaVersion >= 6 && IsContextual("of")) &&
                    init.Definitions.Count == 1 &&
                    !(kind != VariableKind.Var && init.Definitions[0].Value != null))
                {
                    return(ParseForIn(nodeStart, init));
                }
                return(ParseFor(nodeStart, init));
            }
            else
            {
                var refDestructuringErrors = new DestructuringErrors();
                var init = ParseExpression(true, refDestructuringErrors);
                if (Type == TokenType.In || Options.EcmaVersion >= 6 && IsContextual("of"))
                {
                    init = ToAssignable(init);
                    CheckLVal(init, false, null);
                    CheckPatternErrors(refDestructuringErrors, true);
                    return(ParseForIn(nodeStart, init));
                }

                CheckExpressionErrors(refDestructuringErrors, true);
                return(ParseFor(nodeStart, init));
            }
        }
Example #3
0
        static bool CheckExpressionErrors([CanBeNull] DestructuringErrors refDestructuringErrors, bool andThrow = false)
        {
            var pos = refDestructuringErrors?.ShorthandAssign ?? default;

            if (!andThrow)
            {
                return(pos.Line > 0);
            }
            if (pos.Line > 0)
            {
                Raise(pos, "Shorthand property assignments are valid only in destructuring patterns");
            }
            return(false);
        }
Example #4
0
        static void CheckPatternErrors([CanBeNull] DestructuringErrors refDestructuringErrors, bool isAssign)
        {
            if (refDestructuringErrors == null)
            {
                return;
            }
            if (refDestructuringErrors.TrailingComma.Line > 0)
            {
                RaiseRecoverable(refDestructuringErrors.TrailingComma, "Comma is not permitted after the rest element");
            }
            var parens = isAssign ? refDestructuringErrors.ParenthesizedAssign : refDestructuringErrors.ParenthesizedBind;

            if (parens.Line > 0)
            {
                RaiseRecoverable(parens, "Parenthesized pattern");
            }
        }