Skip to content

Commit

Permalink
fix: use null prototype for map objects (#177)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: a null prototype is now used in several places rather than the default `{}` assignment.
  • Loading branch information
bmeck authored and bcoe committed Jun 6, 2018
1 parent 01096ae commit 9a5a30c
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/istanbul-api/lib/file-matcher.js
Expand Up @@ -58,7 +58,7 @@ function matcherFor(options, callback) {
options.realpath = true; //force real paths (to match Node.js module paths)

filesFor(options, function (err, files) {
var fileMap = {},
var fileMap = Object.create(null),
matchFn;
/* istanbul ignore if - untestable */
if (err) { return callback(err); }
Expand Down
4 changes: 2 additions & 2 deletions packages/istanbul-lib-coverage/lib/coverage-map.js
Expand Up @@ -8,7 +8,7 @@ var FileCoverage = require('./file').FileCoverage,
CoverageSummary = require('./file').CoverageSummary;

function loadMap(source) {
var data = {};
var data = Object.create(null);
Object.keys(source).forEach(function (k) {
var cov = source[k];
if (cov instanceof FileCoverage) {
Expand All @@ -27,7 +27,7 @@ function loadMap(source) {
*/
function CoverageMap(obj) {
if (!obj) {
this.data = {};
this.data = Object.create(null);
} else if (obj instanceof CoverageMap) {
this.data = obj.data;
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/istanbul-lib-coverage/lib/file.js
Expand Up @@ -170,7 +170,7 @@ function FileCoverage(pathOrObj) {
FileCoverage.prototype.getLineCoverage = function () {
var statementMap = this.data.statementMap,
statements = this.data.s,
lineMap = {};
lineMap = Object.create(null);

Object.keys(statements).forEach(function (st) {
if (!statementMap[st]) {
Expand Down
2 changes: 1 addition & 1 deletion packages/istanbul-lib-report/lib/summarizer.js
Expand Up @@ -166,7 +166,7 @@ function toInitialList(coverageMap) {
}

function toDirParents(list) {
var nodeMap = {},
var nodeMap = Object.create(null),
parentNodeList = [];
list.forEach(function (o) {
var node = new ReportNode(o.path, o.fileCoverage),
Expand Down
20 changes: 20 additions & 0 deletions packages/istanbul-lib-report/test/summarizer.test.js
Expand Up @@ -30,6 +30,19 @@ function makeCoverage(filePath, numStatements, numCovered) {
return fc;
}

function protoDirMap(dir) {
var files = ['constructor.js', 'toString.js'],
count = 0,
map = {};
files.forEach(function (f) {
var filePath = dir ? dir + '/' + f : f,
fc = makeCoverage(filePath, 4, count);
count += 1;
map[filePath] = fc;
});
return coverage.createCoverageMap(map);
}

function singleDirMap(dir) {
var files = ['file3.js', 'file4.js', 'file2.js', 'file1.js'],
count = 0,
Expand Down Expand Up @@ -107,6 +120,13 @@ describe('summarizer', function () {
assert.deepEqual(nodes, ['g:', 'f:file1.js', 'f:file2.js', 'f:file3.js', 'f:file4.js']);
});

it('supports a list of files containing Object.prototype names', function () {
var map = protoDirMap(),
tree = fn(map),
nodes = getStructure(tree);
assert.deepEqual(nodes, ['g:', 'f:constructor.js', 'f:toString.js']);
});

it('supports a list of files at the same nesting level', function () {
var map = singleDirMap('/lib/handlers'),
tree = fn(map),
Expand Down
2 changes: 1 addition & 1 deletion packages/istanbul-lib-source-maps/lib/map-store.js
Expand Up @@ -29,7 +29,7 @@ function MapStore(opts) {
this.baseDir = opts.baseDir || null;
this.verbose = opts.verbose || false;
this.sourceStore = sourceStore.create(opts.sourceStore, { tmpdir: opts.tmpdir});
this.data = {};
this.data = Object.create(null);
}
/**
* registers a source map URL with this store. It makes some input sanity checks
Expand Down
2 changes: 2 additions & 0 deletions packages/istanbul-lib-source-maps/test/map-store.test.js
Expand Up @@ -14,6 +14,8 @@ describe('map store', function () {
var coverageMap = libCoverage.createCoverageMap(coverageData);

var transformed = mapStore.transformCoverage(coverageMap);
assert.isUndefined(transformed.map.data.constructor);

var transformedCoverage = transformed.map.data[path.resolve("./test.ts")].data;

assert.deepEqual(transformedCoverage.statementMap, {
Expand Down

0 comments on commit 9a5a30c

Please sign in to comment.