diff --git a/.prettierrc.json b/.prettierrc.json index 878026f924c..93b4f718c03 100644 --- a/.prettierrc.json +++ b/.prettierrc.json @@ -8,7 +8,7 @@ ], "options": { "plugins": [ - "./scripts/prettier-swagger-plugin" + "./eng/scripts/prettier-swagger-plugin.js" ], "parser": "json-swagger", "printWidth": 20, diff --git a/documentation/ci-fix.md b/documentation/ci-fix.md index 2109fc2b1d4..c521df89a55 100644 --- a/documentation/ci-fix.md +++ b/documentation/ci-fix.md @@ -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 ``` diff --git a/eng/scripts/prettier-swagger-plugin.js b/eng/scripts/prettier-swagger-plugin.js new file mode 100644 index 00000000000..b51cf338b4f --- /dev/null +++ b/eng/scripts/prettier-swagger-plugin.js @@ -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 + } +}; diff --git a/package-lock.json b/package-lock.json index 6ca83e2fbda..7808fa7661a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,14 +13,13 @@ "@azure-tools/typespec-client-generator-core": "0.33.0", "@azure-tools/typespec-providerhub": "0.33.0", "@azure/avocado": "^0.8.4", - "@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-rest-api-specs-eng-tools": "file:eng/tools", - "prettier": "^2.8.8", + "prettier": "~3.0.3", "typescript": "~5.1.3" } }, @@ -605,12 +604,6 @@ "form-data": "^3.0.0" } }, - "node_modules/@types/prettier": { - "version": "2.7.3", - "resolved": "https://registry.npmjs.org/@types/prettier/-/prettier-2.7.3.tgz", - "integrity": "sha512-+68kP9yzs4LMp7VNh8gdzMSPZFL44MLGqiHWvttYJe+6qnuVr4Ek9wSBQoveqY/r+LwjCcU29kNVkidwim+kYA==", - "dev": true - }, "node_modules/@types/retry": { "version": "0.12.2", "resolved": "https://registry.npmjs.org/@types/retry/-/retry-0.12.2.tgz", @@ -710,21 +703,6 @@ "js-yaml": "bin/js-yaml.js" } }, - "node_modules/@typespec/compiler/node_modules/prettier": { - "version": "3.0.3", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.0.3.tgz", - "integrity": "sha512-L/4pUDMxcNa8R/EthV08Zt42WBO4h1rarVtK0K+QJG0X187OLo7l699jWw0GKuwzkPQ//jMFA/8Xm6Fh3J/DAg==", - "dev": true, - "bin": { - "prettier": "bin/prettier.cjs" - }, - "engines": { - "node": ">=14" - }, - "funding": { - "url": "https://github.com/prettier/prettier?sponsor=1" - } - }, "node_modules/@typespec/compiler/node_modules/wrap-ansi": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz", @@ -1833,15 +1811,15 @@ } }, "node_modules/prettier": { - "version": "2.8.8", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.8.8.tgz", - "integrity": "sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==", + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.0.3.tgz", + "integrity": "sha512-L/4pUDMxcNa8R/EthV08Zt42WBO4h1rarVtK0K+QJG0X187OLo7l699jWw0GKuwzkPQ//jMFA/8Xm6Fh3J/DAg==", "dev": true, "bin": { - "prettier": "bin-prettier.js" + "prettier": "bin/prettier.cjs" }, "engines": { - "node": ">=10.13.0" + "node": ">=14" }, "funding": { "url": "https://github.com/prettier/prettier?sponsor=1" diff --git a/package.json b/package.json index f0f6b4374be..4a881b09d9b 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,6 @@ "@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", @@ -15,7 +14,7 @@ "@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 diff --git a/scripts/prettier-swagger-plugin.ts b/scripts/prettier-swagger-plugin.ts deleted file mode 100644 index ee005c7ccc5..00000000000 --- a/scripts/prettier-swagger-plugin.ts +++ /dev/null @@ -1,92 +0,0 @@ -import { ParserOptions, FastPath, Doc, doc, Plugin, AST } from 'prettier'; -import { parsers as bundledParsers } from 'prettier/parser-babel'; - -const { concat, indent, hardline, join } = doc.builders; - -// Modified from https://github.com/prettier/prettier/blob/master/src/language-js/printer-estree-json.js -const print = (path: FastPath, _: ParserOptions, print: (path: FastPath) => Doc): Doc => { - const node = path.getValue(); - switch (node.type) { - case "JsonRoot": - return concat([path.call(print, "node"), hardline]); - case "ArrayExpression": - return node.elements.length === 0 - ? "[]" - : concat([ - "[", - indent( - concat([ - hardline, - join(concat([",", hardline]), path.map(print, "elements")) - ]) - ), - hardline, - "]" - ]); - case "ObjectExpression": - return node.properties.length === 0 - ? "{}" - : concat([ - "{", - indent( - concat([ - hardline, - join(concat([",", hardline]), path.map(print, "properties")) - ]) - ), - hardline, - "}" - ]); - case "ObjectProperty": - return concat([path.call(print, "key"), ": ", path.call(print, "value")]); - case "UnaryExpression": - return concat([ - node.operator === "+" ? "" : node.operator, - path.call(print, "argument") - ]); - case "NullLiteral": - return "null"; - case "BooleanLiteral": - return node.value ? "true" : "false"; - case "StringLiteral": - return JSON.stringify(node.value); - case "NumericLiteral": - // Modified: Keep numeric literal as-is - return node.extra.raw; - case "Identifier": - return JSON.stringify(node.name); - default: - /* istanbul ignore next */ - throw new Error("unknown type: " + JSON.stringify(node.type)); - } -} - -const preprocess = (ast: AST, _: any): AST => { - return Object.assign({}, ast, { - type: "JsonRoot", - node: ast, - comments: [] - }); -} - -export const languages: Plugin['languages'] = [ - { - name: 'json-swagger', - extensions: ['.json'], - parsers: ['json-swagger'] - } -] - -export const parsers = { - 'json-swagger': { - ...bundledParsers['json-stringify'], - astFormat: 'estree-swagger-customized' - } -}; - -export const printers = { - 'estree-swagger-customized': { - preprocess, - print - } -};