UNPKG

2.15 kBJavaScriptView Raw
1'use strict';
2
3var esprima = require('esprima');
4var escodegen = require('escodegen');
5var start = {
6 type: 'ExpressionStatement',
7 expression: {
8 type: 'CallExpression',
9 callee: {
10 type: 'MemberExpression',
11 computed: false,
12 object: {
13 type: 'MemberExpression',
14 computed: false,
15 object: {
16 type: 'MemberExpression',
17 computed: false,
18 object: {
19 type: 'Identifier',
20 name: 'window'
21 },
22 property: {
23 type: 'Identifier',
24 name: 'ClientCoverage'
25 }
26 },
27 property: {
28 type: 'Identifier',
29 name: 'JS'
30 }
31 },
32 property: {
33 type: 'Identifier',
34 name: 'visitMethod'
35 }
36 },
37 arguments: [{
38 type: 'Literal',
39 value: 'filename',
40 raw: "'filename'"
41 }, {
42 type: 'Literal',
43 value: 'functionName',
44 raw: "'functionName'"
45 }]
46 }
47};
48
49function instrument(source) {
50 var parsed = esprima.parseModule(source, { jsx: true });
51 for (var i in parsed.body) {
52 if (parsed.body[i].type === 'FunctionDeclaration' || parsed.body[i].type === 'ExportDefaultDeclaration' || parsed.body[i].type === 'ExportNamedDeclaration') {
53 if (parsed.body[i].type === 'FunctionDeclaration') {
54 var ParsedTemp = parsed.body[i];
55 } else {
56 var ParsedTemp = parsed.body[i].declaration;
57 }
58 if (ParsedTemp) {
59 if (ParsedTemp.id) {
60 var funcname = ParsedTemp.id.name;
61 if (funcname !== 'mapStateToProps') {
62 if (!funcname.includes('_')) {
63 var startFunc = JSON.parse(JSON.stringify(start));
64 startFunc.expression.arguments[0].value = this.resourcePath;
65 startFunc.expression.arguments[0].raw = this.resourcePath;
66 startFunc.expression.arguments[1].value = funcname;
67 startFunc.expression.arguments[1].raw = funcname;
68 ParsedTemp.body.body.unshift(startFunc);
69 }
70 }
71 }
72 }
73 }
74 }
75
76 return escodegen.generate(parsed);
77}
78
79module.exports = instrument;
\No newline at end of file