Skip to content

Commit

Permalink
Prettier v3 (#305)
Browse files Browse the repository at this point in the history
Minimum supported Prettier version is v3.0.0
Minimum supported stylelint version is 15.8.0

- Use `await import()` instead of require.
- Use async (and now the only) versions of resolveConfig, getFileInfo
  and format
- Tweak jest/eslint configs to account for prettier using esmodules
- Ignore testing svelte files as prettier-plugin-svelte doesn't yet have
  a release that supports prettier v3
  • Loading branch information
BPScott authored Jul 7, 2023
1 parent cb2ad8d commit 3ac94a4
Show file tree
Hide file tree
Showing 6 changed files with 662 additions and 700 deletions.
5 changes: 5 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ module.exports = {
plugins: ['node'],
extends: ['plugin:node/recommended', 'plugin:prettier/recommended'],
env: {node: true, jest: true},
parserOptions: {ecmaVersion: '2020'},
root: true,
rules: {
'node/no-unpublished-require': ['error', {allowModules: ['stylelint']}],
'node/no-missing-require': ['error', {allowModules: ['styled-components']}],
'node/no-unsupported-features/es-syntax': [
'error',
{ignores: ['dynamicImport']},
],
},
};
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
fail-fast: false
matrix:
stylelint-version: [14.x, 15.x]
stylelint-version: [15.x]
node-version: [18.x, 16.x, 14.x]

steps:
Expand Down
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"main": "stylelint-prettier.js",
"scripts": {
"lint": "eslint .",
"test": "yarn run lint && jest",
"test": "yarn run lint && node --experimental-vm-modules node_modules/jest/bin/jest.js",
"format": "yarn run prettier '**/*.{js,json,md}' --write && yarn run lint --fix"
},
"repository": {
Expand All @@ -32,26 +32,26 @@
"prettier-linter-helpers": "^1.0.0"
},
"peerDependencies": {
"prettier": ">=2.0.0",
"stylelint": ">=14.0.0"
"prettier": ">=3.0.0",
"stylelint": ">=15.8.0"
},
"devDependencies": {
"eslint": "^8.34.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^29.4.3",
"eslint-plugin-prettier": "^5.0.0-alpha.2",
"jest": "^29.6.1",
"jest-preset-stylelint": "^6.1.0",
"postcss": "^8.4.21",
"postcss-html": "^1.5.0",
"postcss-markdown": "^1.2.0",
"postcss-scss": "^4.0.6",
"postcss-styled-syntax": "^0.3.3",
"postcss-syntax": "^0.36.2",
"prettier": "^2.8.4",
"prettier": "^3.0.0",
"prettier-plugin-svelte": "^2.9.0",
"strip-ansi": "^6.0.0",
"stylelint": "^15.0.0",
"stylelint": "^15.8.0",
"svelte": "^3.55.0",
"typescript": "4.9.5"
},
Expand All @@ -60,9 +60,10 @@
},
"jest": {
"preset": "jest-preset-stylelint",
"transform": {},
"setupFiles": [
"./jest-setup.js"
]
},
"license": "MIT"
}
}
12 changes: 7 additions & 5 deletions stylelint-prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const {
generateDifferences,
} = require('prettier-linter-helpers');

const prettierPromise = import('prettier');

const {INSERT, DELETE, REPLACE} = generateDifferences;

let prettier;
Expand All @@ -21,7 +23,7 @@ const messages = stylelint.utils.ruleMessages(ruleName, {
module.exports = stylelint.createPlugin(
ruleName,
(expectation, options, context) => {
return (root, result) => {
return async (root, result) => {
const validOptions = stylelint.utils.validateOptions(result, ruleName, {
actual: expectation,
});
Expand All @@ -39,19 +41,19 @@ module.exports = stylelint.createPlugin(

if (!prettier) {
// Prettier is expensive to load, so only load it if needed.
prettier = require('prettier');
prettier = await prettierPromise;
}

// 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 = prettier.resolveConfig.sync(filepath, {
const prettierRcOptions = await prettier.resolveConfig(filepath, {
editorconfig: true,
});

const prettierFileInfo = prettier.getFileInfo.sync(filepath, {
const prettierFileInfo = await prettier.getFileInfo(filepath, {
resolveConfig: true,
ignorePath: '.prettierignore',
});
Expand Down Expand Up @@ -104,7 +106,7 @@ module.exports = stylelint.createPlugin(
{filepath}
);
try {
prettierSource = prettier.format(source, prettierOptions);
prettierSource = await prettier.format(source, prettierOptions);
} catch (err) {
if (!(err instanceof SyntaxError)) {
throw err;
Expand Down
5 changes: 4 additions & 1 deletion test/stylelint-prettier-e2e.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ check.invalid.scss
* files
*/
test('HTML/Markdown/Vue/Svelte files', () => {
const result = runStylelint('*.{html,md,vue,svelte}');
// Once prettier-plugin-svelte is updated to support Prettier v3
// bring back testing against svelte files
// const result = runStylelint('*.{html,md,vue,svelte}');
const result = runStylelint('*.{html,md,vue}');

const expectedResult = ``;

Expand Down
Loading

0 comments on commit 3ac94a4

Please sign in to comment.