Skip to content

Commit ade9c7b

Browse files
committed
Undoing unnecessary changes to js files
1 parent eff507c commit ade9c7b

8 files changed

Lines changed: 65 additions & 85 deletions

File tree

src/common/printer-helpers.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
const { group, indent, join, line, softline, hardline } = doc.builders;
99

1010
export const printComments = (node, path, options, filter = () => true) => {
11-
if (!node.comments) return [];
11+
if (!node.comments) return '';
1212
const document = join(
1313
line,
1414
path
@@ -37,26 +37,35 @@ export const printComments = (node, path, options, filter = () => true) => {
3737
};
3838

3939
export function printPreservingEmptyLines(path, key, options, print) {
40-
return path.map((childPath, index) => {
41-
const node = childPath.getNode();
42-
return [
40+
const parts = [];
41+
path.each((childPath, index) => {
42+
const node = childPath.getValue();
43+
const nodeType = node.type;
44+
45+
if (
4346
// Avoid adding a hardline at the beginning of the document.
44-
index > 0 &&
47+
parts.length !== 0 &&
4548
// LabelDefinition adds a dedented line so we don't have to prepend a
4649
// hardline.
47-
node.type !== 'LabelDefinition'
48-
? hardline
49-
: '',
50-
print(childPath),
51-
// Only attempt to append an empty line if `node` is not the last item
50+
nodeType !== 'LabelDefinition'
51+
) {
52+
parts.push(hardline);
53+
}
54+
55+
parts.push(print(childPath));
56+
57+
// Only attempt to append an empty line if `node` is not the last item
58+
if (
5259
!isLast(childPath, key, index) &&
60+
isNextLineEmpty(options.originalText, options.locEnd(node) + 1)
61+
) {
5362
// Append an empty line if the original text already had an one after
5463
// the current `node`
55-
isNextLineEmpty(options.originalText, options.locEnd(node) + 1)
56-
? hardline
57-
: ''
58-
];
64+
parts.push(hardline);
65+
}
5966
}, key);
67+
68+
return parts;
6069
}
6170

6271
// This function will add an indentation to the `item` and separate it from the

src/common/util.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,3 @@ export function hasNodeIgnoreComment(node) {
4444
node.comments.some((comment) => comment.value.trim() === 'prettier-ignore')
4545
);
4646
}
47-
48-
export function isLabel(doc) {
49-
return doc.type === 'label';
50-
}

src/nodes/ExpressionStatement.js

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,6 @@
1-
import { doc } from 'prettier';
2-
import { printComments } from '../common/printer-helpers.js';
3-
4-
const { hardline } = doc.builders;
5-
61
export const ExpressionStatement = {
7-
print: ({ node, options, path, print }) => {
8-
const parts = [];
9-
10-
const parent = path.getParentNode();
11-
12-
if (parent.type === 'IfStatement') {
13-
if (node.comments?.length) {
14-
const comments = printComments(node, path, options);
15-
if (comments?.length) {
16-
parts.push(comments);
17-
parts.push(hardline);
18-
}
19-
}
20-
}
21-
22-
parts.push(path.call(print, 'expression'));
23-
parts.push(node.omitSemicolon ? '' : ';');
24-
25-
return parts;
26-
}
2+
print: ({ node, path, print }) => [
3+
path.call(print, 'expression'),
4+
node.omitSemicolon ? '' : ';'
5+
]
276
};

src/nodes/IfStatement.js

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { doc } from 'prettier';
2-
import {
3-
printComments,
4-
printSeparatedItem
5-
} from '../common/printer-helpers.js';
2+
import { printSeparatedItem } from '../common/printer-helpers.js';
63

74
const { group, hardline, indent, line } = doc.builders;
85

@@ -22,10 +19,9 @@ const printFalseBody = (node, path, print) =>
2219
? [' ', path.call(print, 'falseBody')]
2320
: group(indent([line, path.call(print, 'falseBody')]));
2421

25-
const printElse = (node, path, print, commentsBetweenIfAndElse) => {
22+
const printElse = (node, path, print) => {
2623
if (node.falseBody) {
27-
const elseOnSameLine =
28-
node.trueBody.type === 'Block' && commentsBetweenIfAndElse.length === 0;
24+
const elseOnSameLine = node.trueBody.type === 'Block';
2925
return [
3026
elseOnSameLine ? ' ' : hardline,
3127
'else',
@@ -36,22 +32,11 @@ const printElse = (node, path, print, commentsBetweenIfAndElse) => {
3632
};
3733

3834
export const IfStatement = {
39-
print: ({ node, options, path, print }) => {
40-
const comments = node.comments || [];
41-
const commentsBetweenIfAndElse = comments.filter(
42-
(comment) => !comment.leading && !comment.trailing
43-
);
44-
45-
const parts = [];
46-
47-
parts.push('if (', printSeparatedItem(path.call(print, 'condition')), ')');
48-
parts.push(printTrueBody(node, path, print));
49-
if (commentsBetweenIfAndElse.length && node.falseBody) {
50-
parts.push(hardline);
51-
parts.push(printComments(node, path, options));
52-
}
53-
parts.push(printElse(node, path, print, commentsBetweenIfAndElse));
54-
55-
return parts;
56-
}
35+
print: ({ node, path, print }) => [
36+
'if (',
37+
printSeparatedItem(path.call(print, 'condition')),
38+
')',
39+
printTrueBody(node, path, print),
40+
printElse(node, path, print)
41+
]
5742
};

src/prettier-comments/language-js/comments.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,24 @@ function handleIfStatementComments(
257257
precedingNode === enclosingNode.trueBody &&
258258
followingNode === enclosingNode.falseBody
259259
) {
260-
if (precedingNode.type === "Block") {
261-
if(precedingNode.statements.length === 0) {
262-
addDanglingComment(precedingNode, comment);
260+
if (followingNode.type === "Block") {
261+
if(followingNode.statements.length === 0) {
262+
addDanglingComment(followingNode, comment);
263+
} else {
264+
addLeadingComment(followingNode.statements[followingNode.statements.length - 1], comment);
265+
}
266+
} else if (followingNode.type === "IfStatement") {
267+
if (followingNode.trueBody.type === "Block") {
268+
if(followingNode.trueBody.statements.length === 0) {
269+
addDanglingComment(followingNode.trueBody, comment);
270+
} else {
271+
addLeadingComment(followingNode.trueBody.statements[0], comment);
272+
}
263273
} else {
264-
addTrailingComment(precedingNode.statements[precedingNode.statements.length - 1], comment);
274+
addLeadingComment(followingNode.trueBody, comment);
265275
}
266276
} else {
267-
addTrailingComment(precedingNode, comment);
277+
addLeadingComment(precedingNode, comment);
268278
}
269279
return true;
270280
}
@@ -283,7 +293,15 @@ function handleIfStatementComments(
283293
}
284294

285295
if (followingNode.type === "IfStatement") {
286-
addBlockOrNotComment(followingNode.trueBody, comment);
296+
if (followingNode.trueBody.type === "Block") {
297+
if(followingNode.trueBody.statements.length === 0) {
298+
addDanglingComment(followingNode.trueBody, comment);
299+
} else {
300+
addLeadingComment(followingNode.trueBody.statements[0], comment);
301+
}
302+
} else {
303+
addLeadingComment(followingNode.trueBody, comment);
304+
}
287305
return true;
288306
}
289307

tests/format/BreakingChangesV0.8.0/BreakingChangesV0.8.0.sol

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// SPDX-License-Identifier: MIT
2-
// https://github.com/NomicFoundation/slang/issues/986
32
pragma solidity >=0.7.0 <=0.8.5;
43

54
contract BreakingChangesV080 {

tests/format/BreakingChangesV0.8.0/__snapshots__/format.test.js.snap

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ printWidth: 80
88
| printWidth
99
=====================================input======================================
1010
// SPDX-License-Identifier: MIT
11-
// https://github.com/NomicFoundation/slang/issues/986
1211
pragma solidity >=0.7.0 <=0.8.5;
1312
1413
contract BreakingChangesV080 {
@@ -53,7 +52,6 @@ contract BreakingChangesV080 {
5352
5453
=====================================output=====================================
5554
// SPDX-License-Identifier: MIT
56-
// https://github.com/NomicFoundation/slang/issues/986
5755
pragma solidity >=0.7.0 <=0.8.5;
5856
5957
contract BreakingChangesV080 {
@@ -98,7 +96,6 @@ printWidth: 80
9896
| printWidth
9997
=====================================input======================================
10098
// SPDX-License-Identifier: MIT
101-
// https://github.com/NomicFoundation/slang/issues/986
10299
pragma solidity >=0.7.0 <=0.8.5;
103100
104101
contract BreakingChangesV080 {
@@ -143,7 +140,6 @@ contract BreakingChangesV080 {
143140
144141
=====================================output=====================================
145142
// SPDX-License-Identifier: MIT
146-
// https://github.com/NomicFoundation/slang/issues/986
147143
pragma solidity >=0.7.0 <=0.8.5;
148144
149145
contract BreakingChangesV080 {
@@ -187,7 +183,6 @@ printWidth: 80
187183
| printWidth
188184
=====================================input======================================
189185
// SPDX-License-Identifier: MIT
190-
// https://github.com/NomicFoundation/slang/issues/986
191186
pragma solidity >=0.7.0 <=0.8.5;
192187
193188
contract BreakingChangesV080 {
@@ -232,7 +227,6 @@ contract BreakingChangesV080 {
232227
233228
=====================================output=====================================
234229
// SPDX-License-Identifier: MIT
235-
// https://github.com/NomicFoundation/slang/issues/986
236230
pragma solidity >=0.7.0 <=0.8.5;
237231
238232
contract BreakingChangesV080 {

tests/format/Comments/__snapshots__/format.test.js.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,18 +260,18 @@ contract Comments7 {
260260
261261
contract Comments8 {
262262
function someFunction() {
263-
if (something) {
263+
if (something) {} else {
264264
// comment
265-
} else {}
265+
}
266266
}
267267
}
268268
269269
contract Comments8 {
270270
function someFunction() {
271-
if (something) {
271+
if (something) {} else {
272272
/* comment
273273
* comment */
274-
} else {}
274+
}
275275
}
276276
}
277277

0 commit comments

Comments
 (0)