less / less/less.js

Can not find variable when a variable be created in plugin.

Open
#4,241 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
JavaScript
Stars
17k
Forks
3.4k
Avg merge
7h 42m
Merged PRs (30d)
26

Description

To reproduce:

I want to define a variable in plugin. It will insert @x: 33px to current scope.

module.exports = class LessPluginBestMixin {
    install(less, pluginManager, functions) {
        functions.add("x", () => {
            return new less.tree.Declaration(
                "@x",
                new less.tree.Value(new less.tree.Keyword("33px"))
            );
        });
        functions.add("define_var", (key) => {});
    }
};

When I write the code to a less file:

@plugin "the-plugin-path";

div {
    x();
    width: @x;
}

It works and return the css code:

div {
    width: 33px;
}

But when I write the code to a less file:

@plugin "the-plugin-path";

@a: 1px;

div {
    height: @a;
    x();
    width: @x;
}

It not works and return a error cause can not find the variable of @x.

I had tried debug the source code of less and I found the code:
image
The height need read this variable and execute the function of variable and be cached.
It lead to the function of variable can not run again when the width read the @x.

I changed the code that will clear the cache when the function return a Declaration.

I tried to fix the code. URL

Current behavior:

Expected behavior:

Environment information:

  • less version:
  • nodejs version:
  • operating system:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the plugin example with the @a declaration before x() and compare it with the working example. Inspect the variable evaluation and caching logic shown in the issue, then review PR #4240; the issue is done when both @x and @a resolve correctly in the generated CSS.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
compilers, frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.