Skip to content

Commit

Permalink
using optional chaining since this has been implemented by all our ta…
Browse files Browse the repository at this point in the history
…rgets (#913)
  • Loading branch information
Janther authored Dec 15, 2023
1 parent ba16ec5 commit 18fb196
Show file tree
Hide file tree
Showing 19 changed files with 23 additions and 33 deletions.
7 changes: 2 additions & 5 deletions src/comments/handlers/handleContractDefinitionComments.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,10 @@ function handleContractDefinitionComments({
);

// The comment is behind the start of the Block `{}` or behind a base contract
if (
(followingNode && followingNode.type === 'InheritanceSpecifier') ||
nextCharacter === '{'
) {
if (followingNode?.type === 'InheritanceSpecifier' || nextCharacter === '{') {
// In this scenario the comment belongs to a base contract.
// contract A is B, /* comment for B */ C /* comment for C */ {}
if (precedingNode && precedingNode.type === 'InheritanceSpecifier') {
if (precedingNode?.type === 'InheritanceSpecifier') {
addTrailingComment(precedingNode, comment);
return true;
}
Expand Down
4 changes: 1 addition & 3 deletions src/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ export function printString(rawContent, options) {

export function hasNodeIgnoreComment(node) {
return (
node &&
node.comments &&
node.comments.length > 0 &&
node?.comments?.length > 0 &&
node.comments.some((comment) => comment.value.trim() === 'prettier-ignore')
);
}
2 changes: 1 addition & 1 deletion src/loc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function getRange(index, node) {
if (node.range) {
return node.range[index];
}
if (node.expression && node.expression.range) {
if (node.expression?.range) {
return node.expression.range[index];
}
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/ContractDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const inheritance = (node, path, print) =>

const body = (node, path, options, print) => {
const comments = printComments(node, path, options);
return node.subNodes.length > 0 || (comments && comments.length)
return node.subNodes.length > 0 || comments?.length
? printSeparatedItem(
[printPreservingEmptyLines(path, 'subNodes', options, print), comments],
{ firstSeparator: hardline, grouped: false }
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/CustomErrorDefinition.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { printSeparatedList } from '../common/printer-helpers.js';

const parameters = (node, path, print) =>
node.parameters && node.parameters.length > 0
node.parameters?.length > 0
? printSeparatedList(path.map(print, 'parameters'))
: '';

Expand Down
4 changes: 1 addition & 3 deletions src/nodes/ElementaryTypeName.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const stateMutability = (node) =>
node.stateMutability && node.stateMutability.length > 0
? [' ', node.stateMutability]
: '';
node.stateMutability?.length > 0 ? [' ', node.stateMutability] : '';

export const ElementaryTypeName = {
print: ({ node }) => [node.name, stateMutability(node)]
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/EventDefinition.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { printSeparatedList } from '../common/printer-helpers.js';

const parameters = (node, path, print) =>
node.parameters && node.parameters.length > 0
node.parameters?.length > 0
? printSeparatedList(path.map(print, 'parameters'))
: '';

Expand Down
4 changes: 2 additions & 2 deletions src/nodes/ExpressionStatement.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export const ExpressionStatement = {
const parent = path.getParentNode();

if (parent.type === 'IfStatement') {
if (node.comments && node.comments.length) {
if (node.comments?.length) {
const comments = printComments(node, path, options);
if (comments && comments.length) {
if (comments?.length) {
parts.push(comments);
parts.push(hardline);
}
Expand Down
4 changes: 2 additions & 2 deletions src/nodes/FunctionCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export const FunctionCall = {
let expressionDoc = path.call(print, 'expression');
let argumentsDoc = ')';

if (node.arguments && node.arguments.length > 0) {
if (node.identifiers && node.identifiers.length > 0) {
if (node.arguments?.length > 0) {
if (node.identifiers?.length > 0) {
argumentsDoc = printObject(path, print, options);
} else {
argumentsDoc = printArguments(path, print);
Expand Down
4 changes: 2 additions & 2 deletions src/nodes/FunctionDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ const functionName = (node, options) => {
};

const parameters = (parametersType, node, path, print, options) => {
if (node[parametersType] && node[parametersType].length > 0) {
if (node[parametersType]?.length > 0) {
return printSeparatedList(path.map(print, parametersType), {
grouped: false
});
}
if (node.comments && node.comments.length > 0) {
if (node.comments?.length > 0) {
// we add a check to see if the comment is inside the parentheses
const parameterComments = printComments(
node,
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/InheritanceSpecifier.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { printSeparatedList } from '../common/printer-helpers.js';

const printArguments = (node, path, print) =>
node.arguments && node.arguments.length
node.arguments?.length
? ['(', printSeparatedList(path.map(print, 'arguments')), ')']
: '';

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/InlineAssemblyStatement.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const InlineAssemblyStatement = {
print: ({ node, path, print, options }) => [
'assembly ',
node.language ? `${printString(node.language, options)} ` : '',
node.flags && node.flags.length > 0
node.flags?.length > 0
? [
'(',
printSeparatedList(
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/ModifierDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { printSeparatedList } from '../common/printer-helpers.js';
const { group, hardline, indent, line } = doc.builders;

const modifierParameters = (node, path, print) => {
if (node.parameters && node.parameters.length > 0) {
if (node.parameters?.length > 0) {
return [
'(',
printSeparatedList(path.map(print, 'parameters'), {
Expand Down
3 changes: 1 addition & 2 deletions src/nodes/ModifierInvocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ const modifierArguments = (node, path, print, options) => {
}

if (
node.comments &&
node.comments.some(
node.comments?.some(
(comment) => !comment.leading && !comment.trailing && !comment.printed
)
) {
Expand Down
4 changes: 1 addition & 3 deletions src/nodes/TupleExpression.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import { printSeparatedList } from '../common/printer-helpers.js';
const { group } = doc.builders;

const contents = (node, path, print) =>
node.components &&
node.components.length === 1 &&
node.components[0].type === 'BinaryOperation'
node.components?.length === 1 && node.components[0].type === 'BinaryOperation'
? path.map(print, 'components')
: [printSeparatedList(path.map(print, 'components'))];

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/UsingForDeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { line, softline } = doc.builders;
export const UsingForDeclaration = {
print: ({ node, path, print, options }) => [
'using ',
node.functions && node.functions.length
node.functions?.length
? [
'{',
printSeparatedList(
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/VariableDeclarationStatement.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let groupIndex = 0;
export const VariableDeclarationStatement = {
print: ({ node, path, print }) => {
const startsWithVar =
node.variables.filter((x) => x && x.typeName).length === 0;
node.variables.filter((x) => x?.typeName).length === 0;

const declarationDoc = group(
[
Expand Down
2 changes: 1 addition & 1 deletion src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function parse(text, _parsers, options = _parsers) {
FunctionDefinition(ctx) {
if (!ctx.isConstructor) {
ctx.modifiers.forEach((modifier) => {
if (modifier.arguments && modifier.arguments.length === 0) {
if (modifier.arguments?.length === 0) {
// eslint-disable-next-line no-param-reassign
modifier.arguments = null;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/config/utils/compile-contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async function compileContract(filename, content) {
const output = JSON.parse(solc.compile(JSON.stringify(input)));

// We throw if the contract doesn't compile.
if (output.errors && output.errors.length > 0) {
if (output.errors?.length > 0) {
throw output.errors[0].formattedMessage;
}

Expand Down

0 comments on commit 18fb196

Please sign in to comment.