Skip to content

Instantly share code, notes, and snippets.

@snakeUni
Forked from threepointone/console-function-names.js
Created November 29, 2019 03:58
Show Gist options
  • Save snakeUni/5b412f2cd48c70cb9113eeaa3d557a27 to your computer and use it in GitHub Desktop.
Save snakeUni/5b412f2cd48c70cb9113eeaa3d557a27 to your computer and use it in GitHub Desktop.

Revisions

  1. Sunil Pai created this gist Nov 28, 2019.
    57 changes: 57 additions & 0 deletions console-function-names.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    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)
    })
    );
    }
    }
    }
    };
    };