Skip to content

Commit

Permalink
Add prettier opinionated formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Dec 18, 2022
1 parent 4bab7d9 commit a439660
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 2 deletions.
16 changes: 16 additions & 0 deletions .github/fix-prettier-14007-no-unstable-semicolon.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
diff --git a/src/language-css/printer-postcss.js b/src/language-css/printer-postcss.js
index 4ba444c8d..eaa5e38ff 100644
--- a/src/language-css/printer-postcss.js
+++ b/src/language-css/printer-postcss.js
@@ -101,7 +101,10 @@ function genericPrint(path, options, print) {
return [node.raw, hardline];
case "css-root": {
const nodes = printNodeSequence(path, options, print);
- const after = node.raws.after.trim();
+ let after = node.raws.after.trim();
+ if (after.startsWith(";")) {
+ after = after.slice(1).trim();
+ }

return [
nodes,
23 changes: 23 additions & 0 deletions .github/fix-prettier-14008-no-space-after-unary-minus.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
diff --git a/src/language-css/printer-postcss.js b/src/language-css/printer-postcss.js
index 4ba444c8d..a15f7981f 100644
--- a/src/language-css/printer-postcss.js
+++ b/src/language-css/printer-postcss.js
@@ -744,6 +744,18 @@ function genericPrint(path, options, print) {
continue;
}

+ // No space before unary minus followed by an opening parenthesis `-(`
+ if (
+ (options.parser === "scss" || options.parser === "less") &&
+ isMathOperator &&
+ iNode.value === "-" &&
+ isParenGroupNode(iNextNode) &&
+ locEnd(iNode) === locStart(iNextNode.open) &&
+ iNextNode.open.value === "("
+ ) {
+ continue;
+ }
+
// Add `hardline` after inline comment (i.e. `// comment\n foo: bar;`)
if (isInlineValueCommentNode(iNode)) {
if (parentNode.type === "value-paren_group") {
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
diff --git a/src/language-css/printer-postcss.js b/src/language-css/printer-postcss.js
index 4ba444c8d..431cdc7a6 100644
--- a/src/language-css/printer-postcss.js
+++ b/src/language-css/printer-postcss.js
@@ -155,7 +155,12 @@ function genericPrint(path, options, print) {

return [
node.raws.before.replace(/[\s;]/g, ""),
- insideICSSRuleNode(path) ? node.prop : maybeToLowerCase(node.prop),
+ (parentNode.type === "css-atrule" &&
+ typeof parentNode.raws.params === "string" &&
+ parentNode.raws.params.startsWith(":")) ||
+ insideICSSRuleNode(path)
+ ? node.prop
+ : maybeToLowerCase(node.prop),
trimmedBetween.startsWith("//") ? " " : "",
trimmedBetween,
node.extend ? "" : " ",
18 changes: 18 additions & 0 deletions .github/fix-prettier-value-with-comma.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
diff --git a/src/language-css/printer-postcss.js b/src/language-css/printer-postcss.js
index 4ba444c8d..4f7738bfa 100644
--- a/src/language-css/printer-postcss.js
+++ b/src/language-css/printer-postcss.js
@@ -886,11 +886,11 @@ function genericPrint(path, options, print) {

if (!node.open) {
const printed = path.map(print, "groups");
- const res = [];
+ const res = [hardline];

for (let i = 0; i < printed.length; i++) {
if (i !== 0) {
- res.push([",", line]);
+ res.push([",", hardline]);
}
res.push(printed[i]);
}
14 changes: 12 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,19 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- name: Install dependencies
run: yarn && yarn add -D eslint-config-airbnb-base eslint-plugin-unicorn eslint-plugin-jest@^26.9.0 @typescript-eslint/eslint-plugin @typescript-eslint/parser typescript
run: >
yarn
&& yarn add -D eslint-config-airbnb-base eslint-plugin-unicorn eslint-plugin-jest@^26.9.0 @typescript-eslint/eslint-plugin @typescript-eslint/parser typescript github:prettier/prettier#^2.8.1
&& (cd node_modules/prettier && git apply -v ../../.github/fix-prettier-14007-no-unstable-semicolon.patch)
&& (cd node_modules/prettier && git apply -v ../../.github/fix-prettier-14008-no-space-after-unary-minus.patch)
&& (cd node_modules/prettier && git apply -v ../../.github/fix-prettier-14034-no-property-name-lower-if-inside-variable-declaration.patch)
&& (cd node_modules/prettier && git apply -v ../../.github/fix-prettier-value-with-comma.patch)
- name: Lint JS and LESS files
run: npm run lint
run: >
npm run lint
&& npx prettier --loglevel warn '!dist' '!test/coverage' '!src/semantic.less' '**/*.{css,less,overrides,variables}' --write
&& git restore package.json yarn.lock
&& git add . -N && git diff --exit-code
test:
name: Test build process on node ${{ matrix.node-version }}
runs-on: ubuntu-latest
Expand Down
12 changes: 12 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
tabWidth: 4,
printWidth: 120,
overrides: [
{
files: ['*.less', '*.overrides', '*.variables'],
options: {
parser: 'less',
},
},
],
};
7 changes: 7 additions & 0 deletions .stylelintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ module.exports = {
'string-quotes': 'double',
'value-keyword-case': null,

// fix compatibility with Prettier
'declaration-bang-space-before': null,
'declaration-block-semicolon-space-before': null,
'declaration-colon-newline-after': null,
'declaration-empty-line-before': null, // TODO
'selector-combinator-space-before': null,

// TODO rules to be removed/fixed in v2.10.0 as fixes are not compatible with IE11
'alpha-value-notation': 'number', // https://caniuse.com/mdn-css_properties_opacity_percentages

Expand Down

0 comments on commit a439660

Please sign in to comment.