Skip to content

Commit

Permalink
Fix unexpected output in Node.js API lint result when any rule cont…
Browse files Browse the repository at this point in the history
…ains `disableFix: true` (#6543)

Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com>
  • Loading branch information
adrianjost and ybiquitous committed Dec 28, 2022
1 parent c0db3fd commit b165c0b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-cheetahs-complain.md
@@ -0,0 +1,5 @@
---
"stylelint": patch
---

Fixed: unexpected `output` in Node.js API lint result when any rule contains `disableFix: true`
24 changes: 14 additions & 10 deletions lib/__tests__/standalone-fix.test.js
@@ -1,7 +1,7 @@
'use strict';

const path = require('path');
const { stripIndent } = require('common-tags');
const { stripIndent, stripIndents } = require('common-tags');
const fs = require('fs').promises;

const removeFile = require('../testUtils/removeFile');
Expand Down Expand Up @@ -246,7 +246,8 @@ it('one rule being disabled', async () => {
fix: true,
});

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

it('two rules being disabled', async () => {
Expand Down Expand Up @@ -276,18 +277,20 @@ it('two rules being disabled', async () => {
fix: true,
});

const warnings = JSON.parse(result.output)[0].warnings;
const warnings = result.results[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,
);
expect(result.output).toBe(code);
});

it('one rule being disabled and another still autofixing', async () => {
const code = `
// use stripIndent to remove first linebreak that is also removed in the stripIndents expect
const code = stripIndent`
a {
COLOR: red;
}`;
Expand All @@ -296,24 +299,25 @@ it('one rule being disabled and another still autofixing', async () => {
code,
config: {
rules: {
indentation: [
2,
indentation: [0],
'property-case': [
'lower',
{
disableFix: true,
},
],
'property-case': ['lower'],
},
},
fix: true,
});

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

expect(warnings.some((w) => w.text === 'Expected indentation of 0 spaces (indentation)')).toBe(
true,
false,
);
expect(warnings.some((w) => w.text === 'Expected "COLOR" to be "color" (property-case)')).toBe(
false,
true,
);
expect(result.output).toBe(stripIndents(code));
});
4 changes: 0 additions & 4 deletions lib/lintPostcssResult.js
Expand Up @@ -91,10 +91,6 @@ function lintPostcssResult(stylelintOptions, postcssResult, config) {
// 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;
postcssResult.stylelint.customMessages[ruleName] = secondaryOptions && secondaryOptions.message;
Expand Down
7 changes: 1 addition & 6 deletions lib/standalone.js
Expand Up @@ -139,12 +139,7 @@ async function standalone({
const postcssResult = stylelintResult._postcssResult;
const returnValue = prepareReturnValue([stylelintResult], maxWarnings, formatterFunction, cwd);

if (
fix &&
postcssResult &&
!postcssResult.stylelint.ignored &&
!postcssResult.stylelint.ruleDisableFix
) {
if (fix && postcssResult && !postcssResult.stylelint.ignored) {
returnValue.output =
!postcssResult.stylelint.disableWritingFix && postcssResult.opts
? // If we're fixing, the output should be the fixed code
Expand Down
1 change: 0 additions & 1 deletion types/stylelint/index.d.ts
Expand Up @@ -110,7 +110,6 @@ declare module 'stylelint' {
stylelintWarning?: boolean;
disableWritingFix?: boolean;
config?: Config;
ruleDisableFix?: boolean;
};

type EmptyResult = {
Expand Down

0 comments on commit b165c0b

Please sign in to comment.