Skip to content

Commit

Permalink
challenge(formatter): highligh prettier reformat issue (#790)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos committed Nov 19, 2023
1 parent 70c10d4 commit f327f7b
Show file tree
Hide file tree
Showing 18 changed files with 532 additions and 0 deletions.
8 changes: 8 additions & 0 deletions crates/biome_formatter_test/src/prettier/prepare_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ async function traverseDir(dir, input_config) {
'jsfmt.spec.js.snap'
);
const snapFile = path.basename(file) + '.prettier-snap';
const snapOriginalFile = path.basename(file) + '.prettier-snap-original';

const snapshot = require(snapshotPath);

Expand Down Expand Up @@ -103,6 +104,7 @@ async function traverseDir(dir, input_config) {
const outputEndOffset = outputEnd.index;
snapshotContent = snapshotContent.substring(outputStartOffset, outputEndOffset);

let originalSnapshot = snapshotContent;
try {
// We need to reformat prettier snapshot
// because Biome and Prettier have different default options
Expand All @@ -111,6 +113,12 @@ async function traverseDir(dir, input_config) {
} catch (error) {
console.error(`Prettier format error in ${filePath}: ${error}`);
}

if (originalSnapshot !== snapshotContent) {
// Prettier has a reformat issue
await fs.writeFile(path.resolve(outDir, snapOriginalFile), originalSnapshot);
}

// Write the expected output to an additional prettier-snap
// file in the specs directory
await fs.writeFile(path.resolve(outDir, snapFile), snapshotContent);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class x {
focus() { // comment 1
// comment 2
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
function jsx() {
return (
// Comment
<div />
);
}

function unary() {
return (
// Comment
!!x
);
}

function numericLiteralNoParen() {
return 1337; // Comment
}

function logical() {
return (
// Reason for 42
42 && 84
);
}

function binary() {
return (
// Reason for 42
42 * 84
);
}

function binaryInBinaryLeft() {
return (
// Reason for 42
42 *
84 +
2
);
}

function binaryInBinaryRight() {
return (
// Reason for 42
42 +
84 * 2
);
}

function conditional() {
return (
// Reason for 42
42
? 1
: 2
);
}

function binaryInConditional() {
return (
// Reason for 42
42 * 3
? 1
: 2
);
}

function call() {
return (
// Reason for a
a()
);
}

function memberInside() {
return (
// Reason for a.b
a.b.c
);
}

function memberOutside() {
return (
// Reason for a
a.b.c
);
}

function memberInAndOutWithCalls() {
return (
aFunction
.b// Reason for a
()
.c.d()
);
}

function excessiveEverything() {
return (
// Reason for stuff
a.b() * 3 + 4 ? ((a`hi`, 1) ? 1 : 1) : 1
);
}

// See https://github.com/prettier/prettier/issues/2392
// function sequenceExpression() {
// return (
// // Reason for a
// a
// ), b
// }

function sequenceExpressionInside() {
return (
// Reason for a
a, b
);
}

function taggedTemplate() {
return (
// Reason for a
a`b`
);
}

function inlineComment() {
return /* hi */ 42 || 42;
}

function multilineBlockSameLine() {
return (
/**
* @type {string}
*/ "result"
);
}

function multilineBlockNextLine() {
return (
/**
* @type {string}
*/
"result"
);
}

function multilineBlockSameLineJsx() {
return (
/**
* JSX Same line
*/ <div></div>
);
}

function multilineBlockNextLineJsx() {
return (
/**
* JSX Next line
*/
<div></div>
);
}

function singleLineBlockSameLine() {
return /** Result -> */ "result";
}

function singleLineBlockNextLine() {
return (
/** Result below */
"result"
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
foo``; // comment

foo // comment
``;

foo // comment
`
`;

foo/* comment */ `
`;

foo /* comment */`
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
for (;;)
continue;
// comment

for (;;)
break;
// comment

for (const f of [])
continue;
// comment

for (const f of [])
break;
// comment

for (const f in {})
continue;
// comment

for (const f in {})
break;
// comment

for (;;)
continue; // comment

for (;;)
break; // comment

for (const f of [])
continue; // comment

for (const f of [])
break; // comment

for (const f in {})
continue; // comment

for (const f in {})
break; // comment

for (;;) continue; /* comment */

for (;;) break; /* comment */

for (const f of []) continue; /* comment */

for (const f of []) break; /* comment */

for (const f in {}) continue; /* comment */

for (const f in {}) break; /* comment */

for (;;)
continue;
/* comment */

for (;;)
break;
/* comment */

for (const f of [])
continue;
/* comment */

for (const f of [])
break;
/* comment */

for (const f in {})
continue;
/* comment */

for (const f in {})
break;
/* comment */

label1: for (;;) continue label1 /* comment */;

label1: for (;;)
continue label1;
/* comment */

label1: for (;;)
continue label1; // comment

label1: for (;;)
continue label1;
// comment
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
foo(
/* HTML */ `<!-- bar1 -->
bar
<!-- bar2 -->`,
);
foo(/* HTML */ `
<!-- bar1 -->
bar
<!-- bar2 -->
`);
foo(
/* HTML */ `<div>
<p>bar</p>
foo
</div>`,
);
foo(/* HTML */ `
<div>
<p>bar</p>
foo
</div>
`);
foo(/* GraphQL */ `
query {
foo {
bar
}
}
`);
foo(/* ... */ css`
color: magenta;
`);
const a = (b) =>
/* HTML */ `<!-- bar1 -->
bar
<!-- bar2 -->`;
const c = (b) => /* HTML */ `
<!-- bar1 -->
bar
<!-- bar2 -->
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
foo = 1.0000;bar = 1.0;baz=1.0000;
// The range will be 13~26
// `foo` ends at 13, should not format
// `bar` ends at 26, should format
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@


class a {
b() {}
}

let x
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
call(
1, 2,3
);

call(1, 2, 3);

call(1, 2, 3);

call(
1, 2,3
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
try {
1;if (condition) {
body;
}
}
catch (err) {}

0 comments on commit f327f7b

Please sign in to comment.