Forked from threepointone/console-function-names.js
Created
November 28, 2019 12:55
-
-
Save AgtLucas/7a6d69d65399c09a0cdd2320e9436588 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const bypass = [ | |
// function names to avoid logging | |
]; | |
const collapsed = [ | |
// function names to groupCollapsed | |
]; | |
module.exports = function(babel) { | |
const { types: t } = babel; | |
const wrapFunctionBody = babel.template(`{ | |
try{ | |
console.group(NAME) | |
BODY | |
} | |
finally { | |
console.groupEnd(); | |
} | |
}`); | |
const wrapFunctionBodyCollapsed = babel.template(`{ | |
try{ | |
console.groupCollapsed(NAME) | |
BODY | |
} | |
finally { | |
console.groupEnd(); | |
} | |
}`); | |
return { | |
visitor: { | |
FunctionDeclaration(path) { | |
if (path.node.id && path.node.id.name) { | |
if (bypass.includes(path.node.id.name)) { | |
return; | |
} | |
if (collapsed.includes(path.node.id.name)) { | |
path.get("body").replaceWith( | |
wrapFunctionBodyCollapsed({ | |
BODY: path.node.body.body, | |
NAME: t.stringLiteral(path.node.id.name) | |
}) | |
); | |
return; | |
} | |
path.get("body").replaceWith( | |
wrapFunctionBody({ | |
BODY: path.node.body.body, | |
NAME: t.stringLiteral(path.node.id.name) | |
}) | |
); | |
} | |
} | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment