less/less.js

[ToDo] Clarify in documentation that `.parent` is an internal property and not reliable on the node API

Open

#3,570 opened on Nov 26, 2020

View on GitHub
 (6 comments) (0 reactions) (0 assignees)JavaScript (3,517 forks)batch import
good first issuemedium priority

Repository metrics

Stars
 (16,977 stars)
PR merge metrics
 (Avg merge 19d 1h) (15 merged PRs in 30d)

Description

I'm Writing a ThemeColor Plugin, use Visitor.

In fn:visitColor , node.parent sometimes is null (box-shadow:0 0 0 1px #999 inset). I can't find the parent Declaration Node of Color Node. so I want to less.js/lib/less/visitors/visitor.js Visitor.prototype.visit, funcOut.call(impl, node); => funcOut.call(impl, node, visitArgs);. And my Code will be like this.

class ThemeVisitor {
 
    visitDeclaration(node, visitArgs) {
        node.isColorDeclaration = false;
        visitArgs.isColorDeclaration = false;
        return node;
    }

    visitColor(node, visitArgs) {
        visitArgs.isColorDeclaration = true;
        return node;
    }

    visitDeclarationOut(node, visitArgs) {
        if (visitArgs.isColorDeclaration) {
            node.isColorDeclaration = true;
        }
        return node;
    }

    visitRulesetOut(node) {
        node.rules = node.rules.filter((item) => {
            if (item instanceof Declaration) {
                return item.isColorDeclaration;
            }
            if (item instanceof Ruleset) {
                return !!item.rules.length;
            }
        });
        return node;
    }

}

Contributor guide