@@ -26,6 +26,25 @@ interface DartVersionResponse {
2626 flutterVersion ?: string ;
2727}
2828
29+ interface AnalysisIssue {
30+ kind : "error" | "warning" | "info" ;
31+ message : string ;
32+ location : {
33+ charStart : number ;
34+ charLength : number ;
35+ line : number ;
36+ column : number ;
37+ } ;
38+ code ?: string ;
39+ correction ?: string ;
40+ url ?: string ;
41+ }
42+
43+ interface AnalysisResponse {
44+ issues : AnalysisIssue [ ] ;
45+ imports ?: unknown [ ] ;
46+ }
47+
2948const versionFetcher = async ( url : string ) : Promise < DartVersionResponse > => {
3049 const res = await fetch ( url ) ;
3150 if ( ! res . ok ) {
@@ -74,6 +93,41 @@ export function DartProvider({ children }: { children: ReactNode }) {
7493 ) ;
7594}
7695
96+ async function performAnalysis (
97+ source : string ,
98+ onOutput : ( output : ReplOutput | UpdatedFile ) => void
99+ ) : Promise < void > {
100+ try {
101+ const res = await fetch ( `${ DART_PAD_API_BASE } /analyze` , {
102+ method : "POST" ,
103+ headers : {
104+ "Content-Type" : "application/json" ,
105+ } ,
106+ body : JSON . stringify ( { source } ) ,
107+ } ) ;
108+
109+ if ( ! res . ok ) return ;
110+
111+ const data : AnalysisResponse = await res . json ( ) ;
112+ if ( Array . isArray ( data . issues ) ) {
113+ for ( const issue of data . issues ) {
114+ const line = issue . location ?. line ?? 1 ;
115+ const column = issue . location ?. column ?? 1 ;
116+ const kindStr = ( issue . kind || "info" ) . toUpperCase ( ) ;
117+ const correctionStr = issue . correction ? ` (${ issue . correction } )` : "" ;
118+ const formattedMsg = `[ANALYZER ${ kindStr } ] line ${ line } :${ column } - ${ issue . message } ${ correctionStr } ` ;
119+
120+ onOutput ( {
121+ type : issue . kind === "error" ? "error" : "stderr" ,
122+ message : formattedMsg ,
123+ } ) ;
124+ }
125+ }
126+ } catch ( err ) {
127+ console . warn ( "Failed to perform Dart static analysis:" , err ) ;
128+ }
129+ }
130+
77131export function useDart ( ) : RuntimeContext {
78132 const { init : dartInit , ready, dartVersion } = useContext ( DartContext ) ;
79133 const onErrorRef = useRef < RuntimeErrorHandler | undefined > ( undefined ) ;
@@ -109,13 +163,16 @@ export function useDart(): RuntimeContext {
109163 }
110164
111165 try {
112- const response = await fetch ( `${ DART_PAD_API_BASE } /compileNewDDC` , {
113- method : "POST" ,
114- headers : {
115- "Content-Type" : "application/json" ,
116- } ,
117- body : JSON . stringify ( { source } ) ,
118- } ) ;
166+ const [ _ , response ] = await Promise . all ( [
167+ performAnalysis ( source , onOutput ) ,
168+ fetch ( `${ DART_PAD_API_BASE } /compileNewDDC` , {
169+ method : "POST" ,
170+ headers : {
171+ "Content-Type" : "application/json" ,
172+ } ,
173+ body : JSON . stringify ( { source } ) ,
174+ } ) ,
175+ ] ) ;
119176
120177 if ( ! response . ok ) {
121178 const errorText = await response . text ( ) ;
0 commit comments