Skip to content

Commit

Permalink
fix: handle error when inputSourceMap is not a plain object (#662)
Browse files Browse the repository at this point in the history
  • Loading branch information
marekdedic committed Oct 5, 2022
1 parent 1ce0c7d commit 3e3611f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/istanbul-lib-instrument/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ is supported. To instrument ES6 modules, make sure that you set the

- `code` **[string][14]** the code to instrument
- `filename` **[string][14]** the filename against which to track coverage.
- `inputSourceMap` **[object][13]?** the source map that maps the not instrumented code back to it's original form.
- `inputSourceMap` **[Object][13]?** the source map that maps the not instrumented code back to it's original form.
Is assigned to the coverage object and therefore, is available in the json output and can be used to remap the
coverage to the untranspiled source.
coverage to the untranspiled source. Must be a plain object.

Returns **[string][14]** the instrumented code.

Expand All @@ -80,7 +80,7 @@ the callback will be called in the same process tick and is not asynchronous.
- `callback` **[Function][17]** the callback
- `inputSourceMap` **[Object][13]** the source map that maps the not instrumented code back to it's original form.
Is assigned to the coverage object and therefore, is available in the json output and can be used to remap the
coverage to the untranspiled source.
coverage to the untranspiled source. Must be a plain object.

### lastFileCoverage

Expand Down
9 changes: 9 additions & 0 deletions packages/istanbul-lib-instrument/src/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,15 @@ function programVisitor(types, sourceFilePath = 'unknown.js', opts = {}) {
.update(JSON.stringify(coverageData))
.digest('hex');
coverageData.hash = hash;
if (
coverageData.inputSourceMap &&
Object.getPrototypeOf(coverageData.inputSourceMap) !==
Object.prototype
) {
coverageData.inputSourceMap = {
...coverageData.inputSourceMap
};
}
const coverageNode = T.valueToNode(coverageData);
delete coverageData[MAGIC_KEY];
delete coverageData.hash;
Expand Down
11 changes: 11 additions & 0 deletions packages/istanbul-lib-instrument/test/specs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ const files = fs.readdirSync(dir).filter(f => {
return f.match(/\.yaml$/) && match;
});

class NonPojo {
constructor(props) {
Object.assign(this, props);
}
}

function loadDocs() {
const docs = [];
files.forEach(f => {
Expand Down Expand Up @@ -68,6 +74,11 @@ function generateTests(docs) {
const fn = async function() {
const genOnly = (doc.opts || {}).generateOnly;
const noCoverage = (doc.opts || {}).noCoverage;
if (doc.inputSourceMapClass) {
doc.inputSourceMap = new NonPojo(
doc.inputSourceMap
);
}
const v = verifier.create(
doc.code,
doc.opts || {},
Expand Down
13 changes: 13 additions & 0 deletions packages/istanbul-lib-instrument/test/specs/input-source-map.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,16 @@ tests:
out: "test"
lines: { '1': 1 }
statements: { '0': 1 }
---
name: non-pojo source map
code: |
output = "test"
inputSourceMap: { file: "test.js", mappings: "", names: [], sourceRoot: undefined, sources: [ "test.js" ], sourcesContent: [ 'output = "test"' ], version: 3 }
inputSourceMapClass: true
tests:
- name: sets the input source map
args: []
out: "test"
lines: { '1': 1 }
statements: { '0': 1 }
inputSourceMap: { file: "test.js", mappings: "", names: [], sourceRoot: undefined, sources: [ "test.js" ], sourcesContent: [ 'output = "test"' ], version: 3 }

0 comments on commit 3e3611f

Please sign in to comment.