istanbuljs / v8-to-istanbul

convert from v8 coverage format to istanbul's format
ISC License
115 stars 40 forks source link

Doubts about the v8-to-istanbul parameter #250

Open zhangtao25 opened 5 months ago

zhangtao25 commented 5 months ago

Why does the first parameter of v8-to-istanbul need to be a string, I see that you can just pass in sources.source. https://github.com/istanbuljs/v8-to-istanbul/blob/master/lib/v8-to-istanbul.js#L48

const v8toIstanbul = require('v8-to-istanbul')
// the path to the original source-file is required, as its contents are
// used during the conversion algorithm.
const converter = v8toIstanbul('',undefined,{
    source:"function add(a:number,b:number){\n    return a+b\n}\n\nconsole.log(add(1,2))"
})
// await converter.load() // this is required due to async file reading.
// provide an array of coverage information in v8 format.

converter.load().then(res=>{
    converter.applyCoverage([
        {
            "functionName": "",
            "ranges": [
                {
                    "startOffset": 0,
                    "endOffset": 520,
                    "count": 1
                }
            ],
            "isBlockCoverage": true
        },
        // ...
    ])
// output coverage information in a form that can
// be consumed by Istanbul.
    console.info(JSON.stringify(converter.toIstanbul()))
})