Skip to content

Commit

Permalink
Merge "Less_VisitorReplacing: Refactor to more closely match upstream"
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Mar 20, 2024
2 parents 0bbf55f + 2dd4699 commit 5b9a7fb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 30 deletions.
19 changes: 14 additions & 5 deletions lib/Less/Visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,22 @@ public function __construct() {
}

public function visitObj( $node ) {
if ( !$node || !is_object( $node ) ) {
return $node;
}
$funcName = 'visit' . str_replace( [ 'Less_Tree_', '_' ], '', get_class( $node ) );
if ( isset( $this->_visitFnCache[$funcName] ) ) {
$visitDeeper = true;
$this->$funcName( $node, $visitDeeper );
$newNode = $this->$funcName( $node, $visitDeeper );
if ( $this instanceof Less_VisitorReplacing ) {
$node = $newNode;
}

if ( $visitDeeper ) {
if ( $visitDeeper && is_object( $node ) ) {
$node->accept( $this );
}

$funcName .= "Out";
$funcName .= 'Out';
if ( isset( $this->_visitFnCache[$funcName] ) ) {
$this->$funcName( $node );
}
Expand All @@ -34,8 +40,11 @@ public function visitObj( $node ) {
return $node;
}

public function visitArray( $nodes ) {
foreach ( $nodes as $node ) {
public function visitArray( &$nodes ) {
// NOTE: The use of by-ref in a normal (non-replacing) Visitor may be surprising,
// but upstream relies on this for Less_ImportVisitor, which modifies values of
// `$importParent->rules` yet is not a replacing visitor.
foreach ( $nodes as &$node ) {
$this->visitObj( $node );
}
return $nodes;
Expand Down
26 changes: 1 addition & 25 deletions lib/Less/VisitorReplacing.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,7 @@
*/
class Less_VisitorReplacing extends Less_Visitor {

public function visitObj( $node ) {
$funcName = 'visit' . str_replace( [ 'Less_Tree_', '_' ], '', get_class( $node ) );
if ( isset( $this->_visitFnCache[$funcName] ) ) {
$visitDeeper = true;
$node = $this->$funcName( $node, $visitDeeper );

if ( $node ) {
if ( $visitDeeper && is_object( $node ) ) {
$node->accept( $this );
}

$funcName .= "Out";
if ( isset( $this->_visitFnCache[$funcName] ) ) {
$this->$funcName( $node );
}
}

} else {
$node->accept( $this );
}

return $node;
}

public function visitArray( $nodes ) {
public function visitArray( &$nodes ) {
$newNodes = [];
foreach ( $nodes as $node ) {
$evald = $this->visitObj( $node );
Expand Down

0 comments on commit 5b9a7fb

Please sign in to comment.