npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

stylelint-declaration-use-variable-or-custom-fn

v1.0.1

Published

A stylelint custom rule to check the use of scss variable or custom function on declaration.

Downloads

303

Readme

stylelint-declaration-use-variable-or-custom-fn

Build Status

A stylelint plugin that check the use of scss, less or custom css variable on declaration. Either used with '$', map-get(), '@', 'var(--var-name)', or with a user-specified custom function(s).

Installation

npm install stylelint-declaration-use-variable-or-palette

Be warned: v1.0.0+ is only compatible with stylelint v3+.

Usage

Add it to your stylelint config plugins array, then add "declaration-use-variable-or-custom-fn" to your rules, specifying the property for which you want to check the usage of variable.

Basic usage:

// .stylelintrc
{
  "plugins": [
    "stylelint-declaration-use-variable-or-custom-fn"
  ],
  "rules": {
    // ...
    "chrismendis/declaration-use-variable-or-custom-fn": "color",
    // ...
  }
}

Multiple properties

Multiple properties can be watched by passing them inside array. Regex can also be used inside arrays.

// .stylelintrc
"rules": {
  // ...
  "chrismendis/declaration-use-variable-or-custom-fn": [["/color/", "z-index", "font-size"]],
  // ...
}

Regex support

Passing a regex will watch the variable usage for all matching properties. This rule will match all CSS properties while ignoring Sass and Less variables.

// .stylelintrc
"rules": {
  // ...
  "chrismendis/declaration-use-variable-or-custom-fn": "/color/",
  // ...
}

Custom function support

Passing an object with properties props and functionNames will check the variable or custom function usage for all matching properties of props.

// .stylelintrc
"rules": {
  // ...
  "chrismendis/declaration-use-variable-or-custom-fn": {comparison: "color", functionNames: ["myCustomFn"]},
  // ...
}

Details

Preprocessers like scss, less uses variables to make the code clean, maintainable and reusable. But since developers are lazy they might get a chance to miss the use of variables in styling code and that kinda sucks.

$some-cool-color: #efefef;

.foo {
    display: inline-block;
    text-align: center;
    color: #efefef; // Wait a min! there should be a variable.
}

Supported scss variables

Scss variables using '$' notation and map-get are supported

// Simple variables
$some-cool-color: #efefef;
$some-integer: 123;
$some-pixels: 4px;

color: $some-cool-color;

// Using map-get
$cool-colors: (
    blue: #3ea1ec,
    red: #eb5745,
);

color: map-get($cool-colors, blue);

Supported color functions

If you plan to level-up the usage of your variables by the usage of fancy color-functions - you can!

Instead of a direct variable usage like this:

$white: #fff;

.foo {
  color: $white;
}

You could also do things like:

.foo {
  color: color($white shade(10%));
}

This will still be seen as a valid code by the plugin. As a side-note, it doesn't matter what kind of variable-syntax you are using in the color function itself - if the line starts with color( then it is seen as valid.

If you contribute and wonder about the check for color( - it couldn't be done via regex because not only values will be passed to the plugin, but also property names. For some reason, the plugin just dies if you start extending the regex to look for "color". So it must be done via extra-if-check.