ここでの回答の多くは正規表現を使用していますが、これは問題ありませんが、言語への新しい追加(矢印関数やクラスなど)をうまく処理しません。また、これらの関数のいずれかを縮小コードで使用すると、それが実行されることにも注意してください。縮小名が何であれ使用します。Angularは、DIコンテナーに引数を登録するときに、引数の順序と一致する文字列の順序付けされた配列を渡すことができるようにすることで、これを回避します。だからソリューションで:
var esprima = require('esprima');
var _ = require('lodash');
const parseFunctionArguments = (func) => {
    // allows us to access properties that may or may not exist without throwing 
    // TypeError: Cannot set property 'x' of undefined
    const maybe = (x) => (x || {});
    // handle conversion to string and then to JSON AST
    const functionAsString = func.toString();
    const tree = esprima.parse(functionAsString);
    console.log(JSON.stringify(tree, null, 4))
    // We need to figure out where the main params are. Stupid arrow functions 👊
    const isArrowExpression = (maybe(_.first(tree.body)).type == 'ExpressionStatement');
    const params = isArrowExpression ? maybe(maybe(_.first(tree.body)).expression).params 
                                     : maybe(_.first(tree.body)).params;
    // extract out the param names from the JSON AST
    return _.map(params, 'name');
};
これは、元の解析の問題といくつかの関数タイプ(矢印関数など)を処理します。これがそのまま処理できるものと処理できないものの考え方です。
// I usually use mocha as the test runner and chai as the assertion library
describe('Extracts argument names from function signature. 💪', () => {
    const test = (func) => {
        const expectation = ['it', 'parses', 'me'];
        const result = parseFunctionArguments(toBeParsed);
        result.should.equal(expectation);
    } 
    it('Parses a function declaration.', () => {
        function toBeParsed(it, parses, me){};
        test(toBeParsed);
    });
    it('Parses a functional expression.', () => {
        const toBeParsed = function(it, parses, me){};
        test(toBeParsed);
    });
    it('Parses an arrow function', () => {
        const toBeParsed = (it, parses, me) => {};
        test(toBeParsed);
    });
    // ================= cases not currently handled ========================
    // It blows up on this type of messing. TBH if you do this it deserves to 
    // fail 😋 On a tech note the params are pulled down in the function similar 
    // to how destructuring is handled by the ast.
    it('Parses complex default params', () => {
        function toBeParsed(it=4*(5/3), parses, me) {}
        test(toBeParsed);
    });
    // This passes back ['_ref'] as the params of the function. The _ref is a 
    // pointer to an VariableDeclarator where the ✨🦄 happens.
    it('Parses object destructuring param definitions.' () => {
        function toBeParsed ({it, parses, me}){}
        test(toBeParsed);
    });
    it('Parses object destructuring param definitions.' () => {
        function toBeParsed ([it, parses, me]){}
        test(toBeParsed);
    });
    // Classes while similar from an end result point of view to function
    // declarations are handled completely differently in the JS AST. 
    it('Parses a class constructor when passed through', () => {
        class ToBeParsed {
            constructor(it, parses, me) {}
        }
        test(ToBeParsed);
    });
});
ES6プロキシに何を使用したいかによって、デストラクタが最善の策となる場合があります。たとえば、依存関係の注入に(パラメーターの名前を使用して)使用したい場合は、次のように実行できます。
class GuiceJs {
    constructor() {
        this.modules = {}
    }
    resolve(name) {
        return this.getInjector()(this.modules[name]);
    }
    addModule(name, module) {
        this.modules[name] = module;
    }
    getInjector() {
        var container = this;
        return (klass) => {
            console.log(klass);
            var paramParser = new Proxy({}, {
                // The `get` handler is invoked whenever a get-call for
                // `injector.*` is made. We make a call to an external service
                // to actually hand back in the configured service. The proxy
                // allows us to bypass parsing the function params using
                // taditional regex or even the newer parser.
                get: (target, name) => container.resolve(name),
                // You shouldn't be able to set values on the injector.
                set: (target, name, value) => {
                    throw new Error(`Don't try to set ${name}! 😑`);
                }
            })
            return new klass(paramParser);
        }
    }
}
最先端のリゾルバーではありませんが、単純なDIにargsパーサーを使用する場合に、プロキシーを使用してそれを処理する方法についてのアイデアを提供します。ただし、このアプローチには1つ注意点があります。通常のパラメータの代わりに、構造化代入を使用する必要があります。インジェクタプロキシを渡すと、構造化はオブジェクトのゲッターを呼び出すのと同じです。
class App {
   constructor({tweeter, timeline}) {
        this.tweeter = tweeter;
        this.timeline = timeline;
    }
}
class HttpClient {}
class TwitterApi {
    constructor({client}) {
        this.client = client;
    }
}
class Timeline {
    constructor({api}) {
        this.api = api;
    }
}
class Tweeter {
    constructor({api}) {
        this.api = api;
    }
}
// Ok so now for the business end of the injector!
const di = new GuiceJs();
di.addModule('client', HttpClient);
di.addModule('api', TwitterApi);
di.addModule('tweeter', Tweeter);
di.addModule('timeline', Timeline);
di.addModule('app', App);
var app = di.resolve('app');
console.log(JSON.stringify(app, null, 4));
これは以下を出力します:
{
    "tweeter": {
        "api": {
            "client": {}
        }
    },
    "timeline": {
        "api": {
            "client": {}
        }
    }
}
その全体のアプリケーションを配線しました。最善の点は、アプリのテストが簡単であることです(各クラスをインスタンス化して、モック/スタブなどに渡すことができます)。また、実装を入れ替える必要がある場合は、単一の場所から行うことができます。JSプロキシオブジェクトにより、これらすべてが可能です。
注:本番環境で使用する前に、これまでに行う必要のある多くの作業がありますが、実際にどのように見えるかはわかります。
答えは少し遅いですが、同じことを考えている他の人を助けるかもしれません。👍