Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warning: "The ?? operator will always return the left operand" in SupabaseClient.ts #1338

Open
leongobbs opened this issue Dec 15, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@leongobbs
Copy link

leongobbs commented Dec 15, 2024

I encountered a warning while using [email protected] with Deno. The warning states that the ?? operator will always return the left operand because the left-hand expression can never be null or undefined. This seems to be unnecessary usage of the ?? operator.

Here’s the exact warning:

[WARNING] The "??" operator here will always return the left operand [suspicious-nullish-coalescing]

https://jsr.io/@supabase/supabase-js/2.47.7/src/SupabaseClient.ts:309:68:
  309 │       hasCustomAuthorizationHeader: 'Authorization' in this.headers ?? false,
      ╵                                                                     ~~
The left operand of the "??" operator here will never be null or undefined, so it will always be returned.

Steps to Reproduce:

  1. Import supabase-js in a Deno project:
  import { createClient } from "jsr:@supabase/supabase-js@2";

const options = {
    global: {
        headers: { "x-my-custom-header": "my-app" }, 
    },
    auth: {
        persistSession: true, 
        detectSessionInUrl: true, 
        autoRefreshToken: true, 
    },
};

const supabaseUrl = "https://ayomz.supabase.co";
const supabaseKey =
    "my_key";

const supabase = createClient(supabaseUrl, supabaseKey, options);

export { supabase };
  1. Run the project in Deno with the following command:

    deno run --allow-net main.ts
  2. Observe the warning in the terminal.

Expected Behavior:
No warning should be emitted, as this seems like an unnecessary check.

Actual Behavior:
The following warning is displayed in the terminal:

[WARNING] The "??" operator here will always return the left operand [suspicious-nullish-coalescing]

Proposed Fix:
It seems that the nullish coalescing operator (?? false) can be removed in this case since 'Authorization' in this.headers will always return a boolean (true or false).

The current code:

hasCustomAuthorizationHeader: 'Authorization' in this.headers ?? false,

Suggested fix:

hasCustomAuthorizationHeader: 'Authorization' in this.headers,

Environment:

Additional Context:
This warning does not break functionality but clutters the logs during development.

@leongobbs leongobbs added the bug Something isn't working label Dec 15, 2024
@j4w8n
Copy link
Contributor

j4w8n commented Dec 19, 2024

Nice catch @leongobbs! Would you like to submit a PR for this?

@leongobbs
Copy link
Author

Hi @j4w8n,

Thank you for your response! I’d be happy to submit a PR, but it might take me some time to get to it. In the meantime, here’s a brief guide on how to fix the issue in case someone else wants to address it:


Instructions to Fix the Issue:

  1. Locate the File:

    • Open the file: src/SupabaseClient.ts.
  2. Find the Affected Line:

    • Look for the following code:
      hasCustomAuthorizationHeader: 'Authorization' in this.headers ?? false,
  3. Apply the Fix:

    • Replace the line with:
      hasCustomAuthorizationHeader: 'Authorization' in this.headers,
  4. Test the Changes:

    • Run the test suite to ensure that the change does not introduce any issues:
      npm test
  5. Create a Commit:

    • Save the changes and commit them:
      git add src/SupabaseClient.ts
      git commit -m "Fix unnecessary nullish coalescing operator in hasCustomAuthorizationHeader"
  6. Submit a Pull Request:

    • Push the changes to a branch and open a Pull Request to the main repository.

Let me know if you'd like me to clarify anything! If no one addresses it before I get a chance, I’ll work on the PR myself.

Thanks again! 😊

@j4w8n
Copy link
Contributor

j4w8n commented Dec 22, 2024

Thanks, @leongobbs. Whenever you get to it; or if someone else would like to tackle this as a "first good issue," that works too.

If enough time goes by without a PR, I or a maintainer will take care of it.

osscode-r pushed a commit to raghavyuva/supabase-js that referenced this issue Dec 23, 2024
…nHeader

fixes the following bug : The left operand of the "??" operator here will never be null or undefined, so it will always be returned.

supabase#1338
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants