Skip to content

Commit

Permalink
Use AstPath getters (#2214)
Browse files Browse the repository at this point in the history
* Update comments

* Update needs-parens.js

* Remove redundant check

* Update print logic

* util.js

* Another place

* fix: comment

---------

Co-authored-by: Christian Zosel <christian@zosel.ch>
  • Loading branch information
fisker and czosel authored Aug 18, 2023
1 parent eb900b0 commit 22c90f9
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 168 deletions.
41 changes: 21 additions & 20 deletions src/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -822,11 +822,11 @@ function handleWhileComments(
return false;
}

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

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

path.each((commentPath) => {
const comment = commentPath.getValue();
path.each(() => {
const comment = path.node;
if (
comment &&
!comment.leading &&
!comment.trailing &&
(!filter || filter(comment))
) {
parts.push(printComment(commentPath, options));
parts.push(printComment(path, options));
}
}, "comments");

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

return printed;
}

function printLeadingComment(commentPath, print, options) {
const comment = commentPath.getValue();
const contents = printComment(commentPath, options);
function printLeadingComment(path, print, options) {
const contents = printComment(path, options);

if (!contents) {
return "";
}

const comment = path.node;
const isBlock =
options.printer.isBlockComment && options.printer.isBlockComment(comment);

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

function printTrailingComment(commentPath, print, options) {
const comment = commentPath.getValue();
const contents = printComment(commentPath, options);
function printTrailingComment(path, print, options) {
const contents = printComment(path, options);
if (!contents) {
return "";
}

const comment = path.node;
const isBlock =
options.printer.isBlockComment && options.printer.isBlockComment(comment);

Expand Down Expand Up @@ -991,9 +993,9 @@ function printTrailingComment(commentPath, print, options) {
}

function printAllComments(path, print, options, needsSemi) {
const value = path.getValue();
const { node } = path;
const printed = print(path);
const comments = value && value.comments;
const comments = node && node.comments;

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

path.each((commentPath) => {
const comment = commentPath.getValue();
path.each(({ node: comment }) => {
const { leading, trailing } = comment;

if (leading) {
const contents = printLeadingComment(commentPath, print, options);
const contents = printLeadingComment(path, print, options);
if (!contents) {
return;
}
Expand All @@ -1018,7 +1019,7 @@ function printAllComments(path, print, options, needsSemi) {
leadingParts.push(hardline);
}
} else if (trailing) {
trailingParts.push(printTrailingComment(commentPath, print, options));
trailingParts.push(printTrailingComment(path, print, options));
}
}, "comments");

Expand Down
9 changes: 4 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ const printers = {
remaining: handleRemainingComment,
},
willPrintOwnComments(path) {
const node = path.getValue();
const { node } = path;

return node && node.kind === "noop";
},
printComment(commentPath) {
const comment = commentPath.getValue();
printComment(path) {
const comment = path.node;

switch (comment.kind) {
case "commentblock": {
Expand Down Expand Up @@ -149,8 +149,7 @@ const printers = {
!comment.value.includes("prettier-ignore-start") &&
!comment.value.includes("prettier-ignore-end");

const parentNode = path.getParentNode();
const node = path.getNode();
const { node, parent: parentNode } = path;

return (
(node &&
Expand Down
33 changes: 14 additions & 19 deletions src/needs-parens.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import assert from "assert";
import { getPrecedence, shouldFlatten, isBitwiseOperator } from "./util.js";

function needsParens(path) {
const parent = path.getParentNode();
const { parent } = path;

if (!parent) {
return false;
}

const name = path.getName();
const node = path.getNode();
const { key, node } = path;

if (
[
Expand Down Expand Up @@ -53,9 +52,9 @@ function needsParens(path) {
case "staticlookup":
case "offsetlookup":
case "call":
return name === "what" && parent.what === node;
return key === "what";
case "bin":
return parent.type === "**" && name === "left";
return parent.type === "**" && key === "left";
default:
return false;
}
Expand All @@ -77,7 +76,7 @@ function needsParens(path) {
case "nullsafepropertylookup":
case "staticlookup":
case "offsetlookup":
return name === "what" && parent.what === node;
return key === "what";
case "bin": {
const po = parent.type;
const pp = getPrecedence(po);
Expand All @@ -92,7 +91,7 @@ function needsParens(path) {
return true;
}

if (pp === np && name === "right") {
if (pp === np && key === "right") {
assert.strictEqual(parent.right, node);

return true;
Expand Down Expand Up @@ -124,11 +123,7 @@ function needsParens(path) {
case "staticlookup": {
switch (parent.kind) {
case "call":
return (
name === "what" &&
parent.what === node &&
node.parenthesizedExpression
);
return key === "what" && node.parenthesizedExpression;

default:
return false;
Expand All @@ -142,7 +137,7 @@ function needsParens(path) {
case "staticlookup":
case "offsetlookup":
case "call":
return name === "what" && parent.what === node;
return key === "what";
default:
return false;
}
Expand All @@ -154,10 +149,10 @@ function needsParens(path) {
case "staticlookup":
case "offsetlookup":
case "call":
return name === "what" && parent.what === node;
return key === "what";

case "retif":
return parent.test === node;
return key === "test";

default:
return !!(node.key || node.value);
Expand Down Expand Up @@ -192,7 +187,7 @@ function needsParens(path) {
case "unary":
case "bin":
case "retif":
if (name === "test" && !parent.trueExpr) {
if (key === "test" && !parent.trueExpr) {
return false;
}

Expand All @@ -202,15 +197,15 @@ function needsParens(path) {
case "staticlookup":
case "offsetlookup":
case "call":
return name === "what" && parent.what === node;
return key === "what";

default:
return false;
}
case "closure":
switch (parent.kind) {
case "call":
return name === "what" && parent.what === node;
return key === "what";

// https://github.com/prettier/plugin-php/issues/1675
case "propertylookup":
Expand Down Expand Up @@ -240,7 +235,7 @@ function needsParens(path) {
return false;
}

return name === "what" && parent.what === node;
return key === "what";
default:
return false;
}
Expand Down
Loading

0 comments on commit 22c90f9

Please sign in to comment.