Closed
Description
- ESLint Version: 4.3.0
- Node Version: 7.10.1
- npm Version: 4.2.0
What rule do you want to change?
no-unused-vars
Does this change cause the rule to produce more or fewer warnings?
- Fewer warnings
How will the change be implemented? (New option, new default behavior, etc.)?
- New default behavior
Please provide some example code that this change will affect:
import Foo from "foo";
/**
* @param {Foo} f
*/
function bar(f){ return f.baz; }
What does the rule currently do for this code?
- The rule flags the first instance of
Foo
(inimport Foo
) as an unused var.
What will the rule do after it's changed?
- The rule should not flag
Foo
as unused because it is referenced in the JSDoc.
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
thw0rted commentedon Jul 31, 2017
Trackback to #7200 which proposes the same change but I guess didn't have enough information to kick off the process?
not-an-aardvark commentedon Aug 1, 2017
Thanks for the proposal. This seems like it might be a good idea (at least as an option), although it might be tricky to handle the edge cases correctly, because JSDoc comments don't have a well-defined variable scope as far as I know.
thw0rted commentedon Aug 1, 2017
Would it be appropriate to survey how this is implemented in various JSDoc-consuming toolsets and see if there's a consensus? Just as a data point, VSCode treats the JSDoc param references as being scoped where they're declared:
The
@param
ref above gives a tooltip of "This is function scoped" when you mouse over the{Foo}
(as IMHO you'd probably expect).ChadKillingsworth commentedon Nov 1, 2017
As a maintainer of closure-compiler, I can pipe in here. The scope of a JSDoc comment is exactly where it occurs. From the above example:
e00dan commentedon Nov 10, 2017
Hello, what's the status of this? It would be great to have it.
clshortfuse commentedon Dec 30, 2017
As much as I would like to see this warning go away, importing a module that isn't used is more of a dirty fix for VSCode. It also has it's own issues. For example, performing an
import angular from 'angular';
would cause AngularJS to load and start processing the DOM, even if you only plan on using the module as a JSDoc reference.It could create an out of order module loading, therefore the eslint warning/error is a good one.
PS: You can use
typings.d.ts
in your workspace root and VSCode will use it in conjunction with JSDoc. It's another VSCode workaround, but a less intrusive one.See: microsoft/TypeScript#14377 for a more permanent fix.
ljharb commentedon Dec 30, 2017
JSDoc is comments; variables aren't "used" by comments, only by code. If this support is added (I don't think it should be; it belongs in a separate JSDoc-related rule, just like jsx vars being marked as used are in an eslint-plugin-react rule), I hope it's at least in a separate option.
thw0rted commentedon Jan 2, 2018
The above comments make good points. Maybe it would make more sense to have a directive specific to JSDoc that allows it to resolve imports that are used in docs, without affecting the actual code? Would something like
/* import Foo from 'foo' */
be any more useful to JSDoc than a simple/* global Foo */
(which of course works today)?ChadKillingsworth commentedon Jan 2, 2018
No - the code is not unused. The import is actually needed to order dependencies correctly. And additionally - it's not global. During module references, closure-compiler will update the type nodes as well and the import is required to do that.
This is a case where an alternative scheme should be used. If you just want to mention
angular
as a type for documentation but don't need dependency ordering or a true type reference, you would want to skip this and useangular
as a global.clshortfuse commentedon Jan 2, 2018
@ChadKillingsworth Documentation isn't used by the compiler. When you load a module, you're actively loading all its dependencies and whatever initialization it wants into runtime. This means more objects into memory and whatever else is going on with the module import process.
Using a runtime command for the sake of a transpile process (closure-compiler) isn't appropriate. That is, unless you want the transpile process to remove the import line which doesn't sound safe at all. That's why it should be in comments.
Microsoft's Typescript handles the same issue by using triple slashes (
///
) which are comments that tell the transpiler how to load references and types: http://www.typescriptlang.org/docs/handbook/triple-slash-directives.htmlThat's why the argument about handling this issue within Microsoft's Salsa (JS Typechecking) leans to using a comment based import. microsoft/TypeScript#14377
But back to the original reported issue at hand, the question is why is @thw0rted even importing a module without referencing it in code in the first place? If he's doing it only to workaround an issue with Microsoft's Salsa typechecking (which I'm guessing he is), he should follow the issue I posted before. If he's using it for Closure transpiling, he should file the issue there.
27 remaining items