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.

Rule Idea: code instead of variable/mixin use

See original GitHub issue

I have a tough but helpful rule(s) to add. They’re similar but should be split into two rules.

Rule 1: Let’s say I have a variable $color-gray90: #111; . Then, in my code I see color: #111; I would want a rule to check for this and alert that we should be using the variable instead of the declaration.

Rule 2: Similar but perhaps more important, let’s say I have defined a mixin:

@mixin opacity($opacity) {
  opacity: $opacity;
  $opacity-ie: $opacity * 100;
  filter: alpha(opacity=$opacity-ie);
}

Then, in my code I see the following:

button {
  opacity: 0.8;
  filter: alpha(opacity=80);
}

I would like a rule to identify that the user should have used the mixin instead of the definitions directly. This becomes even more valuable when: 1) linting legacy or older code, or 2) when linting after creating a new mixin to determine where else it can be placed.

I find great value in both of these rules. Could they be added to the list of potential additions? They’re probably tough to make but would be extremely helpful.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
shawnbotcommented, Oct 6, 2019

FWIW, I’ve built something like this into our monolithic stylelint-config-primer’s new primer/variables rule. I’m AFK right now so I can’t get too into it, but if anyone would be interested in helping me clean it up and abstract it out, I’d be more than happy to make that happen.

0reactions
branparcommented, Nov 17, 2016

I’ve been thinking about how we might be able to improve this without the performance hit and I came up with an idea. Multiple new rules (specific for variables) under the $-variable category. I’m thinking something like:

dollar-variable-after-color
dollar-variable-after-font
dollar-variable-after-border
...

As an example dollar-variable-after-color would check for the property colorand enforce that only a variable is required there. This way it doesn’t check to see if a variable already exists for the given hex/named color, just that we want to use a variable instead of a hex/color.

The other rule ideas I mentioned are to extend this to things like font, which would then allow enforcement of a variable to pass font family strings, and borderand border-color for the dollar-variable-after-border option.

Thoughts? I think this would be more universal and more appealing as you can enable it for specific aspects of the SCSS. I haven’t figured out a more performant way to enforce mixin usage yet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rule Idea: code instead of variable/mixin use · Issue #80
I would like a rule to identify that the user should have used the mixin instead of the definitions directly. This becomes even...
Read more >
7 Sass techniques to help you write better code
This post explores seven Sass (syntactically awesome stylesheets) techniques that help developers write more efficient code.
Read more >
creating mixins with arguments in LESS
1 Answer 1 ... Yes, it is possible to send parameters/arguments to a mixin and use it to generate the rules. In official...
Read more >
mixin and @include
Mixins are defined using the @mixin at-rule, which is written @mixin <name> { . ... a good idea to only pass arguments by...
Read more >
Sass finally introduced a new module system and nobody ...
The @use rule is the heard of the new concept and makes the changes to the module ... Whenever you declare a variable,...
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