Skip to content

Commit b7b1edd

Browse files
committed
use the node and grandparent getters instead of getNode() and getNode(2)
1 parent f9e083b commit b7b1edd

9 files changed

Lines changed: 13 additions & 15 deletions

File tree

src/slang-comments/printer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { AstPath, Doc } from 'prettier';
44
import type { AstNode } from '../slang-nodes/types.d.ts';
55

66
export function printComment(commentPath: AstPath<AstNode>): Doc {
7-
const comment = commentPath.getNode()!;
7+
const comment = commentPath.node;
88

99
if (isComment(comment)) {
1010
return comment.print();

src/slang-nodes/ConditionalExpression.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function experimentalTernaries(
2424
print: PrintFunction,
2525
options: ParserOptions<AstNode>
2626
): Doc {
27-
const grandparent = path.getNode(2) as StrictAstNode;
27+
const grandparent = path.grandparent as StrictAstNode;
2828
const isNested = grandparent.kind === NonterminalKind.ConditionalExpression;
2929
const isNestedAsTrueExpression =
3030
isNested && grandparent.trueExpression.variant === node;
@@ -87,7 +87,7 @@ function traditionalTernaries(
8787
indent([
8888
// Nested trueExpression and falseExpression are always printed in a new
8989
// line
90-
(path.getNode(2) as StrictAstNode).kind ===
90+
(path.grandparent as StrictAstNode).kind ===
9191
NonterminalKind.ConditionalExpression
9292
? hardline
9393
: line,

src/slang-printers/create-binary-operation-printer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function rightOperandPrint(
2626
// If it's a single binary operation, avoid having a small right
2727
// operand like - 1 on its own line
2828
const leftOperand = node.leftOperand.variant;
29-
const grandparentNode = path.getNode(2) as StrictAstNode;
29+
const grandparentNode = path.grandparent as StrictAstNode;
3030
const shouldGroup =
3131
!(
3232
leftOperand.kind !== TerminalKind.Identifier &&

src/slang-printers/print-binary-operation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const binaryGroupRulesBuilder =
1818
(shouldGroup: (node: BinaryOperation) => boolean) =>
1919
(path: AstPath<BinaryOperation>) =>
2020
(document: Doc): Doc => {
21-
const grandparentNode = path.getNode(2) as StrictAstNode;
21+
const grandparentNode = path.grandparent as StrictAstNode;
2222
if (!isBinaryOperation(grandparentNode)) return group(document);
2323
if (shouldGroup(grandparentNode)) return group(document);
2424
return document;
@@ -44,7 +44,7 @@ export const binaryIndentRulesBuilder =
4444
(shouldIndent: (node: BinaryOperation) => boolean) =>
4545
(path: AstPath<BinaryOperation>) =>
4646
(document: Doc): Doc => {
47-
let node = path.getNode() as StrictAstNode;
47+
let node = path.node;
4848
for (let i = 2; ; i += 2) {
4949
const grandparentNode = path.getNode(i) as StrictAstNode;
5050
if (shouldNotIndent(grandparentNode, path, i)) break;

src/slang-printers/print-comments.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export function printComments(path: AstPath<AstNode>): Doc[] {
1212
const document = joinExisting(
1313
line,
1414
path.map((commentPath) => {
15-
const comment = commentPath.getNode()!;
15+
const comment = commentPath.node;
1616
if (comment.trailing || comment.leading || comment.printed) {
1717
return '';
1818
}

src/slang-printers/print-logical-operation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const logicalGroupRulesBuilder = binaryGroupRulesBuilder(() => false);
2121
const logicalIndentRulesBuilder =
2222
(path: AstPath<BinaryOperation>, options: ParserOptions<AstNode>) =>
2323
(document: Doc): Doc => {
24-
let node = path.getNode() as StrictAstNode;
24+
let node = path.node;
2525
for (let i = 2; ; i += 2) {
2626
const grandparentNode = path.getNode(i) as StrictAstNode;
2727
if (shouldNotIndent(grandparentNode, path, i)) break;

src/slang-printers/print-preserving-empty-lines.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function printPreservingEmptyLines(
1818
options: ParserOptions<AstNode>
1919
): Doc {
2020
return path.map((childPath) => {
21-
const node = childPath.getNode() as StrictAstNode;
21+
const node = childPath.node as StrictAstNode;
2222

2323
return [
2424
// Only attempt to prepend an empty line if `node` is not the first item

src/slangPrinter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function hasNodeIgnoreComment(node: StrictAstNode): boolean {
2020
}
2121

2222
function ignoreComments(path: AstPath<AstNode>): void {
23-
const node = path.getNode();
23+
const node = path.node;
2424
// We ignore anything that is not an object
2525
if (node === null || typeof node !== 'object') return;
2626

@@ -36,7 +36,7 @@ function ignoreComments(path: AstPath<AstNode>): void {
3636
// The key `comments` will contain every comment for this node
3737
case 'comments':
3838
path.each((commentPath) => {
39-
const comment = commentPath.getNode()!;
39+
const comment = commentPath.node;
4040
comment.printed = true;
4141
}, 'comments');
4242
break;
@@ -61,7 +61,7 @@ function genericPrint(
6161
options: ParserOptions<AstNode>,
6262
print: PrintFunction
6363
): Doc {
64-
const node = path.getNode();
64+
const node = path.node;
6565

6666
if (node === null) {
6767
return '';

tests/unit/comments/printer.test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { printComment } from '../../../src/slang-comments/printer.ts';
22

33
test('given an unknown comment type then printComment function should throw', () => {
4-
const mockCommentPath = {
5-
getNode: () => ({ type: 'UnknownComment', range: [0, 1] })
6-
};
4+
const mockCommentPath = { node: { type: 'UnknownComment', range: [0, 1] } };
75

86
expect(() => {
97
printComment(mockCommentPath);

0 commit comments

Comments
 (0)