Skip to content

Commit

Permalink
Fix prettier and other rules working at the same time (#360)
Browse files Browse the repository at this point in the history
* add tests for --fix option

* fix issue #354
  • Loading branch information
dartess committed Jul 13, 2024
1 parent 3f808b1 commit 35efe65
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ module.exports = {
'plugin:n/recommended',
'plugin:prettier/recommended',
],
env: {
node: true,
},
};
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ const ruleFunction = (expectation, options, context) => {
// Default to '<input>' if a filepath was not provided.
// This mimics eslint's behaviour
const filepath = root.source.input.file || '<input>';
const source = root.source.input.css;

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

let prettierSource;

const source = root.toString();
try {
prettierSource = await prettier.format(source, prettierOptions);
} catch (err) {
Expand Down Expand Up @@ -182,7 +182,7 @@ const ruleFunction = (expectation, options, context) => {
insertText +
rawData.substring(difference.offset + deleteText.length)
);
}, root.source.input.css);
}, source);

// If root.source.syntax exists then it means stylelint had to use
// postcss-syntax to guess the postcss parser that it should use based
Expand Down
30 changes: 27 additions & 3 deletions test/stylelint-prettier-e2e.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ import {spawnSync} from 'node:child_process';
import {resolve, sep, dirname} from 'node:path';
import {fileURLToPath} from 'node:url';

import stylelint from 'stylelint';

import baseConfig from './fixtures/stylelint.config.js';

const __dirname = dirname(fileURLToPath(import.meta.url));

const stylelintCwd = `${__dirname}/fixtures`;

/**
* Tests that report errors in multiple files may change the order of the files
* across multiple runs.
Expand Down Expand Up @@ -65,15 +71,33 @@ describe('E2E Tests', () => {
assert.strictEqual(result.error, expectedResult);
assert.strictEqual(result.status, 0);
});

/** @see https://github.com/prettier/stylelint-prettier/issues/354 */
test('the --fix option works correctly with other rules', async () => {
const inputCode = `.a {\n color: #ffffff;\n font-size: 16px\n}\n`;
const fixConfig = structuredClone(baseConfig);
fixConfig.rules['color-hex-length'] = 'short';

const {code: outputCode} = await stylelint.lint({
code: inputCode,
configBasedir: stylelintCwd,
fix: true,
config: fixConfig,
});

assert.strictEqual(
outputCode,
'.a {\n color: #fff;\n font-size: 16px;\n}\n'
);
});
});

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

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

return {
Expand All @@ -82,6 +106,6 @@ function runStylelint(pattern) {
error: result.stderr
.toString()
.trim()
.replaceAll(`file=${cwd}${sep}`, 'file='),
.replaceAll(`file=${stylelintCwd}${sep}`, 'file='),
};
}

0 comments on commit 35efe65

Please sign in to comment.