From a00d31e1a4f6db1e16fb7a6f48496fcfb430544d Mon Sep 17 00:00:00 2001 From: Or Rosenblatt Date: Sat, 9 Jul 2022 00:30:26 -0400 Subject: [PATCH 1/4] Support `.lintstagedrc` (#13081) * Test `.lintstagedrc` JSON and YML formatting (TDD) * Support `.lintstagedrc` formatting * Update spell checking for `.lintstagedrc` * Update changelog * Update changelog --- changelog_unreleased/cli/13081.md | 3 +++ cspell.json | 2 ++ src/language-yaml/embed.js | 4 +-- src/language-yaml/index.js | 1 + .../__snapshots__/support-info.js.snap | 3 ++- .../__tests__/with-parser-inference.js | 25 +++++++++++++++++++ 6 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 changelog_unreleased/cli/13081.md diff --git a/changelog_unreleased/cli/13081.md b/changelog_unreleased/cli/13081.md new file mode 100644 index 000000000..67acee028 --- /dev/null +++ b/changelog_unreleased/cli/13081.md @@ -0,0 +1,3 @@ +#### Infer parser for `.lintstagedrc` (#13081 by @OrRosenblatt) + +A `.lintstagedrc` file (without extension) is handled using `json` and `yaml` parsers. diff --git a/cspell.json b/cspell.json index 37f04c11c..8bd613b15 100644 --- a/cspell.json +++ b/cspell.json @@ -184,6 +184,8 @@ "linearize", "linebreak", "linebreaks", + "lintstaged", + "lintstagedrc", "literalline", "loglevel", "lowercased", diff --git a/src/language-yaml/embed.js b/src/language-yaml/embed.js index cc3e0722c..244eb07a2 100644 --- a/src/language-yaml/embed.js +++ b/src/language-yaml/embed.js @@ -3,11 +3,11 @@ function embed(path, print, textToDoc, options) { const node = path.getValue(); - // Try to format `.prettierrc` and `.stylelintrc` as `json` first + // Try to format `.prettierrc`, `.stylelintrc`, and `.lintstagedrc` as `json` first if ( node.type === "root" && options.filepath && - /(?:[/\\]|^)\.(?:prettier|stylelint)rc$/.test(options.filepath) + /(?:[/\\]|^)\.(?:prettier|stylelint|lintstaged)rc$/.test(options.filepath) ) { return textToDoc(options.originalText, { ...options, parser: "json" }); } diff --git a/src/language-yaml/index.js b/src/language-yaml/index.js index 600ed4193..0a6815457 100644 --- a/src/language-yaml/index.js +++ b/src/language-yaml/index.js @@ -15,6 +15,7 @@ const languages = [ ...data.filenames.filter((filename) => filename !== "yarn.lock"), ".prettierrc", ".stylelintrc", + ".lintstagedrc", ], })), ]; diff --git a/tests/integration/__tests__/__snapshots__/support-info.js.snap b/tests/integration/__tests__/__snapshots__/support-info.js.snap index 8e6f7d9e0..fa53d0982 100644 --- a/tests/integration/__tests__/__snapshots__/support-info.js.snap +++ b/tests/integration/__tests__/__snapshots__/support-info.js.snap @@ -774,7 +774,8 @@ exports[`CLI --support-info (stdout) 1`] = ` "CITATION.cff", "glide.lock", ".prettierrc", - ".stylelintrc" + ".stylelintrc", + ".lintstagedrc" ], "linguistLanguageId": 407, "name": "YAML", diff --git a/tests/integration/__tests__/with-parser-inference.js b/tests/integration/__tests__/with-parser-inference.js index 2e5d64527..f503eef40 100644 --- a/tests/integration/__tests__/with-parser-inference.js +++ b/tests/integration/__tests__/with-parser-inference.js @@ -70,4 +70,29 @@ describe("infers parser from filename", () => { ` + "\n" ); }); + + test("json from .lintstagedrc", () => { + expect( + prettier.format(" { '*': 'your-cmd' } ", { + filepath: "/path/to/.lintstagedrc", + }) + ).toBe('{ "*": "your-cmd" }\n'); + }); + + test("yaml from .lintstagedrc", () => { + expect( + prettier.format( + /* indent */ ` + '*': + - your-cmd + `, + { filepath: "/path/to/.lintstagedrc" } + ) + ).toBe( + outdent` + "*": + - your-cmd + ` + "\n" + ); + }); }); From 6bc6914d0b61acfaa5a0b558f50c3812141e4f04 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sat, 9 Jul 2022 15:03:46 +0800 Subject: [PATCH 2/4] Enable `unicorn/prefer-logical-operator-over-ternary` rule (#13060) --- .eslintrc.js | 1 + package.json | 2 +- src/language-css/parser-postcss.js | 12 +++--------- src/language-css/printer-postcss.js | 2 +- yarn.lock | 28 +++++++++++++++++++--------- 5 files changed, 25 insertions(+), 20 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index fe05e9084..bc48344c8 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -146,6 +146,7 @@ module.exports = { "unicorn/prefer-array-some": "error", "unicorn/prefer-includes": "error", "unicorn/prefer-json-parse-buffer": "error", + "unicorn/prefer-logical-operator-over-ternary": "error", "unicorn/prefer-native-coercion-functions": "error", "unicorn/prefer-number-properties": "error", "unicorn/prefer-optional-catch-binding": "error", diff --git a/package.json b/package.json index 36c128026..1903ef8a3 100644 --- a/package.json +++ b/package.json @@ -121,7 +121,7 @@ "eslint-plugin-prettier-internal-rules": "2.0.1", "eslint-plugin-react": "7.29.4", "eslint-plugin-regexp": "1.7.0", - "eslint-plugin-unicorn": "42.0.0", + "eslint-plugin-unicorn": "43.0.0", "esm-utils": "3.0.0", "execa": "6.1.0", "jest": "27.5.1", diff --git a/src/language-css/parser-postcss.js b/src/language-css/parser-postcss.js index ac334014d..46c696fae 100644 --- a/src/language-css/parser-postcss.js +++ b/src/language-css/parser-postcss.js @@ -306,9 +306,7 @@ function parseNestedCSS(node, options) { if (typeof node.selector === "string") { selector = node.raws.selector - ? node.raws.selector.scss - ? node.raws.selector.scss - : node.raws.selector.raw + ? node.raws.selector.scss ?? node.raws.selector.raw : node.selector; if (node.raws.between && node.raws.between.trim().length > 0) { @@ -322,9 +320,7 @@ function parseNestedCSS(node, options) { if (typeof node.value === "string") { value = node.raws.value - ? node.raws.value.scss - ? node.raws.value.scss - : node.raws.value.raw + ? node.raws.value.scss ?? node.raws.value.raw : node.value; value = value.trim(); @@ -336,9 +332,7 @@ function parseNestedCSS(node, options) { if (typeof node.params === "string") { params = node.raws.params - ? node.raws.params.scss - ? node.raws.params.scss - : node.raws.params.raw + ? node.raws.params.scss ?? node.raws.params.raw : node.params; if (node.raws.afterName && node.raws.afterName.trim().length > 0) { diff --git a/src/language-css/printer-postcss.js b/src/language-css/printer-postcss.js index d4de2f191..825961ff2 100644 --- a/src/language-css/printer-postcss.js +++ b/src/language-css/printer-postcss.js @@ -415,7 +415,7 @@ function genericPrint(path, options, print) { ? [node.namespace === true ? "" : node.namespace.trim(), "|"] : "", node.attribute.trim(), - node.operator ? node.operator : "", + node.operator ?? "", node.value ? quoteAttributeValue( adjustStrings(node.value.trim(), options), diff --git a/yarn.lock b/yarn.lock index f98e5be60..56830743f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -263,11 +263,16 @@ dependencies: "@babel/types" "^7.16.7" -"@babel/helper-validator-identifier@^7.15.7", "@babel/helper-validator-identifier@^7.16.7": +"@babel/helper-validator-identifier@^7.16.7": version "7.16.7" resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.16.7.tgz#e8c602438c4a8195751243da9031d1607d247cad" integrity sha512-hsEnFemeiW4D08A5gUAZxLBTXpZ39P+a+DGDsHw1yxqyQ/jzFEnxf5uTEGp+3bzAbNOxU1paTgYS4ECU/IgfDw== +"@babel/helper-validator-identifier@^7.18.6": + version "7.18.6" + resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.18.6.tgz#9c97e30d31b2b8c72a1d08984f2ca9b574d7a076" + integrity sha512-MmetCkz9ej86nJQV+sFCxoGGrUbU3q02kgLciwkrt9QqEB7cP39oKEY0PakknEO0Gu20SskMRi+AYZ3b1TpN9g== + "@babel/helper-validator-option@^7.16.7": version "7.16.7" resolved "https://registry.yarnpkg.com/@babel/helper-validator-option/-/helper-validator-option-7.16.7.tgz#b203ce62ce5fe153899b617c08957de860de4d23" @@ -2277,11 +2282,16 @@ character-reference-invalid@^1.0.0: resolved "https://registry.yarnpkg.com/character-reference-invalid/-/character-reference-invalid-1.1.4.tgz#083329cda0eae272ab3dbbf37e9a382c13af1560" integrity sha512-mKKUkUbhPpQlCOfIuZkvSEgktjPFIsZKRRbC6KWVEMvlzblj3i3asQv5ODsrwt0N3pHAEvjP8KTQPHkp0+6jOg== -ci-info@3.3.0, ci-info@^3.2.0, ci-info@^3.3.0: +ci-info@3.3.0, ci-info@^3.2.0: version "3.3.0" resolved "https://registry.yarnpkg.com/ci-info/-/ci-info-3.3.0.tgz#b4ed1fb6818dea4803a55c623041f9165d2066b2" integrity sha512-riT/3vI5YpVH6/qomlDnJow6TBee2PBKSEpx3O32EGPYbWGIRsIlGRms3Sm74wYE1JMo8RnO04Hb12+v1J5ICw== +ci-info@^3.3.2: + version "3.3.2" + resolved "https://registry.yarnpkg.com/ci-info/-/ci-info-3.3.2.tgz#6d2967ffa407466481c6c90b6e16b3098f080128" + integrity sha512-xmDt/QIAdeZ9+nfdPsaBCpMvHNLFiLdjj59qjqn+6iPe6YmHGQ35sBnQ8uslRBXFmXkiZQOJRjvQeoGppoTjjg== + cjk-regex@2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/cjk-regex/-/cjk-regex-2.0.1.tgz#98cca187aa67931db14f0d9dde556150c8116d95" @@ -3122,13 +3132,13 @@ eslint-plugin-regexp@1.7.0: regexpp "^3.2.0" scslre "^0.1.6" -eslint-plugin-unicorn@42.0.0: - version "42.0.0" - resolved "https://registry.yarnpkg.com/eslint-plugin-unicorn/-/eslint-plugin-unicorn-42.0.0.tgz#47d60c00c263ad743403b052db689e39acbacff1" - integrity sha512-ixBsbhgWuxVaNlPTT8AyfJMlhyC5flCJFjyK3oKE8TRrwBnaHvUbuIkCM1lqg8ryYrFStL/T557zfKzX4GKSlg== +eslint-plugin-unicorn@43.0.0: + version "43.0.0" + resolved "https://registry.yarnpkg.com/eslint-plugin-unicorn/-/eslint-plugin-unicorn-43.0.0.tgz#c26fdfd146036b3f7951fa0f8c9af2b81bd87096" + integrity sha512-Z/6HX8yry+zAjo4jHHTAbe1rfniox7qgmCReGBfTc/CVgotfScaMCc4dtSSTHlJ+7Yix5o6LPXzwwpuGGFricg== dependencies: - "@babel/helper-validator-identifier" "^7.15.7" - ci-info "^3.3.0" + "@babel/helper-validator-identifier" "^7.18.6" + ci-info "^3.3.2" clean-regexp "^1.0.0" eslint-utils "^3.0.0" esquery "^1.4.0" @@ -3139,7 +3149,7 @@ eslint-plugin-unicorn@42.0.0: read-pkg-up "^7.0.1" regexp-tree "^0.1.24" safe-regex "^2.1.1" - semver "^7.3.5" + semver "^7.3.7" strip-indent "^3.0.0" eslint-scope@^5.1.1: From ae3dd17114bfb9831b85816f927eea90dcb0968b Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Sat, 9 Jul 2022 20:05:27 +0900 Subject: [PATCH 3/4] Refactor: Extract `printEndOfOpeningTag` (#13086) * Refactor * Rename * Fix typo --- src/language-js/print/jsx.js | 62 +++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/src/language-js/print/jsx.js b/src/language-js/print/jsx.js index cb307e43e..fe18414a9 100644 --- a/src/language-js/print/jsx.js +++ b/src/language-js/print/jsx.js @@ -574,27 +574,6 @@ function printJsxOpeningElement(path, options, print) { ]); } - const lastAttrHasTrailingComments = - node.attributes.length > 0 && - hasComment(getLast(node.attributes), CommentCheckFlags.Trailing); - - const bracketSameLine = - // Simple tags (no attributes and no comment in tag name) should be - // kept unbroken regardless of `bracketSameLine`. - // jsxBracketSameLine is deprecated in favour of bracketSameLine, - // but is still needed for backwards compatibility. - (node.attributes.length === 0 && !nameHasComments) || - ((options.bracketSameLine || options.jsxBracketSameLine) && - // We should print the bracket in a new line for the following cases: - //
- //
- (!nameHasComments || node.attributes.length > 0) && - !lastAttrHasTrailingComments); - // We should print the opening element expanded if any prop value is a // string literal with newlines const shouldBreak = @@ -617,13 +596,50 @@ function printJsxOpeningElement(path, options, print) { print("name"), print("typeParameters"), indent(path.map(() => [attributeLine, print()], "attributes")), - node.selfClosing ? line : bracketSameLine ? ">" : softline, - node.selfClosing ? "/>" : bracketSameLine ? "" : ">", + ...printEndOfOpeningTag(node, options, nameHasComments), ], { shouldBreak } ); } +function printEndOfOpeningTag(node, options, nameHasComments) { + if (node.selfClosing) { + return [line, "/>"]; + } + const bracketSameLine = shouldPrintBracketSameLine( + node, + options, + nameHasComments + ); + if (bracketSameLine) { + return [">"]; + } + return [softline, ">"]; +} + +function shouldPrintBracketSameLine(node, options, nameHasComments) { + const lastAttrHasTrailingComments = + node.attributes.length > 0 && + hasComment(getLast(node.attributes), CommentCheckFlags.Trailing); + return ( + // Simple tags (no attributes and no comment in tag name) should be + // kept unbroken regardless of `bracketSameLine`. + // jsxBracketSameLine is deprecated in favour of bracketSameLine, + // but is still needed for backwards compatibility. + (node.attributes.length === 0 && !nameHasComments) || + ((options.bracketSameLine || options.jsxBracketSameLine) && + // We should print the bracket in a new line for the following cases: + //
+ //
+ (!nameHasComments || node.attributes.length > 0) && + !lastAttrHasTrailingComments) + ); +} + function printJsxClosingElement(path, options, print) { const node = path.getValue(); const parts = []; From 811b591ee8ece503931af5a84d8813cd29e6ea8f Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Mon, 11 Jul 2022 10:03:49 +0800 Subject: [PATCH 4/4] Update dependency jest-docblock to v28.1.1 (#13054) --- changelog_unreleased/javascript/13054.md | 28 +++++++++++++++++++ package.json | 2 +- .../js/__snapshots__/jsfmt.spec.js.snap | 22 +++++++++++++++ .../misc/insert-pragma/js/with-comment.js | 2 ++ yarn.lock | 9 +++++- 5 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 changelog_unreleased/javascript/13054.md create mode 100644 tests/format/misc/insert-pragma/js/with-comment.js diff --git a/changelog_unreleased/javascript/13054.md b/changelog_unreleased/javascript/13054.md new file mode 100644 index 000000000..6fddc71be --- /dev/null +++ b/changelog_unreleased/javascript/13054.md @@ -0,0 +1,28 @@ +#### Fix docblock parsing (#13054 by @fisker) + + +```jsx +// With `--insert-pragma` flag + +// Input +/* comment */ +foo() + +// Prettier stable +/** + * /* comment + * + * @format + */ + +foo(); + +// Prettier main +/** + * comment + * + * @format + */ + +foo(); +``` diff --git a/package.json b/package.json index 1903ef8a3..d0b8c8ccb 100644 --- a/package.json +++ b/package.json @@ -58,7 +58,7 @@ "html-tag-names": "2.0.1", "html-void-elements": "2.0.1", "ignore": "5.2.0", - "jest-docblock": "27.5.1", + "jest-docblock": "28.1.1", "json5": "2.2.1", "leven": "4.0.0", "lines-and-columns": "2.0.3", diff --git a/tests/format/misc/insert-pragma/js/__snapshots__/jsfmt.spec.js.snap b/tests/format/misc/insert-pragma/js/__snapshots__/jsfmt.spec.js.snap index 8ec5cc9b2..93eafd4a8 100644 --- a/tests/format/misc/insert-pragma/js/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/misc/insert-pragma/js/__snapshots__/jsfmt.spec.js.snap @@ -262,3 +262,25 @@ printWidth: 80 ================================================================================ `; + +exports[`with-comment.js - {"insertPragma":true} format 1`] = ` +====================================options===================================== +insertPragma: true +parsers: ["flow", "babel", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +/* comment */ +foo() + +=====================================output===================================== +/** + * comment + * + * @format + */ + +foo(); + +================================================================================ +`; diff --git a/tests/format/misc/insert-pragma/js/with-comment.js b/tests/format/misc/insert-pragma/js/with-comment.js new file mode 100644 index 000000000..23f24e53c --- /dev/null +++ b/tests/format/misc/insert-pragma/js/with-comment.js @@ -0,0 +1,2 @@ +/* comment */ +foo() diff --git a/yarn.lock b/yarn.lock index 56830743f..e3c427e3d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4175,7 +4175,14 @@ jest-diff@^27.5.1: jest-get-type "^27.5.1" pretty-format "^27.5.1" -jest-docblock@27.5.1, jest-docblock@^27.5.1: +jest-docblock@28.1.1: + version "28.1.1" + resolved "https://registry.yarnpkg.com/jest-docblock/-/jest-docblock-28.1.1.tgz#6f515c3bf841516d82ecd57a62eed9204c2f42a8" + integrity sha512-3wayBVNiOYx0cwAbl9rwm5kKFP8yHH3d/fkEaL02NPTkDojPtheGB7HZSFY4wzX+DxyrvhXz0KSCVksmCknCuA== + dependencies: + detect-newline "^3.0.0" + +jest-docblock@^27.5.1: version "27.5.1" resolved "https://registry.yarnpkg.com/jest-docblock/-/jest-docblock-27.5.1.tgz#14092f364a42c6108d42c33c8cf30e058e25f6c0" integrity sha512-rl7hlABeTsRYxKiUfpHrQrG4e2obOiTQWfMEH3PxPjOtdsfLQO4ReWSZaQ7DETm4xu07rl4q/h4zcKXyU0/OzQ==