@@ -16,15 +16,16 @@ function createCompilerHost(
1616 cache : GlobalCache ,
1717 compilerOptions : ts . CompilerOptions ,
1818 projectOptions : ProjectOptions ,
19- hasSvelte : boolean
19+ hasSvelte : boolean ,
20+ sourceInfos : Map < ts . SourceFile , SourceInfo >
2021) : ts . CompilerHost {
2122 const host = ts . createCompilerHost ( compilerOptions )
2223 if ( ! hasSvelte && ! projectOptions . globalCaches ) {
2324 return host
2425 }
2526 const hostCopy = { ...host }
2627 const svelte = hasSvelte
27- ? new SvelteSupport ( hostCopy , compilerOptions , cache . sourceInfos )
28+ ? new SvelteSupport ( hostCopy , compilerOptions , sourceInfos )
2829 : undefined
2930 if ( svelte ) {
3031 host . fileExists = fileName => svelte . fileExists ( fileName )
@@ -118,7 +119,16 @@ export class ProjectIndexer {
118119 cache : GlobalCache
119120 ) {
120121 const hasSvelte = config . fileNames . some ( isSvelteFile )
121- const host = createCompilerHost ( cache , config . options , options , hasSvelte )
122+ const sourceInfos = options . globalCaches
123+ ? cache . sourceInfos
124+ : new Map < ts . SourceFile , SourceInfo > ( )
125+ const host = createCompilerHost (
126+ cache ,
127+ config . options ,
128+ options ,
129+ hasSvelte ,
130+ sourceInfos
131+ )
122132 const rootNames = hasSvelte
123133 ? [
124134 ...config . fileNames ,
@@ -132,7 +142,7 @@ export class ProjectIndexer {
132142 this . checker = this . program . getTypeChecker ( )
133143 this . packages = new Packages ( options . projectRoot )
134144 this . indexedFiles = cache . indexedFiles
135- this . sourceInfos = cache . sourceInfos
145+ this . sourceInfos = sourceInfos
136146 }
137147 public index ( ) : void {
138148 const startTimestamp = Date . now ( )
@@ -261,13 +271,38 @@ export function languageForFileName(fileName: string): string | undefined {
261271 return undefined
262272}
263273
264- function deduplicateOccurrences ( document : scip . scip . Document ) : void {
274+ export function deduplicateOccurrences ( document : scip . scip . Document ) : void {
265275 const occurrences = new Map < string , scip . scip . Occurrence > ( )
266276 for ( const occurrence of document . occurrences ) {
267277 const key = `${ occurrence . range . join ( ':' ) } ${ occurrence . symbol } `
268278 const existing = occurrences . get ( key )
269279 if ( existing ) {
270- existing . symbol_roles |= occurrence . symbol_roles
280+ const symbolRoles = existing . symbol_roles | occurrence . symbol_roles
281+ const existingIsDefinition =
282+ ( existing . symbol_roles & scip . scip . SymbolRole . Definition ) !== 0
283+ const occurrenceIsDefinition =
284+ ( occurrence . symbol_roles & scip . scip . SymbolRole . Definition ) !== 0
285+ // svelte2tsx can map a generated reference and definition to the same
286+ // source range. Keep the definition as the survivor because it carries
287+ // the enclosing range and diagnostics associated with the declaration.
288+ if ( occurrenceIsDefinition && ! existingIsDefinition ) {
289+ occurrence . symbol_roles = symbolRoles
290+ if ( occurrence . enclosing_range . length === 0 ) {
291+ occurrence . enclosing_range = existing . enclosing_range
292+ }
293+ if ( occurrence . diagnostics . length === 0 ) {
294+ occurrence . diagnostics = existing . diagnostics
295+ }
296+ occurrences . set ( key , occurrence )
297+ } else {
298+ existing . symbol_roles = symbolRoles
299+ if ( existing . enclosing_range . length === 0 ) {
300+ existing . enclosing_range = occurrence . enclosing_range
301+ }
302+ if ( existing . diagnostics . length === 0 ) {
303+ existing . diagnostics = occurrence . diagnostics
304+ }
305+ }
271306 } else {
272307 occurrences . set ( key , occurrence )
273308 }
0 commit comments