Skip to content

Commit

Permalink
Update prettier to 3.x (Azure#25673)
Browse files Browse the repository at this point in the history
- Updated the json-swagger plugin to support the 3.x pretter
- Switch the plugin to be JS based to avoid the need to compile via TS
- Simplified plugin to delegate most work to the base estree plugin
- Move swagger plugin source under eng
  • Loading branch information
weshaggard authored Sep 6, 2023
1 parent ebb636f commit 7eaa071
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 133 deletions.
2 changes: 1 addition & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
],
"options": {
"plugins": [
"./scripts/prettier-swagger-plugin"
"./eng/scripts/prettier-swagger-plugin.js"
],
"parser": "json-swagger",
"printWidth": 20,
Expand Down
10 changes: 0 additions & 10 deletions documentation/ci-fix.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,9 @@ cd specification/contosowidgetmanager
# Install the dependencies to the local 'node_modules' folder.
npm install
# Compile TypeScript. Compilation will fail, this is expected. But it will compile 'scripts/prettier-swagger-plugin', which is what we need.
npx tsc
# As of 5/25/2023, the prettier version should be 2.1.2
npx prettier --version
# Run 'prettier --check' to verify the problems can be reproduced locally
npx prettier --check **/*.json
# Run 'prettier --list-different' to understand which files have problems.
# Note: there is no way to view the exact problems without actually changing the affected files. See https://github.com/prettier/prettier/issues/6069.
npx prettier --list-different **/*.json
# Run 'prettier --write' to fix the problems.
npx prettier --write **/*.json
```
Expand Down
33 changes: 33 additions & 0 deletions eng/scripts/prettier-swagger-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const plugins_estree = require("prettier/plugins/estree");
const plugins_babel = require("prettier/plugins/babel")

// Customize the standard json-stringify parser from https://github.com/prettier/prettier/blob/50404103ef7d96fb36f8cba1ac7bc40c671fd6cf/src/language-json/parser-json.js
const print = (path, options, print) => {
const node = path.getValue();

if (node.type === "NumericLiteral") {
// Keep numeric literal as-is to allow values to have more then one trailing digit after the decimal like 100.00
return node.extra.raw;
}

return plugins_estree.printers['estree-json'].print(path, options, print);
};

exports.languages = [
{
name: 'json-swagger',
extensions: ['.json'],
parsers: ['json-swagger']
}
];
exports.parsers = {
'json-swagger': {
...plugins_babel.parsers['json-stringify'],
astFormat: 'estree-swagger-customized'
}
};
exports.printers = {
'estree-swagger-customized': {
print
}
};
34 changes: 6 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
"@azure-tools/typespec-azure-resource-manager": "0.33.0",
"@azure-tools/typespec-client-generator-core": "0.33.0",
"@azure-tools/typespec-providerhub": "0.33.0",
"@types/prettier": "^2.7.2",
"@typespec/compiler": "0.47.0",
"@typespec/http": "0.47.0",
"@typespec/openapi": "0.47.0",
"@typespec/rest": "0.47.0",
"@typespec/versioning": "0.47.0",
"@azure/avocado": "^0.8.4",
"azure-rest-api-specs-eng-tools": "file:eng/tools",
"prettier": "^2.8.8",
"prettier": "~3.0.3",
"typescript": "~5.1.3"
},
"private": true
Expand Down
92 changes: 0 additions & 92 deletions scripts/prettier-swagger-plugin.ts

This file was deleted.

0 comments on commit 7eaa071

Please sign in to comment.