Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • C create-react-app
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 1,547
    • Issues 1,547
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 417
    • Merge requests 417
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Meta
  • create-react-app
  • Merge requests
  • !10714

feat(confusing-browser-globals): Add relevant messaging to no-restricted-globals eslint rule

  • Review changes

  • Download
  • Email patches
  • Plain diff
Open Administrator requested to merge github/fork/yanneves/feature/no-restricted-globals into main Mar 19, 2021
  • Overview 2
  • Commits 1
  • Pipelines 1
  • Changes 2

Created by: yanneves

BREAKING CHANGE: export from confusing-browser-globals package changed from String[] to Object[] - no change required if used in eslint no-restricted-globals config

I came across unclear messaging in eslint-config-airbnb today and traced it to this package, which is included in eslint-config-airbnb-base.

If you have code like:

const SomeComponent = () => {
  useEffect(() => {
    const queryString = location.search.substr(1)
    // do something with query string
  }, [])

  // ...
}

Using this package in eslint no-restricted-globals rules will result in the error Unexpected use of 'location'. no-restricted-globals

image

Which may communicate "don't use location at all" rather than the intended "prefer window.location or a local parameter to avoid unintended effects"

So I've updated this package with clearer messaging, like in this case Unexpected use of 'location'. Use local parameter or window.location instead no-restricted-globals

image

And in doing so realised we don't want to recommend using window.<property> everywhere, for example the following code:

const handleEvent = () => {
  doSomethingWith(event.target.value)
}

We'd catch this and suggest using the (missing) local value and not window.event.

So I extended the list with a suggestion between window, document, WorkerGlobalScope*, or none to determine the correct messaging, and added tests covering each scenario.

Since the output of this package changes, it would break anywhere that strictly expects an array of strings to be exported (i.e. not being used in eslint config). It should release as a breaking change and new major version.

* WorkerGlobalScope applies to self included in this list, which made most sense to me but could be removed entirely from the list considering https://developer.mozilla.org/en-US/docs/Web/API/Window/self

Assignee
Assign to
Reviewers
Request review from
Time tracking
Source branch: github/fork/yanneves/feature/no-restricted-globals