@@ -7,6 +7,7 @@ import babelParser from 'prettier/parser-babel';
77import htmlParser from 'prettier/parser-html' ;
88import cssParser from 'prettier/parser-postcss' ;
99import { withTranslation } from 'react-i18next' ;
10+ import StackTrace from 'stacktrace-js' ;
1011import 'codemirror/mode/css/css' ;
1112import 'codemirror/addon/selection/active-line' ;
1213import 'codemirror/addon/lint/lint' ;
@@ -252,42 +253,25 @@ class Editor extends React.Component {
252253
253254 this . props . consoleEvents . forEach ( ( consoleEvent ) => {
254255 if ( consoleEvent . method === 'error' ) {
255- if ( consoleEvent . data && consoleEvent . data [ 0 ] ) {
256- let sourceAndLoc ;
257- if (
258- consoleEvent . data [ 0 ] . indexOf &&
259- consoleEvent . data [ 0 ] . indexOf ( ')' ) > - 1
260- ) {
261- sourceAndLoc = consoleEvent . data [ 0 ] // eslint-disable-line
262- . split ( '\n' ) [ 1 ]
263- . split ( '(' ) [ 1 ]
264- . split ( ')' ) [ 0 ] ;
265- } else if ( consoleEvent . data [ 0 ] . indexOf ( 'at ' ) > - 1 ) {
266- sourceAndLoc = consoleEvent . data [ 0 ] // eslint-disable-line
267- . split ( '\n' ) [ 1 ]
268- . split ( 'at ' ) [ 1 ] ;
269- } else {
256+ StackTrace . fromError ( new Error ( consoleEvent . data [ 0 ] ) ) . then (
257+ ( stackLines ) => {
270258 this . props . expandConsole ( ) ;
271- return ;
259+ const line = stackLines . find ( ( l ) => l . fileName . startsWith ( '/' ) ) ;
260+ if ( ! line ) return ;
261+ const fileNameArray = line . fileName . split ( '/' ) ;
262+ const fileName = fileNameArray . slice ( - 1 ) [ 0 ] ;
263+ const filePath = fileNameArray . slice ( 0 , - 1 ) . join ( '/' ) ;
264+ const fileWithError = this . props . files . find (
265+ ( f ) => f . name === fileName && f . filePath === filePath
266+ ) ;
267+ this . props . setSelectedFile ( fileWithError . id ) ;
268+ this . _cm . addLineClass (
269+ line . lineNumber - 1 ,
270+ 'background' ,
271+ 'line-runtime-error'
272+ ) ;
272273 }
273- const [ source , line ] = sourceAndLoc . split ( ':' ) ;
274-
275- // get the file that this message is coming from, and then select it
276- const sourceArray = source . split ( '/' ) ;
277- const fileName = sourceArray . slice ( - 1 ) [ 0 ] ;
278- const filePath = sourceArray . slice ( 0 , - 1 ) . join ( '/' ) ;
279- const fileWithError = this . props . files . find (
280- ( f ) => f . name === fileName && f . filePath === filePath
281- ) ;
282- this . props . setSelectedFile ( fileWithError . id ) ;
283- this . props . expandConsole ( ) ;
284- const lineNumber = parseInt ( line , 10 ) - 1 ;
285- this . _cm . addLineClass (
286- lineNumber ,
287- 'background' ,
288- 'line-runtime-error'
289- ) ;
290- }
274+ ) ;
291275 }
292276 } ) ;
293277 } else {
0 commit comments