Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,667 changes: 977 additions & 690 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,16 @@
"devDependencies": {
"@babel/code-frame": "^7.26.2",
"@types/jest": "^29.5.14",
"@types/semver": "^7.5.8",
"@typescript-eslint/eslint-plugin": "^8.28.0",
"@typescript-eslint/parser": "^8.28.0",
"@types/semver": "^7.7.0",
"@typescript-eslint/eslint-plugin": "^8.31.0",
"@typescript-eslint/parser": "^8.31.0",
"c8": "^10.1.3",
"cross-env": "^7.0.3",
"eslint": "^9.23.0",
"eslint-config-prettier": "10.1.1",
"eslint": "^9.25.1",
"eslint-config-prettier": "10.1.2",
"esm-utils": "^4.3.0",
"jest": "^29.7.0",
"jest-light-runner": "^0.6.0",
"jest-light-runner": "^0.7.8",
"jest-snapshot-serializer-ansi": "^2.2.1",
"jest-snapshot-serializer-raw": "^2.0.0",
"jest-watch-typeahead": "^2.2.2",
Expand All @@ -103,8 +103,8 @@
"solc": "^0.8.29",
"ts-loader": "^9.5.2",
"ts-node": "^10.9.2",
"typescript": "^5.8.2",
"webpack": "^5.98.0",
"typescript": "^5.8.3",
"webpack": "^5.99.6",
"webpack-cli": "^6.0.1"
},
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const parsers = {
[antlrParserId]: antlrParser
};

const solidityCanAttachComment = (node: { type: string }): boolean =>
const antlrCanAttachComment = (node: { type: string }): boolean =>
typeof node.type === 'string' &&
node.type !== 'BlockComment' &&
node.type !== 'LineComment';
Expand All @@ -63,7 +63,7 @@ const canAttachComment = (node: AstNode): boolean =>

