@stdex/presets-prettier 0.0.0-stage → 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 安坚实
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,47 @@
1
- # Temporary Holding Version
1
+ # 通用 Prettier 配置
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ ## 如何使用
4
+
5
+ 1、安装依赖
6
+
7
+ ```shell
8
+ pnpm add --save-dev @stdex/presets-prettier
9
+ ```
10
+
11
+ 此包已包含 Prettier 依赖,无需再手动添加。
12
+
13
+ 2、引用配置
14
+ 在 `package.json` 中添加 `"prettier": "@stdex/presets-prettier/prettierrc"`,
15
+ 或在项目根目录建立 `.prettierrc.js` 文件:
16
+
17
+ ```js
18
+ module.exports = '@stdex/presets-prettier/prettierrc'
19
+ ```
20
+
21
+ ## 配置 VSCode
22
+
23
+ 1、VSCode 安装 `Prettier` 插件
24
+
25
+ 2、VSCode 配置里指定如下内容:
26
+
27
+ ```json
28
+ "editor.formatOnSave": true,
29
+
30
+ // 可按需添加其他类型。
31
+ // 或直接设置成所有文件类型的默认 { formatter:{ "editor.defaultFormatter": "esbenp.prettier-vscode" } }
32
+ "[javascript][javascriptreact][typescript][typescriptreact][json][jsonc]": { "editor.defaultFormatter": "esbenp.prettier-vscode" },
33
+ ```
34
+
35
+ 3、也可以把此配置粘贴到 VSCode 配置里作为全局配置,这样临时项目也能应用格式化,格式如下:
36
+
37
+ ```json
38
+ "prettier.printWidth": 100,
39
+ "prettier.semi": false,
40
+ "prettier.singleQuote": true,
41
+ "prettier.arrowParens": "avoid",
42
+ ```
43
+
44
+ ### 格式化 wxml
45
+
46
+ 推荐 VSCode 下安装 `WXML - Language Service` 扩展,以更方便地编辑 wxml 文件。
47
+ 在 VSCode 配置里添加:`"minapp-vscode.wxmlFormatter": "prettier"` 以让其使用 Prettier 来格式化 wxml 代码。
package/package.json CHANGED
@@ -1,6 +1,24 @@
1
1
  {
2
2
  "name": "@stdex/presets-prettier",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "1.0.0",
4
+ "description": "通用 Prettier 配置",
5
+ "homepage": "https://github.com/anjianshi/stdex-packages/presets-prttier",
6
+ "bugs": {
7
+ "url": "https://github.com/anjianshi/stdex-packages/issues",
8
+ "email": "anjianshi@gmail.com"
9
+ },
10
+ "license": "MIT",
11
+ "author": "anjianshi <anjianshi@gmail.com>",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "github:anjianshi/stdex-packages"
15
+ },
16
+ "publishConfig": {
17
+ "registry": "https://registry.npmjs.org/",
18
+ "access": "public"
19
+ },
20
+ "dependencies": {
21
+ "prettier": "^3.8.1"
22
+ },
23
+ "prettier": "./prettierrc"
6
24
  }
@@ -0,0 +1,73 @@
1
+ /**
2
+ * 新增选项 jsxElementParens,值为 avoid 时去掉包裹 JSX 元素的括号。
3
+ *
4
+ * 这个括号是在 prettier/src/language-js/print/jsx.js 的 maybeWrapJsxElementInParens() 里加上的。
5
+ * 解决思路源自:https://github.com/prettier/prettier/issues/2724#issuecomment-1010212039
6
+ * 不过帖子里是 fork 了一个新 package,此处是通过插件来实现。
7
+ *
8
+ * 实现思路:
9
+ * 1. 在插件里自定义 `babel-plugin-ext` 和 `typescript-plugin-ext` 两个 parser,把 astFormat 都设置为 `estree-plugin-ext`。
10
+ * prettier 会找与 parser 定义的 astFormat 同名的 printer 来执行格式化。
11
+ *
12
+ * 2. 在插件里自定义 `estree-plugin-ext` printer,使用修改过的 print() 函数。
13
+ *
14
+ * 3. 要求用户在 prettier 配置里,把 jsx 文件的 parser 设置为 `babel-plugin-ext`,tsx 文件的 parser 设置为 `typescript-plugin-ext`。
15
+ */
16
+ import estreePlugin from 'prettier/plugins/estree.mjs'
17
+ import typescriptPlugin from 'prettier/plugins/typescript.mjs'
18
+ import babelPlugin from 'prettier/plugins/babel.mjs'
19
+
20
+ export const options = {
21
+ jsxElementParens: {
22
+ category: 'JavaScript',
23
+ type: 'choice',
24
+ default: 'always',
25
+ description: 'Include parentheses around JSX Element.',
26
+ choices: [
27
+ {
28
+ value: 'always',
29
+ description: 'Always include parens.',
30
+ },
31
+ {
32
+ value: 'avoid',
33
+ description: 'Omit parens when possible.',
34
+ },
35
+ ],
36
+ },
37
+ }
38
+
39
+ export const parsers = {
40
+ 'babel-plugin-ext': {
41
+ ...babelPlugin.parsers.babel,
42
+ astFormat: 'estree-plugin-ext',
43
+ },
44
+ 'typescript-plugin-ext': {
45
+ ...typescriptPlugin.parsers.typescript,
46
+ astFormat: 'estree-plugin-ext',
47
+ },
48
+ }
49
+
50
+ export const printers = {
51
+ 'estree-plugin-ext': {
52
+ ...estreePlugin.printers.estree,
53
+ print,
54
+ },
55
+ }
56
+
57
+ function print(path, options, print, args) {
58
+ const elem = estreePlugin.printers.estree.print(path, options, print, args)
59
+
60
+ if (
61
+ options.jsxElementParens === 'avoid' &&
62
+ (path.node.type === 'JSXFragment' || path.node.type === 'JSXElement') &&
63
+ elem.type === 'group' &&
64
+ elem.contents.length > 2 &&
65
+ elem.contents[0].type === 'if-break' &&
66
+ elem.contents[0].breakContents === '(' &&
67
+ elem.contents[1].contents.length >= 2
68
+ ) {
69
+ return elem.contents[1].contents[1]
70
+ }
71
+
72
+ return elem
73
+ }
package/prettierrc.js ADDED
@@ -0,0 +1,37 @@
1
+ const path = require('path')
2
+ const packageDir = path.resolve(__dirname)
3
+
4
+ // 只定义与默认值不同的项,完整列表见:https://prettier.io/docs/en/options.html
5
+ module.exports = {
6
+ printWidth: 100,
7
+ semi: false,
8
+ singleQuote: true,
9
+ arrowParens: 'avoid',
10
+
11
+ // 来自插件的选项
12
+ jsxElementParens: 'avoid',
13
+ plugins: [path.join(packageDir, 'plugin-jsx-element-parens.mjs')],
14
+
15
+ overrides: [
16
+ {
17
+ files: ['*.html', '*.wxml'],
18
+ options: {
19
+ printWidth: 200,
20
+ },
21
+ },
22
+
23
+ // 使用插件自己定义的 parser 来处理文件
24
+ {
25
+ files: ['*.jsx'],
26
+ options: {
27
+ parser: 'babel-plugin-ext',
28
+ },
29
+ },
30
+ {
31
+ files: ['*.tsx'],
32
+ options: {
33
+ parser: 'typescript-plugin-ext',
34
+ },
35
+ },
36
+ ],
37
+ }