Skip to content

Commit ec8c04d

Browse files
rbrigbrail
authored andcommitted
TokenStream sets back the cursor (to a value+1) when it shouldn't do that (only when EOF)
fix and testcases provided by @jcompagner; see mozilla#2367 for more
1 parent 80beeed commit ec8c04d

2 files changed

Lines changed: 59 additions & 6 deletions

File tree

rhino/src/main/java/org/mozilla/javascript/TokenStream.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -781,13 +781,9 @@ final int getToken() throws IOException {
781781
}
782782
}
783783

784-
// `ungetChar` will decrement the cursor, however the _actual_ tokenEnd should
785-
// still be restored correctly after `getStringFromBuffer()` mutates it, so we save
786-
// and restore it.
787-
int savedTokenEnd = cursor;
788-
ungetChar(c); // decrements cursor
784+
if (c != EOF_CHAR)
785+
ungetChar(c); // decrements cursor, don't do this if the char is already EOF
789786
String str = getStringFromBuffer(); // mutates tokenEnd to point to cursor
790-
tokenEnd = savedTokenEnd; // restore tokenEnd
791787

792788
if (!containsEscape
793789
|| parser.compilerEnv.getLanguageVersion() >= Context.VERSION_ES6) {

rhino/src/test/java/org/mozilla/javascript/tests/ArrowFunctionTest.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.junit.jupiter.api.Test;
1111
import org.mozilla.javascript.Parser;
1212
import org.mozilla.javascript.ast.AstNode;
13+
import org.mozilla.javascript.ast.Block;
1314
import org.mozilla.javascript.ast.FunctionNode;
1415
import org.mozilla.javascript.ast.NodeVisitor;
1516
import org.mozilla.javascript.ast.ReturnStatement;
@@ -77,4 +78,60 @@ public void arrowFnEvalToString() {
7778
String source = "eval(\"()=>this\").toString()";
7879
Utils.assertWithAllModes("()=>this", source);
7980
}
81+
82+
@Test
83+
public void testArrowRhinoWithLineEndingOnlyThis() {
84+
FunctionNode arrowFn = parseAndExtractArrowFn("()=>this");
85+
86+
assertEquals(0, arrowFn.getPosition());
87+
assertEquals(0, arrowFn.getAbsolutePosition());
88+
assertEquals(8, arrowFn.getLength());
89+
90+
Block body = (Block) arrowFn.getBody();
91+
assertEquals(4, body.getPosition());
92+
assertEquals(4, body.getAbsolutePosition());
93+
assertEquals(4, body.getLength());
94+
}
95+
96+
@Test
97+
public void testArrowRhinoWithLineEndingOnlyThisNl() {
98+
FunctionNode arrowFn = parseAndExtractArrowFn("()=>this\n");
99+
100+
assertEquals(0, arrowFn.getPosition());
101+
assertEquals(0, arrowFn.getAbsolutePosition());
102+
assertEquals(8, arrowFn.getLength());
103+
104+
Block body = (Block) arrowFn.getBody();
105+
assertEquals(4, body.getPosition());
106+
assertEquals(4, body.getAbsolutePosition());
107+
assertEquals(4, body.getLength());
108+
}
109+
110+
@Test
111+
public void testArrowRhinoWithLineEndingThisWithProperty() {
112+
FunctionNode arrowFn = parseAndExtractArrowFn("()=>this.xs");
113+
114+
assertEquals(0, arrowFn.getPosition());
115+
assertEquals(0, arrowFn.getAbsolutePosition());
116+
assertEquals(11, arrowFn.getLength());
117+
118+
Block body = (Block) arrowFn.getBody();
119+
assertEquals(4, body.getPosition());
120+
assertEquals(4, body.getAbsolutePosition());
121+
assertEquals(7, body.getLength());
122+
}
123+
124+
@Test
125+
public void testArrowRhinoWithLineEndingThisWithPropertyNl() {
126+
FunctionNode arrowFn = parseAndExtractArrowFn("()=>this.xs\n");
127+
128+
assertEquals(0, arrowFn.getPosition());
129+
assertEquals(0, arrowFn.getAbsolutePosition());
130+
assertEquals(11, arrowFn.getLength());
131+
132+
Block body = (Block) arrowFn.getBody();
133+
assertEquals(4, body.getPosition());
134+
assertEquals(4, body.getAbsolutePosition());
135+
assertEquals(7, body.getLength());
136+
}
80137
}

0 commit comments

Comments
 (0)