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

[feature](istanbul-lib-instrument): update ignore regex for "legal" comments #693

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

Conversation

kindoflew
Copy link

@kindoflew kindoflew commented Sep 14, 2022

Resolves #658.

This PR adds support for what esbuild calls "legal comments" (https://esbuild.github.io/api/#legal-comments).

The issue being that esbuild strips comments when building, so any /* istanbul ignore (if | else | next) */ comments are ignored when using it (I recently ran into this exact issue trying to get test coverage with a vite project). However, esbuild will leave "legal" comments in (ie, if the comment starts with /*! or //!).

Caveats

According to the above-linked section in esbuild's docs, this only applies to "statement-level" "legal" comments:

Note that "statement-level" for JS and "rule-level" for CSS means the comment must appear in a context where multiple statements or rules are allowed such as in the top-level scope or in a statement or rule block. So comments inside expressions or at the declaration level are not considered license comments.

I interpret this to mean that the following example might not work:

const fooOrBar = baz ? 'foo' : /*! istanbul ignore next */ 'bar'; 

However, I don't believe there is anything istanbul can do to solve this, so I'm only noting it here for completeness.

Documentation (?)

Should we document this somewhere? esbuild is becoming a pretty popular tool and this issue may start to come up more often. I think a footnote in istanbul/nyc's README would probably be enough?

Side Note

I also discovered that there is inconsistent handling of RegEx matching between /* istanbul ignore (if | else | next) */ and /* istanbul ignore file */.

When the if | else | next RegEx is tested, it uses v.match(COMMENT_RE). But when file is tested, it uses COMMENT_FILE_RE.test(c.value), which is less strict (I altered one of the tests so the comment read /* plz istanbul ignore file */ and it still passed). I don't know if this is intentional or not, but I noticed it while implementing this and thought I'd bring it up in case it was an oversight.

Thanks for this rad library! Let me know if there is anything else I can do!

@9still
Copy link

9still commented Mar 27, 2023

@kindoflew @AriPerkkio any hope on getting this updated/merged?

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.

Support /*! istanbul ignore ... */ comments
3 participants