-
Notifications
You must be signed in to change notification settings - Fork 238
JS ES6 Support and Template Literals Support #1493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
571d19c
4f4d535
b513d4d
f3f8869
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,7 @@ | |
import java.util.Objects; | ||
import java.util.Set; | ||
import org.mozilla.javascript.CompilerEnvirons; | ||
import org.mozilla.javascript.Context; | ||
import org.mozilla.javascript.ErrorReporter; | ||
import org.mozilla.javascript.EvaluatorException; | ||
import org.mozilla.javascript.Kit; | ||
|
@@ -104,6 +105,9 @@ | |
import org.mozilla.javascript.ast.SwitchCase; | ||
import org.mozilla.javascript.ast.SwitchStatement; | ||
import org.mozilla.javascript.ast.Symbol; | ||
import org.mozilla.javascript.ast.TaggedTemplateLiteral; | ||
import org.mozilla.javascript.ast.TemplateCharacters; | ||
import org.mozilla.javascript.ast.TemplateLiteral; | ||
import org.mozilla.javascript.ast.ThrowStatement; | ||
import org.mozilla.javascript.ast.TryStatement; | ||
import org.mozilla.javascript.ast.UnaryExpression; | ||
|
@@ -1596,6 +1600,40 @@ public CAstNode visitSymbol(Symbol node, WalkContext arg) { | |
return null; | ||
} | ||
|
||
@Override | ||
public CAstNode visitTemplateCharacters(TemplateCharacters node, WalkContext arg) { | ||
return Ast.makeConstant(node.getValue()); | ||
} | ||
|
||
@Override | ||
public CAstNode visitTemplateLiteral(TemplateLiteral node, WalkContext arg) { | ||
List<AstNode> elements = node.getElements(); | ||
if (elements.size() == 1) { | ||
return this.visit(elements.get(0), arg); | ||
} | ||
CAstNode lastBinaryNode = | ||
Ast.makeNode( | ||
CAstNode.BINARY_EXPR, | ||
translateOpcode(Token.ADD), | ||
visit(elements.get(elements.size() - 2), arg), | ||
visit(elements.get(elements.size() - 1), arg)); | ||
for (int i = elements.size() - 3; i >= 0; i--) { | ||
lastBinaryNode = | ||
Ast.makeNode( | ||
CAstNode.BINARY_EXPR, | ||
translateOpcode(Token.ADD), | ||
visit(elements.get(i), arg), | ||
lastBinaryNode); | ||
} | ||
return lastBinaryNode; | ||
} | ||
|
||
@Override | ||
public CAstNode visitTaggedTemplateLiteral(TaggedTemplateLiteral node, WalkContext arg) { | ||
// TODO Auto-generated method stub | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace with a comment indicating this feature is not yet implemented |
||
return null; | ||
} | ||
Comment on lines
+1632
to
+1635
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm concerned this will cause a crash if we get an input with a tagged template literal. If we don't handle this case yet, maybe we should not add this method and just skip visiting the nodes? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any thoughts on this comment? |
||
|
||
@Override | ||
public CAstNode visitThrowStatement(ThrowStatement n, WalkContext context) { | ||
CAstNode catchNode = context.getCatchTarget(); | ||
|
@@ -2758,6 +2796,7 @@ public void warning(String arg0, String arg1, int arg2, String arg3, int arg4) { | |
|
||
CAstErrorReporter reporter = new CAstErrorReporter(); | ||
CompilerEnvirons compilerEnv = new CompilerEnvirons(); | ||
compilerEnv.setLanguageVersion(Context.VERSION_ES6); | ||
compilerEnv.setErrorReporter(reporter); | ||
compilerEnv.setReservedKeywordAsIdentifier(true); | ||
compilerEnv.setIdeMode(true); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,9 @@ | |
import org.mozilla.javascript.ast.SwitchCase; | ||
import org.mozilla.javascript.ast.SwitchStatement; | ||
import org.mozilla.javascript.ast.Symbol; | ||
import org.mozilla.javascript.ast.TaggedTemplateLiteral; | ||
import org.mozilla.javascript.ast.TemplateCharacters; | ||
import org.mozilla.javascript.ast.TemplateLiteral; | ||
import org.mozilla.javascript.ast.ThrowStatement; | ||
import org.mozilla.javascript.ast.TryStatement; | ||
import org.mozilla.javascript.ast.UnaryExpression; | ||
|
@@ -158,6 +161,12 @@ public R visit(AstNode node, A arg) { | |
return visitSwitchCase((SwitchCase) node, arg); | ||
} else if (node instanceof SwitchStatement) { | ||
return visitSwitchStatement((SwitchStatement) node, arg); | ||
} else if (node instanceof TaggedTemplateLiteral) { | ||
return visitTaggedTemplateLiteral((TaggedTemplateLiteral) node, arg); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment above about not visiting these nodes if we don't yet handle them; or is the issue that we'll get an unexpected node type? In general, what happens right now when we try to pass an ES6 input to WALA, given that we haven't set the language version? Do we just get a parse error in Rhino? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right now, it errors based on a Rhino parse error when given an ES6 input without the language version set. TaggedTemplateLiteral is a separate node type from Rhino. I haven't tested if I could put a "dummy node" instead to prevent a crash, but I was mirroring some of the other unimplemented node types. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. I imagine there are various other ES6 nodes we don't handle yet. I think we should update line 205 to throw some kind of catchable exception (maybe |
||
} else if (node instanceof TemplateCharacters) { | ||
return visitTemplateCharacters((TemplateCharacters) node, arg); | ||
} else if (node instanceof TemplateLiteral) { | ||
return visitTemplateLiteral((TemplateLiteral) node, arg); | ||
} else if (node instanceof ThrowStatement) { | ||
return visitThrowStatement((ThrowStatement) node, arg); | ||
} else if (node instanceof TryStatement) { | ||
|
@@ -283,6 +292,12 @@ public R visit(AstNode node, A arg) { | |
|
||
public abstract R visitSymbol(Symbol node, A arg); | ||
|
||
public abstract R visitTaggedTemplateLiteral(TaggedTemplateLiteral node, A arg); | ||
|
||
public abstract R visitTemplateCharacters(TemplateCharacters node, A arg); | ||
|
||
public abstract R visitTemplateLiteral(TemplateLiteral node, A arg); | ||
|
||
public abstract R visitThrowStatement(ThrowStatement node, A arg); | ||
|
||
public abstract R visitTryStatement(TryStatement node, A arg); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Code generated with the assistance of ChatGPT (OpenAI) | ||
// Date: March 10, 2025 | ||
|
||
function testSimpleTemplateLiteral() { | ||
let name = "Alice"; | ||
let result = `Hello, ${name}!`; | ||
return result; | ||
} | ||
|
||
function testMultilineTemplateLiteral() { | ||
let multiline = `Line 1 | ||
Line 2 | ||
Line 3`; | ||
return multiline; // Expected: "Line 1\nLine 2\nLine 3" | ||
} | ||
|
||
function testTemplateLiteralWithExpression() { | ||
let a = 10, b = 20; | ||
let result = `The sum of ${a} and ${b} is ${a + b}.`; | ||
return result; // Expected: "The sum of 10 and 20 is 30." | ||
} | ||
|
||
function testFunctionInTemplateLiteral() { | ||
function multiply(x, y) { | ||
return x * y; | ||
} | ||
let result = `The product of 5 and 6 is ${multiply(5, 6)}.`; | ||
return result; // Expected: "The product of 5 and 6 is 30." | ||
} | ||
|
||
function testTemplateLiteralWithFallback() { | ||
let name = null; | ||
let result = `Hello, ${name || "Guest"}!`; | ||
return result; // Expected: "Hello, Guest!" | ||
} | ||
|
||
function testIllegalEscapeSequence() { | ||
try { | ||
let invalidTemplate = `Unescaped sequence: \u{D800}`; // Invalid Unicode escape | ||
return invalidTemplate; | ||
} catch (e) { | ||
return `Error with illegal escape sequence: ${e}`; // Expected: Error about invalid Unicode escape sequence | ||
} | ||
} | ||
|
||
function testNestedExpressions() { | ||
let x = 2, y = 3, z = 4; | ||
let result = `Result is: ${x + y * z}`; // Parentheses around x + y * z to control order of operations | ||
return result; // Expected: "Result is: 14" | ||
} | ||
|
||
function testTemplateWithFunctions() { | ||
function greet(name) { | ||
return `Hello, ${name}!`; | ||
} | ||
|
||
let result = `Message: ${greet('Alice')}`; | ||
return result; // Expected: "Message: Hello, Alice!" | ||
} | ||
|
||
function testNestedTemplateLiterals() { | ||
let name = "World"; | ||
let greeting = `Hello, ${`Dear ${name}`}`; | ||
return greeting; // Expected: "Hello, Dear World" | ||
} | ||
|
||
function testTemplateLiteralWithObject() { | ||
let user = { name: "John", age: 30 }; | ||
let result = `User info: ${user.name}, Age: ${user.age}`; | ||
return result; // Expected: "User info: John, Age: 30" | ||
} | ||
|
||
function testTemplateLiteralWithUndefined() { | ||
let x; | ||
let result = `Value is ${x}`; | ||
return result; // Expected: "Value is undefined" | ||
|
||
let y = null; | ||
result = `Value is ${y}`; | ||
return result; // Expected: "Value is null" | ||
} | ||
|
||
function testEscapeSequences() { | ||
let escaped = `This is a backslash: \\ and this is a quote: \"`; | ||
return escaped; // Expected: "This is a backslash: \\ and this is a quote: \"" | ||
} | ||
|
||
function testTemplateWithFunctionCalls() { | ||
let func = (x) => `Result: ${x}`; | ||
let result = `Output: ${func(5)}`; | ||
return result; // Expected: "Output: Result: 5" | ||
} | ||
|
||
function runTemplateTests() { | ||
testSimpleTemplateLiteral(); | ||
testMultilineTemplateLiteral(); | ||
testTemplateLiteralWithExpression(); | ||
testFunctionInTemplateLiteral(); | ||
testTemplateLiteralWithFallback(); | ||
testIllegalEscapeSequence(); | ||
testNestedExpressions(); | ||
testTemplateWithFunctions(); | ||
testNestedTemplateLiterals(); | ||
testTemplateLiteralWithObject(); | ||
testTemplateLiteralWithUndefined(); | ||
testEscapeSequences(); | ||
testTemplateWithFunctionCalls(); | ||
} | ||
|
||
runTemplateTests(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1071,4 +1071,38 @@ public void testComplexFinally() | |
|
||
CAstCallGraphUtil.dumpCG(B.getCFAContextInterpreter(), B.getPointerAnalysis(), CG); | ||
} | ||
|
||
private static final Object[][] assertionsES6Args = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the test cases where the template substitution has a function call, can we assert that the appropriate call edge(s) are present? |
||
new Object[][] { | ||
new Object[] {ROOT, new String[] {"es6.js"}}, | ||
new Object[] {"es6.js", new String[] {"es6.js/runTemplateTests"}}, | ||
new Object[] { | ||
"es6.js/runTemplateTests", | ||
new String[] { | ||
"es6.js/testSimpleTemplateLiteral", | ||
"es6.js/testMultilineTemplateLiteral", | ||
"es6.js/testTemplateLiteralWithExpression", | ||
"es6.js/testFunctionInTemplateLiteral", | ||
"es6.js/testTemplateLiteralWithFallback", | ||
"es6.js/testIllegalEscapeSequence", | ||
"es6.js/testNestedExpressions", | ||
"es6.js/testTemplateWithFunctions", | ||
"es6.js/testNestedTemplateLiterals", | ||
"es6.js/testTemplateLiteralWithObject", | ||
"es6.js/testTemplateLiteralWithUndefined", | ||
"es6.js/testEscapeSequences", | ||
"es6.js/testTemplateWithFunctionCalls" | ||
} | ||
} | ||
}; | ||
|
||
@Test | ||
public void testES6() | ||
throws IllegalArgumentException, IOException, CancelException, WalaException { | ||
JSCFABuilder B = JSCallGraphBuilderUtil.makeScriptCGBuilder("tests", "es6.js"); | ||
CallGraph CG = B.makeCallGraph(B.getOptions()); | ||
verifyGraphAssertions(CG, assertionsES6Args); | ||
|
||
CAstCallGraphUtil.dumpCG(B.getCFAContextInterpreter(), B.getPointerAnalysis(), CG); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't really understand the logic here; I think we need some comments to explain. Is the code in this method handling just the string concatenation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the code handling the string concatenation. I can add a comment, and also simplify the logic a bit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, please add comments.