Skip to content

Commit

Permalink
Fix Unexpected Node API output when any rule contains `disableFix: tr…
Browse files Browse the repository at this point in the history
…ue` stylelint#6542

fixes stylelint#6542 and bv that should also fix stylelint/vscode-stylelint#369

Issue: The API did not return the fixed css in `output` if any rule contained `ruleDisableFix`.

I have removed the `ruleDisableFix` code, because it did not affect the code fixing itself and was assumingly wrongly used to change the content format of `output`.
I have adjusted some tests to adapt to the changed behaviour and extended the test conditions to check that the returned code has the correct fixes applied.

Issue was introduced in stylelint#5460
  • Loading branch information
adrianjost committed Dec 23, 2022
1 parent cae5880 commit 8f972da
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 19 deletions.
19 changes: 11 additions & 8 deletions lib/__tests__/standalone-fix.test.js
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('\n a {\n color: red;\n }');
});

it('two rules being disabled', async () => {
Expand Down Expand Up @@ -276,14 +277,15 @@ 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('\n a {\n COLOR: red;\n }');
});

it('one rule being disabled and another still autofixing', async () => {
Expand All @@ -296,24 +298,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('\na {\nCOLOR: red;\n}');
});
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 8f972da

Please sign in to comment.