Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.whitesource.agent.parser;

import org.apache.commons.lang3.StringUtils;
import org.mozilla.javascript.CompilerEnvirons;
import org.mozilla.javascript.Parser;
import org.mozilla.javascript.ast.AstRoot;
Expand Down Expand Up @@ -71,20 +70,37 @@ public ParseResult parse(String fileContent) {
* Go over each comment and remove from content until reaching the beginning of the actual code.
*/
private String removeHeaderComments(String fileContent, SortedSet<Comment> comments) {
String headerlessFileContent = fileContent;
int contentLength = fileContent.length();
int currentIndex = 0;
for (Comment comment : comments) {
String commentValue = comment.getValue();
if (headerlessFileContent.startsWith(commentValue)) {
headerlessFileContent = headerlessFileContent.replace(commentValue, EMPTY_STRING);
// remove all leading white spaces and new line characters
while (StringUtils.isNotBlank(headerlessFileContent) && Character.isWhitespace(headerlessFileContent.charAt(0))) {
headerlessFileContent = headerlessFileContent.substring(1);
}
} else {
int commentStart = comment.getAbsolutePosition();

if (commentStart > currentIndex) {
// finished removing all header comments
break;
}

if (commentStart + comment.getLength() > contentLength) {
// safety check – malformed comment location
break;
}

if (commentStart < currentIndex) {
// comment already removed as part of a previous comment
continue;
}

currentIndex += comment.getLength();

while (currentIndex < contentLength && Character.isWhitespace(fileContent.charAt(currentIndex))) {
currentIndex++;
}
}
return headerlessFileContent;

if (currentIndex >= contentLength) {
return EMPTY_STRING;
}

return fileContent.substring(currentIndex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ public void testParseJavaScript() throws IOException {
assertTrue(commentlessContent.startsWith("(function(factory) {"));
}

@Test
public void testRemoveHeaderCommentEndingWithP() {
String fileContent = "//# sourceMappingURL=IRotate.js.map";
ParseResult parseResult = new JavaScriptParser().parse(fileContent);
String headerlessContent = parseResult.getContentWithoutHeaderComments();
Assert.assertNotNull(headerlessContent);
Assert.assertEquals(0, headerlessContent.length());
}

// @Ignore
@Test
public void testCalculateJavaScriptHash() throws IOException, WssHashException {
Expand Down