Skip to content

Commit aa33e47

Browse files
authored
We use Prettier's FastPath strategy to avoid modifying parameters. (#433)
1 parent 555f5c3 commit aa33e47

4 files changed

Lines changed: 51 additions & 24 deletions

File tree

src/comments/ignore.js

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
1-
function ignoreComments(node) {
2-
if (node === undefined || node === null) return;
1+
function ignoreComments(path) {
2+
const node = path.getValue();
3+
// We ignore anything that is not an object
4+
if (node === null || typeof node !== 'object') return;
5+
36
const keys = Object.keys(node);
47
keys.forEach((key) => {
5-
if (key === 'comments') {
6-
node.comments.forEach((comment) => {
7-
// eslint-disable-next-line no-param-reassign
8-
comment.printed = true;
9-
});
10-
return;
11-
}
12-
if (typeof node[key] === 'object') {
13-
ignoreComments(node[key]);
8+
switch (key) {
9+
// We ignore `loc` and `range` since these are added by the parser
10+
case 'loc':
11+
case 'range':
12+
break;
13+
// The key `comments` will contain every comment for this node
14+
case 'comments':
15+
path.each((commentPath) => {
16+
const comment = commentPath.getValue();
17+
comment.printed = true;
18+
}, 'comments');
19+
break;
20+
default:
21+
// If the value for that key is an Array or an Object we go deeper.
22+
if (typeof node[key] === 'object') {
23+
if (Array.isArray(node[key])) {
24+
path.each(ignoreComments, key);
25+
return;
26+
}
27+
path.call(ignoreComments, key);
28+
}
1429
}
1530
});
1631
}

src/printer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function genericPrint(path, options, print) {
1313
}
1414

1515
if (hasNodeIgnoreComment(node)) {
16-
ignoreComments(node);
16+
ignoreComments(path);
1717

1818
return options.originalText.slice(
1919
options.locStart(node),

tests/PrettierIgnore/PrettierIgnore.sol

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,19 @@ contract PrettierIgnore {
2121
contract Example {
2222
// Test comment
2323
function() public payable {
24-
// this should be marked as printed
24+
// This should be marked as printed
2525
// Everything inside is also ignored
2626
matrix(
2727
1, 0, 0,
2828
0, 1, 0,
2929
0, 0, 1
3030
);
31-
if (true) {
32-
// comments of children should be marked as printed
33-
}
31+
// Comments for members of an Array are marked as printed
32+
if (true)
33+
// Comments of single child Objects are marked as printed
34+
return;
35+
else
36+
// Comments of children should be marked as printed
37+
return;
3438
}
3539
}

tests/PrettierIgnore/__snapshots__/jsfmt.spec.js.snap

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,20 @@ contract PrettierIgnore {
2424
contract Example {
2525
// Test comment
2626
function() public payable {
27-
// this should be marked as printed
27+
// This should be marked as printed
2828
// Everything inside is also ignored
2929
matrix(
3030
1, 0, 0,
3131
0, 1, 0,
3232
0, 0, 1
3333
);
34-
if (true) {
35-
// comments of children should be marked as printed
36-
}
34+
// Comments for members of an Array are marked as printed
35+
if (true)
36+
// Comments of single child Objects are marked as printed
37+
return;
38+
else
39+
// Comments of children should be marked as printed
40+
return;
3741
}
3842
}
3943
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -56,16 +60,20 @@ contract PrettierIgnore {
5660
contract Example {
5761
// Test comment
5862
function() public payable {
59-
// this should be marked as printed
63+
// This should be marked as printed
6064
// Everything inside is also ignored
6165
matrix(
6266
1, 0, 0,
6367
0, 1, 0,
6468
0, 0, 1
6569
);
66-
if (true) {
67-
// comments of children should be marked as printed
68-
}
70+
// Comments for members of an Array are marked as printed
71+
if (true)
72+
// Comments of single child Objects are marked as printed
73+
return;
74+
else
75+
// Comments of children should be marked as printed
76+
return;
6977
}
7078
}
7179

0 commit comments

Comments
 (0)