supabase-js icon indicating copy to clipboard operation
supabase-js copied to clipboard

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

Open leongobbs opened this issue 11 months ago • 3 comments

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 avatar Dec 15 '24 18:12 leongobbs

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

j4w8n avatar Dec 19 '24 02:12 j4w8n

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! 😊

leongobbs avatar Dec 22 '24 11:12 leongobbs

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.

j4w8n avatar Dec 22 '24 13:12 j4w8n