Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undeclared variable with LESS @import. What is causing this? Is there a fix?

I have 2 LESS files. Globals.less and Site.less. Globals.less contains all of my global (go figure) variables and an import to a CSS reset definition. Site.less contains the styles in use.

Globals.less:

//Imports
@import "CSSReset.less";

//Colors
@color-background: rgb(0, 0 , 0);

Site.less:

@import "Globals.less";

body {
    background: @color-background url('/Images/BackgroundTextureBody.png');
}

The problem is this: In Visual Studio @color-background in Site.less is underlined and the error is "Undeclared variable", but the LESS compiles to CSS just fine and the background color is properly set to #000. It is more of an annoyance than anything, but I lose Intellisense and I get warnings in my error list. I would like the editor to act as expected and be able to "see" my declarations in Globals.less when I am editing Site.less. Am I doing something wrong, is this a bug, or is my environment not setup correctly?

like image 376
TheHurt Avatar asked Dec 09 '12 19:12

TheHurt


People also ask

What type of error is undeclared variable?

Undeclared: It occurs when we try to access any variable that is not initialized or declared earlier using var or const keyword. If we use 'typeof' operator to get the value of an undeclared variable, we will face the runtime error with return value as “undefined”.

What is the meaning of undeclared variable?

Undeclared variables do not exist until the code assigning to them is executed. Declared variables are a non-configurable property of their execution context (function or global). Undeclared variables are configurable (e.g. can be deleted).

What does undeclared mean in C programming?

The identifier is undeclared: In any programming language, all variables have to be declared before they are used. If you try to use the name of a such that hasn't been declared yet, an “undeclared identifier” compile-error will occur. Example: #include <stdio.h> int main()

What is undeclared variable in javascript?

Undeclared variable means that the variable does not exist in the program at all.


2 Answers

To get intellisense for a particular less file you can add a reference path in the same way you would to get intellisense in a js file.

Example

/// <reference path="Globals.less" />

@import "Globals.less";

body {
    background: @color-background url('/Images/BackgroundTextureBody.png');
}
like image 51
Colin Bacon Avatar answered Oct 07 '22 00:10

Colin Bacon


It appears that Visual Studio (or it's LESS interpreter) does not understand the scope of the variable within the imported Globals.less

Importing variables is a normal and common thing to do so I'd suggest that it's a bug or missing feature in your Visual Studio setup.

like image 43
mnorrish Avatar answered Oct 06 '22 23:10

mnorrish