Skip to content

Commit 8cae601

Browse files
authored
Small refactor (#1111)
* updating dependencies * removing unnecessary non-null assertions * Expression.variant no longer can be a string so all checks for that have been removed. * cleaner types imported from slang * since we are not supporting prettier 2, getNode will not return undefined * the type for the experimentalTernaries option is exported by prettier now * unused files
1 parent bd5ece9 commit 8cae601

15 files changed

+1019
-979
lines changed

package-lock.json

Lines changed: 977 additions & 690 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,16 @@
8585
"devDependencies": {
8686
"@babel/code-frame": "^7.26.2",
8787
"@types/jest": "^29.5.14",
88-
"@types/semver": "^7.5.8",
89-
"@typescript-eslint/eslint-plugin": "^8.28.0",
90-
"@typescript-eslint/parser": "^8.28.0",
88+
"@types/semver": "^7.7.0",
89+
"@typescript-eslint/eslint-plugin": "^8.31.0",
90+
"@typescript-eslint/parser": "^8.31.0",
9191
"c8": "^10.1.3",
9292
"cross-env": "^7.0.3",
93-
"eslint": "^9.23.0",
94-
"eslint-config-prettier": "10.1.1",
93+
"eslint": "^9.25.1",
94+
"eslint-config-prettier": "10.1.2",
9595
"esm-utils": "^4.3.0",
9696
"jest": "^29.7.0",
97-
"jest-light-runner": "^0.6.0",
97+
"jest-light-runner": "^0.7.8",
9898
"jest-snapshot-serializer-ansi": "^2.2.1",
9999
"jest-snapshot-serializer-raw": "^2.0.0",
100100
"jest-watch-typeahead": "^2.2.2",
@@ -103,8 +103,8 @@
103103
"solc": "^0.8.29",
104104
"ts-loader": "^9.5.2",
105105
"ts-node": "^10.9.2",
106-
"typescript": "^5.8.2",
107-
"webpack": "^5.98.0",
106+
"typescript": "^5.8.3",
107+
"webpack": "^5.99.6",
108108
"webpack-cli": "^6.0.1"
109109
},
110110
"dependencies": {

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const parsers = {
5151
[antlrParserId]: antlrParser
5252
};
5353

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

6464
// https://prettier.io/docs/en/plugins.html#printers
6565
const antlrPrinter = {
66-
canAttachComment: solidityCanAttachComment,
66+
canAttachComment: antlrCanAttachComment,
6767
handleComments: {
6868
ownLine: comments.solidityHandleOwnLineComment,
6969
endOfLine: comments.solidityHandleEndOfLineComment,

src/slang-nodes/ConditionalExpression.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,13 @@ function experimentalTernaries(
2929
const isNestedAsTrueExpression =
3030
isNested && grandparent.trueExpression.variant === node;
3131
const falseExpressionInSameLine =
32-
typeof node.falseExpression.variant !== 'string' &&
33-
(node.falseExpression.variant.kind === NonterminalKind.TupleExpression ||
34-
node.falseExpression.variant.kind ===
35-
NonterminalKind.ConditionalExpression);
32+
node.falseExpression.variant.kind === NonterminalKind.TupleExpression ||
33+
node.falseExpression.variant.kind === NonterminalKind.ConditionalExpression;
3634

3735
// If the `condition` breaks into multiple lines, we add parentheses,
3836
// unless it already is a `TupleExpression`.
3937
const operand = path.call(print, 'operand');
4038
const operandDoc = group([
41-
typeof node.operand.variant !== 'string' &&
4239
node.operand.variant.kind === NonterminalKind.TupleExpression
4340
? operand
4441
: ifBreak(['(', printSeparatedItem(operand), ')'], operand),
@@ -137,13 +134,10 @@ export class ConditionalExpression implements SlangNode {
137134
// `condition` must be a single `bool` value.
138135
const operandLoc = this.operand.loc;
139136
while (
140-
typeof this.operand.variant !== 'string' &&
141137
this.operand.variant.kind === NonterminalKind.TupleExpression &&
142138
this.operand.variant.items.items.length === 1 &&
143-
(typeof this.operand.variant.items.items[0].expression!.variant ===
144-
'string' ||
145-
this.operand.variant.items.items[0].expression!.variant.kind !==
146-
NonterminalKind.ConditionalExpression)
139+
this.operand.variant.items.items[0].expression!.variant.kind !==
140+
NonterminalKind.ConditionalExpression
147141
) {
148142
this.operand = this.operand.variant.items.items[0].expression!;
149143
}

src/slang-nodes/FunctionCallExpression.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,9 @@ export class FunctionCallExpression implements SlangNode {
4545
// If we are at the end of a MemberAccessChain we should indent the
4646
// arguments accordingly.
4747
if (isLabel(operandDoc) && operandDoc.label === 'MemberAccessChain') {
48-
operandDoc = group(operandDoc.contents, {
49-
id: Symbol('FunctionCallExpression.operand')
50-
});
51-
argumentsDoc = indentIfBreak(argumentsDoc, { groupId: operandDoc.id! });
48+
const groupId = Symbol('Slang.FunctionCallExpression.operand');
49+
operandDoc = group(operandDoc.contents, { id: groupId });
50+
argumentsDoc = indentIfBreak(argumentsDoc, { groupId });
5251
// We wrap the expression in a label in case there is an IndexAccess or
5352
// a FunctionCall following this IndexAccess.
5453
return label('MemberAccessChain', [operandDoc, argumentsDoc]);

src/slang-nodes/IndexAccessExpression.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,10 @@ export class IndexAccessExpression implements SlangNode {
5454
// If we are at the end of a MemberAccessChain we should indent the
5555
// arguments accordingly.
5656
if (isLabel(operandDoc) && operandDoc.label === 'MemberAccessChain') {
57-
operandDoc = group(operandDoc.contents, {
58-
id: Symbol('IndexAccessExpression.operand')
59-
});
57+
const groupId = Symbol('Slang.IndexAccessExpression.operand');
58+
operandDoc = group(operandDoc.contents, { id: groupId });
6059

61-
indexDoc = indentIfBreak(indexDoc, { groupId: operandDoc.id! });
60+
indexDoc = indentIfBreak(indexDoc, { groupId });
6261
// We wrap the expression in a label in case there is an IndexAccess or
6362
// a FunctionCall following this IndexAccess.
6463
return label('MemberAccessChain', [operandDoc, indexDoc]);

src/slang-nodes/ReturnStatement.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@ function printExpression(
1717
options: ParserOptions<AstNode>
1818
): Doc {
1919
if (node.expression) {
20-
return typeof node.expression.variant !== 'string' &&
21-
(node.expression.variant.kind === NonterminalKind.TupleExpression ||
22-
(options.experimentalTernaries &&
23-
node.expression.variant.kind ===
24-
NonterminalKind.ConditionalExpression))
20+
return node.expression.variant.kind === NonterminalKind.TupleExpression ||
21+
(options.experimentalTernaries &&
22+
node.expression.variant.kind === NonterminalKind.ConditionalExpression)
2523
? [' ', path.call(print, 'expression')]
2624
: group(indent([line, path.call(print, 'expression')]));
2725
}

src/slang-nodes/StateVariableDefinition.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,17 @@ export class StateVariableDefinition implements SlangNode {
5252
}
5353

5454
print(path: AstPath<StateVariableDefinition>, print: PrintFunction): Doc {
55+
const groupId = Symbol('Slang.StateVariableDefinition.attributes');
5556
const attributesDoc = group(indent(path.call(print, 'attributes')), {
56-
id: Symbol('Slang.StateVariableDefinition.attributes')
57+
id: groupId
5758
});
5859

5960
return [
6061
path.call(print, 'typeName'),
6162
attributesDoc,
6263
' ',
6364
path.call(print, 'name'),
64-
this.value
65-
? indentIfBreak(path.call(print, 'value'), {
66-
groupId: attributesDoc.id!
67-
})
68-
: '',
65+
this.value ? indentIfBreak(path.call(print, 'value'), { groupId }) : '',
6966
';'
7067
];
7168
}

src/slang-nodes/StateVariableDefinitionValue.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ export class StateVariableDefinitionValue implements SlangNode {
3737
path: AstPath<StateVariableDefinitionValue>,
3838
print: PrintFunction
3939
): Doc {
40-
return typeof this.value.variant !== 'string' &&
41-
this.value.variant.kind === NonterminalKind.ArrayExpression
40+
return this.value.variant.kind === NonterminalKind.ArrayExpression
4241
? [' = ', path.call(print, 'value')]
4342
: group([' =', indent([line, path.call(print, 'value')])]);
4443
}

src/slang-nodes/TupleDeconstructionStatement.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,15 @@ export class TupleDeconstructionStatement implements SlangNode {
4444
path: AstPath<TupleDeconstructionStatement>,
4545
print: PrintFunction
4646
): Doc {
47+
const groupId = Symbol('Slang.VariableDeclarationStatement.variables');
4748
const declarationDoc = group(
4849
[this.varKeyword ? 'var (' : '(', path.call(print, 'elements'), ')'],
49-
{ id: Symbol('Slang.VariableDeclarationStatement.variables') }
50+
{ id: groupId }
5051
);
5152

5253
return [
5354
declarationDoc,
54-
indentIfBreak([' = ', path.call(print, 'expression'), ';'], {
55-
groupId: declarationDoc.id!
56-
})
55+
indentIfBreak([' = ', path.call(print, 'expression'), ';'], { groupId })
5756
];
5857
}
5958
}

src/slang-nodes/VariableDeclarationStatement.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export class VariableDeclarationStatement implements SlangNode {
5757
path: AstPath<VariableDeclarationStatement>,
5858
print: PrintFunction
5959
): Doc {
60+
const groupId = Symbol('Slang.VariableDeclarationStatement.variables');
6061
const declarationDoc = group(
6162
[
6263
path.call(print, 'variableType'),
@@ -68,12 +69,12 @@ export class VariableDeclarationStatement implements SlangNode {
6869
path.call(print, 'name')
6970
])
7071
],
71-
{ id: Symbol('Slang.VariableDeclarationStatement.variables') }
72+
{ id: groupId }
7273
);
7374

7475
return [
7576
declarationDoc,
76-
indentIfBreak(path.call(print, 'value'), { groupId: declarationDoc.id! }),
77+
indentIfBreak(path.call(print, 'value'), { groupId }),
7778
';'
7879
];
7980
}

src/slang-utils/prettier-version-satisfies.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/slang-utils/print-warning.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/slangPrinter.ts

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

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

2827
const keys = Object.keys(node) as (keyof StrictAstNode)[];
2928
for (const key of keys) {
@@ -64,7 +63,7 @@ function genericPrint(
6463
): Doc {
6564
const node = path.getNode();
6665

67-
if (typeof node === 'undefined' || node === null) {
66+
if (node === null) {
6867
return '';
6968
}
7069

0 commit comments

Comments
 (0)