Skip to content

Commit

Permalink
Add meta.fixable property to each rule (#6181)
Browse files Browse the repository at this point in the history
This commit adds a new property `meta.fixable` to each rule.
Also, this fixes inaccurate docs about autofix.

---

I used the following script for [`jscodeshift`](https://github.com/facebook/jscodeshift) to perform the task easily.
(though I needed to fix a few files manually)

```js
/**
 * @type {import('jscodeshift').Transform}
 */
module.exports = function transform(fileInfo, api) {
	const j = api.jscodeshift;
	const root = j(fileInfo.source);

	root.findVariableDeclarators('meta').forEach((decl) => {
		if (root.find(j.Identifier, { name: 'fix' }).length > 0) {
			decl.node.init.properties.push(j.property('init', j.identifier('fixable'), j.literal(true)));
		}
	});

	return root.toSource();
};
```

```console
$ npm i -D jscodeshift @types/jscodeshift
...

$ npx jscodeshift lib/rules/*/index.js
...
```
  • Loading branch information
ybiquitous committed Jun 28, 2022
1 parent a41a632 commit 2e3fa9a
Show file tree
Hide file tree
Showing 105 changed files with 462 additions and 11 deletions.
6 changes: 3 additions & 3 deletions docs/user-guide/rules/list.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Within each cateogory, the rules are grouped by the [_thing_](http://apps.workfl

### Function

- [`function-calc-no-unspaced-operator`](../../../lib/rules/function-calc-no-unspaced-operator/README.md): Disallow an unspaced operator within `calc` functions.
- [`function-calc-no-unspaced-operator`](../../../lib/rules/function-calc-no-unspaced-operator/README.md): Disallow an unspaced operator within `calc` functions (Autofixable).
- [`function-linear-gradient-no-nonstandard-direction`](../../../lib/rules/function-linear-gradient-no-nonstandard-direction/README.md): Disallow direction values in `linear-gradient()` calls that are not valid according to the [standard syntax](https://developer.mozilla.org/en-US/docs/Web/CSS/linear-gradient#Syntax).
- [`function-no-unknown`](../../../lib/rules/function-no-unknown/README.md): Disallow unknown functions.

Expand Down Expand Up @@ -140,7 +140,7 @@ Within each cateogory, the rules are grouped by the [_thing_](http://apps.workfl

### Keyframe selector

- [`keyframe-selector-notation`](../../../lib/rules/keyframe-selector-notation/README.md): Specify keyword or percentage notation for keyframe selectors.
- [`keyframe-selector-notation`](../../../lib/rules/keyframe-selector-notation/README.md): Specify keyword or percentage notation for keyframe selectors (Autofixable).

### Keyframes

Expand Down Expand Up @@ -196,7 +196,7 @@ Within each cateogory, the rules are grouped by the [_thing_](http://apps.workfl
- [`selector-attribute-name-disallowed-list`](../../../lib/rules/selector-attribute-name-disallowed-list/README.md): Specify a list of disallowed attribute names.
- [`selector-attribute-operator-allowed-list`](../../../lib/rules/selector-attribute-operator-allowed-list/README.md): Specify a list of allowed attribute operators.
- [`selector-attribute-operator-disallowed-list`](../../../lib/rules/selector-attribute-operator-disallowed-list/README.md): Specify a list of disallowed attribute operators.
- [`selector-attribute-quotes`](../../../lib/rules/selector-attribute-quotes/README.md): Require or disallow quotes for attribute values.
- [`selector-attribute-quotes`](../../../lib/rules/selector-attribute-quotes/README.md): Require or disallow quotes for attribute values (Autofixable).
- [`selector-class-pattern`](../../../lib/rules/selector-class-pattern/README.md): Specify a pattern for class selectors.
- [`selector-combinator-allowed-list`](../../../lib/rules/selector-combinator-allowed-list/README.md): Specify a list of allowed combinators.
- [`selector-combinator-disallowed-list`](../../../lib/rules/selector-combinator-disallowed-list/README.md): Specify a list of disallowed combinators.
Expand Down
35 changes: 28 additions & 7 deletions lib/rules/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
'use strict';

const fs = require('fs');
const path = require('path');

const rules = require('..');

describe('rules', () => {
for (const [ruleName, rule] of Object.entries(rules)) {
test(`the "${ruleName}" rule has metadata`, () => {
expect(rule.meta).toBeTruthy();
expect(rule.meta.url).toMatch(/^https:\/\/stylelint\.io\/.+/);
});
}
describe('all rules', () => {
test.each(Object.entries(rules))('"%s" should have metadata', (_name, rule) => {
expect(rule.meta).toBeTruthy();
expect(rule.meta.url).toMatch(/^https:\/\/stylelint\.io\/.+/);
expect([true, undefined]).toContain(rule.meta.fixable);
});
});

describe('fixable rules', () => {
const fixableRules = Object.entries(rules).filter(([, rule]) => rule.meta.fixable);

test.each(fixableRules)('"%s" should describe fixable in the doc', async (name) => {
const doc = await fs.promises.readFile(path.join(__dirname, '..', name, 'README.md'), 'utf8');

expect(doc).toMatch('`fix` option');
});

const rulesListDoc = fs.readFileSync(
path.join(__dirname, '..', '..', '..', 'docs', 'user-guide', 'rules', 'list.md'),
'utf8',
);

test.each(fixableRules)('"%s" should describe fixable in the rule list doc', (name) => {
expect(rulesListDoc).toMatch(new RegExp(`^.+\\b${name}\\b.+\\(Autofixable\\)\\.$`, 'm'));
});
});
1 change: 1 addition & 0 deletions lib/rules/alpha-value-notation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/alpha-value-notation',
fixable: true,
};

const ALPHA_PROPS = new Set(['opacity', 'shape-image-threshold']);
Expand Down
1 change: 1 addition & 0 deletions lib/rules/at-rule-empty-line-before/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/at-rule-empty-line-before',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/at-rule-name-case/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/at-rule-name-case',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/at-rule-name-space-after/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/at-rule-name-space-after',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/at-rule-no-vendor-prefix/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/at-rule-no-vendor-prefix',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/at-rule-semicolon-newline-after/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/at-rule-semicolon-newline-after',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/block-closing-brace-empty-line-before/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/block-closing-brace-empty-line-before',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/block-closing-brace-newline-after/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/block-closing-brace-newline-after',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/block-closing-brace-newline-before/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/block-closing-brace-newline-before',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/block-closing-brace-space-before/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/block-closing-brace-space-before',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/block-opening-brace-newline-after/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/block-opening-brace-newline-after',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/block-opening-brace-newline-before/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/block-opening-brace-newline-before',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/block-opening-brace-space-after/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/block-opening-brace-space-after',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/block-opening-brace-space-before/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/block-opening-brace-space-before',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/color-function-notation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/color-function-notation',
fixable: true,
};

const LEGACY_FUNCS = new Set(['rgba', 'hsla']);
Expand Down
1 change: 1 addition & 0 deletions lib/rules/color-hex-case/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/color-hex-case',
fixable: true,
};

const HEX = /^#[0-9A-Za-z]+/;
Expand Down
1 change: 1 addition & 0 deletions lib/rules/color-hex-length/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/color-hex-length',
fixable: true,
};

const HEX = /^#[0-9A-Za-z]+/;
Expand Down
1 change: 1 addition & 0 deletions lib/rules/comment-empty-line-before/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/comment-empty-line-before',
fixable: true,
};

const stylelintCommandPrefix = 'stylelint-';
Expand Down
1 change: 1 addition & 0 deletions lib/rules/comment-whitespace-inside/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/comment-whitespace-inside',
fixable: true,
};

/**
Expand Down
1 change: 1 addition & 0 deletions lib/rules/custom-property-empty-line-before/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/custom-property-empty-line-before',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/declaration-bang-space-after/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/declaration-bang-space-after',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/declaration-bang-space-before/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/declaration-bang-space-before',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/declaration-block-semicolon-newline-after',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/declaration-block-semicolon-space-after/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/declaration-block-semicolon-space-after',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/declaration-block-semicolon-space-before',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/declaration-block-trailing-semicolon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/declaration-block-trailing-semicolon',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/declaration-colon-newline-after/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/declaration-colon-newline-after',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/declaration-colon-space-after/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/declaration-colon-space-after',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/declaration-colon-space-before/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/declaration-colon-space-before',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/declaration-empty-line-before/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/declaration-empty-line-before',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/font-family-name-quotes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/font-family-name-quotes',
fixable: true,
};

/**
Expand Down
1 change: 1 addition & 0 deletions lib/rules/function-calc-no-unspaced-operator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/function-calc-no-unspaced-operator',
fixable: true,
};

const OPERATORS = new Set(['+', '-']);
Expand Down
1 change: 1 addition & 0 deletions lib/rules/function-comma-newline-after/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/function-comma-newline-after',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/function-comma-newline-before/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/function-comma-newline-before',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/function-comma-space-after/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/function-comma-space-after',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/function-comma-space-before/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/function-comma-space-before',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/function-max-empty-lines/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/function-max-empty-lines',
fixable: true,
};

/**
Expand Down
1 change: 1 addition & 0 deletions lib/rules/function-name-case/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/function-name-case',
fixable: true,
};

const mapLowercaseFunctionNamesToCamelCase = new Map();
Expand Down
1 change: 1 addition & 0 deletions lib/rules/function-parentheses-newline-inside/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/function-parentheses-newline-inside',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/function-parentheses-space-inside/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/function-parentheses-space-inside',
fixable: true,
};

/** @type {import('stylelint').Rule} */
Expand Down
1 change: 1 addition & 0 deletions lib/rules/function-whitespace-after/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const messages = ruleMessages(ruleName, {

const meta = {
url: 'https://stylelint.io/user-guide/rules/list/function-whitespace-after',
fixable: true,
};

const ACCEPTABLE_AFTER_CLOSING_PAREN = new Set([')', ',', '}', ':', '/', undefined]);
Expand Down

0 comments on commit 2e3fa9a

Please sign in to comment.