Skip to content

12$: diagnostic: typeof operator should warn when comparing to invalid string literals  #1213

@CoderMuffin

Description

@CoderMuffin
Contributor

When performing checks like typeof x == "string", it is perfectly possible to misspell it as something like typeof x == "strng" (missing i), which will always return false. Maybe there should be a warning for this?

More generally, expressions of the form typeof <a> [== or != or === or !==] <b> where b is a string and not "undefined", "object", "boolean", "number", "bigint", "string", "symbol" or "function"

I would be happy to implement this if approved and no-one else wants to :)

Activity

changed the title [-][diagnostic] typeof operator should warn when comparing to invalid string literals [/-] [+]diagnostic: typeof operator should warn when comparing to invalid string literals [/+] on Mar 20, 2024
strager

strager commented on Apr 27, 2024

@strager
Collaborator

This is a great idea! I'll put 12$ on it.

added
good first issueGood for newcomers and C++ beginners
for hireGet paid for working on this task: https://quick-lint-js.com/hiring.html
on Apr 27, 2024
changed the title [-]diagnostic: typeof operator should warn when comparing to invalid string literals [/-] [+]12$: diagnostic: typeof operator should warn when comparing to invalid string literals [/+] on Apr 27, 2024
milanjijo

milanjijo commented on May 14, 2024

@milanjijo

Hey Strager, I would love to try and tackle this issue!

singalhimanshu

singalhimanshu commented on Jun 9, 2024

@singalhimanshu
Contributor

@milanjijo are you working on this task?

singalhimanshu

singalhimanshu commented on Jun 9, 2024

@singalhimanshu
Contributor

@strager Is there any way to currently check a string literal against this list ("undefined", "object", "boolean", "number", "bigint", "string", "symbol" or "function") in the code?

CoderMuffin

CoderMuffin commented on Jun 9, 2024

@CoderMuffin
ContributorAuthor

@singalhimanshu yes, you can compare the Expression::Literal->span()->string_view() to each element of the array. The detection code for a similar diagnostic which might help can be seen here: src/quick-lint-js/fe/parse.cpp:524

dubloom

dubloom commented on Aug 26, 2024

@dubloom

Is anyone still working on this issue ? Otherwise I would be interested to tackle it as i already have a working piece of code. Thank you !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    for hireGet paid for working on this task: https://quick-lint-js.com/hiring.htmlgood first issueGood for newcomers and C++ beginners

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @strager@singalhimanshu@CoderMuffin@milanjijo@dubloom

      Issue actions

        12$: diagnostic: typeof operator should warn when comparing to invalid string literals · Issue #1213 · quick-lint/quick-lint-js