-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeError: Cannot read property 'some' of undefined when ast.comments
is missing on the AST
#842
Comments
What code generates this error? |
@ljharb I'm still investigating, but for some reason |
If that's a guarantee that other tools provide, then it might be a bug with |
@ljharb OK, let's keep it as is then, no change required. I have accidentally fed up JavaScript code to All that because while waiting for #839 to be merged and published I'm trying to detect TypeScript code by file contents, and my naive detection failed. |
I'd recommend using the extension, since the sole purpose of file extensions is to tell you how to parse a file :-) |
I just hit this same issue with |
@samhh Depends on what you're trying to do. The #839 makes it possible for me to provide ESLint with a custom parser which can parse both TypeScript and JavaScript while Babel/Babylon is lagging behind: // ./webpack/parserForEslint.js
function shouldUseTypeScriptParser(text, parserOptions) {
return isTypeScriptFilePath(parserOptions.filePath);
}
// ...
module.exports = {
parse: function (text, parserOptions) {
if (shouldUseTypeScriptParser(text, parserOptions)) {
return parseTypeScript(text, parserOptions);
}
return parseJavaScript(text, parserOptions);
},
}; // .eslintrc.js
// ...
"parser": "./webpack/parserForEslint.js",
// ... |
I had this issue when accidentally trying to import a JS file in a TS file. |
@mattdell Yes, looks like typescript-eslint-parser cannot parse JS (only TS), and that's fine. You'll have to do the same as I did for a JS+TS project: a conditional parser that decides which real parser to use. This conditional parser requires filePath in parserOptions to reliably guess the type if the code it's been given for parsing. But eslint-plugin-import doesn't provide this yet, hence #839. |
hmm got the same error, using i was thinking that
|
@piecyk Because either I missed this |
Was looking on this issue because after upgrading to |
oki problem was that i didn't set that option is passed to |
@piecyk The thing is that the You're saying
|
@sompylasar hmm based on so now when eslint-plugin-import run with espree ( as default ) will run with strange 🤔 hard to say what is best approach here because don't grasp exact difference between |
@piecyk The difference is (if you know what AST looks like), in pseudo code: // comment
const ast = espree.parse(...);
ast.comments // -> array // attachComment
const ast = espree.parse(...);
ast.body[0].leadingComments // -> array
ast.body[0].trailingComments // -> array
types.visit(ast, {
visitFunctionExpression: (path) => {
const node = path.value;
node.leadingComments // -> array
node.trailingComments // -> array
},
}) I have no time or a repro right now to track down the issue, sorry. If you have the repro of the error and the curiosity, please find that out. |
@sompylasar thanks 👍 will check that out ( and read more about AST internals ) from quick look, basic so this https://github.com/benmosher/eslint-plugin-import/blob/master/src/ExportMap.js#L182 |
@piecyk The But the So in fact CC @ljharb P.S. Please link to commits, not branches (i.e. |
@piecyk I realized why I need a conditional parser, and why |
Got this error using TypeScript 2.4.1, eslint-plugin-import 2.7.0, eslint-plugin-typescript 0.1.0, and typescript-eslint-parser 3.0.0. I'm having a hard time understanding what the fix is. I have this in my ESLint config: settings: {
'import/parsers': {
'typescript-eslint-parser': ['.ts', '.tsx'],
},
} Can anyone provide a conclusion of what the fix is? |
The issue with the |
Is there any workaround? I seemed to have some success with setting |
@OliverJAsh Yes, setting |
The fix appears to be pretty trivial, unless there's something else going on. Can I just submit a PR with You can reproduce the issue with: https://github.com/alexgorbatchev/typescript-module-boilerplate/tree/0cc9383c82de065b2a9f5f2d487004a28d97b30f and by running |
ping ☝️ |
Might as well submit it :-) |
Done ☝️ |
Would really like to see this published. This error makes the whole experience of linting in editor like VSCode completely broken because plugin exceptions end up as alerts. So any time I save and linter runs, i get exceptions. Had to disable linting for |
https://github.com/benmosher/eslint-plugin-import/blob/1377f55bc3d003519a74a89b048e11b20d53990e/src/ExportMap.js#L333
The text was updated successfully, but these errors were encountered: