Howto remove a entry from the tree in a Less visitor plugin

159 views Asked by At

I tried the following:

module.exports = function(less) {
function RemoveProperty() {
    this._visitor = new less.visitors.Visitor(this);
};

RemoveProperty.prototype = {
    isReplacing: true,
    isPreEvalVisitor: true,
    run: function (root) {
        return this._visitor.visit(root);
    },
    visitRule: function (ruleNode, visitArgs) {
        if(ruleNode.name[0].value != '-some-aribitrary-property')
        {        
            return ruleNode;
        }
        else
        {
            return  new less.tree.Rule([], [], 0,"");
        }   
    }
};
return RemoveProperty;
};

return new less.tree.Rule([], [], 0,""); still result in a empty output like : ; also return nothing will give me an error: TypeError: Cannot read property 'splice' of undefined.

1

There are 1 answers

0
Luke Page On BEST ANSWER

It can.. but its not ideal from a performance perspective.. return an empty array

visitRule: function (ruleNode, visitArgs) {
    if (ruleNode.variable) {
        return [];
    }
    return ruleNode;
},

If you check out the toCSS visitor it does this alot.

But I think it should allow undefined too.. Will look at adding that soon.