-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
no-unused-modules does not work with shebangs #1369
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
Comments
It doesn't really make any sense to have an export in the same file as a shebang; overwhelming convention is to separate the library part from the bin part of a package. That said, we should be able to strip the shebang. |
Yes I agree. I only added an However even without that |
It's not the rule having a problem with the shebang, it's the default parser used by ESLint: Switching to another parser (e.g. babel-eslint or @typescript-eslint/parser solves this problem in my test case. Can you please verify it also fixes the error in your project, @ehmicky |
Yes it does, thanks! However I think some users might not consider switching ESLint parser to get this rule to work. Not sure if this is solvable by |
@ehmicky great to hear, thanks for the feedback. I just had a look at We should do the same for |
Shebangs will soon become a regular part of the language, at which point eslint will parse then. If the file doesn’t parse, these rules should probably bail out. |
Bailing out is what already happens. The rule basically fails silently, unfortunately for the wrong file, namely the importing file, not the exporting file. I think, telling the user that a parsing error occurred at least lets the user know that something unexpected happened. In addition to returning the parsing error as a lint error, I would also explain this error on the readme. What do you think about that? |
As long as there's a workaround besides "remove the shebang" or "change the parser". |
I don't think this will be a good workaround, as it only tells the user that something unexpected happened. Technically seen there is no way of catching this issue and still reporting the correct results. The only good workaround would include listening for that parsing error, programmatically and temporarily remove the shebang and re-parse the file. According to how the parsing works, this might be complex, as we can only pass file names to the parser, but no file content (in this case it would be far more easy to just strip out the shebang). |
That might actually be tenable if the parser can be made to accept string content, but if not, i'm not really sure what could be done. |
I did some quick tests on this. The parsers do accept a string (at least espree does, and the other parsers have to be compatible), and Technically seen, we could strip out the shebang here (only until it is an official part of the language, of course). I can't evaluate, if this is a clean solution or just some quick-and-dirty solution, though. |
@ljharb what do you think about my last comment? Would that be a clean solution for this issue? |
@rfermann unless node itself exposes a helper function to strip the shebang (which it actually might), i don't think a string replace is reliable, especially because the shebang doesn't have to call into env, doesn't have to be called "node" (even "iojs" would work when installed), etc. |
It seems at least the rules |
Would it not be possible to do what eslint does? const textToParse = stripUnicodeBOM(text).replace(astUtils.shebangPattern, (match, captured) => `//${captured}`); That's from the |
if eslint itself does it, then perhaps this is no longer an issue? |
Bug is still valid using latest versions of eslint and this plugin. I've set up a test repo at https://github.com/silverwind/eslint-hashbang-bug which you can clone and run
|
Issue is probably that Maybe it's possible to even re-use the linked |
index.js
:foo.js
:.eslintrc.yml
:Then:
$ eslint /home/user/example/foo.js 1:1 error exported declaration 'example' not used within other modules import/no-unused-modules ✖ 1 problem (1 error, 0 warnings)
However this works when removing
#!/usr/bin/env node
.eslint
:5.16.0
eslint-plugin-import
:2.17.3
node
:12.3.1
OS: Ubuntu
19.04
The text was updated successfully, but these errors were encountered: