From e301b1f9ce49963a5958a543ad2f604f2d5d085e Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 30 Dec 2022 10:36:58 +0800 Subject: [PATCH 1/8] Fix comments inside JSX attributes (#14082) Fixes https://github.com/prettier/prettier/issues/11242 --- changelog_unreleased/javascript/14082.md | 17 ++++++ src/language-js/print/jsx.js | 9 ++- tests/config/format-test.js | 1 + .../comments/__snapshots__/jsfmt.spec.js.snap | 56 +++++++++++++++++++ tests/format/jsx/comments/in-attributes.js | 30 ++++++++++ 5 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 changelog_unreleased/javascript/14082.md create mode 100644 tests/format/jsx/comments/in-attributes.js diff --git a/changelog_unreleased/javascript/14082.md b/changelog_unreleased/javascript/14082.md new file mode 100644 index 000000000..e56693154 --- /dev/null +++ b/changelog_unreleased/javascript/14082.md @@ -0,0 +1,17 @@ +#### Fix formatting for comments inside JSX attribute (#14082 with by @fisker) + + +```jsx +// Input +function MyFunctionComponent() { + +} + +// Prettier stable +Error: Comment "old" was not printed. Please report this error! + +// Prettier main +function MyFunctionComponent() { + ; +} +``` diff --git a/src/language-js/print/jsx.js b/src/language-js/print/jsx.js index fe18414a9..db4a68497 100644 --- a/src/language-js/print/jsx.js +++ b/src/language-js/print/jsx.js @@ -3,6 +3,7 @@ const { printComments, printDanglingComments, + printCommentsSeparately, } = require("../../main/comments.js"); const { builders: { @@ -490,7 +491,11 @@ function printJsxAttribute(path, options, print) { options.jsxSingleQuote ? "'" : '"' ); final = final.replace(regex, escaped); - res = [quote, final, quote]; + const { leading, trailing } = path.call( + () => printCommentsSeparately(path, options), + "value" + ); + res = [leading, quote, final, quote, trailing]; } else { res = print("value"); } @@ -766,7 +771,7 @@ function printJsx(path, options, print) { return printJsxEmptyExpression(path, options /*, print*/); case "JSXText": /* istanbul ignore next */ - throw new Error("JSXTest should be handled by JSXElement"); + throw new Error("JSXText should be handled by JSXElement"); default: /* istanbul ignore next */ throw new Error(`Unknown JSX node type: ${JSON.stringify(node.type)}.`); diff --git a/tests/config/format-test.js b/tests/config/format-test.js index c5703e99e..3a084702f 100644 --- a/tests/config/format-test.js +++ b/tests/config/format-test.js @@ -42,6 +42,7 @@ const unstableTests = new Map( "js/for/continue-and-break-comment-without-blocks.js", "typescript/satisfies-operators/comments-unstable.ts", ["js/identifier/parentheses/let.js", (options) => options.semi === false], + "jsx/comments/in-attributes.js", ].map((fixture) => { const [file, isUnstable = () => true] = Array.isArray(fixture) ? fixture diff --git a/tests/format/jsx/comments/__snapshots__/jsfmt.spec.js.snap b/tests/format/jsx/comments/__snapshots__/jsfmt.spec.js.snap index 498158e46..6a7ba0281 100644 --- a/tests/format/jsx/comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/jsx/comments/__snapshots__/jsfmt.spec.js.snap @@ -27,6 +27,62 @@ const render = (items) => ( ================================================================================ `; +exports[`in-attributes.js - {"bracketSameLine":true} format 1`] = ` +====================================options===================================== +bracketSameLine: true +parsers: ["flow", "babel", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +
; + +
; + +
; + +
; + +
; + +
; + +=====================================output===================================== +
; + +
; + +
; + +
; + +
; + +
; // comment + +================================================================================ +`; + exports[`in-end-tag.js - {"bracketSameLine":true} [typescript] format 1`] = ` "Identifier expected. (2:6) 1 | /* =========== before slash =========== */ diff --git a/tests/format/jsx/comments/in-attributes.js b/tests/format/jsx/comments/in-attributes.js new file mode 100644 index 000000000..4b3565c38 --- /dev/null +++ b/tests/format/jsx/comments/in-attributes.js @@ -0,0 +1,30 @@ +
; + +
; + +
; + +
; + +
; + +
; From 369bf2a1c117bdf4346e851d1039d6d2b014e160 Mon Sep 17 00:00:00 2001 From: 0024 <62194324+onishi-kohei@users.noreply.github.com> Date: Fri, 30 Dec 2022 15:05:14 +0900 Subject: [PATCH 2/8] Fix to add parentheses for TSTypeQuery in some case (#14042) * add TSTypeQuery to needsParens * fix to lint prettier * add test * remove comment * add changelog * fix lint * remove template comments at top in changelog * Fix changelog * Add new line * check objectType * remove fallthrough comment * check property name * Simplify changelog Co-authored-by: fisker Cheung --- changelog_unreleased/typescript/14042.md | 19 +++++++++++++++++ src/language-js/needs-parens.js | 6 +++++- .../typeof/__snapshots__/jsfmt.spec.js.snap | 21 +++++++++++++++++++ tests/format/typescript/typeof/jsfmt.spec.js | 1 + tests/format/typescript/typeof/typeof.ts | 4 ++++ 5 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 changelog_unreleased/typescript/14042.md create mode 100644 tests/format/typescript/typeof/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/format/typescript/typeof/jsfmt.spec.js create mode 100644 tests/format/typescript/typeof/typeof.ts diff --git a/changelog_unreleased/typescript/14042.md b/changelog_unreleased/typescript/14042.md new file mode 100644 index 000000000..9b37741bb --- /dev/null +++ b/changelog_unreleased/typescript/14042.md @@ -0,0 +1,19 @@ +#### Add parentheses for TSTypeQuery to improve readability (#14042 by @onishi-kohei) + + +```tsx +// Input +a as (typeof node.children)[number] +a as (typeof node.children)[] +a as ((typeof node.children)[number])[] + +// Prettier stable +a as typeof node.children[number]; +a as typeof node.children[]; +a as typeof node.children[number][]; + +// Prettier main +a as (typeof node.children)[number]; +a as (typeof node.children)[]; +a as (typeof node.children)[number][]; +``` diff --git a/src/language-js/needs-parens.js b/src/language-js/needs-parens.js index dfecf3426..cf9e1d583 100644 --- a/src/language-js/needs-parens.js +++ b/src/language-js/needs-parens.js @@ -507,7 +507,11 @@ function needsParens(path, options) { (parent.type === "TSTypeAnnotation" && path.getParentNode(1).type.startsWith("TSJSDoc")) ); - + case "TSTypeQuery": + return ( + (name === "objectType" && parent.type === "TSIndexedAccessType") || + (name === "elementType" && parent.type === "TSArrayType") + ); case "ArrayTypeAnnotation": return parent.type === "NullableTypeAnnotation"; diff --git a/tests/format/typescript/typeof/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/typeof/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..5f6e5fb92 --- /dev/null +++ b/tests/format/typescript/typeof/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,21 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`typeof.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +a as (typeof node.children)[number] +a as (typeof node.children)[] +a as ((typeof node.children)[number])[] +a as number[(typeof node.children)] + +=====================================output===================================== +a as (typeof node.children)[number]; +a as (typeof node.children)[]; +a as (typeof node.children)[number][]; +a as number[typeof node.children]; + +================================================================================ +`; diff --git a/tests/format/typescript/typeof/jsfmt.spec.js b/tests/format/typescript/typeof/jsfmt.spec.js new file mode 100644 index 000000000..2ea3bb6eb --- /dev/null +++ b/tests/format/typescript/typeof/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, ["typescript"]); diff --git a/tests/format/typescript/typeof/typeof.ts b/tests/format/typescript/typeof/typeof.ts new file mode 100644 index 000000000..accada3df --- /dev/null +++ b/tests/format/typescript/typeof/typeof.ts @@ -0,0 +1,4 @@ +a as (typeof node.children)[number] +a as (typeof node.children)[] +a as ((typeof node.children)[number])[] +a as number[(typeof node.children)] From efa6aa68b6637dfe270d6574b9b3af7de517ee63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Fri, 30 Dec 2022 09:09:55 +0100 Subject: [PATCH 3/8] Fix semicolon duplicated at the end of LESS file (#14007) Fix semicolon duplicated at the end of file --- changelog_unreleased/less/14007.md | 19 +++++++++++++++++++ src/language-css/printer-postcss.js | 5 ++++- .../less/__snapshots__/jsfmt.spec.js.snap | 18 ++++++++++++++++++ .../issue-11483--never-append-anything.less | 3 +++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 changelog_unreleased/less/14007.md create mode 100644 tests/format/less/less/issue-11483--never-append-anything.less diff --git a/changelog_unreleased/less/14007.md b/changelog_unreleased/less/14007.md new file mode 100644 index 000000000..eed8b756d --- /dev/null +++ b/changelog_unreleased/less/14007.md @@ -0,0 +1,19 @@ +#### Fix semicolon duplicated at the end of LESS file (#14007 by @mvorisek) + + +```less +// Input +@variable: { + field: something; +}; + +// Prettier stable +@variable: { + field: something; +}; ; + +// Prettier main +@variable: { + field: something; +}; +``` diff --git a/src/language-css/printer-postcss.js b/src/language-css/printer-postcss.js index a15f7981f..d9894d4ce 100644 --- a/src/language-css/printer-postcss.js +++ b/src/language-css/printer-postcss.js @@ -101,7 +101,10 @@ function genericPrint(path, options, print) { return [node.raw, hardline]; case "css-root": { const nodes = printNodeSequence(path, options, print); - const after = node.raws.after.trim(); + let after = node.raws.after.trim(); + if (after.startsWith(";")) { + after = after.slice(1).trim(); + } return [ nodes, diff --git a/tests/format/less/less/__snapshots__/jsfmt.spec.js.snap b/tests/format/less/less/__snapshots__/jsfmt.spec.js.snap index f52a92aa2..1541bb049 100644 --- a/tests/format/less/less/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/less/less/__snapshots__/jsfmt.spec.js.snap @@ -83,6 +83,24 @@ a: b; ================================================================================ `; +exports[`issue-11483--never-append-anything.less format 1`] = ` +====================================options===================================== +parsers: ["less"] +printWidth: 80 + | printWidth +=====================================input====================================== +@variable: { + field: something; +}; + +=====================================output===================================== +@variable: { + field: something; +}; + +================================================================================ +`; + exports[`less.less format 1`] = ` ====================================options===================================== parsers: ["less"] diff --git a/tests/format/less/less/issue-11483--never-append-anything.less b/tests/format/less/less/issue-11483--never-append-anything.less new file mode 100644 index 000000000..c0fbaa614 --- /dev/null +++ b/tests/format/less/less/issue-11483--never-append-anything.less @@ -0,0 +1,3 @@ +@variable: { + field: something; +}; From af3d6fa80b96514cd3ec99a1a95875b612008aa4 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Tue, 3 Jan 2023 10:47:30 +0800 Subject: [PATCH 4/8] Quote numeric keys for json-stringify parser (#14083) --- changelog_unreleased/json/14083.md | 17 ++ src/language-js/printer-estree-json.js | 29 ++- .../json/__snapshots__/jsfmt.spec.js.snap | 230 +++++++++++++++++- tests/format/json/json/propertyKey.json | 23 +- 4 files changed, 278 insertions(+), 21 deletions(-) create mode 100644 changelog_unreleased/json/14083.md diff --git a/changelog_unreleased/json/14083.md b/changelog_unreleased/json/14083.md new file mode 100644 index 000000000..0ab6322f8 --- /dev/null +++ b/changelog_unreleased/json/14083.md @@ -0,0 +1,17 @@ +#### Quote numeric keys for json-stringify parser (#14083 by @fisker) + + +```jsx +// Input +{0: 'value'} + +// Prettier stable +{ + 0: "value" +} + +// Prettier main +{ + "0": "value" +} +``` diff --git a/src/language-js/printer-estree-json.js b/src/language-js/printer-estree-json.js index d3bc27c51..b0f8cf2c0 100644 --- a/src/language-js/printer-estree-json.js +++ b/src/language-js/printer-estree-json.js @@ -48,15 +48,13 @@ function genericPrint(path, options, print) { case "BooleanLiteral": return node.value ? "true" : "false"; case "StringLiteral": - case "NumericLiteral": return JSON.stringify(node.value); - case "Identifier": { - const parent = path.getParentNode(); - if (parent && parent.type === "ObjectProperty" && parent.key === node) { - return JSON.stringify(node.name); - } - return node.name; - } + case "NumericLiteral": + return isObjectKey(path) + ? JSON.stringify(String(node.value)) + : JSON.stringify(node.value); + case "Identifier": + return isObjectKey(path) ? JSON.stringify(node.name) : node.name; case "TemplateLiteral": // There is only one `TemplateElement` return print(["quasis", 0]); @@ -68,6 +66,12 @@ function genericPrint(path, options, print) { } } +function isObjectKey(path) { + return ( + path.getName() === "key" && path.getParentNode().type === "ObjectProperty" + ); +} + const ignoredProperties = new Set([ "start", "end", @@ -85,8 +89,13 @@ const ignoredProperties = new Set([ function clean(node, newNode /*, parent*/) { const { type } = node; // We print quoted key - if (type === "ObjectProperty" && node.key.type === "Identifier") { - newNode.key = { type: "StringLiteral", value: node.key.name }; + if (type === "ObjectProperty") { + const { key } = node; + if (key.type === "Identifier") { + newNode.key = { type: "StringLiteral", value: key.name }; + } else if (key.type === "NumericLiteral") { + newNode.key = { type: "StringLiteral", value: String(key.value) }; + } return; } if (type === "UnaryExpression" && node.operator === "+") { diff --git a/tests/format/json/json/__snapshots__/jsfmt.spec.js.snap b/tests/format/json/json/__snapshots__/jsfmt.spec.js.snap index 445b92e02..036112b18 100644 --- a/tests/format/json/json/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/json/json/__snapshots__/jsfmt.spec.js.snap @@ -2175,12 +2175,54 @@ trailingComma: "all" | printWidth =====================================input====================================== { - a: 123 + a: '', + null: '', + true: '', + "string": "", + 0: '', + 1e2: '', + 1.0e+2: '', + .10e+2: '', + 1e-2: '', + .1e-2: '', + 0.1e+2: '', + 1.0: '', + 1.00000: '', + .1: '', + .100000: '', + 0.1: '', + 0.100000: '', + 999999999999999999999999999999: '', + .000000000000000000000000000001: '', + 0.000000000000000000000000000001: '', + 1e999999999999999999999999999999: '', + 1_2_3: '', } =====================================output===================================== { - "a": 123 + "a": "", + "null": "", + "true": "", + "string": "", + "0": "", + 1e2: "", + 1.0e2: "", + 0.1e2: "", + 1e-2: "", + 0.1e-2: "", + 0.1e2: "", + 1.0: "", + 1.0: "", + "0.1": "", + "0.1": "", + "0.1": "", + "0.1": "", + 999999999999999999999999999999: "", + 0.000000000000000000000000000001: "", + 0.000000000000000000000000000001: "", + 1e999999999999999999999999999999: "", + 1_2_3: "" } ================================================================================ @@ -2194,12 +2236,54 @@ trailingComma: "all" | printWidth =====================================input====================================== { - a: 123 + a: '', + null: '', + true: '', + "string": "", + 0: '', + 1e2: '', + 1.0e+2: '', + .10e+2: '', + 1e-2: '', + .1e-2: '', + 0.1e+2: '', + 1.0: '', + 1.00000: '', + .1: '', + .100000: '', + 0.1: '', + 0.100000: '', + 999999999999999999999999999999: '', + .000000000000000000000000000001: '', + 0.000000000000000000000000000001: '', + 1e999999999999999999999999999999: '', + 1_2_3: '', } =====================================output===================================== { - a: 123, + a: "", + null: "", + true: "", + string: "", + 0: "", + 1e2: "", + 1.0e2: "", + 0.1e2: "", + 1e-2: "", + 0.1e-2: "", + 0.1e2: "", + 1.0: "", + 1.0: "", + 0.1: "", + 0.1: "", + 0.1: "", + 0.1: "", + 999999999999999999999999999999: "", + 0.000000000000000000000000000001: "", + 0.000000000000000000000000000001: "", + 1e999999999999999999999999999999: "", + 1_2_3: "", } ================================================================================ @@ -2212,12 +2296,54 @@ printWidth: 80 | printWidth =====================================input====================================== { - a: 123 + a: '', + null: '', + true: '', + "string": "", + 0: '', + 1e2: '', + 1.0e+2: '', + .10e+2: '', + 1e-2: '', + .1e-2: '', + 0.1e+2: '', + 1.0: '', + 1.00000: '', + .1: '', + .100000: '', + 0.1: '', + 0.100000: '', + 999999999999999999999999999999: '', + .000000000000000000000000000001: '', + 0.000000000000000000000000000001: '', + 1e999999999999999999999999999999: '', + 1_2_3: '', } =====================================output===================================== { - "a": 123 + "a": "", + "null": "", + "true": "", + "string": "", + "0": "", + 1e2: "", + 1.0e2: "", + 0.1e2: "", + 1e-2: "", + 0.1e-2: "", + 0.1e2: "", + 1.0: "", + 1.0: "", + "0.1": "", + "0.1": "", + "0.1": "", + "0.1": "", + 999999999999999999999999999999: "", + 0.000000000000000000000000000001: "", + 0.000000000000000000000000000001: "", + 1e999999999999999999999999999999: "", + 1_2_3: "" } ================================================================================ @@ -2230,12 +2356,54 @@ printWidth: 80 | printWidth =====================================input====================================== { - a: 123 + a: '', + null: '', + true: '', + "string": "", + 0: '', + 1e2: '', + 1.0e+2: '', + .10e+2: '', + 1e-2: '', + .1e-2: '', + 0.1e+2: '', + 1.0: '', + 1.00000: '', + .1: '', + .100000: '', + 0.1: '', + 0.100000: '', + 999999999999999999999999999999: '', + .000000000000000000000000000001: '', + 0.000000000000000000000000000001: '', + 1e999999999999999999999999999999: '', + 1_2_3: '', } =====================================output===================================== { - a: 123, + a: "", + null: "", + true: "", + string: "", + 0: "", + 1e2: "", + 1.0e2: "", + 0.1e2: "", + 1e-2: "", + 0.1e-2: "", + 0.1e2: "", + 1.0: "", + 1.0: "", + 0.1: "", + 0.1: "", + 0.1: "", + 0.1: "", + 999999999999999999999999999999: "", + 0.000000000000000000000000000001: "", + 0.000000000000000000000000000001: "", + 1e999999999999999999999999999999: "", + 1_2_3: "", } ================================================================================ @@ -2248,12 +2416,54 @@ printWidth: 80 | printWidth =====================================input====================================== { - a: 123 + a: '', + null: '', + true: '', + "string": "", + 0: '', + 1e2: '', + 1.0e+2: '', + .10e+2: '', + 1e-2: '', + .1e-2: '', + 0.1e+2: '', + 1.0: '', + 1.00000: '', + .1: '', + .100000: '', + 0.1: '', + 0.100000: '', + 999999999999999999999999999999: '', + .000000000000000000000000000001: '', + 0.000000000000000000000000000001: '', + 1e999999999999999999999999999999: '', + 1_2_3: '', } =====================================output===================================== { - "a": 123 + "a": "", + "null": "", + "true": "", + "string": "", + "0": "", + "100": "", + "100": "", + "10": "", + "0.01": "", + "0.001": "", + "10": "", + "1": "", + "1": "", + "0.1": "", + "0.1": "", + "0.1": "", + "0.1": "", + "1e+30": "", + "1e-30": "", + "1e-30": "", + "Infinity": "", + "123": "" } ================================================================================ diff --git a/tests/format/json/json/propertyKey.json b/tests/format/json/json/propertyKey.json index 399e7b9a6..12d3f130e 100644 --- a/tests/format/json/json/propertyKey.json +++ b/tests/format/json/json/propertyKey.json @@ -1,3 +1,24 @@ { - a: 123 + a: '', + null: '', + true: '', + "string": "", + 0: '', + 1e2: '', + 1.0e+2: '', + .10e+2: '', + 1e-2: '', + .1e-2: '', + 0.1e+2: '', + 1.0: '', + 1.00000: '', + .1: '', + .100000: '', + 0.1: '', + 0.100000: '', + 999999999999999999999999999999: '', + .000000000000000000000000000001: '', + 0.000000000000000000000000000001: '', + 1e999999999999999999999999999999: '', + 1_2_3: '', } From 0e25b13a288847b91fd3721b051c1736d72c4507 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 3 Jan 2023 13:30:38 +0800 Subject: [PATCH 5/8] Build(deps): Bump json5 from 2.2.1 to 2.2.2 (#14098) Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- package.json | 2 +- yarn.lock | 13 ++++--------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 58abe867f..1b770a75e 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,7 @@ "html-void-elements": "2.0.1", "ignore": "5.2.0", "jest-docblock": "28.1.1", - "json5": "2.2.1", + "json5": "2.2.2", "leven": "4.0.0", "lines-and-columns": "2.0.3", "linguist-languages": "7.21.0", diff --git a/yarn.lock b/yarn.lock index 093d1e47b..b2e313328 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4703,10 +4703,10 @@ json-stable-stringify-without-jsonify@^1.0.1: resolved "https://registry.yarnpkg.com/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz#9db7b59496ad3f3cfef30a75142d2d930ad72651" integrity sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw== -json5@2.2.1: - version "2.2.1" - resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.1.tgz#655d50ed1e6f95ad1a3caababd2b0efda10b395c" - integrity sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA== +json5@2.2.2, json5@^2.2.1: + version "2.2.2" + resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.2.tgz#64471c5bdcc564c18f7c1d4df2e2297f2457c5ab" + integrity sha512-46Tk9JiOL2z7ytNQWFLpj99RZkVgeHf87yGQKsIkaPz1qSH9UczKH1rO7K3wgRselo0tYMUNfecYpm/p1vC7tQ== json5@^1.0.1: version "1.0.1" @@ -4715,11 +4715,6 @@ json5@^1.0.1: dependencies: minimist "^1.2.0" -json5@^2.2.1: - version "2.2.2" - resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.2.tgz#64471c5bdcc564c18f7c1d4df2e2297f2457c5ab" - integrity sha512-46Tk9JiOL2z7ytNQWFLpj99RZkVgeHf87yGQKsIkaPz1qSH9UczKH1rO7K3wgRselo0tYMUNfecYpm/p1vC7tQ== - jsonfile@^6.0.1: version "6.1.0" resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-6.1.0.tgz#bc55b2634793c679ec6403094eb13698a6ec0aae" From 41cee0636ed8c10f97e0b7fc5e2ccf9d096b4439 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Tue, 3 Jan 2023 06:32:00 +0100 Subject: [PATCH 6/8] Do not change case of property name if inside a variable declaration in LESS (#14034) Co-authored-by: fisker Cheung --- changelog_unreleased/less/14034.md | 19 +++ src/language-css/parser-postcss.js | 10 +- src/language-css/printer-postcss.js | 6 +- .../case/__snapshots__/jsfmt.spec.js.snap | 144 +++++++++++++++++- tests/format/less/case/variable.less | 65 ++++++++ 5 files changed, 241 insertions(+), 3 deletions(-) create mode 100644 changelog_unreleased/less/14034.md create mode 100644 tests/format/less/case/variable.less diff --git a/changelog_unreleased/less/14034.md b/changelog_unreleased/less/14034.md new file mode 100644 index 000000000..02d0d2d5f --- /dev/null +++ b/changelog_unreleased/less/14034.md @@ -0,0 +1,19 @@ +#### Do not change case of property name if inside a variable declaration in LESS (#14034 by @mvorisek) + + +```less +// Input +@var: { + preserveCase: 0; +}; + +// Prettier stable +@var: { + preservecase: 0; +}; + +// Prettier main +@var: { + preserveCase: 0; +}; +``` diff --git a/src/language-css/parser-postcss.js b/src/language-css/parser-postcss.js index a0014a2b6..13b42c467 100644 --- a/src/language-css/parser-postcss.js +++ b/src/language-css/parser-postcss.js @@ -492,13 +492,21 @@ function parseNestedCSS(node, options) { node.params[0] === ":" ) { node.variable = true; - node.value = parseValue(node.params.slice(1), options); + const text = node.params.slice(1); + if (text) { + node.value = parseValue(text, options); + } node.raws.afterName += ":"; } // Less variable if (node.variable) { delete node.params; + + if (!node.value) { + delete node.value; + } + return node; } } diff --git a/src/language-css/printer-postcss.js b/src/language-css/printer-postcss.js index d9894d4ce..c847279b3 100644 --- a/src/language-css/printer-postcss.js +++ b/src/language-css/printer-postcss.js @@ -158,7 +158,11 @@ function genericPrint(path, options, print) { return [ node.raws.before.replace(/[\s;]/g, ""), - insideICSSRuleNode(path) ? node.prop : maybeToLowerCase(node.prop), + // Less variable + (parentNode.type === "css-atrule" && parentNode.variable) || + insideICSSRuleNode(path) + ? node.prop + : maybeToLowerCase(node.prop), trimmedBetween.startsWith("//") ? " " : "", trimmedBetween, node.extend ? "" : " ", diff --git a/tests/format/less/case/__snapshots__/jsfmt.spec.js.snap b/tests/format/less/case/__snapshots__/jsfmt.spec.js.snap index 9bc7e9003..9abe3bd1d 100644 --- a/tests/format/less/case/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/less/case/__snapshots__/jsfmt.spec.js.snap @@ -204,7 +204,7 @@ svg[viewBox] linearGradient, } @KeepDetachedRuleset: /*:*/ { - background: RED; + BACKGROUND: RED; }; %KeepScssPlaceholderSelector { @@ -321,3 +321,145 @@ TABLE { ================================================================================ `; + +exports[`variable.less format 1`] = ` +====================================options===================================== +parsers: ["less"] +printWidth: 80 + | printWidth +=====================================input====================================== +// Do not change case of property name if inside a variable declaration + +@var: { + notVar: 0; + @notVarNested: { + notVar: 1; + notVar2: 2; + }; +}; + +@var : { + preserveCase: 5; +}; + +@var: { + preserveCase: 5; +}; + +@var :{ + preserveCase: 5; +}; + +@var /* comment */ : /* comment */ { + preserveCase: 5; +}; + +@var: /* comment */ { + preserveCase: 5; +}; + +@var /* comment */ :{ + preserveCase: 5; +}; + +@var // comment +: // comment +{ + /* FIXME: should not change case */ + preserveCase: 5; +}; + +@var: // comment +{ + preserveCase: 5; +}; + +@var // comment +:{ + /* FIXME: should not change case */ + preserveCase: 5; +}; + +// Known css properties +@var: { + COLoR: RED; +}; + +@not-var { + canChangeCase: @var[@notVarNested][notVar]; + canChangeCase: @var[ @notVarNested][notVar]; + canChangeCase: @var[@notVarNested ][notVar]; + canChangeCase: @var[@notVarNested][ notVar]; + canChangeCase: @var[@notVarNested][notVar ]; + canChangeCase: @var[notVar]; +} + +=====================================output===================================== +// Do not change case of property name if inside a variable declaration + +@var: { + notVar: 0; + @notVarNested: { + notVar: 1; + notVar2: 2; + }; +}; + +@var: { + preserveCase: 5; +}; + +@var: { + preserveCase: 5; +}; + +@var: { + preserveCase: 5; +}; + +@var: /* comment */ { + preserveCase: 5; +}; + +@var: /* comment */ { + preserveCase: 5; +}; + +@var: { + preserveCase: 5; +}; + +@var // comment +: // comment +{ + /* FIXME: should not change case */ + preservecase: 5; +} + +@var: // comment +{ + preserveCase: 5; +}; + +@var // comment +: { + /* FIXME: should not change case */ + preservecase: 5; +} + +// Known css properties +@var: { + COLoR: RED; +}; + +@not-var { + canchangecase: @var[ @notVarNested][notVar]; + canchangecase: @var[ @notVarNested][notVar]; + canchangecase: @var[ @notVarNested ][notVar]; + canchangecase: @var[ @notVarNested][ notVar]; + canchangecase: @var[ @notVarNested][notVar ]; + canchangecase: @var[notVar]; +} + +================================================================================ +`; diff --git a/tests/format/less/case/variable.less b/tests/format/less/case/variable.less new file mode 100644 index 000000000..29fdd9052 --- /dev/null +++ b/tests/format/less/case/variable.less @@ -0,0 +1,65 @@ +// Do not change case of property name if inside a variable declaration + +@var: { + notVar: 0; + @notVarNested: { + notVar: 1; + notVar2: 2; + }; +}; + +@var : { + preserveCase: 5; +}; + +@var: { + preserveCase: 5; +}; + +@var :{ + preserveCase: 5; +}; + +@var /* comment */ : /* comment */ { + preserveCase: 5; +}; + +@var: /* comment */ { + preserveCase: 5; +}; + +@var /* comment */ :{ + preserveCase: 5; +}; + +@var // comment +: // comment +{ + /* FIXME: should not change case */ + preserveCase: 5; +}; + +@var: // comment +{ + preserveCase: 5; +}; + +@var // comment +:{ + /* FIXME: should not change case */ + preserveCase: 5; +}; + +// Known css properties +@var: { + COLoR: RED; +}; + +@not-var { + canChangeCase: @var[@notVarNested][notVar]; + canChangeCase: @var[ @notVarNested][notVar]; + canChangeCase: @var[@notVarNested ][notVar]; + canChangeCase: @var[@notVarNested][ notVar]; + canChangeCase: @var[@notVarNested][notVar ]; + canChangeCase: @var[notVar]; +} From 31d40104f48e9920a4485a6633b67e6a9bd79548 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 3 Jan 2023 14:19:41 +0800 Subject: [PATCH 7/8] Build(deps): Bump json5 from 2.2.1 to 2.2.3 in /website (#14101) Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- website/yarn.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/website/yarn.lock b/website/yarn.lock index 53168b0ca..3abc0b3ae 100644 --- a/website/yarn.lock +++ b/website/yarn.lock @@ -4799,9 +4799,9 @@ json-stringify-safe@~5.0.1: integrity sha512-ZClg6AaYvamvYEE82d3Iyd3vSSIjQ+odgjaTzRuO3s7toCdFKczob2i0zCh7JE8kWn17yvAWhUVxvqGwUalsRA== json5@^2.1.2, json5@^2.2.1: - version "2.2.1" - resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.1.tgz#655d50ed1e6f95ad1a3caababd2b0efda10b395c" - integrity sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA== + version "2.2.3" + resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.3.tgz#78cd6f1a19bdc12b73db5ad0c61efd66c1e29283" + integrity sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg== jsonfile@^6.0.1: version "6.1.0" From 8034bada9653d010f826d1797afe4a79bbf8ce6d Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 3 Jan 2023 18:46:14 +0800 Subject: [PATCH 8/8] Build(deps): Bump json5 from 2.2.0 to 2.2.3 in /scripts/release (#14104) Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- scripts/release/yarn.lock | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/scripts/release/yarn.lock b/scripts/release/yarn.lock index 3080461ff..c498b199a 100644 --- a/scripts/release/yarn.lock +++ b/scripts/release/yarn.lock @@ -1889,11 +1889,9 @@ json-parse-even-better-errors@^2.3.0: integrity sha512-xyFwyhro/JEof6Ghe2iz2NcXoj2sloNsWr/XsERDK/oiPCfaNhl5ONfp+jQdAZRQQ0IJWNzH9zIZF7li91kh2w== json5@^2.1.2: - version "2.2.0" - resolved "https://registry.npmjs.org/json5/-/json5-2.2.0.tgz" - integrity sha512-f+8cldu7X/y7RAJurMEJmdoKXGB/X550w2Nr3tTbezL6RwEE/iMcm+tZnXeoZtKuOq6ft8+CqzEkrIgx1fPoQA== - dependencies: - minimist "^1.2.5" + version "2.2.3" + resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.3.tgz#78cd6f1a19bdc12b73db5ad0c61efd66c1e29283" + integrity sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg== kleur@^3.0.3: version "3.0.3" @@ -1993,7 +1991,7 @@ minimatch@^3.0.4: dependencies: brace-expansion "^1.1.7" -minimist@1.2.6, minimist@^1.2.5: +minimist@1.2.6: version "1.2.6" resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.6.tgz#8637a5b759ea0d6e98702cfb3a9283323c93af44" integrity sha512-Jsjnk4bw3YJqYzbdyBiNsPWHPfO++UGG749Cxs6peCu5Xg4nrena6OVxOYxrQTqww0Jmwt+Ref8rggumkTLz9Q==