@@ -124,6 +124,65 @@ export function fileUrlToPathCrossPlatform(fileUrl: url.URL | string): string {
124124 return / ^ \/ [ A - Z a - z ] : \/ / . test ( path ) ? path . substring ( 1 ) : path ;
125125}
126126
127+ /**
128+ * Returns a pretty URL similar to Dart's `path.prettyUri`.
129+ */
130+ export function prettyUrl ( url : string ) : string {
131+ if ( ! url . startsWith ( 'file:' ) ) return url ;
132+
133+ const absolutePath = fileUrlToPathCrossPlatform ( url ) ;
134+ const relativePath = p . relative ( process . cwd ( ) , absolutePath ) ;
135+ return relativePath . split ( p . sep ) . length > absolutePath . split ( p . sep ) . length
136+ ? absolutePath
137+ : relativePath ;
138+ }
139+
140+ /**
141+ * Reformat URLs in formatted text from the embedded compiler.
142+ */
143+ export function prettyFormatted ( formatted : string , stack : string ) : string {
144+ let longest = 0 ;
145+
146+ const frames = stack
147+ . split ( '\n' )
148+ . filter ( frame => frame !== '' )
149+ . map ( frame => {
150+ let [ location , member ] = frame . split ( / + / , 2 ) ;
151+ let [ url , lineColumn ] = location . split ( ' ' , 2 ) ;
152+
153+ url = prettyUrl ( url ) ;
154+ location = lineColumn === undefined ? url : `${ url } ${ lineColumn } ` ;
155+
156+ if ( location . length > longest ) {
157+ longest = location . length ;
158+ }
159+ return {
160+ frame,
161+ location,
162+ member,
163+ } ;
164+ } ) ;
165+
166+ let offset = formatted . length ;
167+
168+ frames . reverse ( ) . forEach ( ( { frame, location, member} ) => {
169+ const index = formatted . lastIndexOf ( frame , offset ) ;
170+ if ( index !== - 1 ) {
171+ offset = index ;
172+
173+ const replacement = `${ location . padEnd ( longest ) } ${ member } ` ;
174+ if ( frame !== replacement ) {
175+ formatted =
176+ formatted . slice ( 0 , index ) +
177+ replacement +
178+ formatted . slice ( index + frame . length ) ;
179+ }
180+ }
181+ } ) ;
182+
183+ return formatted ;
184+ }
185+
127186/** Returns `path` without an extension, if it had one. */
128187export function withoutExtension ( path : string ) : string {
129188 const extension = p . extname ( path ) ;
0 commit comments