fivetran / typescript-closure-tools

62 stars 11 forks source link

Error when trying to parse es6 js file with import statement #16

Open seveves opened 7 years ago

seveves commented 7 years ago

This is the error when parsing this file using "closure2ts Clustering.js Clustering.d.ts". Of course this happens with every file containing import statements :)

C:\Users\sevef\Code\vis\lib\network\modules>closure2ts Clustering.js Clustering.d.ts

C:\Users\sevef\AppData\Roaming\npm\node_modules\typescript-closure-tools\node_modules\esprima\dist\esprima.js:3385 throw error; ^ Error: Line 2: Unexpected token at ErrorHandler.constructError (C:\Users\sevef\AppData\Roaming\npm\node_modules\typescript-closure-tools\node_modules\esprima\dist\esprima.js:3390:22) at ErrorHandler.createError (C:\Users\sevef\AppData\Roaming\npm\node_modules\typescript-closure-tools\node_modules\esprima\dist\esprima.js:3408:27) at Parser.unexpectedTokenError (C:\Users\sevef\AppData\Roaming\npm\node_modules\typescript-closure-tools\node_modules\esprima\dist\esprima.js:542:39) at Parser.tolerateUnexpectedToken (C:\Users\sevef\AppData\Roaming\npm\node_modules\typescript-closure-tools\node_modules\esprima\dist\esprima.js:555:42) at Parser.parseStatementListItem (C:\Users\sevef\AppData\Roaming\npm\node_modules\typescript-closure-tools\node_modules\esprima\dist\esprima.js:1801:31) at Parser.parseProgram (C:\Users\sevef\AppData\Roaming\npm\node_modules\typescript-closure-tools\node_modules\esprima\dist\esprima.js:3054:29) at Object.parse (C:\Users\sevef\AppData\Roaming\npm\node_modules\typescript-closure-tools\node_modules\esprima\dist\esprima.js:117:24) at Object.jsdoc (C:\Users\sevef\AppData\Roaming\npm\node_modules\typescript-closure-tools\definition-generator\src\parser.js:412:24) at symbols (C:\Users\sevef\AppData\Roaming\npm\node_modules\typescript-closure-tools\definition-generator\src\finder.js:30:59) at C:\Users\sevef\AppData\Roaming\npm\node_modules\typescript-closure-tools\definition-generator\src\finder.js:70:21

seveves commented 7 years ago

I know this has something to do with the esprima parsing infrastructure and it seems it doesn't like the "import" and "export" statements but maybe you have experienced this issue before and can help me. Btw it works with the esprima online parser.

Otherwise I'm going to create an issue at the esprima project.

Kind regards

seveves commented 7 years ago

I've debugged the code and I think the problem is that there is a difference parsing a "script" or a "module". You have to set 'sourceType' to "script" or "module" in the esprima.parse options, f.e. (end of parser.js): var tree = esprima.parse(code, { attachComment: true, sourceType; 'script' });

When setting it to module my file will be parsed without any errors.

Do you want me to create a PR for this or do you fix it on your own?