Skip to content

Commit

Permalink
Add disableFix secondary option (#5460)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuangrs committed Sep 16, 2021
1 parent 5105ac2 commit 6ab4199
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 2 deletions.
20 changes: 20 additions & 0 deletions docs/user-guide/configure.md
Expand Up @@ -158,6 +158,26 @@ For example:

The report is considered to be a lint error.

### `disableFix`

You can set the `disableFix` secondary option to disable autofix _on a per rule basis_.

For example:

```json
{
"rules": {
"indentation": [
2,
{
"except": ["value"],
"disableFix": true
}
]
}
}
```

## Disable Errors

These configurations provide extra validation for `stylelint-disable` comments. This can be helpful for enforcing useful and well-documented disables.
Expand Down
93 changes: 93 additions & 0 deletions lib/__tests__/standalone-fix.test.js
Expand Up @@ -225,3 +225,96 @@ describe('writing fixes to files', () => {
expect(fileContent.startsWith('\uFEFF')).toBe(true);
});
});

it('one rule being disabled', async () => {
const code = `
a {
color: red;
}`;

const result = await standalone({
code,
config: {
rules: {
indentation: [
2,
{
disableFix: true,
},
],
},
},
fix: true,
});

expect(JSON.parse(result.output)[0].errored).toBe(true);
});

it('two rules being disabled', async () => {
const code = `
a {
COLOR: red;
}`;

const result = await standalone({
code,
config: {
rules: {
indentation: [
2,
{
disableFix: true,
},
],
'property-case': [
'lower',
{
disableFix: true,
},
],
},
},
fix: true,
});

const warnings = JSON.parse(result.output)[0].warnings;

expect(warnings.some((w) => w.text === 'Expected indentation of 0 spaces (indentation)')).toBe(
true,
);
expect(warnings.some((w) => w.text === 'Expected "COLOR" to be "color" (property-case)')).toBe(
true,
);
});

it('one rule being disabled and another still autofixing', async () => {
const code = `
a {
COLOR: red;
}`;

const result = await standalone({
code,
config: {
rules: {
indentation: [
2,
{
disableFix: true,
},
],
'property-case': ['lower'],
},
},
fix: true,
});

const warnings = JSON.parse(result.output)[0].warnings;

expect(warnings.some((w) => w.text === 'Expected indentation of 0 spaces (indentation)')).toBe(
true,
);
expect(warnings.some((w) => w.text === 'Expected "COLOR" to be "color" (property-case)')).toBe(
false,
);
});
7 changes: 7 additions & 0 deletions lib/lintPostcssResult.js
Expand Up @@ -87,6 +87,12 @@ function lintPostcssResult(stylelintOptions, postcssResult, config) {

// Log the rule's severity in the PostCSS result
const defaultSeverity = config.defaultSeverity || 'error';
// disableFix in secondary option
const disableFix = (secondaryOptions && secondaryOptions.disableFix === true) || false;

if (disableFix) {
postcssResult.stylelint.ruleDisableFix = true;
}

postcssResult.stylelint.ruleSeverities[ruleName] =
(secondaryOptions && secondaryOptions.severity) || defaultSeverity;
Expand All @@ -97,6 +103,7 @@ function lintPostcssResult(stylelintOptions, postcssResult, config) {
postcssRoots.map((postcssRoot) =>
ruleFunction(primaryOption, secondaryOptions, {
fix:
!disableFix &&
stylelintOptions.fix &&
// Next two conditionals are temporary measures until #2643 is resolved
isFileFixCompatible &&
Expand Down
8 changes: 7 additions & 1 deletion lib/standalone.js
Expand Up @@ -151,7 +151,12 @@ module.exports = function (options) {
const postcssResult = stylelintResult._postcssResult;
const returnValue = prepareReturnValue([stylelintResult], options, formatterFunction);

if (options.fix && postcssResult && !postcssResult.stylelint.ignored) {
if (
options.fix &&
postcssResult &&
!postcssResult.stylelint.ignored &&
!postcssResult.stylelint.ruleDisableFix
) {
if (!postcssResult.stylelint.disableWritingFix) {
// If we're fixing, the output should be the fixed code
returnValue.output = postcssResult.root.toString(postcssResult.opts.syntax);
Expand Down Expand Up @@ -251,6 +256,7 @@ module.exports = function (options) {
postcssResult.root &&
postcssResult.opts &&
!postcssResult.stylelint.ignored &&
!postcssResult.stylelint.ruleDisableFix &&
options.fix &&
!postcssResult.stylelint.disableWritingFix
) {
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/validateOptions.js
Expand Up @@ -3,7 +3,7 @@
const arrayEqual = require('./arrayEqual');
const { isPlainObject } = require('is-plain-object');

const IGNORED_OPTIONS = new Set(['severity', 'message', 'reportDisables']);
const IGNORED_OPTIONS = new Set(['severity', 'message', 'reportDisables', 'disableFix']);

/** @typedef {(value: unknown) => boolean} PossibleFunc */

Expand Down
1 change: 1 addition & 0 deletions types/stylelint/index.d.ts
Expand Up @@ -94,6 +94,7 @@ declare module 'stylelint' {
stylelintError?: boolean;
disableWritingFix?: boolean;
config?: StylelintConfig;
ruleDisableFix?: boolean;
};

type EmptyResult = {
Expand Down

0 comments on commit 6ab4199

Please sign in to comment.