44 *--------------------------------------------------------------------------------------------*/
55
66import * as jsonc from 'jsonc-parser' ;
7- import { basename , posix } from 'path' ;
7+ import { posix } from 'path' ;
88import * as vscode from 'vscode' ;
99import { Utils } from 'vscode-uri' ;
1010import { coalesce } from '../utils/arrays' ;
@@ -42,19 +42,31 @@ class TsconfigLinkProvider implements vscode.DocumentLinkProvider {
4242 }
4343
4444 private getExtendsLink ( document : vscode . TextDocument , root : jsonc . Node ) : vscode . DocumentLink | undefined {
45- const extendsNode = jsonc . findNodeAtLocation ( root , [ 'extends' ] ) ;
46- if ( ! this . isPathValue ( extendsNode ) ) {
45+ const node = jsonc . findNodeAtLocation ( root , [ 'extends' ] ) ;
46+ return node && this . tryCreateTsConfigLink ( document , node ) ;
47+ }
48+
49+ private getReferencesLinks ( document : vscode . TextDocument , root : jsonc . Node ) {
50+ return mapChildren (
51+ jsonc . findNodeAtLocation ( root , [ 'references' ] ) ,
52+ child => {
53+ const pathNode = jsonc . findNodeAtLocation ( child , [ 'path' ] ) ;
54+ return pathNode && this . tryCreateTsConfigLink ( document , pathNode ) ;
55+ } ) ;
56+ }
57+
58+ private tryCreateTsConfigLink ( document : vscode . TextDocument , node : jsonc . Node ) : vscode . DocumentLink | undefined {
59+ if ( ! this . isPathValue ( node ) ) {
4760 return undefined ;
4861 }
4962
50- const extendsValue : string = extendsNode . value ;
5163 const args : OpenExtendsLinkCommandArgs = {
52- resourceUri : { ...document . uri . toJSON ( ) , $mid : undefined } , // Prevent VS Code from trying to transform the uri
53- extendsValue : extendsValue
64+ resourceUri : { ...document . uri . toJSON ( ) , $mid : undefined } ,
65+ extendsValue : node . value
5466 } ;
5567
5668 const link = new vscode . DocumentLink (
57- this . getRange ( document , extendsNode ) ,
69+ this . getRange ( document , node ) ,
5870 vscode . Uri . parse ( `command:${ openExtendsLinkCommandId } ?${ JSON . stringify ( args ) } ` ) ) ;
5971 link . tooltip = vscode . l10n . t ( "Follow link" ) ;
6072 return link ;
@@ -66,22 +78,6 @@ class TsconfigLinkProvider implements vscode.DocumentLinkProvider {
6678 child => this . pathNodeToLink ( document , child ) ) ;
6779 }
6880
69- private getReferencesLinks ( document : vscode . TextDocument , root : jsonc . Node ) {
70- return mapChildren (
71- jsonc . findNodeAtLocation ( root , [ 'references' ] ) ,
72- child => {
73- const pathNode = jsonc . findNodeAtLocation ( child , [ 'path' ] ) ;
74- if ( ! this . isPathValue ( pathNode ) ) {
75- return undefined ;
76- }
77-
78- return new vscode . DocumentLink ( this . getRange ( document , pathNode ) ,
79- basename ( pathNode . value ) . endsWith ( '.json' )
80- ? this . getFileTarget ( document , pathNode )
81- : this . getFolderTarget ( document , pathNode ) ) ;
82- } ) ;
83- }
84-
8581 private pathNodeToLink (
8682 document : vscode . TextDocument ,
8783 node : jsonc . Node | undefined
@@ -91,21 +87,17 @@ class TsconfigLinkProvider implements vscode.DocumentLinkProvider {
9187 : undefined ;
9288 }
9389
94- private isPathValue ( extendsNode : jsonc . Node | undefined ) : extendsNode is jsonc . Node {
95- return extendsNode
96- && extendsNode . type === 'string'
97- && extendsNode . value
98- && ! ( extendsNode . value as string ) . includes ( '*' ) ; // don't treat globs as links.
90+ private isPathValue ( node : jsonc . Node | undefined ) : node is jsonc . Node {
91+ return node
92+ && node . type === 'string'
93+ && node . value
94+ && ! ( node . value as string ) . includes ( '*' ) ; // don't treat globs as links.
9995 }
10096
10197 private getFileTarget ( document : vscode . TextDocument , node : jsonc . Node ) : vscode . Uri {
10298 return vscode . Uri . joinPath ( Utils . dirname ( document . uri ) , node . value ) ;
10399 }
104100
105- private getFolderTarget ( document : vscode . TextDocument , node : jsonc . Node ) : vscode . Uri {
106- return vscode . Uri . joinPath ( Utils . dirname ( document . uri ) , node . value , 'tsconfig.json' ) ;
107- }
108-
109101 private getRange ( document : vscode . TextDocument , node : jsonc . Node ) {
110102 const offset = node . offset ;
111103 const start = document . positionAt ( offset + 1 ) ;
@@ -156,7 +148,7 @@ async function resolveNodeModulesPath(baseDirUri: vscode.Uri, pathCandidates: st
156148/**
157149* @returns Returns undefined in case of lack of result while trying to resolve from node_modules
158150*/
159- async function getTsconfigPath ( baseDirUri : vscode . Uri , extendsValue : string ) : Promise < vscode . Uri | undefined > {
151+ async function getTsconfigPath ( baseDirUri : vscode . Uri , pathValue : string ) : Promise < vscode . Uri | undefined > {
160152 async function resolve ( absolutePath : vscode . Uri ) : Promise < vscode . Uri > {
161153 if ( absolutePath . path . endsWith ( '.json' ) || await exists ( absolutePath ) ) {
162154 return absolutePath ;
@@ -166,21 +158,21 @@ async function getTsconfigPath(baseDirUri: vscode.Uri, extendsValue: string): Pr
166158 } ) ;
167159 }
168160
169- const isRelativePath = [ './' , '../' ] . some ( str => extendsValue . startsWith ( str ) ) ;
161+ const isRelativePath = [ './' , '../' ] . some ( str => pathValue . startsWith ( str ) ) ;
170162 if ( isRelativePath ) {
171- return resolve ( vscode . Uri . joinPath ( baseDirUri , extendsValue ) ) ;
163+ return resolve ( vscode . Uri . joinPath ( baseDirUri , pathValue ) ) ;
172164 }
173165
174- if ( extendsValue . startsWith ( '/' ) || looksLikeAbsoluteWindowsPath ( extendsValue ) ) {
175- return resolve ( vscode . Uri . file ( extendsValue ) ) ;
166+ if ( pathValue . startsWith ( '/' ) || looksLikeAbsoluteWindowsPath ( pathValue ) ) {
167+ return resolve ( vscode . Uri . file ( pathValue ) ) ;
176168 }
177169
178170 // Otherwise resolve like a module
179171 return resolveNodeModulesPath ( baseDirUri , [
180- extendsValue ,
181- ...extendsValue . endsWith ( '.json' ) ? [ ] : [
182- `${ extendsValue } .json` ,
183- `${ extendsValue } /tsconfig.json` ,
172+ pathValue ,
173+ ...pathValue . endsWith ( '.json' ) ? [ ] : [
174+ `${ pathValue } .json` ,
175+ `${ pathValue } /tsconfig.json` ,
184176 ]
185177 ] ) ;
186178}
0 commit comments