Skip to content

Commit

Permalink
Fixing that no braces regex would be triggered by keywords in comments,
Browse files Browse the repository at this point in the history
see issue #34.
  • Loading branch information
Max Rohde committed Feb 1, 2018
1 parent 257badb commit 3bf3d29
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,11 @@ void checkBraces(final String beautifiedJs) throws BracesException {
}

for (final Pattern pattern : LACK_EXPECTED_BRACES) {
final Matcher matcher = pattern.matcher(beautifiedJs);
final Matcher matcher = pattern.matcher(RemoveComments.perform(beautifiedJs));
if (matcher.find()) {
throw new BracesException("No block braces after function|for|while|do");


throw new BracesException("No block braces after function|for|while|do. Found ["+matcher.group()+"]");
}
}
}
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/delight/nashornsandbox/internal/RemoveComments.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package delight.nashornsandbox.internal;

/**
* Based on
* https://codegolf.stackexchange.com/questions/48326/remove-single-line-and-multiline-comments-from-string
*
* @author adminuser
*
*/
public class RemoveComments {

public static final int DEFAULT = 1;
public static final int ESCAPE = 2;
public static final int STRING = 3;
public static final int ONE_LINE = 4;
public static final int MULTI_LINE = 5;

public static String perform(String s) {
String out = "";
int mod = DEFAULT;
for (int i = 0; i < s.length(); i++) {
String substring = s.substring(i, Math.min(i + 2, s.length()));
char c = s.charAt(i);
switch (mod) {
case DEFAULT: // default
mod = substring.equals("/*") ? MULTI_LINE
: substring.equals("//") ? ONE_LINE : c == '"' || c =='\'' ? STRING : DEFAULT;
break;
case STRING: // string
mod = c == '"' || c == '\'' ? DEFAULT : c == '\\' ? ESCAPE : STRING;
break;
case ESCAPE: // string
mod = STRING;
break;
case ONE_LINE: // one line comment
mod = c == '\n' ? DEFAULT : ONE_LINE;
continue;
case MULTI_LINE: // multi line comment
mod = substring.equals("*/") ? DEFAULT : MULTI_LINE;
i += mod == DEFAULT ? 1 : 0;
continue;
}
out += mod < 4 ? c : "";
}

return out;
}

}
32 changes: 28 additions & 4 deletions src/test/java/delight/nashornsandbox/TestIssue34.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.junit.Before;
import org.junit.Test;

import delight.nashornsandbox.exceptions.BracesException;
import delight.nashornsandbox.exceptions.ScriptCPUAbuseException;
import junit.framework.Assert;

Expand Down Expand Up @@ -67,11 +68,17 @@ public void testIssue34_Scenario2() throws ScriptCPUAbuseException, ScriptExcept
String js = "";
js += "function main(){\n" + "logger.debug(\"... In fun1()....\");\n" + "for(var i=0;i<2;i++)//{\n"
+ "logger.debug(\"loop cnt-\"+i);\n" + "}\n" + "main();";



Throwable ex = null;
try {
sandbox.eval(js);
} catch (Throwable t) {
ex = t;
}

sandbox.eval(js);

Assert.assertTrue(logger.getOutput().contains("loop cnt-0"));
Assert.assertTrue(logger.getOutput().contains("... In fun1()...."));
Assert.assertTrue(ex instanceof BracesException);

}

Expand All @@ -86,7 +93,24 @@ public void testIssue34_Scenario3() throws ScriptCPUAbuseException, ScriptExcept
Assert.assertTrue(logger.getOutput().contains("loop cnt=6"));

}

@Test
public void testIssue34_Scenario3_2() throws ScriptCPUAbuseException, ScriptException {
String js = "//simple do-while loop for demo\n";
js += "function loopTest(){\n" +
"var i=0;\n" +
"do{\n" +
"logger.debug(\"loop cnt=\"+(++i));\n"
+ "}while(i<11);" + "}\n" +
"loopTest();";


sandbox.eval(js);

Assert.assertTrue(logger.getOutput().contains("loop cnt=6"));

}

@Test
public void testIssue34_Scenario4() {
String js = "";
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/delight/nashornsandbox/TestRemoveComments.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package delight.nashornsandbox;

import org.junit.Test;

import delight.nashornsandbox.internal.RemoveComments;
import junit.framework.Assert;

public class TestRemoveComments {

@Test
public void test() {

Assert.assertEquals("var url = 'http://hello.com'", RemoveComments.perform("var url = 'http://hello.com'"));

Assert.assertEquals("var url = \"http://hello.com\"", RemoveComments.perform("var url = \"http://hello.com\""));

Assert.assertEquals("var url = 'http://hello.com';", RemoveComments.perform("var url = 'http://hello.com';// mycomment"));

Assert.assertEquals("var url = 'http://hello.com'", RemoveComments.perform("/* whatisthis */var url = 'http://hello.com'"));

}

}

0 comments on commit 3bf3d29

Please sign in to comment.