less.js icon indicating copy to clipboard operation
less.js copied to clipboard

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

Open legu2009 opened this issue 5 years ago • 6 comments

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;
    }

}

legu2009 avatar Nov 26 '20 08:11 legu2009

@legu2009

node.parent sometimes is null

The parent property of nodes is somewhat unreliable, as it doesn't currently have accompanying tests to verify that all nodes a) have a parent property set, b) retain that property as nodes are evaluated.

I've updated this issue to reflect that the API needs to be hardened with tests.

matthew-dean avatar Dec 14 '20 17:12 matthew-dean

@legu2009 I've taken a dive into the .parent property while doing a conversion of Less.js into TypeScript. It really looks like this property was not well-thought-out (which could very well be my fault), and unfortunately there's too much node mutation of the AST during evaluation that it can't really be reliably used. I'm going to recommend deprecating / removing as a property.

matthew-dean avatar Jan 07 '21 04:01 matthew-dean

can i take up this issue??

A-tiwarii avatar Oct 01 '21 19:10 A-tiwarii

@A-tiwarii Absolutely!

matthew-dean avatar Oct 01 '21 20:10 matthew-dean

How do I access the docs file. I am interested in solving this issue.

Catalyst497 avatar Jun 06 '22 13:06 Catalyst497

Is anyone working on this issue? I am a beginner in open source and would love to take up this issue.

TusharP05 avatar Jun 29 '23 12:06 TusharP05