microsoft / dts-gen

dts-gen creates starter TypeScript definition files for any module or library.
MIT License
2.43k stars 102 forks source link

unexpected crash -- message requested a bug #7

Open BurtHarris opened 8 years ago

BurtHarris commented 8 years ago

First thing I tried generated a crash::

npm install -g dts-gen
npm install -g antlr4
dts-gen -m antlr4

Versions:

dts-gen@0.4.16
antlr4@4.5.3
node v6.3.1
npm v3.10.6

Results:

Unexpected crash! Please log a bug with the commandline you specified.
C:\Users\Burt_\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\run.js:107
        throw e;
        ^

TypeError: Cannot read property 'sourceName' of undefined
    at Lexer.sourceName (C:\Users\Burt_\AppData\Roaming\npm\node_modules\antlr4\Lexer.js:240:21)
    at C:\Users\Burt_\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\index.js:272:143
    at Array.map (native)
    at getClassPrototypeMembers (C:\Users\Burt_\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\index.js:272:75)
    at getResult (C:\Users\Burt_\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\index.js:138:17)
    at getTopLevelDeclarations (C:\Users\Burt_\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\index.js:126:15)
    at getResult (C:\Users\Burt_\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\index.js:198:33)
    at getTopLevelDeclarations (C:\Users\Burt_\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\index.js:126:15)
    at Object.generateModuleDeclarationFile (C:\Users\Burt_\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\index.js:49:17)
    at Object.<anonymous> (C:\Users\Burt_\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\run.js:52:24)
BurtHarris commented 8 years ago

I note that the file it's processing (Lexer.js) is effectively an abstract class with a mandatory constructor parameter which sets _input to an instance of another class. At Lexer.js line 240, it's trying to retrieve a property from the object that should have been passed into the constructor (but wasn't).

RyanCavanaugh commented 8 years ago

Thanks for the report. There are some libraries that will throw on certain property reads (sigh!) so we need to try/catch everywhere when probing fields.

BurtHarris commented 8 years ago

Yea, that's the case for sure. But (in addition to trapping) it seems like there should be a way giving the tool more guidance on how the library is to be used. In the case of ANTLR case there's a few lines of common usage constructor invocation that might helpful to inject into the tool

   var input = "your text to parse here"
   var chars = new antlr4.InputStream(input);
   var lexer = new MyGrammarLexer.MyGrammarLexer(chars);    // Derived from antlr4.Lexer
   var tokens  = new antlr4.CommonTokenStream(lexer);
   var parser = new MyGrammarParser.MyGrammarParser(tokens); // Derived from antlr4.Parser
   ...
RyanCavanaugh commented 8 years ago

Yea, I plan to expose this with a more friendly API as a library so you can just write that code in the node REPL and then write something like require('dts-gen').generate(obj, 'Parser').emit('dts');