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
18 changes: 10 additions & 8 deletions src/slang-nodes/ModifierDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import { FunctionBody } from './FunctionBody.js';

import type * as ast from '@nomicfoundation/slang/ast';
import type { AstPath, Doc, ParserOptions } from 'prettier';
import type { CollectedMetadata, PrintFunction } from '../types.d.ts';
import type {
AstLocation,
CollectedMetadata,
PrintFunction
} from '../types.d.ts';
import type { AstNode } from './types.d.ts';

export class ModifierDefinition extends SlangNode {
Expand Down Expand Up @@ -45,14 +49,12 @@ export class ModifierDefinition extends SlangNode {
this.updateMetadata(this.parameters, this.attributes, this.body);

if (!this.parameters) {
const attributesLoc = this.attributes.loc;
const parametersOffset =
attributesLoc.start - attributesLoc.leadingOffset;
const parametersLoc = {
const parametersOffset = this.attributes.loc.outerStart;
const parametersLoc: AstLocation = {
outerStart: parametersOffset,
outerEnd: parametersOffset,
start: parametersOffset,
end: parametersOffset,
leadingOffset: 0,
trailingOffset: 0
end: parametersOffset
};

this.parameters = Object.assign(
Expand Down
38 changes: 20 additions & 18 deletions src/slang-nodes/SlangNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import { MultiLineNatSpecComment } from './MultiLineNatSpecComment.js';
import { SingleLineComment } from './SingleLineComment.js';
import { SingleLineNatSpecComment } from './SingleLineNatSpecComment.js';

import type { NonterminalKind } from '@nomicfoundation/slang/cst';
import type {
AstLocation,
CollectedMetadata,
SlangAstNode
} from '../types.d.ts';
import type { Comment, StrictAstNode } from './types.d.ts';
import type { TerminalNode } from './TerminalNode.js';
import type { TerminalNode } from './TerminalNode.ts';

function reversedIterator<T>(children: T[]): Iterable<T> {
return {
Expand All @@ -30,7 +31,9 @@ function reversedIterator<T>(children: T[]): Iterable<T> {
};
}

export class SlangNode {
export abstract class SlangNode {
abstract readonly kind: TerminalKind | NonterminalKind;

comments?: Comment[];

loc: AstLocation;
Expand All @@ -41,12 +44,13 @@ export class SlangNode {
enclosePeripheralComments = false
) {
if (ast instanceof SlangTerminalNode) {
const offset = collected.offsets.get(ast.id) || 0;
const start = collected.offsets.get(ast.id) || 0;
const end = start + ast.textLength.utf16;
this.loc = {
start: offset,
end: offset + ast.textLength.utf16,
leadingOffset: 0,
trailingOffset: 0
outerStart: start,
outerEnd: end,
start,
end
};
return;
}
Expand Down Expand Up @@ -109,10 +113,10 @@ export class SlangNode {
trailingOffset ??= triviaLength;

this.loc = {
outerStart: initialOffset,
outerEnd: offset,
start: initialOffset + leadingOffset,
end: offset - trailingOffset,
leadingOffset,
trailingOffset
end: offset - trailingOffset
};
}

Expand All @@ -121,26 +125,24 @@ export class SlangNode {
): void {
const { loc } = this;
// calculate correct loc object
if (loc.leadingOffset === 0) {
if (loc.outerStart === loc.start) {
for (const childNode of childNodes) {
if (childNode === undefined) continue;
const { leadingOffset, start } = childNode.loc;
const { outerStart, start } = childNode.loc;

if (start - leadingOffset === loc.start) {
loc.leadingOffset = leadingOffset;
if (outerStart === loc.start) {
loc.start = start;
break;
}
}
}

if (loc.trailingOffset === 0) {
if (loc.outerEnd === loc.end) {
for (const childNode of reversedIterator(childNodes)) {
if (childNode === undefined) continue;
const { trailingOffset, end } = childNode.loc;
const { outerEnd, end } = childNode.loc;

if (end + trailingOffset === loc.end) {
loc.trailingOffset = trailingOffset;
if (outerEnd === loc.end) {
loc.end = end;
break;
}
Expand Down
4 changes: 2 additions & 2 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ interface Location {
}

interface AstLocation extends Location {
leadingOffset: number;
trailingOffset: number;
outerStart: number;
outerEnd: number;
}

type PrintFunction = (path: AstPath<AstNode>) => Doc;
Expand Down