question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Variables in detached rulesets not visited by visitor?

See original GitHub issue

I have the following plugin foo.js

module.exports = {
    install({ tree: { Quoted }, visitors }, manager) {
        class Visitor {
            constructor() {
                this.native = new visitors.Visitor(this);

                this.isPreEvalVisitor = true;
                this.isReplacing = true;
            }

            run(root) {
                console.log(
                    root
                        .rules[2]
                        .rules[0]
                        .arguments[0]
                        .value
                        .ruleset
                        .rules[0]
                        .value
                        .value[0]
                        .value[0]
                        .type,
                ); // 'Variable'

                return this.native.visit(root);
            }

            visitVariable(node) {
                console.log('visited');

                return new Quoted(`'`, 'bar', true);
            }
        }

        manager.addVisitor(new Visitor());
    },
};

and the following Less code

@plugin './foo';

.foo(@rules: {}) {
    :root.foo & {
        @rules();
    }
}

.foo {
    .foo({
        --foo: @foo;
    });
}

When trying to compile this code using lessc and/or less-loader, Less fails with error SyntaxError: variable @foo is undefined.

Variables in other contexts (in non-detached rulesets, at top level, in named detached rulests) are visited as expected:

@plugin './foo';

@bar: @foo;

.foo {
    --foo: @foo;
    --bar: @bar;
    @baz();
}

@baz: {
    --baz: @foo;
};

// ↓↓↓↓

.foo {
  --foo: bar;
  --bar: bar;
  --baz: bar;
}

Maybe other node types also affected, but I didn’t test it.

Less version: 3.0.2.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
matthew-deancommented, Jun 23, 2018

Okay! I figured out that for some reason, MixinCall arguments are not visited. It seems that it’s an array of… objects? instead of nodes? I’ve fixed it, but would like to fix some of the permissive parsing of custom properties to try parsing a value first. Otherwise you’ll get a Variable today, but a Quoted after it’s released, and your visitVariable would stop functioning. I’m just now sure how to make the parser “recover” from a bad path. I’m looking to tweak this code that was merged. - See this comment.

So if someone can help me understand how the Less parser works and addresses that comment, I’ll do a PR for this fix.

0reactions
matthew-deancommented, Jun 24, 2018
Read more comments on GitHub >

github_iconTop Results From Across the Web

Fixes #3205, partial 3.0 math regression #1880 #3228 - GitHub
Fixes Variables in detached rulesets not visited by visitor? #3205 - Mixin args not visited by visitor; Improves permissive parsing quite a bit...
Read more >
Is it possible to use interpolation for detached rulesets in LESS?
Rulesets can be stored in mixins not variables : ... "Detached rulesets" is just the wrong tool for what you want.
Read more >
Features In-Depth | Less.js
A detached ruleset is a group of css properties, nested rulesets, media declarations or anything else stored in a variable. You can include...
Read more >
Predefined variables reference - GitLab Docs
Variable GitLab Runner CHAT_CHANNEL 10.6 all CHAT_INPUT 10.6 all CHAT_USER_ID 14.4 all
Read more >
C++ static code analysis: Threads should not be detached
Unique rules to find Bugs, Vulnerabilities, Security Hotspots, and Code Smells in your C++ code · All rules 585 · Vulnerability13 · Bug114...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found