Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions workers/javascript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,11 @@ export default class JavascriptEventWorker extends EventWorker {
let isAsync = false;

try {
// @todo choose plugins based on source code file extention (related to possible jsx parser usage in future)
const ast = parse(sourceCode, {
sourceType: 'module',
plugins: [
'typescript',
'jsx',
'classProperties',
'decorators',
'optionalChaining',
Expand All @@ -282,9 +282,9 @@ export default class JavascriptEventWorker extends EventWorker {
* @param path
*/
ClassDeclaration(path) {
console.log(`class declaration: loc: ${path.node.loc}, line: ${line}, node.start.line: ${path.node.loc.start.line}, node.end.line: ${path.node.loc.end.line}`);

if (path.node.loc && path.node.loc.start.line <= line && path.node.loc.end.line >= line) {
console.log(`class declaration: loc: ${path.node.loc}, line: ${line}, node.start.line: ${path.node.loc.start.line}, node.end.line: ${path.node.loc.end.line}`);

className = path.node.id.name || null;
}
},
Expand All @@ -295,9 +295,9 @@ export default class JavascriptEventWorker extends EventWorker {
* @param path
*/
ClassMethod(path) {
console.log(`class declaration: loc: ${path.node.loc}, line: ${line}, node.start.line: ${path.node.loc.start.line}, node.end.line: ${path.node.loc.end.line}`);

if (path.node.loc && path.node.loc.start.line <= line && path.node.loc.end.line >= line) {
console.log(`class declaration: loc: ${path.node.loc}, line: ${line}, node.start.line: ${path.node.loc.start.line}, node.end.line: ${path.node.loc.end.line}`);

// Handle different key types
if (path.node.key.type === 'Identifier') {
functionName = path.node.key.name;
Expand All @@ -311,9 +311,9 @@ export default class JavascriptEventWorker extends EventWorker {
* @param path
*/
FunctionDeclaration(path) {
console.log(`function declaration: loc: ${path.node.loc}, line: ${line}, node.start.line: ${path.node.loc.start.line}, node.end.line: ${path.node.loc.end.line}`);

if (path.node.loc && path.node.loc.start.line <= line && path.node.loc.end.line >= line) {
console.log(`function declaration: loc: ${path.node.loc}, line: ${line}, node.start.line: ${path.node.loc.start.line}, node.end.line: ${path.node.loc.end.line}`);

functionName = path.node.id.name || null;
isAsync = path.node.async;
}
Expand All @@ -324,15 +324,15 @@ export default class JavascriptEventWorker extends EventWorker {
* @param path
*/
VariableDeclarator(path) {
console.log(`variable declaration: node.type: ${path.node.init.type}, line: ${line}, `);

if (
path.node.init &&
(path.node.init.type === 'FunctionExpression' || path.node.init.type === 'ArrowFunctionExpression') &&
path.node.loc &&
path.node.loc.start.line <= line &&
path.node.loc.end.line >= line
) {
console.log(`variable declaration: node.type: ${path.node.init.type}, line: ${line}, `);

// Handle different id types
if (path.node.id.type === 'Identifier') {
functionName = path.node.id.name;
Expand All @@ -341,8 +341,11 @@ export default class JavascriptEventWorker extends EventWorker {
}
},
});
} catch (e) {
console.error(`Failed to parse source code: ${e.message}`);
} catch (traverseError) {
console.error(`Failed to parse source code:`);
console.error(traverseError);

HawkCatcher.send(traverseError);
}

return functionName ? `${isAsync ? 'async ' : ''}${className ? `${className}.` : ''}${functionName}` : null;
Expand Down
Loading