Skip to content

Commit

Permalink
Fix instruments in lit-html template (#311)
Browse files Browse the repository at this point in the history
Remove references to 'this' from header which breaks the example in #311
Remove redundant escape characters from regular expressions
  • Loading branch information
tntim96 committed Oct 27, 2022
1 parent 127c009 commit 0eca598
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 17 deletions.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
2.0.16 / 2022-??-??
==================
* Fix instruments in lit-html template (https://github.com/tntim96/JSCover/issues/311)
* Requires Java 11
* Upgrade closure-compiler v20220405 to v20221004, gson 2.9.0 to 2.10
* Internal: HtmlUnit 2.61.0 to 2.66.0, Mockito 4.5.1 to 4.8.1
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/jscover/instrument/NodeProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,24 @@ public Node buildFunctionInstrumentationStatement(int functionNumber) {
return statementBuilder.buildFunctionInstrumentationStatement(functionNumber, fileName);
}

boolean isInTemplateLiteral(Node node) {
if (node.isTemplateLit()) {
return true;
}
Node parent = node.getParent();
if (parent == null) {
return false;
}
return isInTemplateLiteral(parent);
}

boolean processNode(Node node) {
if (statementBuilder.isInstrumentation(node)) {
return false;
}
if (isInTemplateLiteral(node)) {
return false;
}
// Function Coverage (HA-CA), tntim96
if (includeFunctionCoverage && node.isFunction() && !node.isArrowFunction()) {
Node block = node.getChildAtIndex(2);
Expand Down
7 changes: 4 additions & 3 deletions src/main/resources/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ if (!jsCover_isolateBrowser) {

try {
if (typeof top === 'object' && top !== null && top._$jscoverage) {
this._$jscoverage = top._$jscoverage;
_$jscoverage = top._$jscoverage;
}
} catch (e) {
}
}
if (!this._$jscoverage) {
this._$jscoverage = {};
var _$jscoverage;
if (!_$jscoverage) {
_$jscoverage = {};
}
2 changes: 1 addition & 1 deletion src/main/resources/jscoverage-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function jscoverage_pad(s) {
}

function jscoverage_html_escape(s) {
return s.replace(/[<>\&\"\']/g, function (c) {
return s.replace(/[<>&"']/g, function (c) {
return '&#' + c.charCodeAt(0) + ';';
});
}
2 changes: 1 addition & 1 deletion src/main/resources/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ if (!window.jscoverage_report) {
//console.log("Configuring async jsreport cb");
request.onreadystatechange = function () {
//console.log("jsreport cb called");
if (request.readyState == 4 && callback) {
if (request.readyState === 4 && callback) {
callback(request);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ function jscoverage_pad(s) {
}

function jscoverage_html_escape(s) {
return s.replace(/[<>\&\"\']/g, function (c) {
return s.replace(/[<>&"']/g, function (c) {
return '&#' + c.charCodeAt(0) + ';';
});
}
Expand Down Expand Up @@ -237,13 +237,14 @@ if (!jsCover_isolateBrowser) {

try {
if (typeof top === 'object' && top !== null && top._$jscoverage) {
this._$jscoverage = top._$jscoverage;
_$jscoverage = top._$jscoverage;
}
} catch (e) {
}
}
if (!this._$jscoverage) {
this._$jscoverage = {};
var _$jscoverage;
if (!_$jscoverage) {
_$jscoverage = {};
}
if (! _$jscoverage['test-simple.js']) {
_$jscoverage['test-simple.js'] = {};
Expand Down
11 changes: 6 additions & 5 deletions src/test-integration/resources/test-instrumented-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ if (!window.jscoverage_report) {
//console.log("Configuring async jsreport cb");
request.onreadystatechange = function () {
//console.log("jsreport cb called");
if (request.readyState == 4 && callback) {
if (request.readyState === 4 && callback) {
callback(request);
}
};
Expand Down Expand Up @@ -231,7 +231,7 @@ function jscoverage_pad(s) {
}

function jscoverage_html_escape(s) {
return s.replace(/[<>\&\"\']/g, function (c) {
return s.replace(/[<>&"']/g, function (c) {
return '&#' + c.charCodeAt(0) + ';';
});
}
Expand Down Expand Up @@ -268,13 +268,14 @@ if (!jsCover_isolateBrowser) {

try {
if (typeof top === 'object' && top !== null && top._$jscoverage) {
this._$jscoverage = top._$jscoverage;
_$jscoverage = top._$jscoverage;
}
} catch (e) {
}
}
if (!this._$jscoverage) {
this._$jscoverage = {};
var _$jscoverage;
if (!_$jscoverage) {
_$jscoverage = {};
}
if (! _$jscoverage['test-simple.js']) {
_$jscoverage['test-simple.js'] = {};
Expand Down
15 changes: 12 additions & 3 deletions src/test/java/jscover/instrument/InstrumenterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,9 @@ You should also get your employer (if you work as a programmer) or your
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.io.IOException;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.junit.Assert.assertEquals;
import static org.mockito.BDDMockito.given;

Expand Down Expand Up @@ -1458,4 +1457,14 @@ public void shouldInstrumentFunctionAfterCommaInAssignment() {
"};\n";
assertEquals(expectedSource, instrumentedSource);
}


@Test
public void shouldInstrumentTemplateLiteral() {
String source = "let x = `\n@mouseenter=\"${(evt) => alert('hovered')}\">`;";
String instrumentedSource = sourceProcessor.instrumentSource(source);
String expectedSource = "_$jscoverage['test.js'].lineData[1]++;\n" +
"let x = `\n@mouseenter=\"${evt => alert('hovered')}\">`;\n";
assertEquals(expectedSource, instrumentedSource);
}
}

0 comments on commit 0eca598

Please # to comment.