Skip to content

Commit 22c90f9

Browse files
fiskerczosel
andauthored
Use AstPath getters (#2214)
* Update comments * Update needs-parens.js * Remove redundant check * Update print logic * util.js * Another place * fix: comment --------- Co-authored-by: Christian Zosel <[email protected]>
1 parent eb900b0 commit 22c90f9

File tree

5 files changed

+134
-168
lines changed

5 files changed

+134
-168
lines changed

src/comments.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -822,11 +822,11 @@ function handleWhileComments(
822822
return false;
823823
}
824824

825-
// https://github.com/prettier/prettier/blob/master/src/main/comments.js#L335
826-
function printComment(commentPath, options) {
827-
const comment = commentPath.getValue();
825+
// https://github.com/prettier/prettier/blob/c01661f311a2e1e033f1f9cb127882cc13e293bd/src/main/comments/print.js#L23
826+
function printComment(path, options) {
827+
const comment = path.node;
828828
comment.printed = true;
829-
return options.printer.printComment(commentPath, options);
829+
return options.printer.printComment(path, options);
830830
}
831831

832832
// https://github.com/prettier/prettier/blob/master/src/main/comments.js#L440
@@ -838,15 +838,15 @@ function printDanglingComments(path, options, sameIndent, filter) {
838838
return "";
839839
}
840840

841-
path.each((commentPath) => {
842-
const comment = commentPath.getValue();
841+
path.each(() => {
842+
const comment = path.node;
843843
if (
844844
comment &&
845845
!comment.leading &&
846846
!comment.trailing &&
847847
(!filter || filter(comment))
848848
) {
849-
parts.push(printComment(commentPath, options));
849+
parts.push(printComment(path, options));
850850
}
851851
}, "comments");
852852

@@ -917,21 +917,22 @@ function canAttachComment(node) {
917917
// Based on https://github.com/prettier/prettier/blob/master/src/main/comments.js
918918
// TODO remove after https://github.com/prettier/prettier/issues/5087
919919
function prependCursorPlaceholder(path, options, printed) {
920-
if (path.getNode() === options.cursorNode && path.getValue()) {
920+
const { node } = path;
921+
if (node && node === options.cursorNode) {
921922
return [cursor, printed, cursor];
922923
}
923924

924925
return printed;
925926
}
926927

927-
function printLeadingComment(commentPath, print, options) {
928-
const comment = commentPath.getValue();
929-
const contents = printComment(commentPath, options);
928+
function printLeadingComment(path, print, options) {
929+
const contents = printComment(path, options);
930930

931931
if (!contents) {
932932
return "";
933933
}
934934

935+
const comment = path.node;
935936
const isBlock =
936937
options.printer.isBlockComment && options.printer.isBlockComment(comment);
937938

@@ -949,12 +950,13 @@ function printLeadingComment(commentPath, print, options) {
949950
return [contents, hardline];
950951
}
951952

952-
function printTrailingComment(commentPath, print, options) {
953-
const comment = commentPath.getValue();
954-
const contents = printComment(commentPath, options);
953+
function printTrailingComment(path, print, options) {
954+
const contents = printComment(path, options);
955955
if (!contents) {
956956
return "";
957957
}
958+
959+
const comment = path.node;
958960
const isBlock =
959961
options.printer.isBlockComment && options.printer.isBlockComment(comment);
960962

@@ -991,9 +993,9 @@ function printTrailingComment(commentPath, print, options) {
991993
}
992994

993995
function printAllComments(path, print, options, needsSemi) {
994-
const value = path.getValue();
996+
const { node } = path;
995997
const printed = print(path);
996-
const comments = value && value.comments;
998+
const comments = node && node.comments;
997999

9981000
if (!comments || comments.length === 0) {
9991001
return prependCursorPlaceholder(path, options, printed);
@@ -1002,12 +1004,11 @@ function printAllComments(path, print, options, needsSemi) {
10021004
const leadingParts = [];
10031005
const trailingParts = [needsSemi ? ";" : "", printed];
10041006

1005-
path.each((commentPath) => {
1006-
const comment = commentPath.getValue();
1007+
path.each(({ node: comment }) => {
10071008
const { leading, trailing } = comment;
10081009

10091010
if (leading) {
1010-
const contents = printLeadingComment(commentPath, print, options);
1011+
const contents = printLeadingComment(path, print, options);
10111012
if (!contents) {
10121013
return;
10131014
}
@@ -1018,7 +1019,7 @@ function printAllComments(path, print, options, needsSemi) {
10181019
leadingParts.push(hardline);
10191020
}
10201021
} else if (trailing) {
1021-
trailingParts.push(printTrailingComment(commentPath, print, options));
1022+
trailingParts.push(printTrailingComment(path, print, options));
10221023
}
10231024
}, "comments");
10241025

src/index.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ const printers = {
101101
remaining: handleRemainingComment,
102102
},
103103
willPrintOwnComments(path) {
104-
const node = path.getValue();
104+
const { node } = path;
105105

106106
return node && node.kind === "noop";
107107
},
108-
printComment(commentPath) {
109-
const comment = commentPath.getValue();
108+
printComment(path) {
109+
const comment = path.node;
110110

111111
switch (comment.kind) {
112112
case "commentblock": {
@@ -149,8 +149,7 @@ const printers = {
149149
!comment.value.includes("prettier-ignore-start") &&
150150
!comment.value.includes("prettier-ignore-end");
151151

152-
const parentNode = path.getParentNode();
153-
const node = path.getNode();
152+
const { node, parent: parentNode } = path;
154153

155154
return (
156155
(node &&

src/needs-parens.js

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ import assert from "assert";
22
import { getPrecedence, shouldFlatten, isBitwiseOperator } from "./util.js";
33

44
function needsParens(path) {
5-
const parent = path.getParentNode();
5+
const { parent } = path;
66

77
if (!parent) {
88
return false;
99
}
1010

11-
const name = path.getName();
12-
const node = path.getNode();
11+
const { key, node } = path;
1312

1413
if (
1514
[
@@ -53,9 +52,9 @@ function needsParens(path) {
5352
case "staticlookup":
5453
case "offsetlookup":
5554
case "call":
56-
return name === "what" && parent.what === node;
55+
return key === "what";
5756
case "bin":
58-
return parent.type === "**" && name === "left";
57+
return parent.type === "**" && key === "left";
5958
default:
6059
return false;
6160
}
@@ -77,7 +76,7 @@ function needsParens(path) {
7776
case "nullsafepropertylookup":
7877
case "staticlookup":
7978
case "offsetlookup":
80-
return name === "what" && parent.what === node;
79+
return key === "what";
8180
case "bin": {
8281
const po = parent.type;
8382
const pp = getPrecedence(po);
@@ -92,7 +91,7 @@ function needsParens(path) {
9291
return true;
9392
}
9493

95-
if (pp === np && name === "right") {
94+
if (pp === np && key === "right") {
9695
assert.strictEqual(parent.right, node);
9796

9897
return true;
@@ -124,11 +123,7 @@ function needsParens(path) {
124123
case "staticlookup": {
125124
switch (parent.kind) {
126125
case "call":
127-
return (
128-
name === "what" &&
129-
parent.what === node &&
130-
node.parenthesizedExpression
131-
);
126+
return key === "what" && node.parenthesizedExpression;
132127

133128
default:
134129
return false;
@@ -142,7 +137,7 @@ function needsParens(path) {
142137
case "staticlookup":
143138
case "offsetlookup":
144139
case "call":
145-
return name === "what" && parent.what === node;
140+
return key === "what";
146141
default:
147142
return false;
148143
}
@@ -154,10 +149,10 @@ function needsParens(path) {
154149
case "staticlookup":
155150
case "offsetlookup":
156151
case "call":
157-
return name === "what" && parent.what === node;
152+
return key === "what";
158153

159154
case "retif":
160-
return parent.test === node;
155+
return key === "test";
161156

162157
default:
163158
return !!(node.key || node.value);
@@ -192,7 +187,7 @@ function needsParens(path) {
192187
case "unary":
193188
case "bin":
194189
case "retif":
195-
if (name === "test" && !parent.trueExpr) {
190+
if (key === "test" && !parent.trueExpr) {
196191
return false;
197192
}
198193

@@ -202,15 +197,15 @@ function needsParens(path) {
202197
case "staticlookup":
203198
case "offsetlookup":
204199
case "call":
205-
return name === "what" && parent.what === node;
200+
return key === "what";
206201

207202
default:
208203
return false;
209204
}
210205
case "closure":
211206
switch (parent.kind) {
212207
case "call":
213-
return name === "what" && parent.what === node;
208+
return key === "what";
214209

215210
// https://github.com/prettier/plugin-php/issues/1675
216211
case "propertylookup":
@@ -240,7 +235,7 @@ function needsParens(path) {
240235
return false;
241236
}
242237

243-
return name === "what" && parent.what === node;
238+
return key === "what";
244239
default:
245240
return false;
246241
}

0 commit comments

Comments
 (0)