supabase-js
supabase-js copied to clipboard
Warning: "The ?? operator will always return the left operand" in SupabaseClient.ts
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:
- Import
supabase-jsin 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 };
-
Run the project in Deno with the following command:
deno run --allow-net main.ts -
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:
- Library version:
[email protected] - Environment: Deno
- Deno version:
1.37.0
Additional Context: This warning does not break functionality but clutters the logs during development.
Nice catch @leongobbs! Would you like to submit a PR for this?
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:
-
Locate the File:
- Open the file:
src/SupabaseClient.ts.
- Open the file:
-
Find the Affected Line:
- Look for the following code:
hasCustomAuthorizationHeader: 'Authorization' in this.headers ?? false,
- Look for the following code:
-
Apply the Fix:
- Replace the line with:
hasCustomAuthorizationHeader: 'Authorization' in this.headers,
- Replace the line with:
-
Test the Changes:
- Run the test suite to ensure that the change does not introduce any issues:
npm test
- Run the test suite to ensure that the change does not introduce any issues:
-
Create a Commit:
- Save the changes and commit them:
git add src/SupabaseClient.ts git commit -m "Fix unnecessary nullish coalescing operator in hasCustomAuthorizationHeader"
- Save the changes and commit them:
-
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! 😊
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.