Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Renderer.php

function Renderer::needsBrackets

Check if the node or the array of nodes need brackets to be rendered

Parameters

Syntax\Node\Node $parent Parent node:

Syntax\Node\Node|array $node Node or array of: nodes to render

Return value

bool

1 call to Renderer::needsBrackets()
Renderer::renderStatementBlock in vendor/mck89/peast/lib/Peast/Renderer.php
Renders a node as a block statement

File

vendor/mck89/peast/lib/Peast/Renderer.php, line 1046

Class

Renderer
Nodes renderer class

Namespace

Peast

Code

protected function needsBrackets($parent, $node) {
    $parentType = $parent->getType();
    
    //The SwitchCase content needs brackets if it contains let or const declarations
    $inSwitchCase = $parentType === "SwitchCase";
    
    //Except for SwitchCase, brackets are needed if the formatter requires them or whenever
    
    //there are 0 or multiple nodes to render
    if (!$inSwitchCase && ($this->renderOpts->awb || is_array($node) && count($node) !== 1)) {
        return true;
    }
    if (!is_array($node)) {
        $node = array(
            $node,
        );
    }
    $addBrackets = false;
    $optBracketNodes = array(
        "DoWhileStatement",
        "ForInStatement",
        "ForOfStatement",
        "ForStatement",
        "WhileStatement",
        "WithStatement",
    );
    
    //An IfStatement requires brackets if it contains let or const declarations and if it has the "else" part
    
    //and contains another IfStatement with "else"
    $inIfWithElse = $parentType === "IfStatement" && $parent->getAlternate();
    $checkFn = function ($n) use ($inIfWithElse, $optBracketNodes, &$addBrackets) {
        $type = $n->getType();
        if ($inIfWithElse && $type === "IfStatement") {
            if (!$n->getAlternate()) {
                $addBrackets = true;
            }
            return Traverser::DONT_TRAVERSE_CHILD_NODES;
        }
        elseif ($type === "BlockStatement") {
            if (count($n->getBody()) !== 1) {
                return Traverser::DONT_TRAVERSE_CHILD_NODES;
            }
        }
        elseif ($inIfWithElse && $type === "LabeledStatement") {
            if ($n->getBody()
                ->getType() === "BlockStatement") {
                $addBrackets = true;
            }
            return Traverser::DONT_TRAVERSE_CHILD_NODES;
        }
        elseif ($type === "VariableDeclaration") {
            if (in_array($n->getKind(), array(
                $n::KIND_LET,
                $n::KIND_CONST,
            ))) {
                $addBrackets = true;
            }
            return Traverser::DONT_TRAVERSE_CHILD_NODES;
        }
        elseif (!in_array($type, $optBracketNodes)) {
            return Traverser::DONT_TRAVERSE_CHILD_NODES;
        }
    };
    
    //Traverse every node but stop whenever the $addBrackets variable becomes true
    foreach ($node as $n) {
        $n->traverse($checkFn);
        if ($addBrackets) {
            break;
        }
    }
    return $addBrackets;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal