-
Notifications
You must be signed in to change notification settings - Fork 864
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
fix: parent relationship in TaggedTemplateLiteral. Fixes #1238 Co-authored-by: Ihor Kuzmanenko <[email protected]>
- Loading branch information
Showing
2 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
testsrc/org/mozilla/javascript/tests/es6/TaggedTemplateLiteralTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package org.mozilla.javascript.tests.es6; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import org.junit.Test; | ||
import org.mozilla.javascript.Parser; | ||
import org.mozilla.javascript.ast.*; | ||
|
||
/** Tests for ES6+ tagged templates support. */ | ||
public class TaggedTemplateLiteralTest { | ||
|
||
/** | ||
* Target and {@link org.mozilla.javascript.ast.TemplateLiteral} nodes inside the tagged | ||
* template should have {@link org.mozilla.javascript.ast.TaggedTemplateLiteral} node as their | ||
* parent. | ||
* | ||
* <p>See <a href="https://github.com/mozilla/rhino/issues/1238">#1238</a> for details. | ||
*/ | ||
@Test | ||
public void testTaggedTemplateChildrenHaveParent() { | ||
String script = "tag`template`;"; | ||
|
||
AstRoot root = new Parser().parse(script, "test", 0); | ||
TaggedTemplateFinder finder = new TaggedTemplateFinder(); | ||
root.visit(finder); | ||
TaggedTemplateLiteral taggedTemplate = finder.getNode(); | ||
|
||
assertNotNull(taggedTemplate); | ||
assertEquals(taggedTemplate, taggedTemplate.getTarget().getParent()); | ||
assertEquals(taggedTemplate, taggedTemplate.getTemplateLiteral().getParent()); | ||
} | ||
|
||
/** | ||
* Target and {@link org.mozilla.javascript.ast.TemplateLiteral} nodes inside the tagged | ||
* template should resolve the AST root. | ||
* | ||
* <p>See <a href="https://github.com/mozilla/rhino/issues/1238">#1238</a> for details. | ||
*/ | ||
@Test | ||
public void testTaggedTemplateChildrenHaveAstRoot() { | ||
String script = "tag`template`"; | ||
|
||
AstRoot root = new Parser().parse(script, "test", 0); | ||
TaggedTemplateFinder finder = new TaggedTemplateFinder(); | ||
root.visit(finder); | ||
TaggedTemplateLiteral taggedTemplate = finder.getNode(); | ||
|
||
assertNotNull(taggedTemplate); | ||
assertEquals(root, taggedTemplate.getAstRoot()); | ||
assertEquals(root, taggedTemplate.getTarget().getAstRoot()); | ||
assertEquals(root, taggedTemplate.getTemplateLiteral().getAstRoot()); | ||
} | ||
|
||
/** | ||
* AST nodes, which are descendants of a tagged template node should resolve the AST root. | ||
* | ||
* <p>See <a href="https://github.com/mozilla/rhino/issues/1238">#1238</a> for details. | ||
*/ | ||
@Test | ||
public void testInnerNodeHasAstRoot() { | ||
String script = "someObj.property()`template`"; | ||
|
||
AstRoot root = new Parser().parse(script, "test", 0); | ||
NameFinder finder = new NameFinder("property"); | ||
root.visit(finder); | ||
Name nameNode = finder.getNode(); | ||
|
||
assertNotNull(nameNode); | ||
assertEquals(root, nameNode.getAstRoot()); | ||
} | ||
|
||
/** Finds first {@link TaggedTemplateLiteral} node in the AST. */ | ||
private static class TaggedTemplateFinder implements NodeVisitor { | ||
private TaggedTemplateLiteral node; | ||
|
||
public TaggedTemplateLiteral getNode() { | ||
return node; | ||
} | ||
|
||
@Override | ||
public boolean visit(AstNode node) { | ||
if (node instanceof TaggedTemplateLiteral) { | ||
this.node = (TaggedTemplateLiteral) node; | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
/** Finds first {@link Name} node for given identifier. */ | ||
private static class NameFinder implements NodeVisitor { | ||
private Name node; | ||
private String name; | ||
|
||
public NameFinder(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Name getNode() { | ||
return node; | ||
} | ||
|
||
@Override | ||
public boolean visit(AstNode node) { | ||
if (node instanceof Name) { | ||
Name nameNode = (Name) node; | ||
if (nameNode.getIdentifier().equals(name)) { | ||
this.node = nameNode; | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
} |