Skip to content

Commit

Permalink
Necessary tweaks to the current formatting (#1033)
Browse files Browse the repository at this point in the history
  • Loading branch information
Janther authored Aug 16, 2024
1 parent 0023b9d commit 2b27e68
Show file tree
Hide file tree
Showing 84 changed files with 1,235 additions and 1,006 deletions.
13 changes: 7 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prettier-plugin-solidity",
"version": "1.3.1",
"version": "1.4.0",
"description": "A Prettier Plugin for automatically formatting your Solidity code.",
"type": "module",
"main": "./src/index.js",
Expand Down Expand Up @@ -104,7 +104,7 @@
"lines-and-columns": "^2.0.3",
"prettier": "^3.1.1",
"proxyquire": "^2.1.3",
"solc": "^0.8.25",
"solc": "^0.8.26",
"webpack": "^5.88.2",
"webpack-cli": "^5.1.4"
},
Expand Down
26 changes: 15 additions & 11 deletions src/nodes/AssemblyBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ import {
const { hardline } = doc.builders;

export const AssemblyBlock = {
print: ({ node, options, path, print }) => [
'{',
printSeparatedItem(
[
printPreservingEmptyLines(path, 'operations', options, print),
printComments(node, path, options)
],
{ firstSeparator: hardline, grouped: false }
),
'}'
]
print: ({ node, options, path, print }) =>
// if block is empty, just return the pair of braces
node.operations.length === 0 && !node.comments
? '{}'
: [
'{',
printSeparatedItem(
[
printPreservingEmptyLines(path, 'operations', options, print),
printComments(node, path, options)
],
{ firstSeparator: hardline, grouped: false }
),
'}'
]
};
7 changes: 6 additions & 1 deletion src/nodes/AssemblyStackAssignment.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { doc } from 'prettier';

const { hardline } = doc.builders;

export const AssemblyStackAssignment = {
print: ({ node, path, print }) => [
path.call(print, 'expression'),
' =: ',
hardline,
'=: ',
node.name
]
};
5 changes: 1 addition & 4 deletions src/nodes/Conditional.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { printSeparatedItem } from '../common/printer-helpers.js';

const { group, hardline, ifBreak, indent, line, softline } = doc.builders;

let groupIndex = 0;
const experimentalTernaries = (node, path, print, options) => {
const parent = path.getParentNode();
const isNested = parent.type === 'Conditional';
Expand All @@ -30,11 +29,9 @@ const experimentalTernaries = (node, path, print, options) => {

const conditionAndTrueExpressionGroup = group(
[conditionDoc, trueExpressionDoc],
{ id: `Conditional.trueExpressionDoc-${groupIndex}` }
{ id: Symbol('Conditional.trueExpression') }
);

groupIndex += 1;

// For the odd case of `tabWidth` of 1 or 0 we initiate `fillTab` as a single
// space.
let fillTab = ' ';
Expand Down
29 changes: 4 additions & 25 deletions src/nodes/ExpressionStatement.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,6 @@
import { doc } from 'prettier';
import { printComments } from '../common/printer-helpers.js';

const { hardline } = doc.builders;

export const ExpressionStatement = {
print: ({ node, options, path, print }) => {
const parts = [];

const parent = path.getParentNode();

if (parent.type === 'IfStatement') {
if (node.comments?.length) {
const comments = printComments(node, path, options);
if (comments?.length) {
parts.push(comments);
parts.push(hardline);
}
}
}

parts.push(path.call(print, 'expression'));
parts.push(node.omitSemicolon ? '' : ';');

return parts;
}
print: ({ node, path, print }) => [
path.call(print, 'expression'),
node.omitSemicolon ? '' : ';'
]
};
5 changes: 1 addition & 4 deletions src/nodes/FunctionCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const printArguments = (path, print) =>
lastSeparator: [softline, ')']
});

let groupIndex = 0;
export const FunctionCall = {
print: ({ node, path, print, options }) => {
let expressionDoc = path.call(print, 'expression');
Expand All @@ -42,11 +41,9 @@ export const FunctionCall = {
// arguments accordingly.
if (expressionDoc.label === 'MemberAccessChain') {
expressionDoc = group(expressionDoc.contents, {
id: `FunctionCall.expression-${groupIndex}`
id: Symbol('FunctionCall.expression')
});

groupIndex += 1;

argumentsDoc = indentIfBreak(argumentsDoc, {
groupId: expressionDoc.id
});
Expand Down
27 changes: 15 additions & 12 deletions src/nodes/FunctionTypeName.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@ const stateMutability = (node) =>
: '';

export const FunctionTypeName = {
print: ({ node, path, print }) => [
'function(',
printSeparatedList(path.map(print, 'parameterTypes')),
')',
indent(
group([
visibility(node),
stateMutability(node),
returnTypes(node, path, print)
])
)
]
print: ({ node, path, print }) =>
group([
'function(',
printSeparatedList(path.map(print, 'parameterTypes'), {
grouped: false
}),
')',
indent(
group([
visibility(node),
stateMutability(node),
returnTypes(node, path, print)
])
)
])
};
4 changes: 2 additions & 2 deletions src/nodes/HexLiteral.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { doc } from 'prettier';
import { printString } from '../common/util.js';

const { join, line } = doc.builders;
const { join, hardline } = doc.builders;

export const HexLiteral = {
print: ({ node, options }) => {
const list = node.parts.map((part) => `hex${printString(part, options)}`);
return join(line, list);
return join(hardline, list);
}
};
35 changes: 10 additions & 25 deletions src/nodes/IfStatement.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { doc } from 'prettier';
import {
printComments,
printSeparatedItem
} from '../common/printer-helpers.js';
import { printSeparatedItem } from '../common/printer-helpers.js';

const { group, hardline, indent, line } = doc.builders;

Expand All @@ -22,10 +19,9 @@ const printFalseBody = (node, path, print) =>
? [' ', path.call(print, 'falseBody')]
: group(indent([line, path.call(print, 'falseBody')]));

const printElse = (node, path, print, commentsBetweenIfAndElse) => {
const printElse = (node, path, print) => {
if (node.falseBody) {
const elseOnSameLine =
node.trueBody.type === 'Block' && commentsBetweenIfAndElse.length === 0;
const elseOnSameLine = node.trueBody.type === 'Block';
return [
elseOnSameLine ? ' ' : hardline,
'else',
Expand All @@ -36,22 +32,11 @@ const printElse = (node, path, print, commentsBetweenIfAndElse) => {
};

export const IfStatement = {
print: ({ node, options, path, print }) => {
const comments = node.comments || [];
const commentsBetweenIfAndElse = comments.filter(
(comment) => !comment.leading && !comment.trailing
);

const parts = [];

parts.push('if (', printSeparatedItem(path.call(print, 'condition')), ')');
parts.push(printTrueBody(node, path, print));
if (commentsBetweenIfAndElse.length && node.falseBody) {
parts.push(hardline);
parts.push(printComments(node, path, options));
}
parts.push(printElse(node, path, print, commentsBetweenIfAndElse));

return parts;
}
print: ({ node, path, print }) => [
'if (',
printSeparatedItem(path.call(print, 'condition')),
')',
printTrueBody(node, path, print),
printElse(node, path, print)
]
};
5 changes: 1 addition & 4 deletions src/nodes/IndexAccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { doc } from 'prettier';

const { group, indent, indentIfBreak, label, softline } = doc.builders;

let groupIndex = 0;
export const IndexAccess = {
print: ({ path, print }) => {
let baseDoc = path.call(print, 'base');
Expand All @@ -16,11 +15,9 @@ export const IndexAccess = {
// arguments accordingly.
if (baseDoc.label === 'MemberAccessChain') {
baseDoc = group(baseDoc.contents, {
id: `IndexAccess.base-${groupIndex}`
id: Symbol('IndexAccess.base')
});

groupIndex += 1;

indexDoc = indentIfBreak(indexDoc, {
groupId: baseDoc.id
});
Expand Down
22 changes: 11 additions & 11 deletions src/nodes/ModifierDefinition.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import { doc } from 'prettier';
import { printSeparatedList } from '../common/printer-helpers.js';

const { group, hardline, indent, line } = doc.builders;
const { dedent, group, indent, line } = doc.builders;

const modifierParameters = (node, path, print) => {
if (node.parameters?.length > 0) {
return [
'(',
printSeparatedList(path.map(print, 'parameters'), {
separator: [
',',
// To keep consistency any list of parameters will split if it's longer than 2.
// For more information see:
// https://github.com/prettier-solidity/prettier-plugin-solidity/issues/256
node.parameters.length > 2 ? hardline : line
]
separator: [',', line]
}),
')'
];
Expand All @@ -38,16 +32,22 @@ const override = (node, path, print) => {

const body = (node, path, print) => {
if (!node.body) return ';';
if (node.isVirtual) return group([' ', path.call(print, 'body')]);
return [' ', path.call(print, 'body')];
if (node.isVirtual) return group(path.call(print, 'body'));
return [path.call(print, 'body')];
};

export const ModifierDefinition = {
print: ({ node, path, print }) => [
'modifier ',
node.name,
modifierParameters(node, path, print),
group(indent([virtual(node), override(node, path, print)])),
group(
indent([
virtual(node),
override(node, path, print),
node.body ? dedent(line) : ''
])
),
body(node, path, print)
]
};
4 changes: 1 addition & 3 deletions src/nodes/VariableDeclarationStatement.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const embraceVariables = (document, embrace) =>
const initialValue = (node, path, print) =>
node.initialValue ? [' = ', path.call(print, 'initialValue')] : '';

let groupIndex = 0;
export const VariableDeclarationStatement = {
print: ({ node, path, print }) => {
const startsWithVar =
Expand All @@ -23,9 +22,8 @@ export const VariableDeclarationStatement = {
node.variables.length > 1 || startsWithVar
)
],
{ id: `VariableDeclarationStatement.variables-${groupIndex}` }
{ id: Symbol('VariableDeclarationStatement.variables') }
);
groupIndex += 1;
const initialValueDoc = initialValue(node, path, print);

return group([
Expand Down
Loading

0 comments on commit 2b27e68

Please sign in to comment.