VSC Apex-prettier-plugin-apex 未格式化 - 引发错误
Posted
技术标签:
【中文标题】VSC Apex-prettier-plugin-apex 未格式化 - 引发错误【英文标题】:VSC Apex-prettier-plugin-apex doesn't format - throws error 【发布时间】:2021-08-15 06:46:40 【问题描述】:我使用 Visual Studio Code 并根据https://developer.salesforce.com/tools/vscode/en/user-guide/prettier 安装了 Prettier 插件以及 Prettier-plugin-apex (也是https://www.sfdcnotes.com/2019/12/16/vs-code-extension-prettier-code-formatter/)。
它曾经格式化我的课程,但最近一直失败,但我看到的只是右下角的这个,没有解释我保存类文件时出了什么问题(它在保存时格式化/在保存时部署)。
当我尝试使用 npx prettier --write
手动格式化时,我收到此错误:
PS C:\Users\xx\Documents\VSC\SynDev> npx prettier --write force-
app/main/default/classes/emailReportController_Test.cls
force-app\main\default\classes\emailReportController_Test.cls
[error] force-app\main\default\classes\emailReportController_Test.cls: Error
[error] at parseTextWithSpawn (C:\Users\xx\Documents\VSC\SynDev\node_modules\prettier-plugin-apex\src\parser.js:36:11)
[error] at Object.parse (C:\Users\xx\Documents\VSC\SynDev\node_modules\prettier-plugin-apex\src\parser.js:489:21)
[error] at Object.parse$a [as parse] (C:\Users\xx\Documents\VSC\SynDev\node_modules\prettier\index.js:14892:19)
[error] at coreFormat (C:\Users\xx\Documents\VSC\SynDev\node_modules\prettier\index.js:16425:16)
[error] at formatWithCursor$1 (C:\Users\xx\Documents\VSC\SynDev\node_modules\prettier\index.js:16665:14)
[error] at Object.formatWithCursor (C:\Users\xx\Documents\VSC\SynDev\node_modules\prettier\index.js:60922:12)
[error] at format$1 (C:\Users\xx\Documents\VSC\SynDev\node_modules\prettier\bin-prettier.js:16107:14)
[error] at Object.formatFiles$1 [as formatFiles] (C:\Users\xx\Documents\VSC\SynDev\node_modules\prettier\bin-prettier.js:16218:16)
[error] at main (C:\Users\xx\Documents\VSC\SynDev\node_modules\prettier\bin-prettier.js:17989:10)
[error] at Object.run (C:\Users\xx\Documents\VSC\SynDev\node_modules\prettier\bin-prettier.js:17932:5)
我该如何解决这个问题 - 出了什么问题(我不是 VSC 或 npm 专家)?
这是我的 package.json 文件:
"name": "salesforce-app",
"private": true,
"version": "1.0.0",
"description": "Salesforce App",
"scripts":
"lint": "npm run lint:lwc && npm run lint:aura",
"lint:aura": "eslint **/aura/**",
"lint:lwc": "eslint **/lwc/**",
"test": "npm run test:unit",
"test:unit": "sfdx-lwc-jest",
"test:unit:watch": "sfdx-lwc-jest --watch",
"test:unit:debug": "sfdx-lwc-jest --debug",
"test:unit:coverage": "sfdx-lwc-jest --coverage",
"prettier": "prettier --write \"**/*.cls,cmp,component,css,html,js,json,md,page,trigger,xml,yaml,yml\"",
"prettier:verify": "prettier --list-different \"**/*.cls,cmp,component,css,html,js,json,md,page,trigger,xml,yaml,yml\""
,
"devDependencies":
"@prettier/plugin-xml": "^0.12.0",
"@salesforce/eslint-config-lwc": "^0.11.0",
"@salesforce/eslint-plugin-aura": "^2.0.0",
"@salesforce/sfdx-lwc-jest": "^0.10.4",
"eslint": "^7.24.0",
"eslint-config-prettier": "^6.11.0",
"husky": "^4.2.1",
"lint-staged": "^10.0.7",
"prettier": "2.3.0",
"prettier-plugin-apex": "1.9.1"
,
"husky":
"hooks":
"pre-commit": "lint-staged"
,
"lint-staged":
"**/*.cls,cmp,component,css,html,js,json,md,page,trigger,xml,yaml,yml": [
"prettier --write"
],
"**/aura|lwc/**": [
"eslint"
]
,
"main": "jest.config.js",
"author": "",
"license": "ISC"
还有.prettierrc
"trailingComma": "none",
"overrides": [
"files": "**/lwc/**/*.html",
"options": "parser": "lwc"
,
"files": "*.cmp,page,component",
"options": "parser": "html"
]
【问题讨论】:
【参考方案1】:我遇到了同样的问题并找到了解决方法。
通过更改package.json
中的版本号来安装以前版本的prettier-plugin-apex
:
"prettier-plugin-apex": "1.8.0"
编辑:
然后运行npm install prettier-plugin-apex
来实际执行降级。
【讨论】:
这在我的开发环境中有效,谢谢,尽管在某些时候肯定需要使用较新的版本。编辑 package.json 文件后,我还必须运行“npm install prettier-plugin-apex”。 感谢您的反馈,很高兴它成功了!你是对的,我理所当然地认为需要npm install
,我相应地更新了我的答案。以上是关于VSC Apex-prettier-plugin-apex 未格式化 - 引发错误的主要内容,如果未能解决你的问题,请参考以下文章