Closed Bug 1509270 Opened Last year Closed Last year

[eslint] Consider allowing writing to 'location' variable

Categories

(Firefox Build System :: Lint and Formatting, enhancement)

enhancement
Not set

Tracking

(firefox65 fixed)

RESOLVED FIXED
mozilla65
Tracking Status
firefox65 --- fixed

People

(Reporter: standard8, Assigned: standard8)

Details

Attachments

(1 file)

Boris pointed out off-line that ESLint doesn't allow writing to the `location` variable - it fails for the "no-global-assign" rule.

https://eslint.org/docs/rules/no-global-assign

The documentation says that although location is read-only, you can assign a DOMString to it. 

https://developer.mozilla.org/en-US/docs/Web/API/Window/location

The definition built into ESLint via the browser environment, marks it as read-only: https://github.com/sindresorhus/globals/blob/2f083e33aba018cb669d48baba472d77964f858e/globals.json#L488

That seems to date back to when it was first added, so the write nature may have not been considered, or it was considered and decided not to allow due to the restricted item you can assign to it.

The workaround is to use the slightly longer window.location.

I'm thinking we could possibly allow writing to location for browser/chrome/mochitest tests. However, at the moment I'm only seeing about 18 instances throughout the tree, but maybe that's because people have been using the longer version:

https://searchfox.org/mozilla-central/search?q=%5E%5Cs*location+%3D&case=false&regexp=true&path=*.html
> The documentation says that although location is read-only, you can assign a DOMString to it. 

It's "readonly" but [PutForwards], so there is actually a setter, and that setter forwards the set to the location object's href attribute.  See https://heycam.github.io/webidl/#PutForwards

Also, there are no restrictions on what you can assign.  Whatever JS value you assign will have https://tc39.github.io/ecma262/#sec-tostring applied to it before being passed on into the browser.  So you can assign objects, whatever. ;)

Basically, "location" is not actually readonly.  It's a writable property in Window.
Thanks for the clarification.
Assignee: nobody → standard8
Status: NEW → ASSIGNED
I've raised a PR to change the original source of the global definition for ESLint:

https://github.com/sindresorhus/globals/pull/143

In the meantime, as this might affect some tests that I have people working on, I'm proposing the attached patch as a temporary fix.
No movement on the PR yet, so I'll land the fix as is, and keep an eye out later on.
Pushed by mbanner@mozilla.com:
https://hg.mozilla.org/integration/autoland/rev/cc540ed50749
Allow 'location' as a writeable global property for ESLint (previously read-only). r=mossop
https://hg.mozilla.org/mozilla-central/rev/cc540ed50749
Status: ASSIGNED → RESOLVED
Closed: Last year
Resolution: --- → FIXED
Target Milestone: --- → Firefox 65
Target Milestone: Firefox 65 → mozilla65
You need to log in before you can comment on or make changes to this bug.