Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unexpected output in Node.js API lint result when any rule contains disableFix: true #6543

Merged
merged 3 commits into from Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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