@taiga-ui/prettier-config 0.489.0 → 0.491.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +63 -0
- package/index.js +7 -0
- package/package.json +2 -2
- package/plugins/prettier-plugin-css-custom-properties.js +146 -0
- package/project.json +0 -15
package/README.md
CHANGED
|
@@ -15,3 +15,66 @@ npm i --save-dev @taiga-ui/prettier-config
|
|
|
15
15
|
```js
|
|
16
16
|
module.exports = require('@taiga-ui/prettier-config');
|
|
17
17
|
```
|
|
18
|
+
|
|
19
|
+
## Internal Prettier plugins
|
|
20
|
+
|
|
21
|
+
The package ships with three internal Prettier plugins that extend default Prettier behavior for project-specific
|
|
22
|
+
formatting cases.
|
|
23
|
+
|
|
24
|
+
### `prettier-plugin-css-custom-properties`
|
|
25
|
+
|
|
26
|
+
Used for CSS-family files: `*.css`, `*.scss`, and `*.less`.
|
|
27
|
+
|
|
28
|
+
- Reuses Prettier built-in PostCSS parsers for CSS, SCSS, and LESS.
|
|
29
|
+
- Keeps CSS custom property declarations on one line, even when the declaration is longer than `printWidth`.
|
|
30
|
+
- Applies only to declarations whose property name starts with `--`.
|
|
31
|
+
- Leaves regular CSS, SCSS, and LESS declarations formatted by the original Prettier printer.
|
|
32
|
+
|
|
33
|
+
```css
|
|
34
|
+
:root {
|
|
35
|
+
--tui-font-offset: 0;
|
|
36
|
+
--tui-typography-family-display: inherit;
|
|
37
|
+
--tui-typography-heading-h1: bold calc(var(--tui-font-offset) + 3.125rem) / calc(56 / 50) var(--tui-typography-family-display);
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### `prettier-plugin-embedded-ts`
|
|
42
|
+
|
|
43
|
+
Used for TypeScript files through the `typescript-embedded-ts` parser.
|
|
44
|
+
|
|
45
|
+
- Reuses Prettier built-in TypeScript parser and ESTree printer.
|
|
46
|
+
- Formats template literals as embedded TypeScript when they are preceded directly by a `/* TS */` or `/* TYPESCRIPT */`
|
|
47
|
+
block comment.
|
|
48
|
+
- Marker matching is case-insensitive.
|
|
49
|
+
- Formats only plain template literals without expressions.
|
|
50
|
+
- Respects Prettier's `embeddedLanguageFormatting: 'off'` option and falls back to the default printer when embedded
|
|
51
|
+
formatting is disabled.
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
const code = /* TypeScript */ `
|
|
55
|
+
const value = {foo: 'bar'};
|
|
56
|
+
`;
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### `prettier-plugin-sort`
|
|
60
|
+
|
|
61
|
+
Used for selected JSON files with the `json-stringify` parser:
|
|
62
|
+
|
|
63
|
+
- `package.json`
|
|
64
|
+
- `ng-package.json`
|
|
65
|
+
- `project.json`
|
|
66
|
+
- `renovate.json`
|
|
67
|
+
- `default.json`
|
|
68
|
+
- `tsconfig*.json`
|
|
69
|
+
|
|
70
|
+
Capabilities:
|
|
71
|
+
|
|
72
|
+
- Sorts `package.json` with `sort-package-json`.
|
|
73
|
+
- Preserves the original order of `package.json` scripts.
|
|
74
|
+
- Sorts `tsconfig*.json` top-level keys in this order: `$schema`, `extends`, `compileOnSave`, `compilerOptions`,
|
|
75
|
+
`angularCompilerOptions`, `files`, `include`, `exclude`, `references`; unknown keys are appended alphabetically.
|
|
76
|
+
- Sorts `compilerOptions` with `baseUrl`, `rootDir`, and `strict` first; the rest are appended alphabetically.
|
|
77
|
+
- Recursively sorts nested object keys alphabetically.
|
|
78
|
+
- Preserves array item order.
|
|
79
|
+
- Keeps arrays with one or two primitive values on one line for JSON files except `package.json`.
|
|
80
|
+
- Leaves objects expanded through Prettier's original JSON printer.
|
package/index.js
CHANGED
|
@@ -5,6 +5,13 @@ module.exports = {
|
|
|
5
5
|
plugins: [
|
|
6
6
|
require.resolve('stylelint-prettier'),
|
|
7
7
|
require.resolve('prettier-plugin-organize-attributes'),
|
|
8
|
+
require.resolve(
|
|
9
|
+
path.resolve(
|
|
10
|
+
__dirname,
|
|
11
|
+
'plugins',
|
|
12
|
+
'prettier-plugin-css-custom-properties.js',
|
|
13
|
+
),
|
|
14
|
+
),
|
|
8
15
|
],
|
|
9
16
|
arrowParens: 'always',
|
|
10
17
|
bracketSpacing: false,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@taiga-ui/prettier-config",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.491.0",
|
|
4
4
|
"description": "Taiga-ui prettier config",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"prettier",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"main": "index.js",
|
|
33
33
|
"peerDependencies": {
|
|
34
34
|
"@prettier/plugin-xml": "^3.4.2",
|
|
35
|
-
"prettier": "^3.8.
|
|
35
|
+
"prettier": "^3.8.3",
|
|
36
36
|
"prettier-package-json": "^2.8.0",
|
|
37
37
|
"prettier-plugin-organize-attributes": "^1.0.0",
|
|
38
38
|
"sort-package-json": "^3.6.1",
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
const postcss = require('prettier/plugins/postcss');
|
|
2
|
+
|
|
3
|
+
const AST_FORMAT = 'postcss-custom-properties';
|
|
4
|
+
const originalPostcssPrinter = postcss.printers.postcss;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @param {unknown} node
|
|
8
|
+
* @returns {node is {type: string; prop: string; value: string}}
|
|
9
|
+
*/
|
|
10
|
+
function isCustomPropertyDeclaration(node) {
|
|
11
|
+
return (
|
|
12
|
+
typeof node === 'object' &&
|
|
13
|
+
node !== null &&
|
|
14
|
+
'type' in node &&
|
|
15
|
+
'prop' in node &&
|
|
16
|
+
node.type === 'css-decl' &&
|
|
17
|
+
typeof node.prop === 'string' &&
|
|
18
|
+
node.prop.startsWith('--')
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Returns true when a Prettier doc contains comment text (`//` or `/*`).
|
|
24
|
+
* Checking the printed doc is more reliable than checking `node.value` because
|
|
25
|
+
* postcss may store inline comments in `node.raws` rather than `node.value`.
|
|
26
|
+
*
|
|
27
|
+
* @param {import('prettier').Doc} doc
|
|
28
|
+
* @returns {boolean}
|
|
29
|
+
*/
|
|
30
|
+
function docContainsComment(doc) {
|
|
31
|
+
if (typeof doc === 'string') {
|
|
32
|
+
return doc.includes('//') || doc.includes('/*');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (Array.isArray(doc)) {
|
|
36
|
+
return doc.some(docContainsComment);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (typeof doc !== 'object' || doc === null) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (doc.type === 'fill') {
|
|
44
|
+
return doc.parts.some(docContainsComment);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (doc.type === 'if-break') {
|
|
48
|
+
return (
|
|
49
|
+
docContainsComment(doc.breakContents) || docContainsComment(doc.flatContents)
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if ('contents' in doc) {
|
|
54
|
+
return docContainsComment(doc.contents);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Converts a Prettier doc to its flat representation so custom properties are
|
|
62
|
+
* not wrapped by printWidth.
|
|
63
|
+
*
|
|
64
|
+
* @param {import('prettier').Doc} doc
|
|
65
|
+
* @returns {import('prettier').Doc}
|
|
66
|
+
*/
|
|
67
|
+
function flattenDoc(doc) {
|
|
68
|
+
if (typeof doc === 'string') {
|
|
69
|
+
return doc;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (Array.isArray(doc)) {
|
|
73
|
+
return doc.map(flattenDoc);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
switch (doc.type) {
|
|
77
|
+
case 'align':
|
|
78
|
+
return flattenDoc(doc.contents);
|
|
79
|
+
|
|
80
|
+
case 'break-parent':
|
|
81
|
+
case 'cursor':
|
|
82
|
+
return '';
|
|
83
|
+
|
|
84
|
+
case 'fill':
|
|
85
|
+
return doc.parts.map(flattenDoc);
|
|
86
|
+
|
|
87
|
+
case 'group':
|
|
88
|
+
return flattenDoc(doc.contents);
|
|
89
|
+
|
|
90
|
+
case 'if-break':
|
|
91
|
+
return flattenDoc(doc.flatContents);
|
|
92
|
+
|
|
93
|
+
case 'indent':
|
|
94
|
+
return flattenDoc(doc.contents);
|
|
95
|
+
|
|
96
|
+
case 'indent-if-break':
|
|
97
|
+
return '';
|
|
98
|
+
|
|
99
|
+
case 'label':
|
|
100
|
+
return flattenDoc(doc.contents);
|
|
101
|
+
|
|
102
|
+
case 'line':
|
|
103
|
+
return doc.soft ? '' : ' ';
|
|
104
|
+
|
|
105
|
+
case 'line-suffix':
|
|
106
|
+
return flattenDoc(doc.contents);
|
|
107
|
+
|
|
108
|
+
case 'line-suffix-boundary':
|
|
109
|
+
case 'trim':
|
|
110
|
+
return '';
|
|
111
|
+
|
|
112
|
+
default:
|
|
113
|
+
return doc;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
exports.printers = {
|
|
118
|
+
[AST_FORMAT]: {
|
|
119
|
+
...originalPostcssPrinter,
|
|
120
|
+
/**
|
|
121
|
+
* @param {import('prettier').AstPath} path
|
|
122
|
+
* @param {import('prettier').ParserOptions} options
|
|
123
|
+
* @param {(path: import('prettier').AstPath) => import('prettier').Doc} print
|
|
124
|
+
* @returns {import('prettier').Doc}
|
|
125
|
+
*/
|
|
126
|
+
print(path, options, print) {
|
|
127
|
+
const printed = originalPostcssPrinter.print(path, options, print);
|
|
128
|
+
|
|
129
|
+
if (!isCustomPropertyDeclaration(path.node)) {
|
|
130
|
+
return printed;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return docContainsComment(printed) ? printed : flattenDoc(printed);
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
exports.parsers = Object.fromEntries(
|
|
139
|
+
Object.entries(postcss.parsers).map(([name, parser]) => [
|
|
140
|
+
name,
|
|
141
|
+
{
|
|
142
|
+
...parser,
|
|
143
|
+
astFormat: AST_FORMAT,
|
|
144
|
+
},
|
|
145
|
+
]),
|
|
146
|
+
);
|
package/project.json
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "../../node_modules/nx/schemas/project-schema.json",
|
|
3
|
-
"name": "prettier-config",
|
|
4
|
-
"prefix": "tui",
|
|
5
|
-
"projectType": "library",
|
|
6
|
-
"sourceRoot": "projects/prettier-config",
|
|
7
|
-
"targets": {
|
|
8
|
-
"publish": {
|
|
9
|
-
"executor": "nx:run-commands",
|
|
10
|
-
"options": {
|
|
11
|
-
"command": "npm publish ./projects/{projectName} --access=public --ignore-scripts"
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
}
|