Skip to content

False positive for no-top-level-browser-globals in markup #1251

Closed
@gurpreetatwal

Description

@gurpreetatwal

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.

What version of ESLint are you using?

9.29.0

What version of eslint-plugin-svelte are you using?

3.9.2

What did you do?

Configuration
import js from "@eslint/js";
import sveltePlugin from "eslint-plugin-svelte";
import svelteParser from "svelte-eslint-parser";
import tseslint from "typescript-eslint";

export default tseslint.config(
  js.configs.recommended,
  ...tseslint.configs.recommended,
  ...sveltePlugin.configs["flat/recommended"],
  {
    files: ["**/*.svelte", "*.svelte"],
    languageOptions: {
      globals: {
        window: false,
        console: false,
      },
      parser: svelteParser,
      parserOptions: {
        parser: {
          ts: "@typescript-eslint/parser",
        },
        extraFileExtensions: [".svelte"],
      },
    },
    rules: {
     'svelte/no-top-level-browser-globals': 'error',
    }
  }
);
<script>
  import { browser } from '$app/environment';

  const notification = browser
    ? 'Notification' in window 
    : false

  if (notification) console.log('hi');
</script>

{#if browser}
  <div>
    <pre>Notification: {'Notification' in window}</pre>
  </div>
{/if}

What did you expect to happen?

No errors to be reported since the access is guarded by {#if browser}

What actually happened?

Unexpected top-level browser global variable "window".
[(svelte/no-top-level-browser-globals)](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-top-level-browser-globals/)
[13:43-49]

Link to GitHub Repo with Minimal Reproducible Example

https://eslint-online-playground.netlify.app/#eNqNU8mSmzAQ/ZUuJVXYLgN3xpmkKsstyz3KgYHGkSNLlCQvUxT/nhYShrGdhQMg9Xu9vO7umDVV/vn5vd63WqFymT2idMgKtrGVEa175ApAkNU46ODJ6JNFAz00Ru8heV22bY7qKIxWe6InD1x5QqWVdaC0E42oSie0gjcj2dsB3kLyZWZOQCg4CVXrEwRAAU0pLQZ/ooHF3NtyiKAlZlJvF8lPkSwp8ia/5MxV94pIMWTvfWxqcRyqod/W4OM8fAHdn9LpN7lHDx7y4KLLRUMuuWJrhlYKko3SacQ221lSLqq1s0Ekzt4FUL6znFGa0R6U/iYPW4oVkQGYtsNlGhB3OKXxTYiccJeO1ME25zgbTCPePbcYdIqcAcwVngd4jU15kBMtlrbwAuxsPNnMYKX31PIa67U3ZVl2xbiLmRc94r5z1sjS5TM4Zz8GRhf61QiJtgACrlb5KjrhbE3VTKfAAJCl2h7KLX5tfSeJFp0AbKV+opma3UBschy26ME/cb6uDf3lLwhdvGjJlfE2g4k2uwLSuvBjctOZfOzmLLEpAwA8O1N+InE+nh0qG4KRSjeaTLTxaw6DpGMaSWDkSqdOt6lEOqZxe9KoW1JAgsZok4y+/IdeywdahLasfpHqtAJa0RIMjsOwcear2+uaQsZSOKvx+AFb32xVCaTFuOTycl88lYYDLY1pDDuuyV+N1zt0F/ofxuu9ugudGvdPwGXn5rioY8/633v43bk=

Additional comments

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions