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

chore(lint): Remove unused null coalescing operator #713

Merged
merged 2 commits into from
Jun 12, 2024

Conversation

effigies
Copy link
Contributor

Was alerted while building a project using cliffy:

    https://deno.land/x/cliffy@v1.0.0-rc.3/command/help/_help_generator.ts:354:59:
      354 │   return string?.charAt(0).toUpperCase() + string.slice(1) ?? "";
          ╵                                                            ~~

  The left operand of the "??" operator here will never be null or undefined, so it will always be
  returned. This usually indicates a bug in your code:

    https://deno.land/x/cliffy@v1.0.0-rc.3/command/help/_help_generator.ts:354:9:
      354 │   return string?.charAt(0).toUpperCase() + string.slice(1) ?? "";
          ╵          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

for the input "", the return value is already "". null and undefined both raise TypeErrors.

Was alerted while building a project using cliffy:

```
    https://deno.land/x/cliffy@v1.0.0-rc.3/command/help/_help_generator.ts:354:59:
      354 │   return string?.charAt(0).toUpperCase() + string.slice(1) ?? "";
          ╵                                                            ~~

  The left operand of the "??" operator here will never be null or undefined, so it will always be
  returned. This usually indicates a bug in your code:

    https://deno.land/x/cliffy@v1.0.0-rc.3/command/help/_help_generator.ts:354:9:
      354 │   return string?.charAt(0).toUpperCase() + string.slice(1) ?? "";
          ╵          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```

for the input `""`, the return value is already `""`. `null` and `undefined` both raise `TypeError`s.
@effigies
Copy link
Contributor Author

It looks like, in d3c2fa1, you went from

function capitalize( string: string ): string {
    if ( !string ) {
        return '';
    }
    return string.charAt( 0 ).toUpperCase() + string.slice( 1 );
}

to

function capitalize( string: string ): string {
    return string?.charAt( 0 ).toUpperCase() + string.slice( 1 ) ?? '';
}

I suppose you were previously getting capitalize(null) == "" and capitalize(undefined) == "" before, but apparently the TypeError that now results hasn't been a problem in the last 4 years.

Copy link
Owner

@c4spar c4spar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx @effigies! LGTM!

command/help/_help_generator.ts Outdated Show resolved Hide resolved
@c4spar c4spar merged commit da5d7b2 into c4spar:main Jun 12, 2024
26 checks passed
@effigies effigies deleted the patch-1 branch June 12, 2024 20:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants