Cannot use istanbul ignore comments for generated code
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 36.1k
- Forks
- 1.1k
- Avg merge
- 9h 18m
- Merged PRs (30d)
- 195
Description
Describe the bug
When using Solid and testing our app with coverage reporting enabled (powered by istanbul/nyc), there are some branches/functions which are uncovered, but they are not branches which exist in our code, instead they are generated by Solid. Because these code paths are generated, we cannot place a /* istanbul ignore next */ comment before the appropriate section because it doesn't exist anywhere in our source.
A few cases where this occurs (I'm sure there's others):
Show/Matchcomponents - If the match is never used, there's an unused function because of theget children()method which got generated- Refs - It generates a ternary with
typeof _ref$ === "function"as the condiiton. Which in most cases will always be true/false so that means there's a branch of the code which never gets hit.
All of these result in an extra uncovered function here, an extra uncovered branch there. And there's no way we can add the appropriate comments before this code to ignore it.
Your Example Website or App
https://playground.solidjs.com/anonymous/7b917606-9c75-4695-b669-accb486afc92
Steps to Reproduce the Bug or Issue
N/A
Expected behavior
When Solid is generating the runtime code, it should relocate the comments appropriately so they keep the same meaning that they have in the source.
Source:
{/* istanbul ignore next */}
<Match when={condition()}>
<div>Hello</div>
</Match>
Generated:
_$createComponent(Show, {
get when() {
return condition();
},
/* istanbul ignore next */
get children() {
return _tmpl$();
}
})
Source:
let ref;
return <div ref={ref}>Hello</div>
Generated: (This should always add the appropriate ignore comments since only one branch will ever be hit.)
let ref;
return (() => {
var _el$ = _tmpl$();
var _ref$ = ref;
/* istanbul ignore next */
typeof _ref$ === "function" ? _$use(_ref$, _el$) : ref = _el$;
return _el$;
})();
Screenshots or Videos
No response
Platform
- OS: N/A
- Browser: N/A
- Version: 1.8.16
Additional context
It's also impossible currently to write our own plugin to do this since comments will get totally deleted during compilation when placed in certain spots of the JSX.
For example, this input:
function Counter() {
let ref;
/* istanbul ignore next -- 1 */
return (
<>
{/* istanbul ignore next -- 2 */}
<div ref={ref}>Hi</div>
</>
);
}
Gets transformed into: (the second comment is just totally lost so we cannot even attempt to move it with our own plugin after Solid is done compiling)
function Counter() {
let ref;
/* istanbul ignore next -- 1 */
return (() => {
var _el$ = _tmpl$();
var _ref$ = ref;
typeof _ref$ === "function" ? _$use(_ref$, _el$) : ref = _el$;
return _el$;
})();
}
This is a unique problem with Solid which doesn't exist with many other frameworks because Solid generates a lot of code rather than just calling functions from the Solid library. If these same bits of code were instead unconditional function calls to code in node_modules, this wouldn't be an issue.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the JSX transformations from the issue in the Solid compiler and inspect how comments are retained or removed. Trace the generated code for Show/Match components and refs, then determine how source ignore comments could be relocated. Done means the documented Istanbul comments survive compilation with their intended meaning and generated branches are handled consistently.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- compilers, frontend, testing-qa
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100