Skip to content

Commit a5e3748

Browse files
feat: recommended preset (#28)
* ADD `recommended` configuration * docs: more infos about the `recommended` preset
1 parent e57a25d commit a5e3748

File tree

3 files changed

+74
-16
lines changed

3 files changed

+74
-16
lines changed

README.md

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,19 @@ Rules enforcing best practices and consistency using [Tailwind CSS](https://tail
1010
>
1111
> 👍 Most of [the new JIT mode features](https://tailwindcss.com/docs/just-in-time-mode#new-features) are also supported.
1212
13+
## Supported Rules
14+
15+
Learn more about each supported rules by reading their documentation:
16+
17+
- [`classnames-order`](docs/rules/classnames-order.md): order classnames by target properties then by variants (`[size:][theme:][state:]`)
18+
- [`no-custom-classname`](docs/rules/no-custom-classname.md): only allow classnames from Tailwind CSS and the values from the `whitelist` option
19+
- [`no-contradicting-classname`](docs/rules/no-contradicting-classname.md): e.g. avoid `p-2 p-3`, different Tailwind CSS classnames (`pt-2` & `pt-3`) but targeting the same property several times for the same variant.
20+
21+
Using ESLint extension for Visual Studio Code, you will get these messages
1322
![detected-errors](https://user-images.githubusercontent.com/704026/120854472-83567f80-c57d-11eb-9eaa-18d33b98c60c.png)
1423

24+
You can can the same information on your favorite command line software as well.
25+
1526
## Installation
1627

1728
You'll first need to install [ESLint](http://eslint.org):
@@ -26,10 +37,6 @@ Next, install `eslint-plugin-tailwindcss`:
2637
$ npm i eslint-plugin-tailwindcss --save-dev
2738
```
2839

29-
[eslint-plugin-tailwindcss on npm](https://www.npmjs.com/package/eslint-plugin-tailwindcss)
30-
31-
## Usage
32-
3340
Add `tailwindcss` to the plugins section of your `.eslintrc` configuration file. You can omit the `eslint-plugin-` prefix:
3441

3542
```json
@@ -38,8 +45,22 @@ Add `tailwindcss` to the plugins section of your `.eslintrc` configuration file.
3845
}
3946
```
4047

48+
## Configuration
49+
50+
Use our preset to get reasonable defaults:
51+
52+
```
53+
"extends": [
54+
"plugin:tailwindcss/recommended"
55+
]
56+
```
57+
58+
If you do not use a preset you will need to specify individual rules and add extra configuration:
59+
4160
Configure the rules you want to use under the rules section.
4261

62+
> The following lines are matching the configuration saved `recommended` preset...
63+
4364
```json
4465
{
4566
"rules": {
@@ -83,14 +104,6 @@ The plugin will look for each setting value in this order and stop looking as so
83104
2. In the shared settings (plugin level)
84105
3. Default value of the requested setting (plugin level)...
85106

86-
## Supported Rules
87-
88-
Learn more about each supported rules by reading their documentation:
89-
90-
- [`classnames-order`](docs/rules/classnames-order.md): order classnames by target properties then by variants (`[size:][theme:][state:]`)
91-
- [`no-custom-classname`](docs/rules/no-custom-classname.md): only allow classnames from Tailwind CSS and the values from the `whitelist` option
92-
- [`no-contradicting-classname`](docs/rules/no-contradicting-classname.md): e.g. avoid `p-2 p-3`, different Tailwind CSS classnames (`pt-2` & `pt-3`) but targeting the same property several times for the same variant.
93-
94107
## Upcoming Rules
95108

96109
- `no-redundant-variant`: e.g. avoid `mx-5 sm:mx-5`, no need to redefine `mx` in `sm:` variant as it uses the same value (`5`)

lib/index.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,24 @@
1010

1111
// import all rules in lib/rules
1212
var base = __dirname + '/rules/';
13-
module.exports.rules = {
14-
'classnames-order': require(base + 'classnames-order'),
15-
'no-contradicting-classname': require(base + 'no-contradicting-classname'),
16-
'no-custom-classname': require(base + 'no-custom-classname'),
13+
module.exports = {
14+
rules: {
15+
'classnames-order': require(base + 'classnames-order'),
16+
'no-contradicting-classname': require(base + 'no-contradicting-classname'),
17+
'no-custom-classname': require(base + 'no-custom-classname'),
18+
},
19+
configs: {
20+
recommended: {
21+
parserOptions: {
22+
ecmaFeatures: {
23+
jsx: true,
24+
},
25+
},
26+
rules: {
27+
'tailwindcss/classnames-order': 'warn',
28+
'tailwindcss/no-custom-classname': 'warn',
29+
'tailwindcss/no-contradicting-classname': 'error',
30+
},
31+
},
32+
},
1733
};

tests/lib/index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @fileoverview Use a consistent orders for the Tailwind CSS classnames, based on property then on variants
3+
* @author François Massart
4+
*/
5+
"use strict";
6+
7+
var plugin = require("../../lib/index");
8+
9+
var assert = require("assert");
10+
var fs = require("fs");
11+
var path = require("path");
12+
13+
var rules = fs.readdirSync(path.resolve(__dirname, "../../lib/rules/")).map(function (f) {
14+
return path.basename(f, ".js");
15+
});
16+
17+
describe("all rule files should be exported by the plugin", function () {
18+
rules.forEach(function (ruleName) {
19+
it(`should export ${ruleName}`, function () {
20+
assert.equal(plugin.rules[ruleName], require(path.join("../../lib/rules", ruleName)));
21+
});
22+
});
23+
});
24+
25+
describe("configurations", function () {
26+
it(`should export a "recommended" configuration`, function () {
27+
assert(plugin.configs.recommended);
28+
});
29+
});

0 commit comments

Comments
 (0)