Skip to content

Commit 35efe65

Browse files
authored
Fix prettier and other rules working at the same time (#360)
* add tests for --fix option * fix issue #354
1 parent 3f808b1 commit 35efe65

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

.eslintrc.cjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ module.exports = {
77
'plugin:n/recommended',
88
'plugin:prettier/recommended',
99
],
10+
env: {
11+
node: true,
12+
},
1013
};

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ const ruleFunction = (expectation, options, context) => {
4343
// Default to '<input>' if a filepath was not provided.
4444
// This mimics eslint's behaviour
4545
const filepath = root.source.input.file || '<input>';
46-
const source = root.source.input.css;
4746

4847
const prettierRcOptions = await prettier.resolveConfig(filepath, {
4948
editorconfig: true,
@@ -107,6 +106,7 @@ const ruleFunction = (expectation, options, context) => {
107106

108107
let prettierSource;
109108

109+
const source = root.toString();
110110
try {
111111
prettierSource = await prettier.format(source, prettierOptions);
112112
} catch (err) {
@@ -182,7 +182,7 @@ const ruleFunction = (expectation, options, context) => {
182182
insertText +
183183
rawData.substring(difference.offset + deleteText.length)
184184
);
185-
}, root.source.input.css);
185+
}, source);
186186

187187
// If root.source.syntax exists then it means stylelint had to use
188188
// postcss-syntax to guess the postcss parser that it should use based

test/stylelint-prettier-e2e.test.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@ import {spawnSync} from 'node:child_process';
44
import {resolve, sep, dirname} from 'node:path';
55
import {fileURLToPath} from 'node:url';
66

7+
import stylelint from 'stylelint';
8+
9+
import baseConfig from './fixtures/stylelint.config.js';
10+
711
const __dirname = dirname(fileURLToPath(import.meta.url));
812

13+
const stylelintCwd = `${__dirname}/fixtures`;
14+
915
/**
1016
* Tests that report errors in multiple files may change the order of the files
1117
* across multiple runs.
@@ -65,15 +71,33 @@ describe('E2E Tests', () => {
6571
assert.strictEqual(result.error, expectedResult);
6672
assert.strictEqual(result.status, 0);
6773
});
74+
75+
/** @see https://github.com/prettier/stylelint-prettier/issues/354 */
76+
test('the --fix option works correctly with other rules', async () => {
77+
const inputCode = `.a {\n color: #ffffff;\n font-size: 16px\n}\n`;
78+
const fixConfig = structuredClone(baseConfig);
79+
fixConfig.rules['color-hex-length'] = 'short';
80+
81+
const {code: outputCode} = await stylelint.lint({
82+
code: inputCode,
83+
configBasedir: stylelintCwd,
84+
fix: true,
85+
config: fixConfig,
86+
});
87+
88+
assert.strictEqual(
89+
outputCode,
90+
'.a {\n color: #fff;\n font-size: 16px;\n}\n'
91+
);
92+
});
6893
});
6994

7095
function runStylelint(pattern) {
7196
const stylelintCmd = resolve(`${__dirname}/../node_modules/.bin/stylelint`);
72-
const cwd = `${__dirname}/fixtures`;
7397

7498
// Use github formatter as it is less likely to change across releases
7599
const result = spawnSync(stylelintCmd, ['--formatter=github', pattern], {
76-
cwd,
100+
cwd: stylelintCwd,
77101
});
78102

79103
return {
@@ -82,6 +106,6 @@ function runStylelint(pattern) {
82106
error: result.stderr
83107
.toString()
84108
.trim()
85-
.replaceAll(`file=${cwd}${sep}`, 'file='),
109+
.replaceAll(`file=${stylelintCwd}${sep}`, 'file='),
86110
};
87111
}

0 commit comments

Comments
 (0)