@@ -10,10 +10,8 @@ import { JavaScriptEventWorkerTask } from '../types/javascript-event-worker-task
1010import { BeautifyBacktracePayload } from '../types/beautify-backtrace-payload' ;
1111import HawkCatcher from '@hawk.so/nodejs' ;
1212import { BacktraceFrame , CatcherMessagePayload , CatcherMessageType , ErrorsCatcherType , SourceCodeLine , SourceMapDataExtended } from '@hawk.so/types' ;
13- import { beautifyUserAgent , getBabelParserPluginsForFile , extractScriptFromSFC } from './utils' ;
13+ import { beautifyUserAgent , getBabelParserPluginsForFile , extractScriptFromSFC , getFunctionContext } from './utils' ;
1414import { Collection } from 'mongodb' ;
15- import { parse } from '@babel/parser' ;
16- import traverse from '@babel/traverse' ;
1715/* eslint-disable-next-line no-unused-vars */
1816import { memoize } from '../../../lib/memoize' ;
1917
@@ -234,7 +232,7 @@ export default class JavascriptEventWorker extends EventWorker {
234232
235233 const originalContent = consumer . sourceContentFor ( originalLocation . source ) ;
236234
237- functionContext = await this . getFunctionContext (
235+ functionContext = await getFunctionContext (
238236 originalContent ,
239237 originalLocation . line ,
240238 originalLocation . source
@@ -255,118 +253,6 @@ export default class JavascriptEventWorker extends EventWorker {
255253 } ) as BacktraceFrame ;
256254 }
257255
258- /**
259- * Method that is used to parse full function context of the code position
260- *
261- * @param sourceCode - content of the source file
262- * @param line - number of the line from the stack trace
263- * @param sourcePath - original source path from the source map (used to pick parser plugins)
264- * @returns {string | null } - string of the function context or null if it could not be parsed
265- */
266- private getFunctionContext ( sourceCode : string , line : number , sourcePath ?: string ) : string | null {
267- if ( ! sourceCode ) {
268- return null ;
269- }
270-
271- const {
272- code : codeToParse ,
273- targetLine,
274- hasTypeScriptLang,
275- } = extractScriptFromSFC ( sourceCode , line , sourcePath ) ;
276-
277- let functionName : string | null = null ;
278- let className : string | null = null ;
279- let isAsync = false ;
280-
281- try {
282- const parserPlugins = getBabelParserPluginsForFile ( sourcePath , hasTypeScriptLang ) ;
283-
284- const ast = parse ( codeToParse , {
285- sourceType : 'module' ,
286- plugins : parserPlugins ,
287- } ) ;
288-
289- traverse ( ast as any , {
290- /**
291- * It is used to get class decorator of the position, it will save class that is related to original position
292- *
293- * @param path
294- */
295- ClassDeclaration ( path ) {
296- if ( path . node . loc && path . node . loc . start . line <= targetLine && path . node . loc . end . line >= targetLine ) {
297- console . log ( `class declaration: loc: ${ path . node . loc } , targetLine: ${ targetLine } , node.start.line: ${ path . node . loc . start . line } , node.end.line: ${ path . node . loc . end . line } ` ) ;
298-
299- className = path . node . id . name || null ;
300- }
301- } ,
302- /**
303- * It is used to get class and its method decorator of the position
304- * It will save class and method, that are related to original position
305- *
306- * @param path
307- */
308- ClassMethod ( path ) {
309- if ( path . node . loc && path . node . loc . start . line <= targetLine && path . node . loc . end . line >= targetLine ) {
310- console . log ( `class declaration: loc: ${ path . node . loc } , targetLine: ${ targetLine } , node.start.line: ${ path . node . loc . start . line } , node.end.line: ${ path . node . loc . end . line } ` ) ;
311-
312- // Handle different key types
313- if ( path . node . key . type === 'Identifier' ) {
314- functionName = path . node . key . name ;
315- }
316- isAsync = path . node . async ;
317- }
318- } ,
319- /**
320- * It is used to get function name that is declared out of class
321- *
322- * @param path
323- */
324- FunctionDeclaration ( path ) {
325- if ( path . node . loc && path . node . loc . start . line <= targetLine && path . node . loc . end . line >= targetLine ) {
326- console . log ( `function declaration: loc: ${ path . node . loc } , targetLine: ${ targetLine } , node.start.line: ${ path . node . loc . start . line } , node.end.line: ${ path . node . loc . end . line } ` ) ;
327-
328- functionName = path . node . id . name || null ;
329- isAsync = path . node . async ;
330- }
331- } ,
332- /**
333- * It is used to get anonimous function names in function expressions or arrow function expressions
334- *
335- * @param path
336- */
337- VariableDeclarator ( path ) {
338- if (
339- path . node . init &&
340- ( path . node . init . type === 'FunctionExpression' || path . node . init . type === 'ArrowFunctionExpression' ) &&
341- path . node . loc &&
342- path . node . loc . start . line <= targetLine &&
343- path . node . loc . end . line >= targetLine
344- ) {
345- console . log ( `variable declaration: node.type: ${ path . node . init . type } , targetLine: ${ targetLine } , ` ) ;
346-
347- // Handle different id types
348- if ( path . node . id . type === 'Identifier' ) {
349- functionName = path . node . id . name ;
350- }
351- isAsync = ( path . node . init as any ) . async ;
352- }
353- } ,
354- } ) ;
355- } catch ( traverseError ) {
356- console . error ( `Failed to parse source code:` ) ;
357- console . error ( traverseError ) ;
358-
359- HawkCatcher . send ( traverseError , {
360- sourceCode : codeToParse ,
361- targetLine,
362- hasTypeScriptLang,
363- sourcePath,
364- } ) ;
365- }
366-
367- return functionName ? `${ isAsync ? 'async ' : '' } ${ className ? `${ className } .` : '' } ${ functionName } ` : null ;
368- }
369-
370256 /**
371257 * Downloads source map file from Grid FS
372258 *
0 commit comments