diff --git a/.eslintrc.cjs b/.eslintrc.cjs index b477205..93e9dcc 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -7,4 +7,7 @@ module.exports = { 'plugin:n/recommended', 'plugin:prettier/recommended', ], + env: { + node: true, + }, }; diff --git a/index.js b/index.js index 15e0ff4..2814d10 100644 --- a/index.js +++ b/index.js @@ -43,7 +43,6 @@ const ruleFunction = (expectation, options, context) => { // Default to '' if a filepath was not provided. // This mimics eslint's behaviour const filepath = root.source.input.file || ''; - const source = root.source.input.css; const prettierRcOptions = await prettier.resolveConfig(filepath, { editorconfig: true, @@ -107,6 +106,7 @@ const ruleFunction = (expectation, options, context) => { let prettierSource; + const source = root.toString(); try { prettierSource = await prettier.format(source, prettierOptions); } catch (err) { @@ -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 diff --git a/test/stylelint-prettier-e2e.test.js b/test/stylelint-prettier-e2e.test.js index e52bafa..5f86200 100644 --- a/test/stylelint-prettier-e2e.test.js +++ b/test/stylelint-prettier-e2e.test.js @@ -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. @@ -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 { @@ -82,6 +106,6 @@ function runStylelint(pattern) { error: result.stderr .toString() .trim() - .replaceAll(`file=${cwd}${sep}`, 'file='), + .replaceAll(`file=${stylelintCwd}${sep}`, 'file='), }; }