// https://prettier.io/docs/en/plugins.html#printers
const antlrPrinter = {
canAttachComment: solidityCanAttachComment,
canAttachComment: antlrCanAttachComment,
handleComments: {
ownLine: comments.solidityHandleOwnLineComment,
endOfLine: comments.solidityHandleEndOfLineComment,
Expand Down
14 changes: 4 additions & 10 deletions src/slang-nodes/ConditionalExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,13 @@ function experimentalTernaries(
const isNestedAsTrueExpression =
isNested && grandparent.trueExpression.variant === node;
const falseExpressionInSameLine =
typeof node.falseExpression.variant !== 'string' &&
(node.falseExpression.variant.kind === NonterminalKind.TupleExpression ||
node.falseExpression.variant.kind ===
NonterminalKind.ConditionalExpression);
node.falseExpression.variant.kind === NonterminalKind.TupleExpression ||
node.falseExpression.variant.kind === NonterminalKind.ConditionalExpression;

// If the `condition` breaks into multiple lines, we add parentheses,
// unless it already is a `TupleExpression`.
const operand = path.call(print, 'operand');
const operandDoc = group([
typeof node.operand.variant !== 'string' &&
node.operand.variant.kind === NonterminalKind.TupleExpression
? operand
: ifBreak(['(', printSeparatedItem(operand), ')'], operand),
Expand Down Expand Up @@ -137,13 +134,10 @@ export class ConditionalExpression implements SlangNode {
// `condition` must be a single `bool` value.
const operandLoc = this.operand.loc;
while (
typeof this.operand.variant !== 'string' &&
this.operand.variant.kind === NonterminalKind.TupleExpression &&
this.operand.variant.items.items.length === 1 &&
(typeof this.operand.variant.items.items[0].expression!.variant ===
'string' ||
this.operand.variant.items.items[0].expression!.variant.kind !==
NonterminalKind.ConditionalExpression)
this.operand.variant.items.items[0].expression!.variant.kind !==
NonterminalKind.ConditionalExpression
) {
this.operand = this.operand.variant.items.items[0].expression!;
}
Expand Down
7 changes: 3 additions & 4 deletions src/slang-nodes/FunctionCallExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ export class FunctionCallExpression implements SlangNode {
// If we are at the end of a MemberAccessChain we should indent the
// arguments accordingly.
if (isLabel(operandDoc) && operandDoc.label === 'MemberAccessChain') {
operandDoc = group(operandDoc.contents, {
id: Symbol('FunctionCallExpression.operand')
});
argumentsDoc = indentIfBreak(argumentsDoc, { groupId: operandDoc.id! });
const groupId = Symbol('Slang.FunctionCallExpression.operand');
operandDoc = group(operandDoc.contents, { id: groupId });
argumentsDoc = indentIfBreak(argumentsDoc, { groupId });
// We wrap the expression in a label in case there is an IndexAccess or
// a FunctionCall following this IndexAccess.
return label('MemberAccessChain', [operandDoc, argumentsDoc]);
Expand Down
7 changes: 3 additions & 4 deletions src/slang-nodes/IndexAccessExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,10 @@ export class IndexAccessExpression implements SlangNode {
// If we are at the end of a MemberAccessChain we should indent the
// arguments accordingly.
if (isLabel(operandDoc) && operandDoc.label === 'MemberAccessChain') {
operandDoc = group(operandDoc.contents, {
id: Symbol('IndexAccessExpression.operand')
});
const groupId = Symbol('Slang.IndexAccessExpression.operand');
operandDoc = group(operandDoc.contents, { id: groupId });

indexDoc = indentIfBreak(indexDoc, { groupId: operandDoc.id! });
indexDoc = indentIfBreak(indexDoc, { groupId });
// We wrap the expression in a label in case there is an IndexAccess or
// a FunctionCall following this IndexAccess.
return label('MemberAccessChain', [operandDoc, indexDoc]);
Expand Down
8 changes: 3 additions & 5 deletions src/slang-nodes/ReturnStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ function printExpression(
options: ParserOptions<AstNode>
): Doc {
if (node.expression) {
return typeof node.expression.variant !== 'string' &&
(node.expression.variant.kind === NonterminalKind.TupleExpression ||
(options.experimentalTernaries &&
node.expression.variant.kind ===
NonterminalKind.ConditionalExpression))
return node.expression.variant.kind === NonterminalKind.TupleExpression ||
(options.experimentalTernaries &&
node.expression.variant.kind === NonterminalKind.ConditionalExpression)
? [' ', path.call(print, 'expression')]
: group(indent([line, path.call(print, 'expression')]));
}
Expand Down
9 changes: 3 additions & 6 deletions src/slang-nodes/StateVariableDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,17 @@ export class StateVariableDefinition implements SlangNode {
}

print(path: AstPath<StateVariableDefinition>, print: PrintFunction): Doc {
const groupId = Symbol('Slang.StateVariableDefinition.attributes');
const attributesDoc = group(indent(path.call(print, 'attributes')), {
id: Symbol('Slang.StateVariableDefinition.attributes')
id: groupId
});

return [
path.call(print, 'typeName'),
attributesDoc,
' ',
path.call(print, 'name'),
this.value
? indentIfBreak(path.call(print, 'value'), {
groupId: attributesDoc.id!
})
: '',
this.value ? indentIfBreak(path.call(print, 'value'), { groupId }) : '',
';'
];
}
Expand Down
3 changes: 1 addition & 2 deletions src/slang-nodes/StateVariableDefinitionValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ export class StateVariableDefinitionValue implements SlangNode {
path: AstPath<StateVariableDefinitionValue>,
print: PrintFunction
): Doc {
return typeof this.value.variant !== 'string' &&
this.value.variant.kind === NonterminalKind.ArrayExpression
return this.value.variant.kind === NonterminalKind.ArrayExpression
? [' = ', path.call(print, 'value')]
: group([' =', indent([line, path.call(print, 'value')])]);
}
Expand Down
7 changes: 3 additions & 4 deletions src/slang-nodes/TupleDeconstructionStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,15 @@ export class TupleDeconstructionStatement implements SlangNode {
path: AstPath<TupleDeconstructionStatement>,
print: PrintFunction
): Doc {
const groupId = Symbol('Slang.VariableDeclarationStatement.variables');
const declarationDoc = group(
[this.varKeyword ? 'var (' : '(', path.call(print, 'elements'), ')'],
{ id: Symbol('Slang.VariableDeclarationStatement.variables') }
{ id: groupId }
);

return [
declarationDoc,
indentIfBreak([' = ', path.call(print, 'expression'), ';'], {
groupId: declarationDoc.id!
})
indentIfBreak([' = ', path.call(print, 'expression'), ';'], { groupId })
];
}
}
5 changes: 3 additions & 2 deletions src/slang-nodes/VariableDeclarationStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class VariableDeclarationStatement implements SlangNode {
path: AstPath<VariableDeclarationStatement>,
print: PrintFunction
): Doc {
const groupId = Symbol('Slang.VariableDeclarationStatement.variables');
const declarationDoc = group(
[
path.call(print, 'variableType'),
Expand All @@ -68,12 +69,12 @@ export class VariableDeclarationStatement implements SlangNode {
path.call(print, 'name')
])
],
{ id: Symbol('Slang.VariableDeclarationStatement.variables') }
{ id: groupId }
);

return [
declarationDoc,
indentIfBreak(path.call(print, 'value'), { groupId: declarationDoc.id! }),
indentIfBreak(path.call(print, 'value'), { groupId }),
';'
];
}
Expand Down
8 changes: 0 additions & 8 deletions src/slang-utils/prettier-version-satisfies.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/slang-utils/print-warning.ts

This file was deleted.

5 changes: 2 additions & 3 deletions src/slangPrinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ function hasNodeIgnoreComment(node: StrictAstNode): boolean {
}

function ignoreComments(path: AstPath<AstNode>): void {
// TODO: remove undefined once we stop supporting prettier 2
const node = path.getNode();
// We ignore anything that is not an object
if (node === null || node === undefined || typeof node !== 'object') return;
if (node === null || typeof node !== 'object') return;

const keys = Object.keys(node) as (keyof StrictAstNode)[];
for (const key of keys) {
Expand Down Expand Up @@ -64,7 +63,7 @@ function genericPrint(
): Doc {
const node = path.getNode();

if (typeof node === 'undefined' || node === null) {
if (node === null) {
return '';
}

Expand Down
Loading