npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

eslint-plugin-no-browser-globals

v1.0.1

Published

An ESLint plugin that disallows the use of browser globals. It is useful when you want to make sure that your code is not using browser globals, which can cause issues when running in a non-browser environment.

Downloads

119

Readme

eslint-plugin-no-browser-globals

An ESLint plugin that disallows the use of browser globals. It is useful when you want to make sure that your code is not using browser globals, which can cause issues when running in a non-browser environment.

NPM Version

Installation

npm install eslint-plugin-no-browser-globals --save-dev

# or

yarn add eslint-plugin-no-browser-globals --dev

# or

pnpm add eslint-plugin-no-browser-globals --save-dev

And then add no-browser-globals to the plugins section of your .eslintrc configuration file:

{
  "plugins": ["no-browser-globals"],
  "rules": {
    "no-browser-globals/no-ssr-browser-globals": ["error"]
  }
}

Usage

This plugin ONLY works in jsx/tsx files.

This plugin ONLY works in jsx/tsx files.

This plugin ONLY works in jsx/tsx files.

Using browser globals inside hooks

Use browser globals directly is not allowed. But you can use them inside a hook like useEffect or useLayoutEffect.

Not Allowed

const foo = location.href;

Allowed

useEffect(() => {
  const foo = location.href;

  // Do something with foo...
}, []);

You can also specify the hooks that are allowed to use browser globals, default is useEffect and useLayoutEffect. Here is a configuration example that allows the use of browser globals inside useEffect , useLayoutEffect and 'useCustomHook'.

{
  "plugins": ["no-browser-globals"],
  "rules": {
    "no-browser-globals/no-ssr-browser-globals": [
      "error",
      { "allowedHooks": ["useEffect", "useLayoutEffect", "useCustomHook"] }
    ]
  }
}

Using browser globals inside event handlers

You can use browser globals inside a on[A-Z]* event handler.

Not Allowed

const App = () => {
  return (
    <button
      onclick={() => {
        const value = location.host;
      }}
    />
  );
};

Allowed

const App = () => {
  return (
    <button
      onClick={() => {
        const value = location.host;
      }}
    />
  );
};

Using browser globals inside specific function callbacks

Note: Only callbacks that are passed to specific functions are allowed to use browser globals. Use browser globals directly is not allowed.

Not Allowed

const foo = () => {
  const value = location.host;
};

Allowed

setTimeout(() => {
  const value = location.host;
}, 1000);

You can also specify the functions that are allowed to use browser globals, default is setTimeout, setInterval. Here is a configuration example that allows the use of browser globals inside setTimeout , setInterval and 'customFunction'.

{
  "plugins": ["no-browser-globals"],
  "rules": {
    "no-browser-globals/no-ssr-browser-globals": [
      "error",
      { "allowedFunctions": ["setTimeout", "setInterval", "customFunction"] }
    ]
  }
}
const specialFunction = (fn) => {
  return fn();
};

// Use browser globals inside the callback in specialFunction, allowed.
specialFunction(() => {
  const value = location.host;
});

Using browser globals inside window !== undefined check

You can use browser globals inside a window !== undefined check.

Not Allowed

if (location) {
  const value = location.host;
  // Do something with value...
}

Allowed

if (window !== undefined) {
  const value = location.host;
  // Do something with value...
}

This logic is only check if the use of browser globals is inside a window !== undefined check, simple but works in most cases. If you have a complex check, use @client instead.

Only works if conditionCheck is true.(Default is true so you don't need to specify it if you want to use this feature)

{
  "plugins": ["no-browser-globals"],
  "rules": {
    "no-browser-globals/no-ssr-browser-globals": ["error", { "conditionCheck": true }]
  }
}

@client annotation

You can use the @client annotation to specify that the use of browser globals is allowed in a specific line of code.

Not Allowed

const value = location.host; // Error: Do not use browser globals directly.

Allowed

// @client
const value = location.host; // No error

Configuration

{
  "allowedGlobals": {
    "type": "array",
    "items": { "type": "string" },
    "default": []
  },
  "allowedHooks": {
    "type": "array",
    "items": { "type": "string" },
    "default": ["useEffect", "useLayoutEffect"]
  },
  "allowedFunctions": {
    "type": "array",
    "items": { "type": "string" },
    "default": ["setTimeout", "setInterval"]
  },
  "conditionCheck": { "type": "boolean", "default": true }
}

| Option | Description | Default value | | ---------------- | ---------------------------------------------------------------------------------- | ---------------------------------- | | allowedGlobals | An array of global variables that are allowed to use. | [] | | allowedHooks | An array of hooks that are allowed to use browser globals inside. | ["useEffect", "useLayoutEffect"] | | allowedFunctions | An array of functions that are allowed to use browser globals inside its callback. | ["setTimeout", "setInterval"] | | conditionCheck | You can use browser globals inside a window !== undefined check if it is true. | true |