diff --git a/src/slang-nodes/ModifierDefinition.ts b/src/slang-nodes/ModifierDefinition.ts index 2cf0f70b5..2dc1eecf7 100644 --- a/src/slang-nodes/ModifierDefinition.ts +++ b/src/slang-nodes/ModifierDefinition.ts @@ -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 { @@ -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( diff --git a/src/slang-nodes/SlangNode.ts b/src/slang-nodes/SlangNode.ts index 639a15a67..65fd95518 100644 --- a/src/slang-nodes/SlangNode.ts +++ b/src/slang-nodes/SlangNode.ts @@ -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(children: T[]): Iterable { return { @@ -30,7 +31,9 @@ function reversedIterator(children: T[]): Iterable { }; } -export class SlangNode { +export abstract class SlangNode { + abstract readonly kind: TerminalKind | NonterminalKind; + comments?: Comment[]; loc: AstLocation; @@ -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; } @@ -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 }; } @@ -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; } diff --git a/src/types.d.ts b/src/types.d.ts index e7fb3db40..1aff0dff0 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -20,8 +20,8 @@ interface Location { } interface AstLocation extends Location { - leadingOffset: number; - trailingOffset: number; + outerStart: number; + outerEnd: number; } type PrintFunction = (path: AstPath) => Doc;