5
5
* LICENSE file in the root directory of this source tree.
6
6
*/
7
7
8
- import { TextDocument } from 'vscode-languageserver-textdocument' ;
8
+ import { Position , TextDocument } from 'vscode-languageserver-textdocument' ;
9
9
import {
10
10
CodeLens ,
11
11
createConnection ,
@@ -24,6 +24,10 @@ import {
24
24
LoggerEvent ,
25
25
} from 'babel-plugin-react-compiler/src/Entrypoint/Options' ;
26
26
import { babelLocationToRange , getRangeFirstCharacter } from './compiler/compat' ;
27
+ import {
28
+ AutoDepsDecorationsLSPEvent ,
29
+ mapCompilerEventToLSPEvent ,
30
+ } from './custom-requests/autodepsdecorations' ;
27
31
28
32
const SUPPORTED_LANGUAGE_IDS = new Set ( [
29
33
'javascript' ,
@@ -37,17 +41,48 @@ const documents = new TextDocuments(TextDocument);
37
41
38
42
let compilerOptions : PluginOptions | null = null ;
39
43
let compiledFns : Set < CompileSuccessEvent > = new Set ( ) ;
44
+ let autoDepsDecorations : Array < AutoDepsDecorationsLSPEvent > = [ ] ;
40
45
41
46
connection . onInitialize ( ( _params : InitializeParams ) => {
42
47
// TODO(@poteto) get config fr
43
48
compilerOptions = resolveReactConfig ( '.' ) ?? defaultOptions ;
44
49
compilerOptions = {
45
50
...compilerOptions ,
51
+ environment : {
52
+ ...compilerOptions . environment ,
53
+ inferEffectDependencies : [
54
+ {
55
+ function : {
56
+ importSpecifierName : 'useEffect' ,
57
+ source : 'react' ,
58
+ } ,
59
+ numRequiredArgs : 1 ,
60
+ } ,
61
+ {
62
+ function : {
63
+ importSpecifierName : 'useSpecialEffect' ,
64
+ source : 'shared-runtime' ,
65
+ } ,
66
+ numRequiredArgs : 2 ,
67
+ } ,
68
+ {
69
+ function : {
70
+ importSpecifierName : 'default' ,
71
+ source : 'useEffectWrapper' ,
72
+ } ,
73
+ numRequiredArgs : 1 ,
74
+ } ,
75
+ ] ,
76
+ } ,
46
77
logger : {
47
78
logEvent ( _filename : string | null , event : LoggerEvent ) {
79
+ connection . console . info ( `Received event: ${ event . kind } ` ) ;
48
80
if ( event . kind === 'CompileSuccess' ) {
49
81
compiledFns . add ( event ) ;
50
82
}
83
+ if ( event . kind === 'AutoDepsDecorations' ) {
84
+ autoDepsDecorations . push ( mapCompilerEventToLSPEvent ( event ) ) ;
85
+ }
51
86
} ,
52
87
} ,
53
88
} ;
@@ -67,6 +102,7 @@ connection.onInitialized(() => {
67
102
documents . onDidChangeContent ( async event => {
68
103
connection . console . info ( `Changed: ${ event . document . uri } ` ) ;
69
104
compiledFns . clear ( ) ;
105
+ autoDepsDecorations = [ ] ;
70
106
if ( SUPPORTED_LANGUAGE_IDS . has ( event . document . languageId ) ) {
71
107
const text = event . document . getText ( ) ;
72
108
await compile ( {
@@ -79,6 +115,7 @@ documents.onDidChangeContent(async event => {
79
115
80
116
connection . onDidChangeWatchedFiles ( change => {
81
117
compiledFns . clear ( ) ;
118
+ autoDepsDecorations = [ ] ;
82
119
connection . console . log (
83
120
change . changes . map ( c => `File changed: ${ c . uri } ` ) . join ( '\n' ) ,
84
121
) ;
@@ -118,6 +155,25 @@ connection.onCodeLensResolve(lens => {
118
155
return lens ;
119
156
} ) ;
120
157
158
+ connection . onRequest ( 'react/autodepsdecorations' , ( position : Position ) => {
159
+ connection . console . log ( 'Client hovering on: ' + JSON . stringify ( position ) ) ;
160
+ connection . console . log ( JSON . stringify ( autoDepsDecorations , null , 2 ) ) ;
161
+
162
+ for ( const dec of autoDepsDecorations ) {
163
+ // TODO: extract to helper
164
+ if (
165
+ position . line >= dec . useEffectCallExpr [ 0 ] . line &&
166
+ position . line <= dec . useEffectCallExpr [ 1 ] . line
167
+ ) {
168
+ connection . console . log (
169
+ 'found decoration: ' + JSON . stringify ( dec . decorations ) ,
170
+ ) ;
171
+ return dec . decorations ;
172
+ }
173
+ }
174
+ return null ;
175
+ } ) ;
176
+
121
177
documents . listen ( connection ) ;
122
178
connection . listen ( ) ;
123
179
connection . console . info ( `React Analyzer running in node ${ process . version } ` ) ;
0 commit comments