function Instrumenter(options) { this.opts = options || { debug: false, walkDebug: false, coverageVariable: '__coverage__', codeGenerationOptions: undefined, noAutoWrap: false, noCompact: false, embedSource: false, preserveComments: false, esModules: false }; if (this.opts.esModules && !this.opts.noAutoWrap) { this.opts.noAutoWrap = true; if (this.opts.debug) { console.log('Setting noAutoWrap to true as required by esModules'); } } this.walker = new Walker({ ArrowFunctionExpression: [ this.arrowBlockConverter ], ExpressionStatement: this.coverStatement, ExportNamedDeclaration: this.coverExport, BreakStatement: this.coverStatement, ContinueStatement: this.coverStatement, DebuggerStatement: this.coverStatement, ReturnStatement: this.coverStatement, ThrowStatement: this.coverStatement, TryStatement: [ this.paranoidHandlerCheck, this.coverStatement], VariableDeclaration: this.coverStatement, IfStatement: [ this.ifBlockConverter, this.coverStatement, this.ifBranchInjector ], ForStatement: [ this.skipInit, this.loopBlockConverter, this.coverStatement ], ForInStatement: [ this.skipLeft, this.loopBlockConverter, this.coverStatement ], ForOfStatement: [ this.skipLeft, this.loopBlockConverter, this.coverStatement ], WhileStatement: [ this.loopBlockConverter, this.coverStatement ], DoWhileStatement: [ this.loopBlockConverter, this.coverStatement ], SwitchStatement: [ this.coverStatement, this.switchBranchInjector ], SwitchCase: [ this.switchCaseInjector ], WithStatement: [ this.withBlockConverter, this.coverStatement ], FunctionDeclaration: [ this.coverFunction, this.coverStatement ], FunctionExpression: this.coverFunction, LabeledStatement: this.coverStatement, ConditionalExpression: this.conditionalBranchInjector, LogicalExpression: this.logicalExpressionBranchInjector, ObjectExpression: this.maybeAddType, MetaProperty: this.coverMetaProperty, }, this.extractCurrentHint, this, this.opts.walkDebug); //unit testing purposes only if (this.opts.backdoor && this.opts.backdoor.omitTrackerSuffix) { this.omitTrackerSuffix = true; } }
n/a
function assertJsonEqual(aa, bb) {
/*
* this function will assert JSON.stringify(<aa>) === JSON.stringify(<bb>)
*/
aa = JSON.stringify(objectDeepCopyWithKeysSorted(aa));
bb = JSON.stringify(objectDeepCopyWithKeysSorted(bb));
if (aa !== bb) {
throw new Error(JSON.stringify(aa) + " !== " + JSON.stringify(bb));
}
}
...
local.arg = 0;
// init opt.coverage1
opt.coverage1 = require("vm").runInNewContext(opt.data, {
arg: 0
});
/* jslint ignore:start */
// validate opt.coverage1
local.assertJsonEqual(opt.coverage1,
{"/test":{"b":{"1":[0,1]},"branchMap":{"1":{"line":2,"locations
":[{"end":{"column":14,"line":3},"start":{"column":2,"line":3}},{&
quot;end":{"column":14,"line":4},"start":{"column":2,"line":4}}],"type
":"cond-expr"}},"code":["(function () {","return arg","? __coverage__","
;: __coverage__;","}());",""],"f":{"1":1},"fnMap":{"1":{"line
":1,"loc":{"end":{"column":13,"line":1},"start":{"column":1,"
;line":1}},"name":"(anonymous_1)"}},"path":"/test","s":{"1":1,&
quot;2":1},"statementMap":{"1":{"end":{"column":5,"line":5},"start"
;:{"column":0,"line":1}},"2":{"end":{"column":15,"line":4},"start
":{"column":0,"line":2}}}}}
);
// test merge-create handling-behavior
opt.coverage1 = local.istanbul.coverageMerge({}, opt.coverage1);
// validate opt.coverage1
local.assertJsonEqual(opt.coverage1,
{"/test":{"b":{"1":[0,1]},"branchMap":{"1":{"line":2,"locations
":[{"end":{"column":14,"line":3},"start":{"column":2,"line":3}},{&
quot;end":{"column":14,"line":4},"start":{"column":2,"line":4}}],"type
":"cond-expr"}},"code":["(function () {","return arg","? __coverage__","
;: __coverage__;","}());",""],"f":{"1":1},"fnMap":{"1":{"line
":1,"loc":{"end":{"column":13,"line":1},"start":{"column":1,"
;line":1}},"name":"(anonymous_1)"}},"path":"/test","s":{"1":1,&
quot;2":1},"statementMap":{"1":{"end":{"column":5,"line":5},"start"
;:{"column":0,"line":1}},"2":{"end":{"column":15,"line":4},"start
":{"column":0,"line":2}}}}}
...
function assertOrThrow(passed, msg) {
/*
* this function will throw <msg> if <passed> is falsy
*/
if (passed) {
return;
}
throw (
(
msg
&& typeof msg.message === "string"
&& typeof msg.stack === "string"
)
// if msg is err, then leave as is
? msg
: new Error(
typeof msg === "string"
// if msg is string, then leave as is
? msg
// else JSON.stringify(msg)
: JSON.stringify(msg, undefined, 4)
)
);
}
...
strDict[str] = strDict[str] || (ii + 2);
ii = strDict[str];
if (commandList[ii]) {
commandList[ii].command.push(key);
return;
}
commandList[ii] = rgxComment.exec(str);
local.assertOrThrow(commandList[ii], (
"cliRun - cannot parse comment in COMMAND "
+ key
+ ":\nnew RegExp("
+ JSON.stringify(rgxComment.source)
+ ").exec(" + JSON.stringify(str).replace((
/\\\\/g
), "\u0000").replace((
...
cliRun = function ({ rgxComment }) {
/*
* this function will run cli
*/
let cliDict;
cliDict = local.cliDict;
cliDict._eval = cliDict._eval || function () {
/*
* <code>
* will eval <code>
*/
globalThis.local = local;
require("vm").runInThisContext(process.argv[3]);
};
cliDict._help = cliDict._help || function () {
/*
*
* will print help
*/
let commandList;
let file;
let packageJson;
let str;
let strDict;
commandList = [
{
argList: "<arg2> ...",
description: "usage:",
command: [
"<arg1>"
]
}, {
argList: "'console.log(\"hello world\")'",
description: "example:",
command: [
"--eval"
]
}
];
file = __filename.replace((
/.*\//
), "");
packageJson = require("./package.json");
// validate comment
rgxComment = rgxComment || (
/\)\u0020\{\n(?:|\u0020{4})\/\*\n(?:\u0020|\u0020{5})\*((?:\u0020<[^>]*?>|\u0020\.\.\.)*?)\n(?:\u0020|\u0020{5})\*\u0020
(will\u0020.*?\S)\n(?:\u0020|\u0020{5})\*\/\n(?:\u0020{4}|\u0020{8})\S/
);
strDict = {};
Object.keys(cliDict).sort().forEach(function (key, ii) {
if (key[0] === "_" && key !== "_default") {
return;
}
str = String(cliDict[key]);
if (key === "_default") {
key = "";
}
strDict[str] = strDict[str] || (ii + 2);
ii = strDict[str];
if (commandList[ii]) {
commandList[ii].command.push(key);
return;
}
commandList[ii] = rgxComment.exec(str);
local.assertOrThrow(commandList[ii], (
"cliRun - cannot parse comment in COMMAND "
+ key
+ ":\nnew RegExp("
+ JSON.stringify(rgxComment.source)
+ ").exec(" + JSON.stringify(str).replace((
/\\\\/g
), "\u0000").replace((
/\\n/g
), "\\n\\\n").replace((
/\u0000/g
), "\\\\") + ");"
));
commandList[ii] = {
argList: local.coalesce(commandList[ii][1], "").trim(),
command: [
key
],
description: commandList[ii][2]
};
});
str = "";
str += packageJson.name + " (" + packageJson.version + ")\n\n";
str += commandList.filter(function (elem) {
return elem;
}).map(function (elem, ii) {
elem.command = elem.command.filter(function (elem) {
return elem;
});
switch (ii) {
case 0:
case 1:
elem.argList = [
elem.argList
];
break;
default:
elem.argList = elem.argList.split(" ");
elem.description = (
"# COMMAND "
+ (elem.command[0] || "<none>") + "\n# "
+ elem.description
);
}
return (
elem.description + "\n " + file
+ " " + elem.command.sort().join("|") + " "
+ elem.argList.join(" ")
);
}).join("\n\n");
console.log(str);...
n/a
function coalesce(...argList) {
/*
* this function will coalesce null, undefined, or "" in <argList>
*/
let arg;
let ii;
ii = 0;
while (ii < argList.length) {
arg = argList[ii];
if (arg !== undefined && arg !== null && arg !== "") {
return arg;
}
ii += 1;
}
return arg;
}
...
), "\u0000").replace((
/\\n/g
), "\\n\\\n").replace((
/\u0000/g
), "\\\\") + ");"
));
commandList[ii] = {
argList: local.coalesce(commandList[ii][1], "").trim(),
command: [
key
],
description: commandList[ii][2]
};
});
str = "";
...
coverageMerge = function (coverage1 = {}, coverage2 = {}) {
/*
* this function will inplace-merge <coverage2> into <coverage1>
*/
let dict1;
let dict2;
Object.keys(coverage2).forEach(function (file) {
// if coverage1[file] is undefined, then override it
if (!coverage1[file]) {
coverage1[file] = coverage2[file];
return;
}
// merge coverage2 into coverage1
[
"b", "f", "s"
].forEach(function (key) {
dict1 = coverage1[file][key];
dict2 = coverage2[file][key];
switch (key) {
// increment coverage for branch lines
case "b":
Object.keys(dict2).forEach(function (key) {
dict2[key].forEach(function (cnt, ii) {
dict1[key][ii] += cnt;
});
});
break;
// increment coverage for function and statement lines
case "f":
case "s":
Object.keys(dict2).forEach(function (key) {
dict1[key] += dict2[key];
});
break;
}
});
});
return coverage1;
}
...
});
/* jslint ignore:start */
// validate opt.coverage1
local.assertJsonEqual(opt.coverage1,
{"/test":{"b":{"1":[0,1]},"branchMap":{"1":{"line":2,"locations
":[{"end":{"column":14,"line":3},"start":{"column":2,"line":3}},{&
quot;end":{"column":14,"line":4},"start":{"column":2,"line":4}}],"type
":"cond-expr"}},"code":["(function () {","return arg","? __coverage__","
;: __coverage__;","}());",""],"f":{"1":1},"fnMap":{"1":{"line
":1,"loc":{"end":{"column":13,"line":1},"start":{"column":1,"
;line":1}},"name":"(anonymous_1)"}},"path":"/test","s":{"1":1,&
quot;2":1},"statementMap":{"1":{"end":{"column":5,"line":5},"start"
;:{"column":0,"line":1}},"2":{"end":{"column":15,"line":4},"start
":{"column":0,"line":2}}}}}
);
// test merge-create handling-behavior
opt.coverage1 = local.istanbul.coverageMerge({}, opt.coverage1);
// validate opt.coverage1
local.assertJsonEqual(opt.coverage1,
{"/test":{"b":{"1":[0,1]},"branchMap":{"1":{"line":2,"locations
":[{"end":{"column":14,"line":3},"start":{"column":2,"line":3}},{&
quot;end":{"column":14,"line":4},"start":{"column":2,"line":4}}],"type
":"cond-expr"}},"code":["(function () {","return arg","? __coverage__","
;: __coverage__;","}());",""],"f":{"1":1},"fnMap":{"1":{"line
":1,"loc":{"end":{"column":13,"line":1},"start":{"column":1,"
;line":1}},"name":"(anonymous_1)"}},"path":"/test","s":{"1":1,&
quot;2":1},"statementMap":{"1":{"end":{"column":5,"line":5},"start"
;:{"column":0,"line":1}},"2":{"end":{"column":15,"line":4},"start
":{"column":0,"line":2}}}}}
);
// init opt.coverage2
opt.coverage2 = require("vm").runInNewContext(opt.data, { arg: 1 });
// validate opt.coverage2
...
coverageReportCreate = function ({ coverage, coverageInclude }) {
/*
* this function will
// 1. merge previous <dirCoverage>/coverage.json into <coverage>
// 2. convert <coverage> to <summaryDict>
// 3. convert <summaryDict> to <nodeRoot>
// 4. convert <nodeRoot> to text-report <dirCoverage>/coverage.txt
// 5. convert <nodeRoot> to html-report <dirCoverage>/\*
// 6. return coverage-report in html-format as single document
*/
let dirCoverage;
let filePrefix;
let htmlAll;
let nodeChildAdd;
let nodeCreate;
let nodeDict;
let nodeNormalize;
let nodeRoot;
let summaryDict;
let tmp;
// init function
nodeChildAdd = function (node, child) {
/*
* this function will add <child> to <node>
*/
node.children.push(child);
child.parent = node;
};
nodeCreate = function (pathname) {
/*
* this function will create a tree-node
*/
return {
children: [],
pathname,
metrics: {
branches: {
total: 0,
covered: 0,
skipped: 0,
pct: "Unknown"
},
functions: {
total: 0,
covered: 0,
skipped: 0,
pct: "Unknown"
},
lines: {
total: 0,
covered: 0,
skipped: 0,
pct: "Unknown"
},
statements: {
total: 0,
covered: 0,
skipped: 0,
pct: "Unknown"
}
},
name: pathname
};
};
nodeNormalize = function (node, level, filePrefix, parent) {
/*
* this function will recursively normalize <node> and its children
*/
let metric;
// init name
if (node.name.indexOf(filePrefix) === 0) {
node.name = node.name.slice(filePrefix.length);
}
if (node.name[0] === path.sep) {
node.name = node.name.slice(1);
}
// init relativeName
node.relativeName = (
parent
? (
parent.name !== "__root__/"
? node.name.slice(parent.name.length)
: node.name
)
: node.name.slice(filePrefix.length)
);
// init nameOrAllFiles
node.nameOrAllFiles = node.name || "All files";
// init relativeNameOrAllFiles
node.relativeNameOrAllFiles = node.relativeName || "All files";
// init href
node.href = node.relativeName.split(path.sep).join("/") + (
node.isFile
? ".html"
: "index.html"
);
// recurse
node.children.forEach(function (child) {
nodeNormalize(child, level + 1, filePrefix, node);
});
// sort children by name
node.children.sort(function (aa, bb) {
return (
aa.name > bb.name
? 1
: -1
);
});
// init metrics
if (!node.isFile) {
node.children.forEach(function (child) {
[
"lines", "statements", "branches", "functions"
].forEach(function (key) {
metric = node.metrics[key];
metric.total += child.metrics[key].total;
metric.covered += child.metrics[key].covered;
metric.skipped += child.metrics[key].skipped;
});
});
}
// calculate pct and score
[
"lines", "statement...
...
"/inputTextarea1.js"\n\
);\n\
eval( // jslint ignore:line\n\
document.querySelector("#outputTextarea1").value\n\
);\n\
document.querySelector(\n\
"#htmlCoverageReport1"\n\
).innerHTML = local.istanbul.coverageReportCreate({\n\
coverage: globalThis.__coverage__\n\
});\n\
} catch (errCaught) {\n\
console.error(errCaught);\n\
}\n\
break;\n\
}\n\
...
fsReadFileOrDefaultSync = function (pathname, type, dflt) {
/*
* this function will sync-read <pathname> with given <type> and <dflt>
*/
let fs;
// do nothing if module not exists
try {
fs = require("fs");
pathname = require("path").resolve(pathname);
} catch (ignore) {
return dflt;
}
// try to read pathname
try {
return (
type === "json"
? JSON.parse(fs.readFileSync(pathname, "utf8"))
: fs.readFileSync(pathname, type)
);
} catch (ignore) {
return dflt;
}
}
n/a
fsWriteFileWithMkdirpSync = function (pathname, data) {
/*
* this function will sync write <data> to <pathname> with "mkdir -p"
*/
let fs;
// do nothing if module not exists
try {
fs = require("fs");
pathname = require("path").resolve(pathname);
} catch (ignore) {
return;
}
// try to write pathname
try {
fs.writeFileSync(pathname, data);
} catch (ignore) {
// mkdir -p
fs.mkdirSync(require("path").dirname(pathname), {
recursive: true
});
// re-write pathname
fs.writeFileSync(pathname, data);
}
console.error("fsWriteFileWithMkdirpSync - " + pathname);
return true;
}
n/a
function identity(val) {
/*
* this function will return <val>
*/
return val;
}
...
cliDict.help = cliDict.help || cliDict._help;
cliDict._interactive = cliDict._interactive || function () {
/*
*
* will start interactive-mode
*/
globalThis.local = local;
local.identity(local.replStart || require("repl").start)({
useGlobal: true
});
};
cliDict["--interactive"] = cliDict["--interactive"] || cliDict._interactive;
cliDict["-i"] = cliDict["-i"] || cliDict._interactive;
cliDict._version = cliDict._version || function () {
/*
...
instrumentInPackage = function (code, file) {
/*
* this function will instrument <code>
* if macro /\* istanbul instrument in package $npm_package_nameLib *\/
* exists in <code>
*/
return (
(
process.env.npm_config_mode_coverage
&& code.indexOf("/* istanbul ignore all */\n") < 0 && (
process.env.npm_config_mode_coverage === "all"
|| process.env.npm_config_mode_coverage === "node_modules"
|| code.indexOf(
"/* istanbul instrument in package "
+ process.env.npm_package_nameLib + " */\n"
) >= 0
|| code.indexOf(
"/* istanbul instrument in package "
+ process.env.npm_config_mode_coverage + " */\n"
) >= 0
)
)
? local.instrumentSync(code, file)
: code
);
}
...
};
local.testCase_istanbulInstrumentInPackage_default = function (opt, onError) {
/*
* this function will test istanbulInstrumentInPackage's
* default handling-behavior
*/
opt.data = local.istanbul.instrumentInPackage("", "/test.js");
local.assertJsonEqual(opt.data, "");
opt.data = local.istanbul.instrumentInPackage(
"/* istanbul instrument in package istanbul */\n;",
"/test.js"
);
onError(undefined, opt);
};
...
instrumentSync = function (code, file) {
/*
* this function will
// 1. normalize <file>
// 2. save <code> to __coverageInclude__[<file>] for future html-report
// 3. return instrumented-code
*/
// 1. normalize <file>
file = path.resolve(file);
// 2. save <code> to __coverageInclude__[<file>] for future html-report
// 3. return instrumented-code
return (
"globalThis.__coverageInclude__ = globalThis.__coverageInclude__ || {};"
+ "globalThis.__coverageInclude__[" + JSON.stringify(file) + "] = 1;\n"
) + new local.Instrumenter({
embedSource: true,
esModules: true,
noAutoWrap: true
}).instrumentSync(code, file).trimStart();
}
...
try {\n\
delete globalThis.__coverage__["/inputTextarea1.js"];\n\
} catch (ignore) {}\n\
// try to cover and eval #inputTextarea1\n\
try {\n\
document.querySelector(\n\
"#outputTextarea1"\n\
).textContent = local.istanbul.instrumentSync(\n\
document.querySelector("#inputTextarea1").value,\n\
"/inputTextarea1.js"\n\
);\n\
eval( // jslint ignore:line\n\
document.querySelector("#outputTextarea1").value\n\
);\n\
document.querySelector(\n\
...
function noop() {
/*
* this function will do nothing
*/
return;
}
...
local.isBrowser
? {
env: {},
stdout: {}
}
: globalThis.process
);
local.noop(escodegen, esprima, estraverse, esutils);
/*
repo https://github.com/acornjs/acorn/tree/6.4.1
committed 2020-03-09T10:38:41Z
*/
...
function objectAssignDefault(tgt = {}, src = {}, depth = 0) {
/*
* this function will if items from <tgt> are null, undefined, or "",
* then overwrite them with items from <src>
*/
let recurse;
recurse = function (tgt, src, depth) {
Object.entries(src).forEach(function ([
key, bb
]) {
let aa;
aa = tgt[key];
if (aa === undefined || aa === null || aa === "") {
tgt[key] = bb;
return;
}
if (
depth !== 0
&& typeof aa === "object" && aa && !Array.isArray(aa)
&& typeof bb === "object" && bb && !Array.isArray(bb)
) {
recurse(aa, bb, depth - 1);
}
});
};
recurse(tgt, src, depth | 0);
return tgt;
}
...
}).join(" ").replace((
/\u001b\[\d*m/g
), "") + "\n";
// scroll textarea to bottom
elem.scrollTop = elem.scrollHeight;
};
});
local.objectAssignDefault(local, globalThis.domOnEventDelegateDict);
globalThis.domOnEventDelegateDict = local;
}());
/* istanbul ignore next */
// run node js-env code - init-test
(function () {
...
function objectDeepCopyWithKeysSorted(obj) {
/*
* this function will recursively deep-copy <obj> with keys sorted
*/
let sorted;
if (typeof obj !== "object" || !obj) {
return obj;
}
// recursively deep-copy list with child-keys sorted
if (Array.isArray(obj)) {
return obj.map(objectDeepCopyWithKeysSorted);
}
// recursively deep-copy obj with keys sorted
sorted = {};
Object.keys(obj).sort().forEach(function (key) {
sorted[key] = objectDeepCopyWithKeysSorted(obj[key]);
});
return sorted;
}
n/a
function onErrorThrow(err) {
/*
* this function will throw <err> if exists
*/
if (err) {
throw err;
}
}
n/a
templateRender = function (template, dict, opt = {}, ii = 0) {
/*
* this function will render <template> with given <dict>
*/
let argList;
let getVal;
let match;
let renderPartial;
let rgx;
let skip;
let val;
if (dict === null || dict === undefined) {
dict = {};
}
getVal = function (key) {
argList = key.split(" ");
val = dict;
if (argList[0] === "#this/") {
return val;
}
if (argList[0] === "#ii/") {
return ii;
}
// iteratively lookup nested val in dict
argList[0].split(".").forEach(function (key) {
val = val && val[key];
});
return val;
};
renderPartial = function (match0, helper, key, partial) {
switch (helper) {
case "each":
case "eachTrimEndComma":
val = getVal(key);
val = (
Array.isArray(val)
? val.map(function (dict, ii) {
// recurse with partial
return local.templateRender(partial, dict, opt, ii);
}).join("")
: ""
);
// remove trailing-comma from last elem
if (helper === "eachTrimEndComma") {
val = val.trimEnd().replace((
/,$/
), "");
}
return val;
case "if":
partial = partial.split("{{#unless " + key + "}}");
partial = (
getVal(key)
? partial[0]
// handle "unless" case
: partial.slice(1).join("{{#unless " + key + "}}")
);
// recurse with partial
return local.templateRender(partial, dict, opt);
case "unless":
return (
getVal(key)
? ""
// recurse with partial
: local.templateRender(partial, dict, opt)
);
default:
// recurse with partial
return match0[0] + local.templateRender(match0.slice(1), dict, opt);
}
};
// render partials
rgx = (
/\{\{#(\w+)\u0020([^}]+?)\}\}/g
);
template = template || "";
match = rgx.exec(template);
while (match) {
rgx.lastIndex += 1 - match[0].length;
template = template.replace(
new RegExp(
"\\{\\{#(" + match[1] + ") (" + match[2]
+ ")\\}\\}([\\S\\s]*?)\\{\\{/" + match[1] + " " + match[2]
+ "\\}\\}"
),
renderPartial
);
match = rgx.exec(template);
}
// search for keys in template
return template.replace((
/\{\{[^}]+?\}\}/g
), function (match0) {
let markdownToHtml;
let notHtmlSafe;
notHtmlSafe = opt.notHtmlSafe;
try {
val = getVal(match0.slice(2, -2));
if (val === undefined) {
return match0;
}
argList.slice(1).forEach(function (fmt, ii, list) {
switch (fmt) {
case "*":
case "+":
case "-":
case "/":
skip = ii + 1;
val = String(
fmt === "*"
? Number(val) * Number(list[skip])
: fmt === "+"
? Number(val) + Number(list[skip])
: fmt === "-"
? Number(val) - Number(list[skip])
: Number(val) / Number(list[skip])
);
break;
case "a...
...
case "each":
case "eachTrimEndComma":
val = getVal(key);
val = (
Array.isArray(val)
? val.map(function (dict, ii) {
// recurse with partial
return local.templateRender(partial, dict, opt, ii);
}).join("")
: ""
);
// remove trailing-comma from last elem
if (helper === "eachTrimEndComma") {
val = val.trimEnd().replace((
/,$/
...
function Instrumenter(options) { this.opts = options || { debug: false, walkDebug: false, coverageVariable: '__coverage__', codeGenerationOptions: undefined, noAutoWrap: false, noCompact: false, embedSource: false, preserveComments: false, esModules: false }; if (this.opts.esModules && !this.opts.noAutoWrap) { this.opts.noAutoWrap = true; if (this.opts.debug) { console.log('Setting noAutoWrap to true as required by esModules'); } } this.walker = new Walker({ ArrowFunctionExpression: [ this.arrowBlockConverter ], ExpressionStatement: this.coverStatement, ExportNamedDeclaration: this.coverExport, BreakStatement: this.coverStatement, ContinueStatement: this.coverStatement, DebuggerStatement: this.coverStatement, ReturnStatement: this.coverStatement, ThrowStatement: this.coverStatement, TryStatement: [ this.paranoidHandlerCheck, this.coverStatement], VariableDeclaration: this.coverStatement, IfStatement: [ this.ifBlockConverter, this.coverStatement, this.ifBranchInjector ], ForStatement: [ this.skipInit, this.loopBlockConverter, this.coverStatement ], ForInStatement: [ this.skipLeft, this.loopBlockConverter, this.coverStatement ], ForOfStatement: [ this.skipLeft, this.loopBlockConverter, this.coverStatement ], WhileStatement: [ this.loopBlockConverter, this.coverStatement ], DoWhileStatement: [ this.loopBlockConverter, this.coverStatement ], SwitchStatement: [ this.coverStatement, this.switchBranchInjector ], SwitchCase: [ this.switchCaseInjector ], WithStatement: [ this.withBlockConverter, this.coverStatement ], FunctionDeclaration: [ this.coverFunction, this.coverStatement ], FunctionExpression: this.coverFunction, LabeledStatement: this.coverStatement, ConditionalExpression: this.conditionalBranchInjector, LogicalExpression: this.logicalExpressionBranchInjector, ObjectExpression: this.maybeAddType, MetaProperty: this.coverMetaProperty, }, this.extractCurrentHint, this, this.opts.walkDebug); //unit testing purposes only if (this.opts.backdoor && this.opts.backdoor.omitTrackerSuffix) { this.omitTrackerSuffix = true; } }
n/a
arrowBlockConverter = function (node) { var retStatement; if (node.expression) { // turn expression nodes into a block with a return statement retStatement = astgen.returnStatement(node.body); // ensure the generated return statement is covered retStatement.loc = node.body.loc; node.body = this.convertToBlock(retStatement); node.expression = false; } }
n/a
branchIncrementExprAst = function (varName, branchIndex, down) { var ret = astgen.postIncrement( astgen.subscript( astgen.subscript( astgen.dot(astgen.variable(this.currentState.trackerVar), astgen.variable('b')), astgen.stringLiteral(varName) ), astgen.numericLiteral(branchIndex) ), down ); return ret; }
n/a
branchLocationFor = function (name, index) { return this.coverState.branchMap[name].locations[index]; }
n/a
branchName = function (type, startLine, pathLocations) { var bName, paths = [], locations = [], i, ignoring = !!this.currentState.ignoring; this.currentState.branch += 1; bName = this.currentState.branch; for (i = 0; i < pathLocations.length; i += 1) { pathLocations[i].skip = pathLocations[i].skip || ignoring || undefined; locations.push(pathLocations[i]); paths.push(0); } this.coverState.b[bName] = paths; this.coverState.branchMap[bName] = { line: startLine, type: type, locations: locations }; return bName; }
n/a
conditionalBranchInjector = function (node, walker) { var bName = this.branchName('cond-expr', walker.startLineForNode(node), this.locationsForNodes([ node.consequent, node.alternate ])), ast1 = this.branchIncrementExprAst(bName, 0), ast2 = this.branchIncrementExprAst(bName, 1); node.consequent.preprocessor = this.maybeAddSkip(this.branchLocationFor(bName, 0)); node.alternate.preprocessor = this.maybeAddSkip(this.branchLocationFor(bName, 1)); node.consequent = astgen.sequence(ast1, node.consequent); node.alternate = astgen.sequence(ast2, node.alternate); }
n/a
convertToBlock = function (node) { if (!node) { return { type: 'BlockStatement', body: [] }; } else if (node.type === 'BlockStatement') { return node; } else { return { type: 'BlockStatement', body: [ node ] }; } }
n/a
coverExport = function (node, walker) { var sName, incrStatementCount; if ( !node.declaration || !node.declaration.declarations ) { return; } this.maybeSkipNode(node, 'next'); sName = this.statementName(node.declaration.loc); incrStatementCount = astgen.statement( astgen.postIncrement( astgen.subscript( astgen.dot(astgen.variable(this.currentState.trackerVar), astgen.variable('s')), astgen.stringLiteral(sName) ) ) ); this.splice(incrStatementCount, node, walker); }
n/a
coverFunction = function (node, walker) { var id, body = node.body, blockBody = body.body, popped; this.maybeSkipNode(node, 'next'); id = this.functionName(node, walker.startLineForNode(node), { start: node.loc.start, end: { line: node.body.loc.start.line, column: node.body.loc.start.column } }); if (blockBody.length > 0 && this.isUseStrictExpression(blockBody[0])) { popped = blockBody.shift(); } blockBody.unshift( astgen.statement( astgen.postIncrement( astgen.subscript( astgen.dot(astgen.variable(this.currentState.trackerVar), astgen.variable('f')), astgen.stringLiteral(id) ) ) ) ); if (popped) { blockBody.unshift(popped); } }
n/a
function(node) { node.skipSelf = true; }
n/a
coverStatement = function (node, walker) {
var sName,
incrStatementCount,
parent,
grandParent;
this.maybeSkipNode(node, 'next');
if (this.isUseStrictExpression(node)) {
grandParent = walker.ancestor(2);
/* istanbul ignore else: difficult to test */
if (grandParent) {
if ((grandParent.node.type === SYNTAX.FunctionExpression.name ||
grandParent.node.type === SYNTAX.FunctionDeclaration.name) &&
walker.parent().node.body[0] === node) {
return;
}
}
}
if (node.type === SYNTAX.FunctionDeclaration.name) {
// Called for the side-effect of setting the function's statement count to 1.
this.statementName(node.loc, 1);
} else {
// We let `coverExport` handle ExportNamedDeclarations.
parent = walker.parent();
if (parent && parent.node.type === SYNTAX.ExportNamedDeclaration.name) {
return;
}
sName = this.statementName(node.loc);
incrStatementCount = astgen.statement(
astgen.postIncrement(
astgen.subscript(
astgen.dot(astgen.variable(this.currentState.trackerVar), astgen.variable('s')),
astgen.stringLiteral(sName)
)
)
);
this.splice(incrStatementCount, node, walker);
}
}
n/a
endIgnore = function () { this.currentState.ignoring -= 1; }
n/a
extractCurrentHint = function (node) { if (!node.range) { return; } var i = this.currentState.lastHintPosition + 1, hints = this.currentState.hints, nodeStart = node.range[0], hint; this.currentState.currentHint = null; // hack-istanbul - allow top-level istanbul-ignore-next if (node.type === "Program") { return; } while (i < hints.length) { hint = hints[i]; if (hint.end < nodeStart) { this.currentState.currentHint = hint; this.currentState.lastHintPosition = i; i += 1; } else { break; } } }
n/a
filterHints = function (comments) {
var ret = [],
i,
comment,
groups;
if (!(comments && isArray(comments))) {
return ret;
}
for (i = 0; i < comments.length; i += 1) {
comment = comments[i];
/* istanbul ignore else: paranoid check */
if (comment && comment.value && comment.range && isArray(comment.range)) {
groups = String(comment.value).match(COMMENT_RE);
if (groups) {
ret.push({ type: groups[1], start: comment.range[0], end: comment.range[1] });
}
}
}
return ret;
}
n/a
findLeaves = function (node, accumulator, parent, property) { if (node.type === SYNTAX.LogicalExpression.name) { this.findLeaves(node.left, accumulator, node, 'left'); this.findLeaves(node.right, accumulator, node, 'right'); } else { accumulator.push({ node: node, parent: parent, property: property }); } }
n/a
fixColumnPositions = function (coverState) {
var offset = LEADER_WRAP.length,
fixer = function (loc) {
if (loc.start.line === 1) {
loc.start.column -= offset;
}
if (loc.end.line === 1) {
loc.end.column -= offset;
}
},
k,
obj,
i,
locations;
obj = coverState.statementMap;
for (k in obj) {
/* istanbul ignore else: has own property */
if (obj.hasOwnProperty(k)) { fixer(obj[k]); }
}
obj = coverState.fnMap;
for (k in obj) {
/* istanbul ignore else: has own property */
if (obj.hasOwnProperty(k)) { fixer(obj[k].loc); }
}
obj = coverState.branchMap;
for (k in obj) {
/* istanbul ignore else: has own property */
if (obj.hasOwnProperty(k)) {
locations = obj[k].locations;
for (i = 0; i < locations.length; i += 1) {
fixer(locations[i]);
}
}
}
}
n/a
functionName = function (node, line, location) { this.currentState.func += 1; var id = this.currentState.func, ignoring = !!this.currentState.ignoring, name = node.id ? node.id.name : '(anonymous_' + id + ')', clone = function (attr) { var obj = location[attr] || /* istanbul ignore next */ {}; return { line: obj.line, column: obj.column }; }; this.coverState.fnMap[id] = { name: name, line: line, loc: { start: clone('start'), end: clone('end') }, skip: ignoring || undefined }; this.coverState.f[id] = 0; return id; }
n/a
getPreamble = function (sourceCode, emitUseStrict) { var varName = this.opts.coverageVariable || '__coverage__', file = this.coverState.path.replace(/\\/g, '\\\\'), tracker = this.currentState.trackerVar, coverState, strictLine = emitUseStrict ? '"use strict";' : '', // return replacements using the function to ensure that the replacement is // treated like a dumb string and not as a string with RE replacement patterns replacer = function (s) { return function () { return s; }; }, code; if (!this.opts.noAutoWrap) { this.fixColumnPositions(this.coverState); } if (this.opts.embedSource) { this.coverState.code = sourceCode.split(/(?:\r?\n)|\r/); } coverState = this.opts.debug ? JSON.stringify(this.coverState, undefined, 4) : JSON.stringify(this.coverState); code = [ "%STRICT%", "var %VAR% = (Function('return this'))();", "if (!%VAR%.%GLOBAL%) { %VAR%.%GLOBAL% = {}; }", "%VAR% = %VAR%.%GLOBAL%;", "if (!(%VAR%['%FILE%'])) {", " %VAR%['%FILE%'] = %OBJECT%;", "}", "%VAR% = %VAR%['%FILE%'];" ].join("\n") .replace(/%STRICT%/g, replacer(strictLine)) .replace(/%VAR%/g, replacer(tracker)) .replace(/%GLOBAL%/g, replacer(varName)) .replace(/%FILE%/g, replacer(file)) .replace(/%OBJECT%/g, replacer(coverState)); return code; }
n/a
ifBlockConverter = function (node) { node.consequent = this.convertToBlock(node.consequent); node.alternate = this.convertToBlock(node.alternate); }
n/a
ifBranchInjector = function (node, walker) { var alreadyIgnoring = !!this.currentState.ignoring, hint = this.currentState.currentHint, ignoreThen = !alreadyIgnoring && hint && hint.type === 'if', ignoreElse = !alreadyIgnoring && hint && hint.type === 'else', line = node.loc.start.line, col = node.loc.start.column, makeLoc = function () { return { line: line, column: col }; }, bName = this.branchName('if', walker.startLineForNode(node), [ { start: makeLoc(), end: makeLoc(), skip: ignoreThen || undefined }, { start: makeLoc(), end: makeLoc(), skip: ignoreElse || undefined } ]), thenBody = node.consequent.body, elseBody = node.alternate.body, child; thenBody.unshift(astgen.statement(this.branchIncrementExprAst(bName, 0))); elseBody.unshift(astgen.statement(this.branchIncrementExprAst(bName, 1))); if (ignoreThen) { child = node.consequent; child.preprocessor = this.startIgnore; child.postprocessor = this.endIgnore; } if (ignoreElse) { child = node.alternate; child.preprocessor = this.startIgnore; child.postprocessor = this.endIgnore; } }
n/a
instrument = function (code, filename, callback) { if (!callback && typeof filename === 'function') { callback = filename; filename = null; } try { callback(null, this.instrumentSync(code, filename)); } catch (ex) { callback(ex); } }
n/a
instrumentASTSync = function (program, filename, originalCode) { var usingStrict = false, codegenOptions, generated, preamble, lineCount, i; filename = filename || String(new Date().getTime()) + '.js'; this.sourceMap = null; this.coverState = { path: filename, s: {}, b: {}, f: {}, fnMap: {}, statementMap: {}, branchMap: {} }; this.currentState = { trackerVar: generateTrackerVar(filename, this.omitTrackerSuffix), func: 0, branch: 0, variable: 0, statement: 0, hints: this.filterHints(program.comments), currentHint: null, lastHintPosition: -1, ignoring: 0 }; if (program.body && program.body.length > 0 && this.isUseStrictExpression(program.body[0])) { //nuke it program.body.shift(); //and add it back at code generation time usingStrict = true; } this.walker.startWalk(program); codegenOptions = this.opts.codeGenerationOptions || { format: { compact: !this.opts.noCompact }}; codegenOptions.comment = this.opts.preserveComments; //console.log(JSON.stringify(program, undefined, 2)); generated = ESPGEN.generate(program, codegenOptions); preamble = this.getPreamble(originalCode || '', usingStrict); if (generated.map && generated.code) { lineCount = preamble.split(/\r\n|\r|\n/).length; // offset all the generated line numbers by the number of lines in the preamble for (i = 0; i < generated.map._mappings._array.length; i += 1) { generated.map._mappings._array[i].generatedLine += lineCount; } this.sourceMap = generated.map; generated = generated.code; } return preamble + '\n' + generated + '\n'; }
n/a
instrumentSync = function (code, filename) { var program; //protect from users accidentally passing in a Buffer object instead if (typeof code !== 'string') { throw new Error('Code must be string'); } if (code.charAt(0) === '#') { //shebang, 'comment' it out, won't affect syntax tree locations for things we care about code = '//' + code; } if (!this.opts.noAutoWrap) { code = LEADER_WRAP + code + TRAILER_WRAP; } try { // hack-istanbul - inline acorn-opt let opt = { locations: true, onComment: [], onToken: this.opts.preserveComments, ranges: true, sourceType: this.opts.esModules ? 'module' : 'script' }; program = ESP.parse(code, opt); program.comments = opt.onComment } catch (e) { console.log('Failed to parse file: ' + filename); throw e; } if (this.opts.preserveComments) { program = ESPGEN.attachComments(program, program.comments, program.tokens); } if (!this.opts.noAutoWrap) { program = { type: SYNTAX.Program.name, body: program.body[0].expression.callee.body.body, comments: program.comments }; } return this.instrumentASTSync(program, filename, code); }
...
try {\n\
delete globalThis.__coverage__["/inputTextarea1.js"];\n\
} catch (ignore) {}\n\
// try to cover and eval #inputTextarea1\n\
try {\n\
document.querySelector(\n\
"#outputTextarea1"\n\
).textContent = local.istanbul.instrumentSync(\n\
document.querySelector("#inputTextarea1").value,\n\
"/inputTextarea1.js"\n\
);\n\
eval( // jslint ignore:line\n\
document.querySelector("#outputTextarea1").value\n\
);\n\
document.querySelector(\n\
...
isUseStrictExpression = function (node) { return node && node.type === SYNTAX.ExpressionStatement.name && node.expression && node.expression.type === SYNTAX.Literal.name && node.expression.value === 'use strict'; }
n/a
lastFileCoverage = function () { return this.coverState; }
n/a
lastSourceMap = function () { return this.sourceMap; }
n/a
locationsForNodes = function (nodes) { var ret = [], i; for (i = 0; i < nodes.length; i += 1) { ret.push(nodes[i].loc); } return ret; }
n/a
logicalExpressionBranchInjector = function (node, walker) { var parent = walker.parent(), leaves = [], bName, tuple, i; this.maybeSkipNode(node, 'next'); if (parent && parent.node.type === SYNTAX.LogicalExpression.name) { //already covered return; } this.findLeaves(node, leaves); bName = this.branchName('binary-expr', walker.startLineForNode(node), this.locationsForNodes(leaves.map(function (item) { return item.node; })) ); for (i = 0; i < leaves.length; i += 1) { tuple = leaves[i]; tuple.parent[tuple.property] = astgen.sequence(this.branchIncrementExprAst(bName, i), tuple.node); tuple.node.preprocessor = this.maybeAddSkip(this.branchLocationFor(bName, i)); } }
n/a
loopBlockConverter = function (node) { node.body = this.convertToBlock(node.body); }
n/a
maybeAddSkip = function (branchLocation) { return function (node) { var alreadyIgnoring = !!this.currentState.ignoring, hint = this.currentState.currentHint, ignoreThis = !alreadyIgnoring && hint && hint.type === 'next'; if (ignoreThis) { this.startIgnore(); node.postprocessor = this.endIgnore; } if (ignoreThis || alreadyIgnoring) { branchLocation.skip = true; } }; }
n/a
maybeAddType = function (node) { var props = node.properties, i, child; for (i = 0; i < props.length; i += 1) { child = props[i]; if (!child.type) { child.type = SYNTAX.Property.name; } } }
n/a
maybeSkipNode = function (node, type) { var alreadyIgnoring = !!this.currentState.ignoring, hint = this.currentState.currentHint, ignoreThis = !alreadyIgnoring && hint && hint.type === type; if (ignoreThis) { this.startIgnore(); node.postprocessor = this.endIgnore; return true; } return false; }
n/a
paranoidHandlerCheck = function (node) {
// if someone is using an older esprima on the browser
// convert handlers array to single handler attribute
// containing its first element
/* istanbul ignore next */
if (!node.handler && node.handlers) {
node.handler = node.handlers[0];
}
}
n/a
skipInit = function (node) { if (node.init) { node.init.skipWalk = true; } }
n/a
skipLeft = function (node) { node.left.skipWalk = true; }
n/a
splice = function (statements, node, walker) { var targetNode = walker.isLabeled() ? walker.parent().node : node; targetNode.prepend = targetNode.prepend || []; pushAll(targetNode.prepend, statements); }
...
Reference.prototype.replace = function replace(node) {
this.parent[this.key] = node;
};
Reference.prototype.remove = function remove() {
if (isArray(this.parent)) {
this.parent.splice(this.key, 1);
return true;
} else {
this.replace(null);
return false;
}
};
...
startIgnore = function () { this.currentState.ignoring += 1; }
n/a
statementName = function (location, initValue) { var sName, ignoring = !!this.currentState.ignoring; location.skip = ignoring || undefined; initValue = initValue || 0; this.currentState.statement += 1; sName = this.currentState.statement; this.coverState.statementMap[sName] = location; this.coverState.s[sName] = initValue; return sName; }
n/a
switchBranchInjector = function (node, walker) { var cases = node.cases, bName, i; if (!(cases && cases.length > 0)) { return; } bName = this.branchName('switch', walker.startLineForNode(node), this.locationsForNodes(cases)); for (i = 0; i < cases.length; i += 1) { cases[i].branchLocation = this.branchLocationFor(bName, i); cases[i].consequent.unshift(astgen.statement(this.branchIncrementExprAst(bName, i))); } }
n/a
switchCaseInjector = function (node) { var location = node.branchLocation; delete node.branchLocation; if (this.maybeSkipNode(node, 'next')) { location.skip = true; } }
n/a
withBlockConverter = function (node) { node.body = this.convertToBlock(node.body); }
n/a
cover = function () {
/*
* <script>
* will run and cover <script>
*/
let moduleExtensionsJs;
let tmp;
try {
tmp = JSON.parse(require("fs").readFileSync("package.json", "utf8"));
process.env.npm_package_nameLib = (
process.env.npm_package_nameLib
|| tmp.nameLib
|| tmp.name.replace((
/-/g
), "_")
);
} catch (ignore) {}
process.env.npm_config_mode_coverage = (
process.env.npm_config_mode_coverage
|| process.env.npm_package_nameLib
|| "all"
);
// add coverage hook to require
tmp = require("module");
moduleExtensionsJs = tmp._extensions[".js"];
tmp._extensions[".js"] = function (module, file) {
if (typeof file === "string" && (
file.indexOf(process.cwd() + require("path").sep) === 0 && (
process.env.npm_config_mode_coverage === "node_modules"
|| file.indexOf(
require("path").resolve("node_modules")
+ require("path").sep
) !== 0
)
)) {
module._compile(local.instrumentInPackage(
require("fs").readFileSync(file, "utf8"),
file
), file);
return;
}
moduleExtensionsJs(module, file);
};
// init process.argv
process.argv.splice(1, 2);
process.argv[1] = require("path").resolve(process.argv[1]);
console.error("\nistanbul - covering $ " + process.argv.join(" "));
// create coverage on exit
process.on("exit", function () {
local.coverageReportCreate({
coverage: globalThis.__coverage__
});
});
// re-init cli
tmp.runMain();
}
n/a
instrument = function () {
/*
* <script>
* will instrument <script> and print result to stdout
*/
process.argv[3] = require("path").resolve(process.argv[3]);
process.stdout.write(local.instrumentSync(
require("fs").readFileSync(process.argv[3], "utf8"),
process.argv[3]
));
}
n/a
report = function () {
/*
* <coverageJson>
* will create coverage-report from file <coverageJson>
*/
process.argv[3] = require("path").resolve(process.argv[3]);
globalThis.__coverage__ = JSON.parse(
require("fs").readFileSync(process.argv[3])
);
globalThis.__coverageInclude__ = {};
Object.keys(globalThis.__coverage__).forEach(function (file) {
globalThis.__coverageInclude__[file] = 1;
});
local.coverageReportCreate({
coverage: globalThis.__coverage__
});
}
n/a
test = function () {
/*
* <script>
* will run and cover <script> if env-var $npm_config_mode_coverage is set
*/
if (process.env.npm_config_mode_coverage) {
process.argv[2] = "cover";
// re-init cli
local.cliDict[process.argv[2]]();
return;
}
// restart node with __filename removed from process.argv
process.argv.splice(1, 2);
process.argv[1] = require("path").resolve(process.argv[1]);
// re-init cli
require("module").runMain();
}
...
// Test whether a given character code starts an identifier.
function isIdentifierStart(code, astral) {
if (code < 65) { return code === 36 }
if (code < 91) { return true }
if (code < 97) { return code === 95 }
if (code < 123) { return true }
if (code <= 0xffff) { return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code)) }
if (astral === false) { return false }
return isInAstralSet(code, astralIdentifierStartCodes)
}
// Test whether a given character is part of an identifier.
function isIdentifierChar(code, astral) {
...