How to use the createPlugin function from stylelint

Find comprehensive JavaScript stylelint.createPlugin code examples handpicked from public code repositorys.

stylelint.createPlugin is a function that creates a stylelint plugin with custom linting rules.

130
131
132
133
134
135
136
137


ruleFunction.ruleName = ruleName;
ruleFunction.messages = ruleMessages;
ruleFunction.meta = ruleMeta;


module.exports = stylelint.createPlugin(ruleName, ruleFunction);
fork icon1
star icon57
watch icon1

+ 9 other calls in file

36
37
38
39
40
41
42
43
44
45
46
 * @typedef {Object} PrimaryOptions
 * @property {AllowedPatterns} [allowedProperties]
 * @property {{[property: string]: AllowedPatterns}} [allowedValues]
 */


const {rule} = stylelint.createPlugin(
  ruleName,
  /** @param {PrimaryOptions} primary */
  (primary) => {
    return (root, result) => {
fork icon0
star icon1
watch icon1

+ 17 other calls in file

How does stylelint.createPlugin work?

stylelint.createPlugin is a method in the stylelint library that allows developers to create custom plugins to extend the functionality of the linter by defining rules and their associated logic. It takes an object as an argument that specifies the name, rule definition, and configuration options for the plugin, and returns an object with methods to load the plugin and handle its execution. When executed, the plugin performs the specified rule validation on CSS files and reports any errors or warnings.

11
12
13
14
15
16
17
18
19
20
21
22


const PX_PER_REM = 16;
const unitRegex = /(px|rem)$/;
const numberRegex = /^([-0-9.]+)/;


module.exports = stylelint.createPlugin(ruleName, (primaryOption, secondaryOptionObject, context) => {
  const secondaryOptions = secondaryOptionObject || {};
  return (root, result) => {
    const validOptions = stylelint.utils.validateOptions(
      result,
fork icon0
star icon0
watch icon1

53
54
55
56
57
58
59
60
61
      });
    });
  });
};


module.exports = stylelint.createPlugin(ruleName, rule);
module.exports.ruleName = ruleName;
module.exports.messages = messages;
fork icon0
star icon0
watch icon6

+ 22 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const { createPlugin } = require("stylelint");

const ruleName = "my-rule";
const ruleMessage = "This is my custom rule.";

const myRulePlugin = createPlugin(ruleName, (input, _, context) => {
  return (root, result) => {
    root.walkRules((rule) => {
      if (rule.selector === ".my-class") {
        context.report({
          message: ruleMessage,
          node: rule,
          result,
          ruleName,
        });
      }
    });
  };
});

module.exports = myRulePlugin;

This example creates a custom stylelint rule called "my-rule" that reports an error message if any rules in the stylesheet contain the selector .my-class. The plugin is created using the stylelint.createPlugin function, which takes the name of the rule and a function that returns a stylelint rule function. The rule function is passed a root object representing the root node of the CSS abstract syntax tree, a result object representing the stylelint result, and a context object containing utility functions for reporting errors and warnings. In this example, the context.report() function is used to report an error message with the specified rule name and message when the rule selector matches .my-class.