Javascript: unresolved reference?
Let's assume the following Javascript-file:
function doSomething() {
"use strict";
if (y === window.x.Foo) {
//do something
}
}
How can I make Rider warn about the undefined variable “y” and NOT warn about “window.x.Foo”?
Reason:
In a well-structured application, using “y” this way is always an error, since global variables should always be accessed via “window”. And thus I expect Rider to display an error.
On the other hand, accessing “window.x.Foo” is 100% normal and Rider should not have an issue with that.
Issue:
If I turn on the inspection for “Unresolved reference”, I get an error for both:
Is there any way to distinguish between the two?
Please sign in to leave a comment.
No. If another js-file defines a matching global variable, “x” will exist in the window-context.
e.g.
File1.js:
File2.js:
And since one might work and the other one will never ever work, there should be different settings for the warning.
It is not even necessary to use the window-context:
The second alert is guaranteed to fail, while the first one is just totally normal in Javascript: Take an object as a parameter and access its properties.
Thus there should be different settings for the two.
```
var x = {Foo: 1};
```
i don't get the Unresolved reference error:
Whoops, I should have attached a screenshot. For some reason, I cannot reproduce the example with the window anymore either.
But the warnings do show up in the function:
So the bottom-line is that there is no solution: Rider is just unable to distinguish between “will always fail” and “unknown by language-design”.
Adding jsdoc-comments is not an option for us. We tried it some time ago and hit the limits of what you can do with jsdoc very quickly.
Javscript just ist not strongly typed by design. A parser should accept that.
Every (!) other parser I used to far was able to distinguish correctly.
Just for example eslint:
I don't see the issue here:
“doo” is an undefined variable. “use strict” has been invented in the first place to guarantee that the language will throw a runtime-error. And since the error is guaranteed, Rider should display it.
“someData.x.foo” is just 100% normal usage of javacript. And thus Rider should (by default) not have any issue with that. Or at least be configurable to accept it.