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?

 

0
9 comments
You are using `window.x.Foo` on the right side of assignment, running this code will result in "Uncaught TypeError: Cannot read properties of undefined (reading 'Foo')". So the "Unresolved variable" warning here is perfectly valid
0

No. If another js-file defines a matching global variable, “x” will exist in the window-context.

e.g. 

File1.js:

var x = {Foo: 1};

File2.js: 

"use strict";
alert(window.x.Foo); //Will work fine in this case
alert(doo); //Guaranteed to fail. Always!

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:

"use strict";
function foo(someData) {
	alert(someData.x.foo); //Unresolved variable foo
	alert(doo) //Unresolved variable or type doo
}

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.

0
With the global x defined like

```
var x = {Foo: 1};
```

i don't get the Unresolved reference error:
0

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:

0
In case of the parameter, the IDE can't know the actual parameter type. You can specify it explicitly using the @param JSDoc annotation to avoid the warning. See https://jsdoc.app/tags-param.html
0

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.

0
The IDE can't know if the certain variable is unknown by design or by mistake - if it can't be resolved when analyzing code statically, it will be reported as unresolved. If annotating your code is not an option for you, I can only suggest suppressing the warning
0

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.

0

Please sign in to leave a comment.