From c5ebee82b083dc205dfa678db9070e3fa802aed6 Mon Sep 17 00:00:00 2001 From: Christoph Hegemann Date: Tue, 27 Oct 2026 09:22:32 +0100 Subject: [PATCH] fix: expand enclosing_range for const/let lambdas to include their name --- snapshots/input/enclosing-ranges-ts/index.ts | 9 +++++++ snapshots/output/enclosing-ranges-ts/index.ts | 27 +++++++++++++++++++ snapshots/output/enclosing-ranges/range.js | 4 +-- src/FileIndexer.ts | 12 ++++++++- 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/snapshots/input/enclosing-ranges-ts/index.ts b/snapshots/input/enclosing-ranges-ts/index.ts index f0a46c22..0620373b 100644 --- a/snapshots/input/enclosing-ranges-ts/index.ts +++ b/snapshots/input/enclosing-ranges-ts/index.ts @@ -17,3 +17,12 @@ enum SimpleEnum { type SimpleTypeAlias = SimpleEnum type ComplexTypeAlias = Single> + +type PageLoad = () => Promise + +export const load: PageLoad = async () => { + return 42 +} + +const first = () => 1, + second = () => 2 diff --git a/snapshots/output/enclosing-ranges-ts/index.ts b/snapshots/output/enclosing-ranges-ts/index.ts index 803f49cb..c873ef65 100644 --- a/snapshots/output/enclosing-ranges-ts/index.ts +++ b/snapshots/output/enclosing-ranges-ts/index.ts @@ -49,4 +49,31 @@ type ComplexTypeAlias = Single> // ^ reference enclosing-ranges-ts 1.0.0 `index.ts`/ComplexTypeAlias#[T] // < end enclosing_range enclosing-ranges-ts 1.0.0 `index.ts`/ComplexTypeAlias# +// < start enclosing_range enclosing-ranges-ts 1.0.0 `index.ts`/PageLoad# +type PageLoad = () => Promise +// ^^^^^^^^ definition enclosing-ranges-ts 1.0.0 `index.ts`/PageLoad# +// ^^^^^^^ reference typescript 6.0.3 lib/`lib.es5.d.ts`/Promise# +// ^^^^^^^ reference typescript 6.0.3 lib/`lib.es2015.iterable.d.ts`/Promise# +// ^^^^^^^ reference typescript 6.0.3 lib/`lib.es2015.promise.d.ts`/Promise. +// ^^^^^^^ reference typescript 6.0.3 lib/`lib.es2015.symbol.wellknown.d.ts`/Promise# +// ^^^^^^^ reference typescript 6.0.3 lib/`lib.es2018.promise.d.ts`/Promise# +// < end enclosing_range enclosing-ranges-ts 1.0.0 `index.ts`/PageLoad# + +// < start enclosing_range enclosing-ranges-ts 1.0.0 `index.ts`/load. +export const load: PageLoad = async () => { +// ^^^^ definition enclosing-ranges-ts 1.0.0 `index.ts`/load. +// ^^^^^^^^ reference enclosing-ranges-ts 1.0.0 `index.ts`/PageLoad# + return 42 +} +// < end enclosing_range enclosing-ranges-ts 1.0.0 `index.ts`/load. + +// ⌄ start enclosing_range enclosing-ranges-ts 1.0.0 `index.ts`/first. +const first = () => 1, +// ^^^^^ definition enclosing-ranges-ts 1.0.0 `index.ts`/first. +// ^ end enclosing_range enclosing-ranges-ts 1.0.0 `index.ts`/first. +// ⌄ start enclosing_range enclosing-ranges-ts 1.0.0 `index.ts`/second. + second = () => 2 +//^^^^^^ definition enclosing-ranges-ts 1.0.0 `index.ts`/second. +// ^ end enclosing_range enclosing-ranges-ts 1.0.0 `index.ts`/second. + // < end enclosing_range enclosing-ranges-ts 1.0.0 `index.ts`/ diff --git a/snapshots/output/enclosing-ranges/range.js b/snapshots/output/enclosing-ranges/range.js index c42206e1..f7874988 100644 --- a/snapshots/output/enclosing-ranges/range.js +++ b/snapshots/output/enclosing-ranges/range.js @@ -3,8 +3,8 @@ // format-options: showRanges +// < start enclosing_range enclosing-ranges 0.0.1 `range.js`/test. // < start enclosing_range enclosing-ranges 0.0.1 `range.js`/ -// ⌄ start enclosing_range enclosing-ranges 0.0.1 `range.js`/test. const test = () => { // ^^^^ definition enclosing-ranges 0.0.1 `range.js`/test. const a = 'a' @@ -16,7 +16,7 @@ const test = () => { // ^ reference local 2 // ^ reference local 5 } -// ^ end enclosing_range enclosing-ranges 0.0.1 `range.js`/test. +// < end enclosing_range enclosing-ranges 0.0.1 `range.js`/test. // < start enclosing_range enclosing-ranges 0.0.1 `range.js`/test2(). function test2() { diff --git a/src/FileIndexer.ts b/src/FileIndexer.ts index 558e0f7a..27b14de9 100644 --- a/src/FileIndexer.ts +++ b/src/FileIndexer.ts @@ -239,7 +239,17 @@ export class FileIndexer { declaration.initializer && ts.isFunctionLike(declaration.initializer) ) { - enclosingRange = Range.fromNode(declaration.initializer).toLsif() + // Cover the whole `export const name = ...` statement so the range + // encloses the name. Fall back to the declaration itself when the + // statement has several declarations or there is no statement (e.g. + // a `for` loop initializer). + const statement = declaration.parent.parent + enclosingRange = Range.fromNode( + ts.isVariableStatement(statement) && + statement.declarationList.declarations.length === 1 + ? statement + : declaration + ).toLsif() } else if ( ts.isFunctionDeclaration(declaration) || ts.isEnumDeclaration(declaration) ||