Stylelint Rule Creator
This is not an official stylelint package.
Create custom stylelint rules with less boilerplate.
This package greatly reduces the complexity of creating custom stylelint rules. All that is needed is calling a single function to generate a testable and directly exportable rule to stylelint as a plugin. All the necessary types are also included to keep everything type safe.
There are two ways of creating a decreased-boilerplate rule with this package. Both export an object which can be directly exported as a
Plugin for stylint.
- There's the opinionated
DefaultRuleis very easy to test, automatically generates tests based on those supplied to it, and is much more type safe.
- There's the bare-bones
createRule. This is less opinionated than
DefaultRulebut requires more checking in the rule and setup for tests.
If possible, prefer using
createDefaultRule to have the best experience.
npm install stylelint-rule-creator
This is the recommended way of creating a rule.
createDefaultRule creates a
DefaultRule object which greatly reduces boilerplate needed for type checking and testing. Extra tests are automatically generated from the tests you supply. Stricter typing is enforced on
DefaultRule and its tests.
DefaultRule requires that the rule is always enabled with a single option (no secondary options) which is either a boolean or an object, as seen in the following type:
This type can easily be extended to add more properties to the object portion of that format (which is done in the example
DefaultRule), like so:
If more flexibility is needed, use the simpler
createRule which is explained in a later section.
DefaultRule like so:
For more info, see the source code here, which is heavily documented.
DefaultRule (the output of
createDefaultRule) is very simple and requires very little boilerplate.
For more info, see the default rule test source code here, which is heavily documented.
This creates a basic
Rule which can be directly exported to stylelint as a plugin just as the above
DefaultRule can be.
Rule is much simpler than
DefaultRule and doesn't provide as many typing or testing benefits. It, however, makes less requirements on your input rule options. For example, this allows primary and secondary options (whereas
DefaultRule requires all information to be in a singular primary option).
To create a
Rule, use the following:
When not using
createDefaultRule, testing is more verbose and requires more boilerplate.
This package exports the functionality of the
stylelint-jest-rule-tester package to be used for these testing purposes.
In a central file somewhere, create your
testRule function by calling
getTestRuleFunction with a reference to your plugin's main file:
In this example,
dist/index.js should be the file which is exporting your rules as its default export. See this plugin test file as an example of this.
Then, in your rule's test file, import and call
testRule like so: