diff --git a/changelog_unreleased/javascript/18205.md b/changelog_unreleased/javascript/18205.md new file mode 100644 index 000000000..89517f4bf --- /dev/null +++ b/changelog_unreleased/javascript/18205.md @@ -0,0 +1,49 @@ +#### Fix inconsistent logical expression print (#18205 by @fisker) + + +```jsx +// Input +fn( + a && + a_long_long_long_long_long_long_long_long_long_long_long_long_long_condition + ? a + : b +); + +new Fn( + a && + a_long_long_long_long_long_long_long_long_long_long_long_long_long_condition + ? a + : b +); + +// Prettier stable +fn( + a && + a_long_long_long_long_long_long_long_long_long_long_long_long_long_condition + ? a + : b, +); + +new Fn( + a && + a_long_long_long_long_long_long_long_long_long_long_long_long_long_condition + ? a + : b, +); + +// Prettier main +fn( + a && + a_long_long_long_long_long_long_long_long_long_long_long_long_long_condition + ? a + : b, +); + +new Fn( + a && + a_long_long_long_long_long_long_long_long_long_long_long_long_long_condition + ? a + : b, +); +``` diff --git a/src/language-js/print/binaryish.js b/src/language-js/print/binaryish.js index e02512aa4..2213ab490 100644 --- a/src/language-js/print/binaryish.js +++ b/src/language-js/print/binaryish.js @@ -108,7 +108,8 @@ function printBinaryishExpression(path, options, print) { (parent.type === "ConditionalExpression" && grandparent.type !== "ReturnStatement" && grandparent.type !== "ThrowStatement" && - !isCallExpression(grandparent)) || + !isCallExpression(grandparent) && + grandparent.type !== "NewExpression") || parent.type === "TemplateLiteral" || isBooleanTypeCoercion(path); diff --git a/tests/format/js/logical-assignment/inside-call/18171.js b/tests/format/js/logical-assignment/inside-call/18171.js new file mode 100644 index 000000000..65af9794c --- /dev/null +++ b/tests/format/js/logical-assignment/inside-call/18171.js @@ -0,0 +1,47 @@ +fn( + _, + glimseGlyphsHazardNoopsTieTie === averredBathersBoxroomBuggyNurl && + // Should this line indent? + anodyneCondosMalateOverateRetinol + ? annularCooeedSplicesWalksWayWay + : kochabCooieGameOnOboleUnweave +); + +new fn( + _, + glimseGlyphsHazardNoopsTieTie === averredBathersBoxroomBuggyNurl && + // Should this line indent? + anodyneCondosMalateOverateRetinol + ? annularCooeedSplicesWalksWayWay + : kochabCooieGameOnOboleUnweave +); + +fn( + glimseGlyphsHazardNoopsTieTie === averredBathersBoxroomBuggyNurl && + // Should this line indent? + anodyneCondosMalateOverateRetinol + ? annularCooeedSplicesWalksWayWay + : kochabCooieGameOnOboleUnweave +); + +new fn( + glimseGlyphsHazardNoopsTieTie === averredBathersBoxroomBuggyNurl && + // Should this line indent? + anodyneCondosMalateOverateRetinol + ? annularCooeedSplicesWalksWayWay + : kochabCooieGameOnOboleUnweave +); + +// https://github.com/typescript-eslint/typescript-eslint/blob/ea2ee6b65a2f14dd2c3fc8d12be969cbeaef80a8/packages/typescript-estree/src/parseSettings/resolveProjectList.ts#L75C1-L80C7 +{{ RESOLUTION_CACHE = new ExpiringCache( + options.singleRun + ? 'Infinity' + : (options.cacheLifetime?.glob ?? + DEFAULT_TSCONFIG_CACHE_DURATION_SECONDS), + ); + RESOLUTION_CACHE = new_ExpiringCache( + options.singleRun + ? 'Infinity' + : (options.cacheLifetime?.glob ?? + DEFAULT_TSCONFIG_CACHE_DURATION_SECONDS), + );}} diff --git a/tests/format/js/logical-assignment/inside-call/__snapshots__/format.test.js.snap b/tests/format/js/logical-assignment/inside-call/__snapshots__/format.test.js.snap new file mode 100644 index 000000000..9bea47ac6 --- /dev/null +++ b/tests/format/js/logical-assignment/inside-call/__snapshots__/format.test.js.snap @@ -0,0 +1,111 @@ +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing + +exports[`18171.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "typescript", "flow"] +printWidth: 80 + | printWidth +=====================================input====================================== +fn( + _, + glimseGlyphsHazardNoopsTieTie === averredBathersBoxroomBuggyNurl && + // Should this line indent? + anodyneCondosMalateOverateRetinol + ? annularCooeedSplicesWalksWayWay + : kochabCooieGameOnOboleUnweave +); + +new fn( + _, + glimseGlyphsHazardNoopsTieTie === averredBathersBoxroomBuggyNurl && + // Should this line indent? + anodyneCondosMalateOverateRetinol + ? annularCooeedSplicesWalksWayWay + : kochabCooieGameOnOboleUnweave +); + +fn( + glimseGlyphsHazardNoopsTieTie === averredBathersBoxroomBuggyNurl && + // Should this line indent? + anodyneCondosMalateOverateRetinol + ? annularCooeedSplicesWalksWayWay + : kochabCooieGameOnOboleUnweave +); + +new fn( + glimseGlyphsHazardNoopsTieTie === averredBathersBoxroomBuggyNurl && + // Should this line indent? + anodyneCondosMalateOverateRetinol + ? annularCooeedSplicesWalksWayWay + : kochabCooieGameOnOboleUnweave +); + +// https://github.com/typescript-eslint/typescript-eslint/blob/ea2ee6b65a2f14dd2c3fc8d12be969cbeaef80a8/packages/typescript-estree/src/parseSettings/resolveProjectList.ts#L75C1-L80C7 +{{ RESOLUTION_CACHE = new ExpiringCache( + options.singleRun + ? 'Infinity' + : (options.cacheLifetime?.glob ?? + DEFAULT_TSCONFIG_CACHE_DURATION_SECONDS), + ); + RESOLUTION_CACHE = new_ExpiringCache( + options.singleRun + ? 'Infinity' + : (options.cacheLifetime?.glob ?? + DEFAULT_TSCONFIG_CACHE_DURATION_SECONDS), + );}} + +=====================================output===================================== +fn( + _, + glimseGlyphsHazardNoopsTieTie === averredBathersBoxroomBuggyNurl && + // Should this line indent? + anodyneCondosMalateOverateRetinol + ? annularCooeedSplicesWalksWayWay + : kochabCooieGameOnOboleUnweave, +); + +new fn( + _, + glimseGlyphsHazardNoopsTieTie === averredBathersBoxroomBuggyNurl && + // Should this line indent? + anodyneCondosMalateOverateRetinol + ? annularCooeedSplicesWalksWayWay + : kochabCooieGameOnOboleUnweave, +); + +fn( + glimseGlyphsHazardNoopsTieTie === averredBathersBoxroomBuggyNurl && + // Should this line indent? + anodyneCondosMalateOverateRetinol + ? annularCooeedSplicesWalksWayWay + : kochabCooieGameOnOboleUnweave, +); + +new fn( + glimseGlyphsHazardNoopsTieTie === averredBathersBoxroomBuggyNurl && + // Should this line indent? + anodyneCondosMalateOverateRetinol + ? annularCooeedSplicesWalksWayWay + : kochabCooieGameOnOboleUnweave, +); + +// https://github.com/typescript-eslint/typescript-eslint/blob/ea2ee6b65a2f14dd2c3fc8d12be969cbeaef80a8/packages/typescript-estree/src/parseSettings/resolveProjectList.ts#L75C1-L80C7 +{ + { + RESOLUTION_CACHE = new ExpiringCache( + options.singleRun + ? "Infinity" + : (options.cacheLifetime?.glob ?? + DEFAULT_TSCONFIG_CACHE_DURATION_SECONDS), + ); + RESOLUTION_CACHE = new_ExpiringCache( + options.singleRun + ? "Infinity" + : (options.cacheLifetime?.glob ?? + DEFAULT_TSCONFIG_CACHE_DURATION_SECONDS), + ); + } +} + +================================================================================ +`; diff --git a/tests/format/js/logical-assignment/inside-call/format.test.js b/tests/format/js/logical-assignment/inside-call/format.test.js new file mode 100644 index 000000000..8adc66f60 --- /dev/null +++ b/tests/format/js/logical-assignment/inside-call/format.test.js @@ -0,0 +1 @@ +runFormatTest(import.meta, ["babel", "typescript", "flow"]);