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
2 changes: 0 additions & 2 deletions src/slang-nodes/ArrayExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ export class ArrayExpression extends SlangNode {
super(ast);

this.items = new ArrayValues(ast.items, options);

this.updateMetadata(this.items);
}

print(path: AstPath<ArrayExpression>, print: PrintFunction): Doc {
Expand Down
2 changes: 0 additions & 2 deletions src/slang-nodes/ArrayValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ export class ArrayValues extends SlangNode {
super(ast, true);

this.items = ast.items.map((item) => new Expression(item, options));

this.updateMetadata(this.items);
}

print(path: AstPath<ArrayValues>, print: PrintFunction): Doc {
Expand Down
2 changes: 0 additions & 2 deletions src/slang-nodes/AssemblyFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ export class AssemblyFlags extends SlangNode {
super(ast, true);

this.items = ast.items.map((item) => new StringLiteral(item, options));

this.updateMetadata(this.items);
}

print(path: AstPath<AssemblyFlags>, print: PrintFunction): Doc {
Expand Down
2 changes: 0 additions & 2 deletions src/slang-nodes/CallOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ export class CallOptions extends SlangNode {
super(ast, true);

this.items = ast.items.map((item) => new NamedArgument(item, options));

this.updateMetadata(this.items);
}

print(
Expand Down
2 changes: 0 additions & 2 deletions src/slang-nodes/CatchClauses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ export class CatchClauses extends SlangNode {
super(ast, true);

this.items = ast.items.map((item) => new CatchClause(item, options));

this.updateMetadata(this.items);
}

print(path: AstPath<CatchClauses>, print: PrintFunction): Doc {
Expand Down
2 changes: 0 additions & 2 deletions src/slang-nodes/ConstructorAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ export class ConstructorAttributes extends SlangNode {
(item) => new ConstructorAttribute(item, options)
);

this.updateMetadata(this.items);

this.items = this.items.sort(sortFunctionAttributes);
}

Expand Down
4 changes: 1 addition & 3 deletions src/slang-nodes/ContractMembers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@ export class ContractMembers extends SlangNode {
super(ast, true);

this.items = ast.items.map((item) => new ContractMember(item, options));

this.updateMetadata(this.items);
}

print(
path: AstPath<ContractMembers>,
print: PrintFunction,
options: ParserOptions<AstNode>
): Doc {
return this.items.length > 0 || this.comments.length > 0
return this.items.length > 0 || (this.comments?.length || 0) > 0
? printSeparatedItem(printPreservingEmptyLines(path, print, options), {
firstSeparator: hardline
})
Expand Down
2 changes: 0 additions & 2 deletions src/slang-nodes/ContractSpecifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ export class ContractSpecifiers extends SlangNode {

this.items = ast.items.map((item) => new ContractSpecifier(item, options));

this.updateMetadata(this.items);

this.items = this.items.sort(sortContractSpecifiers);
}

Expand Down
2 changes: 0 additions & 2 deletions src/slang-nodes/ErrorParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ export class ErrorParameters extends SlangNode {
super(ast, true);

this.items = ast.items.map((item) => new ErrorParameter(item, options));

this.updateMetadata(this.items);
}

print(path: AstPath<ErrorParameters>, print: PrintFunction): Doc {
Expand Down
2 changes: 0 additions & 2 deletions src/slang-nodes/EventParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ export class EventParameters extends SlangNode {
super(ast, true);

this.items = ast.items.map((item) => new EventParameter(item, options));

this.updateMetadata(this.items);
}

print(path: AstPath<EventParameters>, print: PrintFunction): Doc {
Expand Down
2 changes: 0 additions & 2 deletions src/slang-nodes/FallbackFunctionAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ export class FallbackFunctionAttributes extends SlangNode {
(item) => new FallbackFunctionAttribute(item, options)
);

this.updateMetadata(this.items);

this.items = this.items.sort(sortFunctionAttributes);
}

Expand Down
2 changes: 0 additions & 2 deletions src/slang-nodes/FunctionAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ export class FunctionAttributes extends SlangNode {

this.items = ast.items.map((item) => new FunctionAttribute(item, options));

this.updateMetadata(this.items);

this.items = this.items.sort(sortFunctionAttributes);
}

Expand Down
2 changes: 0 additions & 2 deletions src/slang-nodes/FunctionTypeAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ export class FunctionTypeAttributes extends SlangNode {

this.items = ast.items.map((item) => new FunctionTypeAttribute(item));

this.updateMetadata(this.items);

this.items = this.items.sort(sortFunctionAttributes);
}

Expand Down
2 changes: 0 additions & 2 deletions src/slang-nodes/HexStringLiterals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ export class HexStringLiterals extends SlangNode {
super(ast, true);

this.items = ast.items.map((item) => new HexStringLiteral(item, options));

this.updateMetadata(this.items);
}

print(path: AstPath<HexStringLiterals>, print: PrintFunction): Doc {
Expand Down
2 changes: 1 addition & 1 deletion src/slang-nodes/IfStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class IfStatement extends SlangNode {
this.elseBranch
? [
bodyKind !== NonterminalKind.Block || // else on a new line if body is not a block
bodyComments.some(
bodyComments?.some(
(comment) =>
!isBlockComment(comment) || comment.placement === 'ownLine'
) // or if body has trailing single line comments or a block comment on a new line
Expand Down
2 changes: 0 additions & 2 deletions src/slang-nodes/ImportDeconstructionSymbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ export class ImportDeconstructionSymbols extends SlangNode {
super(ast, true);

this.items = ast.items.map((item) => new ImportDeconstructionSymbol(item));

this.updateMetadata(this.items);
}

print(
Expand Down
2 changes: 0 additions & 2 deletions src/slang-nodes/InheritanceTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ export class InheritanceTypes extends SlangNode {
super(ast, true);

this.items = ast.items.map((item) => new InheritanceType(item, options));

this.updateMetadata(this.items);
}

print(path: AstPath<InheritanceTypes>, print: PrintFunction): Doc {
Expand Down
4 changes: 1 addition & 3 deletions src/slang-nodes/InterfaceMembers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@ export class InterfaceMembers extends SlangNode {
super(ast, true);

this.items = ast.items.map((item) => new ContractMember(item, options));

this.updateMetadata(this.items);
}

print(
path: AstPath<InterfaceMembers>,
print: PrintFunction,
options: ParserOptions<AstNode>
): Doc {
return this.items.length > 0 || this.comments.length > 0
return this.items.length > 0 || (this.comments?.length || 0) > 0
? printSeparatedItem(printPreservingEmptyLines(path, print, options), {
firstSeparator: hardline
})
Expand Down
4 changes: 1 addition & 3 deletions src/slang-nodes/LibraryMembers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@ export class LibraryMembers extends SlangNode {
super(ast, true);

this.items = ast.items.map((item) => new ContractMember(item, options));

this.updateMetadata(this.items);
}

print(
path: AstPath<LibraryMembers>,
print: PrintFunction,
options: ParserOptions<AstNode>
): Doc {
return this.items.length > 0 || this.comments.length > 0
return this.items.length > 0 || (this.comments?.length || 0) > 0
? printSeparatedItem(printPreservingEmptyLines(path, print, options), {
firstSeparator: hardline
})
Expand Down
2 changes: 0 additions & 2 deletions src/slang-nodes/ModifierAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ export class ModifierAttributes extends SlangNode {

this.items = ast.items.map((item) => new ModifierAttribute(item));

this.updateMetadata(this.items);

this.items = this.items.sort(sortFunctionAttributes);
}

Expand Down
2 changes: 0 additions & 2 deletions src/slang-nodes/NamedArguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ export class NamedArguments extends SlangNode {
super(ast, true);

this.items = ast.items.map((item) => new NamedArgument(item, options));

this.updateMetadata(this.items);
}

print(
Expand Down
2 changes: 0 additions & 2 deletions src/slang-nodes/OverridePaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ export class OverridePaths extends SlangNode {
super(ast, true);

this.items = ast.items.map((item) => new IdentifierPath(item));

this.updateMetadata(this.items);
}

print(path: AstPath<OverridePaths>, print: PrintFunction): Doc {
Expand Down
2 changes: 0 additions & 2 deletions src/slang-nodes/Parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ export class Parameters extends SlangNode {
super(ast, true);

this.items = ast.items.map((item) => new Parameter(item, options));

this.updateMetadata(this.items);
}

print(
Expand Down
2 changes: 0 additions & 2 deletions src/slang-nodes/PositionalArguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ export class PositionalArguments extends SlangNode {
super(ast, true);

this.items = ast.items.map((item) => new Expression(item, options));

this.updateMetadata(this.items);
}

print(
Expand Down
4 changes: 3 additions & 1 deletion src/slang-nodes/PositionalArgumentsDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ export class PositionalArgumentsDeclaration extends SlangNode {

// We need to check the comments at this point because they will be removed
// from this node into the root node.
// Since we are collecting comments in a different array, we have to query
// the ast directly for possible block comments.
this.isEmpty =
this.arguments.items.length === 0 && // no arguments
!this.comments.some((comment) => isBlockComment(comment)); // no block comments
!ast.cst.children().some(({ node }) => isBlockComment(node)); // no block comments
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the only node that has a change that is not removing updateMetadata

}

print(
Expand Down
2 changes: 0 additions & 2 deletions src/slang-nodes/ReceiveFunctionAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ export class ReceiveFunctionAttributes extends SlangNode {
(item) => new ReceiveFunctionAttribute(item, options)
);

this.updateMetadata(this.items);

this.items = this.items.sort(sortFunctionAttributes);
}

Expand Down
46 changes: 15 additions & 31 deletions src/slang-nodes/SlangNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ import type { Comment, StrictAstNode } from '../slang-nodes/types.d.ts';
import type { AstLocation, SlangAstNode } from '../types.d.ts';

const offsets = new Map<number, number>();
const comments: Comment[] = [];

export function clearOffsets(): void {
offsets.clear();
}

export function clearComments(): Comment[] {
return comments.splice(0);
}

function reversedIterator<T>(children: T[]): Iterable<T> {
return {
[Symbol.iterator](): Iterator<T> {
Expand All @@ -30,23 +36,8 @@ function reversedIterator<T>(children: T[]): Iterable<T> {
};
}

function collectComments(
comments: Comment[],
node: StrictAstNode | StrictAstNode[] | undefined
): Comment[] {
if (node) {
if (Array.isArray(node)) {
return node.reduce(collectComments, comments);
}
if (node.comments.length > 0) {
comments.push(...node.comments.splice(0));
}
}
return comments;
}

export class SlangNode {
comments: Comment[] = [];
comments?: Comment[];

loc: AstLocation;

Expand Down Expand Up @@ -98,16 +89,16 @@ export class SlangNode {
// offset, it's hard to separate these responsibilities into different
// functions without doing the iteration twice.
case TerminalKind.MultiLineComment:
this.comments.push(new MultiLineComment(node, offset));
comments.push(new MultiLineComment(node, offset));
break;
case TerminalKind.MultiLineNatSpecComment:
this.comments.push(new MultiLineNatSpecComment(node, offset));
comments.push(new MultiLineNatSpecComment(node, offset));
break;
case TerminalKind.SingleLineComment:
this.comments.push(new SingleLineComment(node, offset));
comments.push(new SingleLineComment(node, offset));
break;
case TerminalKind.SingleLineNatSpecComment:
this.comments.push(new SingleLineNatSpecComment(node, offset));
comments.push(new SingleLineNatSpecComment(node, offset));
break;
}
// We accumulate the trivia length
Expand All @@ -130,18 +121,12 @@ export class SlangNode {
};
}

updateMetadata(
...childNodes: (StrictAstNode | StrictAstNode[] | undefined)[]
): void {
const { comments, loc } = this;
// Collect comments
this.comments = childNodes.reduce(collectComments, comments);

updateMetadata(...childNodes: (StrictAstNode | undefined)[]): void {
const { loc } = this;
// calculate correct loc object
if (loc.leadingOffset === 0) {
for (const childNode of childNodes) {
if (typeof childNode === 'undefined' || Array.isArray(childNode))
continue;
if (typeof childNode === 'undefined') continue;
const { leadingOffset, start } = childNode.loc;

if (start - leadingOffset === loc.start) {
Expand All @@ -154,8 +139,7 @@ export class SlangNode {

if (loc.trailingOffset === 0) {
for (const childNode of reversedIterator(childNodes)) {
if (typeof childNode === 'undefined' || Array.isArray(childNode))
continue;
if (typeof childNode === 'undefined') continue;
const { trailingOffset, end } = childNode.loc;

if (end + trailingOffset === loc.end) {
Expand Down
4 changes: 0 additions & 4 deletions src/slang-nodes/SourceUnit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ export class SourceUnit extends SlangNode {
this.members = new SourceUnitMembers(ast.members, options);

this.updateMetadata(this.members);

// Because of comments being extracted like a russian doll, the order needs
// to be fixed at the end.
this.comments = this.comments.sort((a, b) => a.loc.start - b.loc.start);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comments are handled at the slangSolidityParser level

}

print(
Expand Down
2 changes: 0 additions & 2 deletions src/slang-nodes/SourceUnitMembers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ export class SourceUnitMembers extends SlangNode {
super(ast, true);

this.items = ast.items.map((item) => new SourceUnitMember(item, options));

this.updateMetadata(this.items);
}

print(
Expand Down
2 changes: 0 additions & 2 deletions src/slang-nodes/StateVariableAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ export class StateVariableAttributes extends SlangNode {

this.items = ast.items.map((item) => new StateVariableAttribute(item));

this.updateMetadata(this.items);

this.items = this.items.sort(sortFunctionAttributes);
}

Expand Down
Loading
Loading