Skip to content

Commit 18fb196

Browse files
authored
using optional chaining since this has been implemented by all our targets (#913)
1 parent ba16ec5 commit 18fb196

19 files changed

Lines changed: 23 additions & 33 deletions

src/comments/handlers/handleContractDefinitionComments.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,10 @@ function handleContractDefinitionComments({
2727
);
2828

2929
// The comment is behind the start of the Block `{}` or behind a base contract
30-
if (
31-
(followingNode && followingNode.type === 'InheritanceSpecifier') ||
32-
nextCharacter === '{'
33-
) {
30+
if (followingNode?.type === 'InheritanceSpecifier' || nextCharacter === '{') {
3431
// In this scenario the comment belongs to a base contract.
3532
// contract A is B, /* comment for B */ C /* comment for C */ {}
36-
if (precedingNode && precedingNode.type === 'InheritanceSpecifier') {
33+
if (precedingNode?.type === 'InheritanceSpecifier') {
3734
addTrailingComment(precedingNode, comment);
3835
return true;
3936
}

src/common/util.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ export function printString(rawContent, options) {
4040

4141
export function hasNodeIgnoreComment(node) {
4242
return (
43-
node &&
44-
node.comments &&
45-
node.comments.length > 0 &&
43+
node?.comments?.length > 0 &&
4644
node.comments.some((comment) => comment.value.trim() === 'prettier-ignore')
4745
);
4846
}

src/loc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function getRange(index, node) {
44
if (node.range) {
55
return node.range[index];
66
}
7-
if (node.expression && node.expression.range) {
7+
if (node.expression?.range) {
88
return node.expression.range[index];
99
}
1010
return null;

src/nodes/ContractDefinition.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const inheritance = (node, path, print) =>
2020

2121
const body = (node, path, options, print) => {
2222
const comments = printComments(node, path, options);
23-
return node.subNodes.length > 0 || (comments && comments.length)
23+
return node.subNodes.length > 0 || comments?.length
2424
? printSeparatedItem(
2525
[printPreservingEmptyLines(path, 'subNodes', options, print), comments],
2626
{ firstSeparator: hardline, grouped: false }

src/nodes/CustomErrorDefinition.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { printSeparatedList } from '../common/printer-helpers.js';
22

33
const parameters = (node, path, print) =>
4-
node.parameters && node.parameters.length > 0
4+
node.parameters?.length > 0
55
? printSeparatedList(path.map(print, 'parameters'))
66
: '';
77

src/nodes/ElementaryTypeName.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
const stateMutability = (node) =>
2-
node.stateMutability && node.stateMutability.length > 0
3-
? [' ', node.stateMutability]
4-
: '';
2+
node.stateMutability?.length > 0 ? [' ', node.stateMutability] : '';
53

64
export const ElementaryTypeName = {
75
print: ({ node }) => [node.name, stateMutability(node)]

src/nodes/EventDefinition.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { printSeparatedList } from '../common/printer-helpers.js';
22

33
const parameters = (node, path, print) =>
4-
node.parameters && node.parameters.length > 0
4+
node.parameters?.length > 0
55
? printSeparatedList(path.map(print, 'parameters'))
66
: '';
77

src/nodes/ExpressionStatement.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ export const ExpressionStatement = {
1010
const parent = path.getParentNode();
1111

1212
if (parent.type === 'IfStatement') {
13-
if (node.comments && node.comments.length) {
13+
if (node.comments?.length) {
1414
const comments = printComments(node, path, options);
15-
if (comments && comments.length) {
15+
if (comments?.length) {
1616
parts.push(comments);
1717
parts.push(hardline);
1818
}

src/nodes/FunctionCall.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export const FunctionCall = {
3030
let expressionDoc = path.call(print, 'expression');
3131
let argumentsDoc = ')';
3232

33-
if (node.arguments && node.arguments.length > 0) {
34-
if (node.identifiers && node.identifiers.length > 0) {
33+
if (node.arguments?.length > 0) {
34+
if (node.identifiers?.length > 0) {
3535
argumentsDoc = printObject(path, print, options);
3636
} else {
3737
argumentsDoc = printArguments(path, print);

src/nodes/FunctionDefinition.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ const functionName = (node, options) => {
2626
};
2727

2828
const parameters = (parametersType, node, path, print, options) => {
29-
if (node[parametersType] && node[parametersType].length > 0) {
29+
if (node[parametersType]?.length > 0) {
3030
return printSeparatedList(path.map(print, parametersType), {
3131
grouped: false
3232
});
3333
}
34-
if (node.comments && node.comments.length > 0) {
34+
if (node.comments?.length > 0) {
3535
// we add a check to see if the comment is inside the parentheses
3636
const parameterComments = printComments(
3737
node,

0 commit comments

Comments
 (0)