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

fix(eslint-plugin): [strict-boolean-expressions] consider assertion function argument a boolean context #9074

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kirkwaiblinger
Copy link
Member

PR Checklist

Overview

Query for call expressions, check if the function being called is an assertion function, and line up the asserted parameter with the arguments that we see.

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @kirkwaiblinger!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint.

Copy link

netlify bot commented May 11, 2024

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 524c870
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/663eb9e6552d9700083ff8b5
😎 Deploy Preview https://deploy-preview-9074--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 91 (🔴 down 5 from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (no change from production)
SEO: 98 (no change from production)
PWA: 80 (no change from production)
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

@kirkwaiblinger kirkwaiblinger changed the title [strict-boolean-expressions] Consider assertion function argument a boolean context fix(eslint-plugin): [strict-boolean-expressions] consider assertion function argument a boolean context May 11, 2024
@kirkwaiblinger kirkwaiblinger marked this pull request as ready for review May 11, 2024 00:35
@bradzacher bradzacher added the bug Something isn't working label May 28, 2024
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

I squinted at the logic a bunch and it all seems pretty reasonable. Nice stuff. Requesting changes to add some tests just to be sure though. ✨

Comment on lines +293 to +294
firstParameter != null &&
firstParameter.name.kind === ts.SyntaxKind.Identifier &&
Copy link
Member

Choose a reason for hiding this comment

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

[Style] Nit: I'm always surprised we don't have a rule for this ... somewhere.

Suggested change
firstParameter != null &&
firstParameter.name.kind === ts.SyntaxKind.Identifier &&
firstParameter?.name.kind === ts.SyntaxKind.Identifier &&

? declaration.parameters.slice(1)
: declaration.parameters;

// Don't even bother inspecting parameters past the number of
Copy link
Member

Choose a reason for hiding this comment

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

[Docs] This is very nitpicky, but unnecessary _"even"_s have always bugged me.

Suggested change
// Don't even bother inspecting parameters past the number of
// Don't bother inspecting parameters past the number of

Comment on lines +61 to +63
declare function myAssert(value: unknown): asserts value;
let maybeString = Math.random() > 0.5 ? '' : undefined;
myAssert(maybeString);
Copy link
Member

Choose a reason for hiding this comment

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

[Docs] Nit: everywhere else generally refers to assert(), and that's a pretty commonly used name. I think it'd be nice to use it here for consistency.

Suggested change
declare function myAssert(value: unknown): asserts value;
let maybeString = Math.random() > 0.5 ? '' : undefined;
myAssert(maybeString);
declare function assert(value: unknown): asserts value;
let maybeString = Math.random() > 0.5 ? '' : undefined;
assert(maybeString);

Comment on lines +351 to +352
const assertedParameter = checkableArguments[assertedParameterIndex];
traverseNode(assertedParameter, true);
Copy link
Member

Choose a reason for hiding this comment

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

[Refactor] Totally optional, but I had a bit of a hard time figuring out the intended flow of all the added code. There's a lot. Extracting out the "what is the asserted parameter index?" logic into a separate might be helpful in making that more clear. What do you think?

Suggested change
const assertedParameter = checkableArguments[assertedParameterIndex];
traverseNode(assertedParameter, true);
const assertedParameter = getAssertedParameter(checkableArguments, node);
if (assertedParameter) {
traverseNode(assertedParameter, true);
}

Copy link
Member

Choose a reason for hiding this comment

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

[Testing] Some more test cases I think would be good to have:

  • Combining this: and function overloads
  • Three very different overloads
  • ... params, including in combination with the other two things

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Jun 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting response Issues waiting for a reply from the OP or another party bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enhancement: [strict-boolean-expressions] Check truthiness assertion functions
3 participants