@teamscale/lib-instrument 1.0.0-beta.7 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.cjs +1 -0
- package/lib/index.mjs +2 -0
- package/package.json +21 -6
- package/lib/index.d.ts +0 -5
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js +0 -10
- package/lib/instrumenter.d.ts +0 -20
- package/lib/instrumenter.d.ts.map +0 -1
- package/lib/instrumenter.js +0 -73
- package/lib/origins.d.ts +0 -12
- package/lib/origins.d.ts.map +0 -1
- package/lib/origins.js +0 -57
- package/lib/utils.d.ts +0 -23
- package/lib/utils.d.ts.map +0 -1
- package/lib/utils.js +0 -2
- package/lib/visitor.d.ts +0 -11
- package/lib/visitor.d.ts.map +0 -1
- package/lib/visitor.js +0 -481
package/lib/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
let e=require(`@babel/core`),t=require(`source-map`);const n=(()=>{let e=process.pid,t=0;return{next:()=>{t++;let n;if(t<1e4)n=e*1e4+t;else if(t<1e5)n=e*1e5+t;else throw Error(`Not more that 100k files supported to be instrumented in one run.`);return n.toString(36)}}})();var r=class{sourceMap;originToIdMap;constructor(e){this.originToIdMap=new Map,this.sourceMap=e}ensureKnownOrigin(e){let t,r,i=e.filename??``;this.sourceMap&&(t=this.sourceMap.originalPositionFor({line:e.start.line,column:e.start.column}),r=this.sourceMap.originalPositionFor({line:e.end.line,column:e.end.column}),i=t.source??e.filename),(!t||!r)&&(t={line:e.start.line,column:e.start.column,source:null,name:null},r={line:e.end.line,column:e.end.column,source:null,name:null});let a=this.originToIdMap.get(i);return a||(a=`_$o${n.next()}`,this.originToIdMap.set(i,a)),[a,{start:{line:t.line,column:t.column,index:-1},end:{line:r.line,column:r.column,index:-1},filename:i,identifierName:t.name}]}};const i=/^\s*istanbul\s+ignore\s+(if|else|next)(?=\W|$)/,a=/^\s*istanbul\s+ignore\s+(file)(?=\W|$)/;var o=class{types;attrs;nextIgnore;ignoreClassMethods;reportLogic;shouldInstrumentCallback;origins;constructor(e,t,n=[],i=!1,a){this.attrs={},this.nextIgnore=null,this.ignoreClassMethods=n,this.types=e,this.reportLogic=i,this.shouldInstrumentCallback=a,this.origins=new r(t)}shouldInstrument(e,t){return this.shouldInstrumentCallback?this.shouldInstrumentCallback(e,t):!0}shouldIgnore(e){return this.nextIgnore!==null||!e.node.loc}hintFor(e){let t=null;return e.leadingComments&&e.leadingComments.forEach(e=>{let n=(e.value||``).trim(),r=i.exec(n);r&&(t=r[1])}),t}counterNeedsHoisting(e){return e.isFunctionExpression()||e.isArrowFunctionExpression()||e.isClassExpression()}onEnter(e){let t=e.node;if(this.nextIgnore===null){if(this.hintFor(t)===`next`){this.nextIgnore=t;return}if(this.getAttr(e.node,`skip-all`)!==null&&(this.nextIgnore=t),e.isFunctionExpression()&&this.ignoreClassMethods.some(t=>t===e.node.id?.name)){this.nextIgnore=t;return}if(e.isClassMethod()&&this.ignoreClassMethods.some(t=>t===e.node.key.name)){this.nextIgnore=t;return}}}onExit(e){e.node===this.nextIgnore&&(this.nextIgnore=null),delete e.node.__cov__}setAttr(e,t,n){e.__cov__=e.__cov__||{},e.__cov__[t]=n}getAttr(e,t){let n=e.__cov__;return n?n[t]:null}insertCounter(e,t){let n=this.types;if(e.isBlockStatement())e.node.body.unshift(n.expressionStatement(t));else if(e.isStatement())e.insertBefore(n.expressionStatement(t));else if(this.counterNeedsHoisting(e)&&n.isVariableDeclarator(e.parent)){let r=e.parentPath?.parentPath;r&&n.isExportNamedDeclaration(r.parent)?r.parentPath?.insertBefore(n.expressionStatement(t)):r&&(n.isProgram(r.parent)||n.isBlockStatement(r.parent))?r.insertBefore(n.expressionStatement(t)):e.replaceWith(n.sequenceExpression([t,e.node]))}else e.isExpression()?e.replaceWith(n.sequenceExpression([t,e.node])):console.error(`Unable to insert counter for node type:`,e.node.type)}insertFunctionCounter(e){let t=this.types;if(!e.node?.loc)return;let n=e.node,r;switch(n.type){case`FunctionDeclaration`:case`FunctionExpression`:n.id&&(r=n.id.loc??void 0);break}let i=e.get(`body`),a=e.node.loc??r,[o,c]=this.origins.ensureKnownOrigin(a);if(i.isBlockStatement()&&this.shouldInstrument(e,c)){c.end=c.start;let e=s(o,c);i.node.body.unshift(t.expressionStatement(e))}}insertStatementCounter(e){let t=e.node?.loc;if(!t)return;let[n,r]=this.origins.ensureKnownOrigin(t);if(!this.shouldInstrument(e,r))return;let i=s(n,r);this.insertCounter(e,i)}insertBranchCounter(e,t){if(t??=e.node.loc,!t)return;let[n,r]=this.origins.ensureKnownOrigin(t);if(this.shouldInstrument(e,r)){let t=s(n,r);this.insertCounter(e,t)}}findLeaves(e,t,n,r){e&&(e.type===`LogicalExpression`?this.hintFor(e)!==`next`&&(this.findLeaves(e.left,t,e,`left`),this.findLeaves(e.right,t,e,`right`)):t.push({node:e,parent:n,property:r}))}};function s(e,t){let n=[{type:`Identifier`,name:e},{type:`NumericLiteral`,value:t.start.line}];return t.start.line!==t.end.line&&n.push({type:`NumericLiteral`,value:t.end.line}),{type:`CallExpression`,callee:{type:`Identifier`,name:`_$l`},arguments:n}}function c(e,t){return{type:`VariableDeclaration`,kind:`const`,declarations:[{type:`VariableDeclarator`,id:{type:`Identifier`,name:e},init:{type:`StringLiteral`,value:t}}]}}function l(...e){return{enter:function(t,n){this.onEnter(t),!this.shouldIgnore(t)&&e.forEach(e=>{e.call(this,t,n)})},exit:function(e){this.onExit(e)}}}function u(e){this.insertStatementCounter(e)}function d(e){this.insertBranchCounter(e.get(`right`),void 0)}function f(e){this.insertFunctionCounter(e)}function p(e){this.insertStatementCounter(e.get(`init`))}function m(e){this.insertStatementCounter(e.get(`value`))}function h(e){let t=this.types;if(!e.isSequenceExpression())return;let n=[];for(let t of e.node.expressions){let[r,i]=this.origins.ensureKnownOrigin(t.loc);if(this.shouldInstrument(e,i)){let e=s(r,i);n.push(e)}n.push(t)}e.replaceWith(t.sequenceExpression(n))}function g(e){let t=this.types;if(e.node||e.replaceWith(t.blockStatement([])),e.isSequenceExpression())h.call(this,e);else if(!e.isBlockStatement()){e.replaceWith(t.blockStatement([e.node]));let n=e.node;e.node.loc=n.body[0].loc,n.body[0].leadingComments=e.node.leadingComments,e.node.leadingComments=void 0}}function _(e){return function(t){g.call(this,t.get(e))}}function v(e){let t=this.types;e.node&&!e.isIdentifier()&&e.replaceWith(t.parenthesizedExpression(e.node))}function y(e){return function(t){v.call(this,t.get(e))}}function b(e){let t=e.node,n=this.types;if(!n.isBlockStatement(t.body)){let e=t.body.loc;t.expression&&=!1,t.body=n.blockStatement([n.returnStatement(t.body)]),t.body.loc=e,t.body.body[0].loc=e}}function x(e){let t=e.node,n=this.hintFor(t),r=n===`if`,i=n===`else`;r?this.setAttr(t.consequent,`skip-all`,!0):this.insertBranchCounter(e.get(`consequent`),t.loc),i?this.setAttr(t.alternate,`skip-all`,!0):this.insertBranchCounter(e.get(`alternate`),void 0)}function S(){}function C(e){let t=this.types,n=e.node.loc;if(!n)return;let[r,i]=this.origins.ensureKnownOrigin(n);if(this.shouldInstrument(e,i)){let n=s(r,i);e.node.consequent.unshift(t.expressionStatement(n))}}function w(e){let t=e.node,n=this.hintFor(t.consequent),r=this.hintFor(t.alternate);n!==`next`&&this.insertBranchCounter(e.get(`consequent`),void 0),r!==`next`&&this.insertBranchCounter(e.get(`alternate`),void 0)}function T(e){let t=this.types;if(e.parentPath.node.type===`LogicalExpression`)return;let n=[];this.findLeaves(e.node,n,void 0,void 0);for(let r of n){if(this.hintFor(r.node)===`next`)continue;let n=e.node.loc;if(!n)continue;let[i,a]=this.origins.ensureKnownOrigin(n);if(!this.shouldInstrument(e,a))continue;let o=s(i,a);o&&(r.parent[r.property]=t.sequenceExpression([o,r.node]))}}const E={ArrowFunctionExpression:l(b,f),AssignmentPattern:l(d),BlockStatement:l(),ExportDefaultDeclaration:l(),ExportNamedDeclaration:l(),ClassMethod:l(f),ClassDeclaration:l(y(`superClass`)),ClassProperty:l(m),ClassPrivateProperty:l(m),ObjectMethod:l(f),ExpressionStatement:l(u),BreakStatement:l(u),ContinueStatement:l(u),DebuggerStatement:l(u),ReturnStatement:l(u),ThrowStatement:l(u),TryStatement:l(u),VariableDeclaration:l(),VariableDeclarator:l(p),IfStatement:l(_(`consequent`),_(`alternate`),u,x),ForStatement:l(_(`body`),u),ForInStatement:l(_(`body`),u),ForOfStatement:l(_(`body`),u),WhileStatement:l(_(`body`),u),DoWhileStatement:l(_(`body`),u),SwitchStatement:l(S,u),SwitchCase:l(C),WithStatement:l(_(`body`),u),FunctionDeclaration:l(f),FunctionExpression:l(f),LabeledStatement:l(u),ConditionalExpression:l(w),LogicalExpression:l(T),SequenceExpression:l(h)};function D(e,t){return e.scope.hasBinding(t.varName)}function O(e){return!e?.parent||!(`comments`in e.parent)?[]:e.parent.comments}function k(e){return e?O(e).some(e=>a.test(e.value)):!1}function A(t,n,r){r={...r};let i=new o(t,n,r.ignoreClassMethods,r.reportLogic,r.shouldInstrumentCallback);return{enter(e){k(e.find(e=>e.isProgram()))||D(e,i)||e.traverse(E,i)},exit(n){if(D(n,i))return;let a=i.origins;if(k(n.find(e=>e.isProgram())))return;let o=n.node.body;if(r.codeToPrepend){let t=(0,e.parse)(r.codeToPrepend,{sourceType:`script`});t!==null&&o.unshift(...t.program.body)}for(let[e,t]of a.originToIdMap.entries()){let n=c(t,e);o.unshift(n)}r.isInstrumentedToken&&t.addComment(n.node,`leading`,r.isInstrumentedToken,!1)}}}function j(e){return e===`none`?!1:e===`external`?`both`:e}var M=class{opts;constructor(e){this.opts={...e}}async instrument(n,r,i,a){r??=String(new Date().getTime())+`.js`;let{opts:o}=this,s=i??o.inputSourceMap,c;return s&&(c=await new t.SourceMapConsumer(s)),(0,e.transformSync)(n,{configFile:!1,babelrc:!1,ast:!0,filename:r,inputSourceMap:i,sourceMaps:j(o.produceSourceMap),compact:o.compact,comments:o.preserveComments,parserOpts:{allowAwaitOutsideFunction:!0,allowReturnOutsideFunction:o.autoWrap,sourceType:o.esModules?`module`:`script`,plugins:o.parserPlugins},plugins:[[({types:e})=>{let t=A(e,c,{reportLogic:o.reportLogic,coverageGlobalScopeFunc:o.coverageGlobalScopeFunc,ignoreClassMethods:o.ignoreClassMethods,inputSourceMap:i,isInstrumentedToken:o.isInstrumentedToken,codeToPrepend:o.codeToPrepend,shouldInstrumentCallback:a??o.shouldInstrumentCallback});return{visitor:{Program:{enter:t.enter,exit(e){t.exit(e)}}}}}]]}).code}};function N(e){return new M(e)}exports.createInstrumenter=N,exports.programVisitor=A;
|
package/lib/index.mjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{parse as e,transformSync as t}from"@babel/core";import{SourceMapConsumer as n}from"source-map";const r=(()=>{let e=process.pid,t=0;return{next:()=>{t++;let n;if(t<1e4)n=e*1e4+t;else if(t<1e5)n=e*1e5+t;else throw Error(`Not more that 100k files supported to be instrumented in one run.`);return n.toString(36)}}})();var i=class{sourceMap;originToIdMap;constructor(e){this.originToIdMap=new Map,this.sourceMap=e}ensureKnownOrigin(e){let t,n,i=e.filename??``;this.sourceMap&&(t=this.sourceMap.originalPositionFor({line:e.start.line,column:e.start.column}),n=this.sourceMap.originalPositionFor({line:e.end.line,column:e.end.column}),i=t.source??e.filename),(!t||!n)&&(t={line:e.start.line,column:e.start.column,source:null,name:null},n={line:e.end.line,column:e.end.column,source:null,name:null});let a=this.originToIdMap.get(i);return a||(a=`_$o${r.next()}`,this.originToIdMap.set(i,a)),[a,{start:{line:t.line,column:t.column,index:-1},end:{line:n.line,column:n.column,index:-1},filename:i,identifierName:t.name}]}};const a=/^\s*istanbul\s+ignore\s+(if|else|next)(?=\W|$)/,o=/^\s*istanbul\s+ignore\s+(file)(?=\W|$)/;var s=class{types;attrs;nextIgnore;ignoreClassMethods;reportLogic;shouldInstrumentCallback;origins;constructor(e,t,n=[],r=!1,a){this.attrs={},this.nextIgnore=null,this.ignoreClassMethods=n,this.types=e,this.reportLogic=r,this.shouldInstrumentCallback=a,this.origins=new i(t)}shouldInstrument(e,t){return this.shouldInstrumentCallback?this.shouldInstrumentCallback(e,t):!0}shouldIgnore(e){return this.nextIgnore!==null||!e.node.loc}hintFor(e){let t=null;return e.leadingComments&&e.leadingComments.forEach(e=>{let n=(e.value||``).trim(),r=a.exec(n);r&&(t=r[1])}),t}counterNeedsHoisting(e){return e.isFunctionExpression()||e.isArrowFunctionExpression()||e.isClassExpression()}onEnter(e){let t=e.node;if(this.nextIgnore===null){if(this.hintFor(t)===`next`){this.nextIgnore=t;return}if(this.getAttr(e.node,`skip-all`)!==null&&(this.nextIgnore=t),e.isFunctionExpression()&&this.ignoreClassMethods.some(t=>t===e.node.id?.name)){this.nextIgnore=t;return}if(e.isClassMethod()&&this.ignoreClassMethods.some(t=>t===e.node.key.name)){this.nextIgnore=t;return}}}onExit(e){e.node===this.nextIgnore&&(this.nextIgnore=null),delete e.node.__cov__}setAttr(e,t,n){e.__cov__=e.__cov__||{},e.__cov__[t]=n}getAttr(e,t){let n=e.__cov__;return n?n[t]:null}insertCounter(e,t){let n=this.types;if(e.isBlockStatement())e.node.body.unshift(n.expressionStatement(t));else if(e.isStatement())e.insertBefore(n.expressionStatement(t));else if(this.counterNeedsHoisting(e)&&n.isVariableDeclarator(e.parent)){let r=e.parentPath?.parentPath;r&&n.isExportNamedDeclaration(r.parent)?r.parentPath?.insertBefore(n.expressionStatement(t)):r&&(n.isProgram(r.parent)||n.isBlockStatement(r.parent))?r.insertBefore(n.expressionStatement(t)):e.replaceWith(n.sequenceExpression([t,e.node]))}else e.isExpression()?e.replaceWith(n.sequenceExpression([t,e.node])):console.error(`Unable to insert counter for node type:`,e.node.type)}insertFunctionCounter(e){let t=this.types;if(!e.node?.loc)return;let n=e.node,r;switch(n.type){case`FunctionDeclaration`:case`FunctionExpression`:n.id&&(r=n.id.loc??void 0);break}let i=e.get(`body`),a=e.node.loc??r,[o,s]=this.origins.ensureKnownOrigin(a);if(i.isBlockStatement()&&this.shouldInstrument(e,s)){s.end=s.start;let e=c(o,s);i.node.body.unshift(t.expressionStatement(e))}}insertStatementCounter(e){let t=e.node?.loc;if(!t)return;let[n,r]=this.origins.ensureKnownOrigin(t);if(!this.shouldInstrument(e,r))return;let i=c(n,r);this.insertCounter(e,i)}insertBranchCounter(e,t){if(t??=e.node.loc,!t)return;let[n,r]=this.origins.ensureKnownOrigin(t);if(this.shouldInstrument(e,r)){let t=c(n,r);this.insertCounter(e,t)}}findLeaves(e,t,n,r){e&&(e.type===`LogicalExpression`?this.hintFor(e)!==`next`&&(this.findLeaves(e.left,t,e,`left`),this.findLeaves(e.right,t,e,`right`)):t.push({node:e,parent:n,property:r}))}};function c(e,t){let n=[{type:`Identifier`,name:e},{type:`NumericLiteral`,value:t.start.line}];return t.start.line!==t.end.line&&n.push({type:`NumericLiteral`,value:t.end.line}),{type:`CallExpression`,callee:{type:`Identifier`,name:`_$l`},arguments:n}}function l(e,t){return{type:`VariableDeclaration`,kind:`const`,declarations:[{type:`VariableDeclarator`,id:{type:`Identifier`,name:e},init:{type:`StringLiteral`,value:t}}]}}function u(...e){return{enter:function(t,n){this.onEnter(t),!this.shouldIgnore(t)&&e.forEach(e=>{e.call(this,t,n)})},exit:function(e){this.onExit(e)}}}function d(e){this.insertStatementCounter(e)}function f(e){this.insertBranchCounter(e.get(`right`),void 0)}function p(e){this.insertFunctionCounter(e)}function m(e){this.insertStatementCounter(e.get(`init`))}function h(e){this.insertStatementCounter(e.get(`value`))}function g(e){let t=this.types;if(!e.isSequenceExpression())return;let n=[];for(let t of e.node.expressions){let[r,i]=this.origins.ensureKnownOrigin(t.loc);if(this.shouldInstrument(e,i)){let e=c(r,i);n.push(e)}n.push(t)}e.replaceWith(t.sequenceExpression(n))}function _(e){let t=this.types;if(e.node||e.replaceWith(t.blockStatement([])),e.isSequenceExpression())g.call(this,e);else if(!e.isBlockStatement()){e.replaceWith(t.blockStatement([e.node]));let n=e.node;e.node.loc=n.body[0].loc,n.body[0].leadingComments=e.node.leadingComments,e.node.leadingComments=void 0}}function v(e){return function(t){_.call(this,t.get(e))}}function y(e){let t=this.types;e.node&&!e.isIdentifier()&&e.replaceWith(t.parenthesizedExpression(e.node))}function b(e){return function(t){y.call(this,t.get(e))}}function x(e){let t=e.node,n=this.types;if(!n.isBlockStatement(t.body)){let e=t.body.loc;t.expression&&=!1,t.body=n.blockStatement([n.returnStatement(t.body)]),t.body.loc=e,t.body.body[0].loc=e}}function S(e){let t=e.node,n=this.hintFor(t),r=n===`if`,i=n===`else`;r?this.setAttr(t.consequent,`skip-all`,!0):this.insertBranchCounter(e.get(`consequent`),t.loc),i?this.setAttr(t.alternate,`skip-all`,!0):this.insertBranchCounter(e.get(`alternate`),void 0)}function C(){}function w(e){let t=this.types,n=e.node.loc;if(!n)return;let[r,i]=this.origins.ensureKnownOrigin(n);if(this.shouldInstrument(e,i)){let n=c(r,i);e.node.consequent.unshift(t.expressionStatement(n))}}function T(e){let t=e.node,n=this.hintFor(t.consequent),r=this.hintFor(t.alternate);n!==`next`&&this.insertBranchCounter(e.get(`consequent`),void 0),r!==`next`&&this.insertBranchCounter(e.get(`alternate`),void 0)}function E(e){let t=this.types;if(e.parentPath.node.type===`LogicalExpression`)return;let n=[];this.findLeaves(e.node,n,void 0,void 0);for(let r of n){if(this.hintFor(r.node)===`next`)continue;let n=e.node.loc;if(!n)continue;let[i,a]=this.origins.ensureKnownOrigin(n);if(!this.shouldInstrument(e,a))continue;let o=c(i,a);o&&(r.parent[r.property]=t.sequenceExpression([o,r.node]))}}const D={ArrowFunctionExpression:u(x,p),AssignmentPattern:u(f),BlockStatement:u(),ExportDefaultDeclaration:u(),ExportNamedDeclaration:u(),ClassMethod:u(p),ClassDeclaration:u(b(`superClass`)),ClassProperty:u(h),ClassPrivateProperty:u(h),ObjectMethod:u(p),ExpressionStatement:u(d),BreakStatement:u(d),ContinueStatement:u(d),DebuggerStatement:u(d),ReturnStatement:u(d),ThrowStatement:u(d),TryStatement:u(d),VariableDeclaration:u(),VariableDeclarator:u(m),IfStatement:u(v(`consequent`),v(`alternate`),d,S),ForStatement:u(v(`body`),d),ForInStatement:u(v(`body`),d),ForOfStatement:u(v(`body`),d),WhileStatement:u(v(`body`),d),DoWhileStatement:u(v(`body`),d),SwitchStatement:u(C,d),SwitchCase:u(w),WithStatement:u(v(`body`),d),FunctionDeclaration:u(p),FunctionExpression:u(p),LabeledStatement:u(d),ConditionalExpression:u(T),LogicalExpression:u(E),SequenceExpression:u(g)};function O(e,t){return e.scope.hasBinding(t.varName)}function k(e){return!e?.parent||!(`comments`in e.parent)?[]:e.parent.comments}function A(e){return e?k(e).some(e=>o.test(e.value)):!1}function j(t,n,r){r={...r};let i=new s(t,n,r.ignoreClassMethods,r.reportLogic,r.shouldInstrumentCallback);return{enter(e){A(e.find(e=>e.isProgram()))||O(e,i)||e.traverse(D,i)},exit(n){if(O(n,i))return;let a=i.origins;if(A(n.find(e=>e.isProgram())))return;let o=n.node.body;if(r.codeToPrepend){let t=e(r.codeToPrepend,{sourceType:`script`});t!==null&&o.unshift(...t.program.body)}for(let[e,t]of a.originToIdMap.entries()){let n=l(t,e);o.unshift(n)}r.isInstrumentedToken&&t.addComment(n.node,`leading`,r.isInstrumentedToken,!1)}}}function M(e){return e===`none`?!1:e===`external`?`both`:e}var N=class{opts;constructor(e){this.opts={...e}}async instrument(e,r,i,a){r??=String(new Date().getTime())+`.js`;let{opts:o}=this,s=i??o.inputSourceMap,c;return s&&(c=await new n(s)),t(e,{configFile:!1,babelrc:!1,ast:!0,filename:r,inputSourceMap:i,sourceMaps:M(o.produceSourceMap),compact:o.compact,comments:o.preserveComments,parserOpts:{allowAwaitOutsideFunction:!0,allowReturnOutsideFunction:o.autoWrap,sourceType:o.esModules?`module`:`script`,plugins:o.parserPlugins},plugins:[[({types:e})=>{let t=j(e,c,{reportLogic:o.reportLogic,coverageGlobalScopeFunc:o.coverageGlobalScopeFunc,ignoreClassMethods:o.ignoreClassMethods,inputSourceMap:i,isInstrumentedToken:o.isInstrumentedToken,codeToPrepend:o.codeToPrepend,shouldInstrumentCallback:a??o.shouldInstrumentCallback});return{visitor:{Program:{enter:t.enter,exit(e){t.exit(e)}}}}}]]}).code}};function P(e){return new N(e)}export{P as createInstrumenter,j as programVisitor};
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@teamscale/lib-instrument",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "Library for adding coverage statements to JS code; forked from istanbul-lib-coverage",
|
|
5
5
|
"author": "Krishnan Anantheswaran <kananthmail-github@yahoo.com>",
|
|
6
6
|
"maintainers": [
|
|
@@ -8,15 +8,30 @@
|
|
|
8
8
|
"name": "CQSE GmbH"
|
|
9
9
|
}
|
|
10
10
|
],
|
|
11
|
-
"main": "lib/index.
|
|
12
|
-
"
|
|
11
|
+
"main": "lib/index.cjs",
|
|
12
|
+
"module": "lib/index.mjs",
|
|
13
|
+
"types": "lib/index.d.cts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"require": {
|
|
17
|
+
"types": "./lib/index.d.cts",
|
|
18
|
+
"default": "./lib/index.cjs"
|
|
19
|
+
},
|
|
20
|
+
"import": {
|
|
21
|
+
"types": "./lib/index.d.mts",
|
|
22
|
+
"default": "./lib/index.mjs"
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
},
|
|
13
26
|
"files": [
|
|
14
|
-
"lib"
|
|
27
|
+
"lib/*.mjs",
|
|
28
|
+
"lib/*.cjs"
|
|
15
29
|
],
|
|
16
30
|
"dependencies": {
|
|
17
31
|
"@babel/core": "^7.28.5",
|
|
18
32
|
"@babel/parser": "^7.28.5",
|
|
19
33
|
"@babel/traverse": "^7.28.5",
|
|
34
|
+
"@babel/types": "^7.28.5",
|
|
20
35
|
"@types/node": "^22.18.12",
|
|
21
36
|
"source-map": "^0.7.6",
|
|
22
37
|
"typescript": "^5.8.3"
|
|
@@ -27,8 +42,8 @@
|
|
|
27
42
|
"access": "public"
|
|
28
43
|
},
|
|
29
44
|
"scripts": {
|
|
30
|
-
"clean": "rimraf lib
|
|
31
|
-
"build": "tsc",
|
|
45
|
+
"clean": "rimraf lib",
|
|
46
|
+
"build": "pnpm tsc --noEmit && pnpm tsdown",
|
|
32
47
|
"test": "node --import tsx --test test/**/*.test.ts"
|
|
33
48
|
}
|
|
34
49
|
}
|
package/lib/index.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { Instrumenter, InstrumenterOptions } from './instrumenter';
|
|
2
|
-
export type { InstrumenterOptions } from './instrumenter';
|
|
3
|
-
export { programVisitor } from './visitor';
|
|
4
|
-
export declare function createInstrumenter(opts: InstrumenterOptions): Instrumenter;
|
|
5
|
-
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAE,mBAAmB,EAAC,MAAM,gBAAgB,CAAC;AAEjE,YAAY,EAAC,mBAAmB,EAAC,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAC,cAAc,EAAC,MAAM,WAAW,CAAC;AAOzC,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,mBAAmB,GAAG,YAAY,CAE1E"}
|
package/lib/index.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.programVisitor = void 0;
|
|
4
|
-
exports.createInstrumenter = createInstrumenter;
|
|
5
|
-
const instrumenter_1 = require("./instrumenter");
|
|
6
|
-
var visitor_1 = require("./visitor");
|
|
7
|
-
Object.defineProperty(exports, "programVisitor", { enumerable: true, get: function () { return visitor_1.programVisitor; } });
|
|
8
|
-
function createInstrumenter(opts) {
|
|
9
|
-
return new instrumenter_1.Instrumenter(opts);
|
|
10
|
-
}
|
package/lib/instrumenter.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { NodePath } from '@babel/core';
|
|
2
|
-
import { SourceLocation } from "@babel/types";
|
|
3
|
-
import { ParserPlugin as PluginConfig } from '@babel/parser';
|
|
4
|
-
import { RawSourceMap } from "source-map";
|
|
5
|
-
import { InstrumentationOptions } from "./utils";
|
|
6
|
-
export type InstrumenterOptions = InstrumentationOptions & Partial<{
|
|
7
|
-
preserveComments: boolean;
|
|
8
|
-
compact: boolean;
|
|
9
|
-
esModules: boolean;
|
|
10
|
-
autoWrap: boolean;
|
|
11
|
-
produceSourceMap: 'none' | 'inline' | 'external';
|
|
12
|
-
debug: boolean;
|
|
13
|
-
parserPlugins: PluginConfig[];
|
|
14
|
-
}>;
|
|
15
|
-
export declare class Instrumenter {
|
|
16
|
-
private readonly opts;
|
|
17
|
-
constructor(opts?: Partial<InstrumenterOptions>);
|
|
18
|
-
instrument(code: string, filename: string | undefined, inputSourceMap: RawSourceMap | undefined, shouldInstrumentCallback?: (path: NodePath, location: SourceLocation) => boolean): Promise<string>;
|
|
19
|
-
}
|
|
20
|
-
//# sourceMappingURL=instrumenter.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"instrumenter.d.ts","sourceRoot":"","sources":["../src/instrumenter.ts"],"names":[],"mappings":"AAIA,OAAO,EAAC,QAAQ,EAAgB,MAAM,aAAa,CAAC;AACpD,OAAO,EAAC,cAAc,EAAC,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAC,YAAY,IAAI,YAAY,EAAC,MAAM,eAAe,CAAC;AAC3D,OAAO,EAAC,YAAY,EAAoB,MAAM,YAAY,CAAC;AAG3D,OAAO,EAAC,sBAAsB,EAAC,MAAM,SAAS,CAAC;AAK/C,MAAM,MAAM,mBAAmB,GAAG,sBAAsB,GAAG,OAAO,CAAC;IAE/D,gBAAgB,EAAE,OAAO,CAAC;IAG1B,OAAO,EAAE,OAAO,CAAC;IAGjB,SAAS,EAAE,OAAO,CAAC;IAGnB,QAAQ,EAAE,OAAO,CAAC;IAGlB,gBAAgB,EAAE,MAAM,GAAG,QAAQ,GAAG,UAAU,CAAC;IAGjD,KAAK,EAAE,OAAO,CAAC;IAGf,aAAa,EAAE,YAAY,EAAE,CAAC;CACjC,CAAC,CAAC;AAeH,qBAAa,YAAY;IAErB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAsB;gBAE/B,IAAI,CAAC,EAAE,OAAO,CAAC,mBAAmB,CAAC;IAgBzC,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,SAAS,EAAE,cAAc,EAAE,YAAY,GAAG,SAAS,EACpF,wBAAwB,CAAC,EAAE,CAAC,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,cAAc,KAAK,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC;CA0DtH"}
|
package/lib/instrumenter.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Instrumenter = void 0;
|
|
4
|
-
const core_1 = require("@babel/core");
|
|
5
|
-
const source_map_1 = require("source-map");
|
|
6
|
-
const visitor_1 = require("./visitor");
|
|
7
|
-
function mapSourceMapsOption(produceSourceMap) {
|
|
8
|
-
if (produceSourceMap === "none") {
|
|
9
|
-
return false;
|
|
10
|
-
}
|
|
11
|
-
if (produceSourceMap === "external") {
|
|
12
|
-
return "both";
|
|
13
|
-
}
|
|
14
|
-
return produceSourceMap;
|
|
15
|
-
}
|
|
16
|
-
class Instrumenter {
|
|
17
|
-
opts;
|
|
18
|
-
constructor(opts) {
|
|
19
|
-
this.opts = { ...opts };
|
|
20
|
-
}
|
|
21
|
-
async instrument(code, filename, inputSourceMap, shouldInstrumentCallback) {
|
|
22
|
-
filename = filename ?? String(new Date().getTime()) + '.js';
|
|
23
|
-
const { opts } = this;
|
|
24
|
-
const sourceMapToUse = inputSourceMap ?? opts.inputSourceMap;
|
|
25
|
-
let inputSourceMapConsumer = undefined;
|
|
26
|
-
if (sourceMapToUse) {
|
|
27
|
-
inputSourceMapConsumer = await new source_map_1.SourceMapConsumer(sourceMapToUse);
|
|
28
|
-
}
|
|
29
|
-
const babelOpts = {
|
|
30
|
-
configFile: false,
|
|
31
|
-
babelrc: false,
|
|
32
|
-
ast: true,
|
|
33
|
-
filename,
|
|
34
|
-
inputSourceMap,
|
|
35
|
-
sourceMaps: mapSourceMapsOption(opts.produceSourceMap),
|
|
36
|
-
compact: opts.compact,
|
|
37
|
-
comments: opts.preserveComments,
|
|
38
|
-
parserOpts: {
|
|
39
|
-
allowAwaitOutsideFunction: true,
|
|
40
|
-
allowReturnOutsideFunction: opts.autoWrap,
|
|
41
|
-
sourceType: (opts.esModules ? 'module' : 'script'),
|
|
42
|
-
plugins: opts.parserPlugins
|
|
43
|
-
},
|
|
44
|
-
plugins: [
|
|
45
|
-
[
|
|
46
|
-
({ types }) => {
|
|
47
|
-
const ee = (0, visitor_1.programVisitor)(types, inputSourceMapConsumer, {
|
|
48
|
-
reportLogic: opts.reportLogic,
|
|
49
|
-
coverageGlobalScopeFunc: opts.coverageGlobalScopeFunc,
|
|
50
|
-
ignoreClassMethods: opts.ignoreClassMethods,
|
|
51
|
-
inputSourceMap,
|
|
52
|
-
isInstrumentedToken: opts.isInstrumentedToken,
|
|
53
|
-
codeToPrepend: opts.codeToPrepend,
|
|
54
|
-
shouldInstrumentCallback: shouldInstrumentCallback ?? opts.shouldInstrumentCallback
|
|
55
|
-
});
|
|
56
|
-
return {
|
|
57
|
-
visitor: {
|
|
58
|
-
Program: {
|
|
59
|
-
enter: ee.enter,
|
|
60
|
-
exit(path) {
|
|
61
|
-
ee.exit(path);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
]
|
|
68
|
-
]
|
|
69
|
-
};
|
|
70
|
-
return (0, core_1.transformSync)(code, babelOpts).code;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
exports.Instrumenter = Instrumenter;
|
package/lib/origins.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { SourceLocation } from "@babel/types";
|
|
2
|
-
import { SourceMapConsumer } from "source-map";
|
|
3
|
-
export declare const fileIdSeqGenerator: {
|
|
4
|
-
next: () => string;
|
|
5
|
-
};
|
|
6
|
-
export declare class SourceOrigins {
|
|
7
|
-
private readonly sourceMap?;
|
|
8
|
-
readonly originToIdMap: Map<string, string>;
|
|
9
|
-
constructor(sourceMap: SourceMapConsumer | undefined);
|
|
10
|
-
ensureKnownOrigin(loc: SourceLocation): [string, SourceLocation];
|
|
11
|
-
}
|
|
12
|
-
//# sourceMappingURL=origins.d.ts.map
|
package/lib/origins.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"origins.d.ts","sourceRoot":"","sources":["../src/origins.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAC,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAyB,iBAAiB,EAAC,MAAM,YAAY,CAAC;AAUrE,eAAO,MAAM,kBAAkB,EAAE;IAAE,IAAI,EAAE,MAAM,MAAM,CAAA;CAkBjD,CAAC;AAML,qBAAa,aAAa;IAEtB,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAoB;IAM/C,SAAgB,aAAa,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;gBAEvC,SAAS,EAAE,iBAAiB,GAAG,SAAS;IASpD,iBAAiB,CAAC,GAAG,EAAE,cAAc,GAAG,CAAC,MAAM,EAAE,cAAc,CAAC;CA6BnE"}
|
package/lib/origins.js
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SourceOrigins = exports.fileIdSeqGenerator = void 0;
|
|
4
|
-
exports.fileIdSeqGenerator = (() => {
|
|
5
|
-
const instrumenterRunId = process.pid;
|
|
6
|
-
let fileIdSeq = 0;
|
|
7
|
-
return {
|
|
8
|
-
next: () => {
|
|
9
|
-
fileIdSeq++;
|
|
10
|
-
let num;
|
|
11
|
-
if (fileIdSeq < 10000) {
|
|
12
|
-
num = instrumenterRunId * 10000 + fileIdSeq;
|
|
13
|
-
}
|
|
14
|
-
else if (fileIdSeq < 100000) {
|
|
15
|
-
num = instrumenterRunId * 100000 + fileIdSeq;
|
|
16
|
-
}
|
|
17
|
-
else {
|
|
18
|
-
throw new Error(`Not more that 100k files supported to be instrumented in one run.`);
|
|
19
|
-
}
|
|
20
|
-
return num.toString(36);
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
})();
|
|
24
|
-
class SourceOrigins {
|
|
25
|
-
sourceMap;
|
|
26
|
-
originToIdMap;
|
|
27
|
-
constructor(sourceMap) {
|
|
28
|
-
this.originToIdMap = new Map();
|
|
29
|
-
this.sourceMap = sourceMap;
|
|
30
|
-
}
|
|
31
|
-
ensureKnownOrigin(loc) {
|
|
32
|
-
let startPos = undefined;
|
|
33
|
-
let endPos = undefined;
|
|
34
|
-
let filename = loc.filename ?? '';
|
|
35
|
-
if (this.sourceMap) {
|
|
36
|
-
startPos = this.sourceMap.originalPositionFor({ line: loc.start.line, column: loc.start.column });
|
|
37
|
-
endPos = this.sourceMap.originalPositionFor({ line: loc.end.line, column: loc.end.column });
|
|
38
|
-
filename = startPos.source ?? loc.filename;
|
|
39
|
-
}
|
|
40
|
-
if (!startPos || !endPos) {
|
|
41
|
-
startPos = { line: loc.start.line, column: loc.start.column, source: null, name: null };
|
|
42
|
-
endPos = { line: loc.end.line, column: loc.end.column, source: null, name: null };
|
|
43
|
-
}
|
|
44
|
-
let id = this.originToIdMap.get(filename);
|
|
45
|
-
if (!id) {
|
|
46
|
-
id = `_$o${exports.fileIdSeqGenerator.next()}`;
|
|
47
|
-
this.originToIdMap.set(filename, id);
|
|
48
|
-
}
|
|
49
|
-
return [id, {
|
|
50
|
-
start: { line: startPos.line, column: startPos.column, index: -1 },
|
|
51
|
-
end: { line: endPos.line, column: endPos.column, index: -1 },
|
|
52
|
-
filename,
|
|
53
|
-
identifierName: startPos.name
|
|
54
|
-
}];
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
exports.SourceOrigins = SourceOrigins;
|
package/lib/utils.d.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { SourceLocation } from "@babel/types";
|
|
2
|
-
import { RawSourceMap } from "source-map";
|
|
3
|
-
import { NodePath } from "@babel/core";
|
|
4
|
-
export type InstrumentationOptions = Partial<{
|
|
5
|
-
reportLogic: boolean;
|
|
6
|
-
coverageGlobalScopeFunc: boolean;
|
|
7
|
-
ignoreClassMethods: string[];
|
|
8
|
-
inputSourceMap?: RawSourceMap;
|
|
9
|
-
isInstrumentedToken?: string;
|
|
10
|
-
codeToPrepend?: string;
|
|
11
|
-
shouldInstrumentCallback?: (path: NodePath, loc: SourceLocation) => boolean;
|
|
12
|
-
}>;
|
|
13
|
-
export type CodeRange = {
|
|
14
|
-
start: {
|
|
15
|
-
line?: number;
|
|
16
|
-
column?: number;
|
|
17
|
-
};
|
|
18
|
-
end: {
|
|
19
|
-
line?: number;
|
|
20
|
-
column?: number;
|
|
21
|
-
};
|
|
22
|
-
};
|
|
23
|
-
//# sourceMappingURL=utils.d.ts.map
|
package/lib/utils.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAC,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAC,YAAY,EAAC,MAAM,YAAY,CAAC;AACxC,OAAO,EAAC,QAAQ,EAAC,MAAM,aAAa,CAAC;AAKrC,MAAM,MAAM,sBAAsB,GAAG,OAAO,CAAC;IAEzC,WAAW,EAAE,OAAO,CAAC;IAGrB,uBAAuB,EAAE,OAAO,CAAC;IAGjC,kBAAkB,EAAE,MAAM,EAAE,CAAC;IAG7B,cAAc,CAAC,EAAE,YAAY,CAAC;IAG9B,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAG7B,aAAa,CAAC,EAAE,MAAM,CAAC;IAGvB,wBAAwB,CAAC,EAAE,CAAC,IAAI,EAAE,QAAQ,EAAE,GAAG,EAAE,cAAc,KAAK,OAAO,CAAC;CAC/E,CAAC,CAAC;AAKH,MAAM,MAAM,SAAS,GAAG;IACpB,KAAK,EAAE;QAAE,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IAC1C,GAAG,EAAE;QAAE,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CAC3C,CAAC"}
|
package/lib/utils.js
DELETED
package/lib/visitor.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { NodePath } from '@babel/core';
|
|
2
|
-
import { Program } from "@babel/types";
|
|
3
|
-
type BabelTypes = typeof import("@babel/types");
|
|
4
|
-
import { SourceMapConsumer } from "source-map";
|
|
5
|
-
import { InstrumentationOptions } from "./utils";
|
|
6
|
-
export declare function programVisitor(types: BabelTypes, inputSourceMapConsumer: SourceMapConsumer | undefined, opts: InstrumentationOptions): {
|
|
7
|
-
enter(path: NodePath<Program>): void;
|
|
8
|
-
exit(path: NodePath<Program>): void;
|
|
9
|
-
};
|
|
10
|
-
export {};
|
|
11
|
-
//# sourceMappingURL=visitor.d.ts.map
|
package/lib/visitor.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"visitor.d.ts","sourceRoot":"","sources":["../src/visitor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAO,QAAQ,EAAQ,MAAM,aAAa,CAAC;AAElD,OAAO,EAK8C,OAAO,EAE3D,MAAM,cAAc,CAAC;AAEtB,KAAK,UAAU,GAAG,cAAc,cAAc,CAAC,CAAA;AAE/C,OAAO,EAAC,iBAAiB,EAAC,MAAM,YAAY,CAAC;AAG7C,OAAO,EAAC,sBAAsB,EAAC,MAAM,SAAS,CAAC;AAwnB/C,wBAAgB,cAAc,CAAC,KAAK,EAAE,UAAU,EACjB,sBAAsB,EAAE,iBAAiB,GAAG,SAAS,EACrD,IAAI,EAAE,sBAAsB;gBAYvC,QAAQ,CAAC,OAAO,CAAC,GAAG,IAAI;eASzB,QAAQ,CAAC,OAAO,CAAC;EA+BnC"}
|
package/lib/visitor.js
DELETED
|
@@ -1,481 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.programVisitor = programVisitor;
|
|
4
|
-
const core_1 = require("@babel/core");
|
|
5
|
-
const origins_1 = require("./origins");
|
|
6
|
-
const COMMENT_RE = /^\s*istanbul\s+ignore\s+(if|else|next)(?=\W|$)/;
|
|
7
|
-
const COMMENT_FILE_RE = /^\s*istanbul\s+ignore\s+(file)(?=\W|$)/;
|
|
8
|
-
class VisitState {
|
|
9
|
-
types;
|
|
10
|
-
attrs;
|
|
11
|
-
nextIgnore;
|
|
12
|
-
ignoreClassMethods;
|
|
13
|
-
reportLogic;
|
|
14
|
-
shouldInstrumentCallback;
|
|
15
|
-
origins;
|
|
16
|
-
constructor(types, inputSourceMapConsumer, ignoreClassMethods = [], reportLogic = false, shouldInstrumentCallback) {
|
|
17
|
-
this.attrs = {};
|
|
18
|
-
this.nextIgnore = null;
|
|
19
|
-
this.ignoreClassMethods = ignoreClassMethods;
|
|
20
|
-
this.types = types;
|
|
21
|
-
this.reportLogic = reportLogic;
|
|
22
|
-
this.shouldInstrumentCallback = shouldInstrumentCallback;
|
|
23
|
-
this.origins = new origins_1.SourceOrigins(inputSourceMapConsumer);
|
|
24
|
-
}
|
|
25
|
-
shouldInstrument(path, loc) {
|
|
26
|
-
if (this.shouldInstrumentCallback) {
|
|
27
|
-
return this.shouldInstrumentCallback(path, loc);
|
|
28
|
-
}
|
|
29
|
-
return true;
|
|
30
|
-
}
|
|
31
|
-
shouldIgnore(path) {
|
|
32
|
-
return this.nextIgnore !== null || !path.node.loc;
|
|
33
|
-
}
|
|
34
|
-
hintFor(node) {
|
|
35
|
-
let hint = null;
|
|
36
|
-
if (node.leadingComments) {
|
|
37
|
-
node.leadingComments.forEach(c => {
|
|
38
|
-
const v = (c.value || '').trim();
|
|
39
|
-
const groups = COMMENT_RE.exec(v);
|
|
40
|
-
if (groups) {
|
|
41
|
-
hint = groups[1];
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
return hint;
|
|
46
|
-
}
|
|
47
|
-
counterNeedsHoisting(path) {
|
|
48
|
-
return (path.isFunctionExpression() ||
|
|
49
|
-
path.isArrowFunctionExpression() ||
|
|
50
|
-
path.isClassExpression());
|
|
51
|
-
}
|
|
52
|
-
onEnter(path) {
|
|
53
|
-
const n = path.node;
|
|
54
|
-
if (this.nextIgnore !== null) {
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
const hint = this.hintFor(n);
|
|
58
|
-
if (hint === 'next') {
|
|
59
|
-
this.nextIgnore = n;
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
if (this.getAttr(path.node, 'skip-all') !== null) {
|
|
63
|
-
this.nextIgnore = n;
|
|
64
|
-
}
|
|
65
|
-
if (path.isFunctionExpression() &&
|
|
66
|
-
this.ignoreClassMethods.some(name => name === path.node.id?.name)) {
|
|
67
|
-
this.nextIgnore = n;
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
if (path.isClassMethod() &&
|
|
71
|
-
this.ignoreClassMethods.some(name => name === path.node.key.name)) {
|
|
72
|
-
this.nextIgnore = n;
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
onExit(path) {
|
|
77
|
-
if (path.node === this.nextIgnore) {
|
|
78
|
-
this.nextIgnore = null;
|
|
79
|
-
}
|
|
80
|
-
delete path.node.__cov__;
|
|
81
|
-
}
|
|
82
|
-
setAttr(node, name, value) {
|
|
83
|
-
node.__cov__ = node.__cov__ || {};
|
|
84
|
-
node.__cov__[name] = value;
|
|
85
|
-
}
|
|
86
|
-
getAttr(node, name) {
|
|
87
|
-
const c = node.__cov__;
|
|
88
|
-
if (!c) {
|
|
89
|
-
return null;
|
|
90
|
-
}
|
|
91
|
-
return c[name];
|
|
92
|
-
}
|
|
93
|
-
insertCounter(path, increment) {
|
|
94
|
-
const T = this.types;
|
|
95
|
-
if (path.isBlockStatement()) {
|
|
96
|
-
path.node.body.unshift(T.expressionStatement(increment));
|
|
97
|
-
}
|
|
98
|
-
else if (path.isStatement()) {
|
|
99
|
-
path.insertBefore(T.expressionStatement(increment));
|
|
100
|
-
}
|
|
101
|
-
else if (this.counterNeedsHoisting(path) &&
|
|
102
|
-
T.isVariableDeclarator(path.parent)) {
|
|
103
|
-
const grandParentPath = path.parentPath?.parentPath;
|
|
104
|
-
if (grandParentPath && T.isExportNamedDeclaration(grandParentPath.parent)) {
|
|
105
|
-
grandParentPath.parentPath?.insertBefore(T.expressionStatement(increment));
|
|
106
|
-
}
|
|
107
|
-
else if (grandParentPath &&
|
|
108
|
-
(T.isProgram(grandParentPath.parent) ||
|
|
109
|
-
T.isBlockStatement(grandParentPath.parent))) {
|
|
110
|
-
grandParentPath.insertBefore(T.expressionStatement(increment));
|
|
111
|
-
}
|
|
112
|
-
else {
|
|
113
|
-
path.replaceWith(T.sequenceExpression([increment, path.node]));
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
else if (path.isExpression()) {
|
|
117
|
-
path.replaceWith(T.sequenceExpression([increment, path.node]));
|
|
118
|
-
}
|
|
119
|
-
else {
|
|
120
|
-
console.error('Unable to insert counter for node type:', path.node.type);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
insertFunctionCounter(path) {
|
|
124
|
-
const T = this.types;
|
|
125
|
-
if (!(path.node?.loc)) {
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
const n = path.node;
|
|
129
|
-
let declarationLocation;
|
|
130
|
-
switch (n.type) {
|
|
131
|
-
case 'FunctionDeclaration':
|
|
132
|
-
case 'FunctionExpression':
|
|
133
|
-
if (n.id) {
|
|
134
|
-
declarationLocation = n.id.loc ?? undefined;
|
|
135
|
-
}
|
|
136
|
-
break;
|
|
137
|
-
}
|
|
138
|
-
const body = path.get('body');
|
|
139
|
-
const loc = path.node.loc ?? declarationLocation;
|
|
140
|
-
const [originFileId, originPos] = this.origins.ensureKnownOrigin(loc);
|
|
141
|
-
if (body.isBlockStatement() && this.shouldInstrument(path, originPos)) {
|
|
142
|
-
originPos.end = originPos.start;
|
|
143
|
-
const increment = newLineCoverageExpression(originFileId, originPos);
|
|
144
|
-
body.node.body.unshift(T.expressionStatement(increment));
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
insertStatementCounter(path) {
|
|
148
|
-
const loc = path.node?.loc;
|
|
149
|
-
if (!loc) {
|
|
150
|
-
return;
|
|
151
|
-
}
|
|
152
|
-
const [originFileId, originPos] = this.origins.ensureKnownOrigin(loc);
|
|
153
|
-
if (!this.shouldInstrument(path, originPos)) {
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
const increment = newLineCoverageExpression(originFileId, originPos);
|
|
157
|
-
this.insertCounter(path, increment);
|
|
158
|
-
}
|
|
159
|
-
insertBranchCounter(path, loc) {
|
|
160
|
-
loc = loc ?? path.node.loc;
|
|
161
|
-
if (!loc) {
|
|
162
|
-
return;
|
|
163
|
-
}
|
|
164
|
-
const [originFileId, originPos] = this.origins.ensureKnownOrigin(loc);
|
|
165
|
-
if (this.shouldInstrument(path, originPos)) {
|
|
166
|
-
const increment = newLineCoverageExpression(originFileId, originPos);
|
|
167
|
-
this.insertCounter(path, increment);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
findLeaves(node, accumulator, parent, property) {
|
|
171
|
-
if (!node) {
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
174
|
-
if (node.type === 'LogicalExpression') {
|
|
175
|
-
const hint = this.hintFor(node);
|
|
176
|
-
if (hint !== 'next') {
|
|
177
|
-
this.findLeaves(node.left, accumulator, node, 'left');
|
|
178
|
-
this.findLeaves(node.right, accumulator, node, 'right');
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
else {
|
|
182
|
-
accumulator.push({
|
|
183
|
-
node,
|
|
184
|
-
parent: parent,
|
|
185
|
-
property: property
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
function newLineCoverageExpression(originFileId, range) {
|
|
191
|
-
const argumentList = [
|
|
192
|
-
{ type: 'Identifier', name: originFileId },
|
|
193
|
-
{ type: 'NumericLiteral', value: range.start.line }
|
|
194
|
-
];
|
|
195
|
-
if (range.start.line !== range.end.line) {
|
|
196
|
-
argumentList.push({ type: 'NumericLiteral', value: range.end.line });
|
|
197
|
-
}
|
|
198
|
-
return {
|
|
199
|
-
type: 'CallExpression',
|
|
200
|
-
callee: { type: 'Identifier', name: '_$l' },
|
|
201
|
-
arguments: argumentList
|
|
202
|
-
};
|
|
203
|
-
}
|
|
204
|
-
function newStringConstDeclarationNode(name, value) {
|
|
205
|
-
return {
|
|
206
|
-
type: 'VariableDeclaration',
|
|
207
|
-
kind: 'const',
|
|
208
|
-
declarations: [
|
|
209
|
-
{
|
|
210
|
-
type: 'VariableDeclarator',
|
|
211
|
-
id: {
|
|
212
|
-
type: 'Identifier',
|
|
213
|
-
name
|
|
214
|
-
},
|
|
215
|
-
init: {
|
|
216
|
-
type: 'StringLiteral',
|
|
217
|
-
value
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
]
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
function entries(...enter) {
|
|
224
|
-
const wrappedEntry = function (path, node) {
|
|
225
|
-
this.onEnter(path);
|
|
226
|
-
if (this.shouldIgnore(path)) {
|
|
227
|
-
return;
|
|
228
|
-
}
|
|
229
|
-
enter.forEach(e => {
|
|
230
|
-
e.call(this, path, node);
|
|
231
|
-
});
|
|
232
|
-
};
|
|
233
|
-
const exit = function (path) {
|
|
234
|
-
this.onExit(path);
|
|
235
|
-
};
|
|
236
|
-
return {
|
|
237
|
-
enter: wrappedEntry,
|
|
238
|
-
exit
|
|
239
|
-
};
|
|
240
|
-
}
|
|
241
|
-
function coverStatement(path) {
|
|
242
|
-
this.insertStatementCounter(path);
|
|
243
|
-
}
|
|
244
|
-
function coverAssignmentPattern(path) {
|
|
245
|
-
this.insertBranchCounter(path.get('right'), undefined);
|
|
246
|
-
}
|
|
247
|
-
function coverFunction(path) {
|
|
248
|
-
this.insertFunctionCounter(path);
|
|
249
|
-
}
|
|
250
|
-
function coverVariableDeclarator(path) {
|
|
251
|
-
this.insertStatementCounter(path.get('init'));
|
|
252
|
-
}
|
|
253
|
-
function coverClassPropDeclarator(path) {
|
|
254
|
-
this.insertStatementCounter(path.get('value'));
|
|
255
|
-
}
|
|
256
|
-
function coverSequenceExpression(path) {
|
|
257
|
-
const T = this.types;
|
|
258
|
-
if (!path.isSequenceExpression()) {
|
|
259
|
-
return;
|
|
260
|
-
}
|
|
261
|
-
const newExpressions = [];
|
|
262
|
-
for (const expression of path.node.expressions) {
|
|
263
|
-
const [originFileId, originPos] = this.origins.ensureKnownOrigin(expression.loc);
|
|
264
|
-
if (this.shouldInstrument(path, originPos)) {
|
|
265
|
-
const increment = newLineCoverageExpression(originFileId, originPos);
|
|
266
|
-
newExpressions.push(increment);
|
|
267
|
-
}
|
|
268
|
-
newExpressions.push(expression);
|
|
269
|
-
}
|
|
270
|
-
path.replaceWith(T.sequenceExpression(newExpressions));
|
|
271
|
-
}
|
|
272
|
-
function makeBlock(path) {
|
|
273
|
-
const T = this.types;
|
|
274
|
-
if (!path.node) {
|
|
275
|
-
path.replaceWith(T.blockStatement([]));
|
|
276
|
-
}
|
|
277
|
-
if (path.isSequenceExpression()) {
|
|
278
|
-
coverSequenceExpression.call(this, path);
|
|
279
|
-
}
|
|
280
|
-
else if (!path.isBlockStatement()) {
|
|
281
|
-
path.replaceWith(T.blockStatement([path.node]));
|
|
282
|
-
const block = path.node;
|
|
283
|
-
path.node.loc = block.body[0].loc;
|
|
284
|
-
block.body[0].leadingComments = path.node.leadingComments;
|
|
285
|
-
path.node.leadingComments = undefined;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
function blockProp(prop) {
|
|
289
|
-
return function (path) {
|
|
290
|
-
makeBlock.call(this, path.get(prop));
|
|
291
|
-
};
|
|
292
|
-
}
|
|
293
|
-
function makeParenthesizedExpressionForNonIdentifier(path) {
|
|
294
|
-
const T = this.types;
|
|
295
|
-
if (path.node && !path.isIdentifier()) {
|
|
296
|
-
path.replaceWith(T.parenthesizedExpression(path.node));
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
function parenthesizedExpressionProp(prop) {
|
|
300
|
-
return function (path) {
|
|
301
|
-
makeParenthesizedExpressionForNonIdentifier.call(this, path.get(prop));
|
|
302
|
-
};
|
|
303
|
-
}
|
|
304
|
-
function convertArrowExpression(path) {
|
|
305
|
-
const node = path.node;
|
|
306
|
-
const T = this.types;
|
|
307
|
-
if (!T.isBlockStatement(node.body)) {
|
|
308
|
-
const bloc = node.body.loc;
|
|
309
|
-
if (node.expression) {
|
|
310
|
-
node.expression = false;
|
|
311
|
-
}
|
|
312
|
-
node.body = T.blockStatement([T.returnStatement(node.body)]);
|
|
313
|
-
node.body.loc = bloc;
|
|
314
|
-
node.body.body[0].loc = bloc;
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
function coverIfBranches(path) {
|
|
318
|
-
const n = path.node;
|
|
319
|
-
const hint = this.hintFor(n);
|
|
320
|
-
const ignoreIf = hint === 'if';
|
|
321
|
-
const ignoreElse = hint === 'else';
|
|
322
|
-
if (ignoreIf) {
|
|
323
|
-
this.setAttr(n.consequent, 'skip-all', true);
|
|
324
|
-
}
|
|
325
|
-
else {
|
|
326
|
-
this.insertBranchCounter(path.get('consequent'), n.loc);
|
|
327
|
-
}
|
|
328
|
-
if (ignoreElse) {
|
|
329
|
-
this.setAttr(n.alternate, 'skip-all', true);
|
|
330
|
-
}
|
|
331
|
-
else {
|
|
332
|
-
this.insertBranchCounter(path.get('alternate'), undefined);
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
function createSwitchBranch() {
|
|
336
|
-
}
|
|
337
|
-
function coverSwitchCase(path) {
|
|
338
|
-
const T = this.types;
|
|
339
|
-
const loc = path.node.loc;
|
|
340
|
-
if (!loc) {
|
|
341
|
-
return;
|
|
342
|
-
}
|
|
343
|
-
const [originFileId, originPos] = this.origins.ensureKnownOrigin(loc);
|
|
344
|
-
if (this.shouldInstrument(path, originPos)) {
|
|
345
|
-
const increment = newLineCoverageExpression(originFileId, originPos);
|
|
346
|
-
path.node.consequent.unshift(T.expressionStatement(increment));
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
function coverTernary(path) {
|
|
350
|
-
const n = path.node;
|
|
351
|
-
const cHint = this.hintFor(n.consequent);
|
|
352
|
-
const aHint = this.hintFor(n.alternate);
|
|
353
|
-
if (cHint !== 'next') {
|
|
354
|
-
this.insertBranchCounter(path.get('consequent'), undefined);
|
|
355
|
-
}
|
|
356
|
-
if (aHint !== 'next') {
|
|
357
|
-
this.insertBranchCounter(path.get('alternate'), undefined);
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
function coverLogicalExpression(path) {
|
|
361
|
-
const T = this.types;
|
|
362
|
-
if (path.parentPath.node.type === 'LogicalExpression') {
|
|
363
|
-
return;
|
|
364
|
-
}
|
|
365
|
-
const leaves = [];
|
|
366
|
-
this.findLeaves(path.node, leaves, undefined, undefined);
|
|
367
|
-
for (const leaf of leaves) {
|
|
368
|
-
const hint = this.hintFor(leaf.node);
|
|
369
|
-
if (hint === 'next') {
|
|
370
|
-
continue;
|
|
371
|
-
}
|
|
372
|
-
const loc = path.node.loc;
|
|
373
|
-
if (!loc) {
|
|
374
|
-
continue;
|
|
375
|
-
}
|
|
376
|
-
const [originFileId, originPos] = this.origins.ensureKnownOrigin(loc);
|
|
377
|
-
if (!this.shouldInstrument(path, originPos)) {
|
|
378
|
-
continue;
|
|
379
|
-
}
|
|
380
|
-
const increment = newLineCoverageExpression(originFileId, originPos);
|
|
381
|
-
if (!increment) {
|
|
382
|
-
continue;
|
|
383
|
-
}
|
|
384
|
-
leaf.parent[leaf.property] = T.sequenceExpression([
|
|
385
|
-
increment,
|
|
386
|
-
leaf.node
|
|
387
|
-
]);
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
const codeVisitor = {
|
|
391
|
-
ArrowFunctionExpression: entries(convertArrowExpression, coverFunction),
|
|
392
|
-
AssignmentPattern: entries(coverAssignmentPattern),
|
|
393
|
-
BlockStatement: entries(),
|
|
394
|
-
ExportDefaultDeclaration: entries(),
|
|
395
|
-
ExportNamedDeclaration: entries(),
|
|
396
|
-
ClassMethod: entries(coverFunction),
|
|
397
|
-
ClassDeclaration: entries(parenthesizedExpressionProp('superClass')),
|
|
398
|
-
ClassProperty: entries(coverClassPropDeclarator),
|
|
399
|
-
ClassPrivateProperty: entries(coverClassPropDeclarator),
|
|
400
|
-
ObjectMethod: entries(coverFunction),
|
|
401
|
-
ExpressionStatement: entries(coverStatement),
|
|
402
|
-
BreakStatement: entries(coverStatement),
|
|
403
|
-
ContinueStatement: entries(coverStatement),
|
|
404
|
-
DebuggerStatement: entries(coverStatement),
|
|
405
|
-
ReturnStatement: entries(coverStatement),
|
|
406
|
-
ThrowStatement: entries(coverStatement),
|
|
407
|
-
TryStatement: entries(coverStatement),
|
|
408
|
-
VariableDeclaration: entries(),
|
|
409
|
-
VariableDeclarator: entries(coverVariableDeclarator),
|
|
410
|
-
IfStatement: entries(blockProp('consequent'), blockProp('alternate'), coverStatement, coverIfBranches),
|
|
411
|
-
ForStatement: entries(blockProp('body'), coverStatement),
|
|
412
|
-
ForInStatement: entries(blockProp('body'), coverStatement),
|
|
413
|
-
ForOfStatement: entries(blockProp('body'), coverStatement),
|
|
414
|
-
WhileStatement: entries(blockProp('body'), coverStatement),
|
|
415
|
-
DoWhileStatement: entries(blockProp('body'), coverStatement),
|
|
416
|
-
SwitchStatement: entries(createSwitchBranch, coverStatement),
|
|
417
|
-
SwitchCase: entries(coverSwitchCase),
|
|
418
|
-
WithStatement: entries(blockProp('body'), coverStatement),
|
|
419
|
-
FunctionDeclaration: entries(coverFunction),
|
|
420
|
-
FunctionExpression: entries(coverFunction),
|
|
421
|
-
LabeledStatement: entries(coverStatement),
|
|
422
|
-
ConditionalExpression: entries(coverTernary),
|
|
423
|
-
LogicalExpression: entries(coverLogicalExpression),
|
|
424
|
-
SequenceExpression: entries(coverSequenceExpression),
|
|
425
|
-
};
|
|
426
|
-
function alreadyInstrumented(path, visitState) {
|
|
427
|
-
return path.scope.hasBinding(visitState.varName);
|
|
428
|
-
}
|
|
429
|
-
function getParentComments(path) {
|
|
430
|
-
if (!path?.parent) {
|
|
431
|
-
return [];
|
|
432
|
-
}
|
|
433
|
-
if (!('comments' in path.parent)) {
|
|
434
|
-
return [];
|
|
435
|
-
}
|
|
436
|
-
return path.parent.comments;
|
|
437
|
-
}
|
|
438
|
-
function shouldIgnoreFile(programNodePath) {
|
|
439
|
-
if (!programNodePath) {
|
|
440
|
-
return false;
|
|
441
|
-
}
|
|
442
|
-
return getParentComments(programNodePath).some(c => COMMENT_FILE_RE.test(c.value));
|
|
443
|
-
}
|
|
444
|
-
function programVisitor(types, inputSourceMapConsumer, opts) {
|
|
445
|
-
opts = { ...opts };
|
|
446
|
-
const visitState = new VisitState(types, inputSourceMapConsumer, opts.ignoreClassMethods, opts.reportLogic, opts.shouldInstrumentCallback);
|
|
447
|
-
return {
|
|
448
|
-
enter(path) {
|
|
449
|
-
if (shouldIgnoreFile(path.find(p => p.isProgram()))) {
|
|
450
|
-
return;
|
|
451
|
-
}
|
|
452
|
-
if (alreadyInstrumented(path, visitState)) {
|
|
453
|
-
return;
|
|
454
|
-
}
|
|
455
|
-
path.traverse(codeVisitor, visitState);
|
|
456
|
-
},
|
|
457
|
-
exit(path) {
|
|
458
|
-
if (alreadyInstrumented(path, visitState)) {
|
|
459
|
-
return;
|
|
460
|
-
}
|
|
461
|
-
const originData = visitState.origins;
|
|
462
|
-
if (shouldIgnoreFile(path.find(p => p.isProgram()))) {
|
|
463
|
-
return;
|
|
464
|
-
}
|
|
465
|
-
const body = path.node.body;
|
|
466
|
-
if (opts.codeToPrepend) {
|
|
467
|
-
const codeToPrependAst = (0, core_1.parse)(opts.codeToPrepend, { sourceType: 'script' });
|
|
468
|
-
if (codeToPrependAst !== null) {
|
|
469
|
-
body.unshift(...codeToPrependAst.program.body);
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
for (const [originPath, originId] of originData.originToIdMap.entries()) {
|
|
473
|
-
const declaration = newStringConstDeclarationNode(originId, originPath);
|
|
474
|
-
body.unshift(declaration);
|
|
475
|
-
}
|
|
476
|
-
if (opts.isInstrumentedToken) {
|
|
477
|
-
types.addComment(path.node, 'leading', opts.isInstrumentedToken, false);
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
};
|
|
481
|
-
}
|