diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-02-01 02:36:45 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-01 10:36:45 +0800 |
commit | d4685640a00a0c998041c96ec197e613bd67b7b3 (patch) | |
tree | 53ed8185a109028e4270993fc1d6962fcc22495b /lib/ast.js | |
parent | ac7b5c07d778d3b70bf39c4c0014e9411d780268 (diff) | |
download | tracifyjs-d4685640a00a0c998041c96ec197e613bd67b7b3.tar.gz tracifyjs-d4685640a00a0c998041c96ec197e613bd67b7b3.zip |
support template literals (#4601)
Diffstat (limited to 'lib/ast.js')
-rw-r--r-- | lib/ast.js | 28 |
1 files changed, 28 insertions, 0 deletions
@@ -1418,6 +1418,34 @@ var AST_This = DEFNODE("This", null, { }, }, AST_Symbol); +var AST_Template = DEFNODE("Template", "expressions strings tag", { + $documentation: "A template literal, i.e. tag`str1${expr1}...strN${exprN}strN+1`", + $propdoc: { + expressions: "[AST_Node*] the placeholder expressions", + strings: "[string*] the interpolating text segments", + tag: "[AST_Node] tag function, or null if absent", + }, + walk: function(visitor) { + var node = this; + visitor.visit(node, function() { + if (node.tag) node.tag.walk(visitor); + node.expressions.forEach(function(expr) { + expr.walk(visitor); + }); + }); + }, + _validate: function() { + if (this.expressions.length + 1 != this.strings.length) { + throw new Error("malformed template with " + this.expressions.length + " placeholder(s) but " + this.strings.length + " text segment(s)"); + } + must_be_expressions(this, "expressions"); + this.strings.forEach(function(string) { + if (typeof string != "string") throw new Error("strings must contain string"); + }); + if (this.tag != null) must_be_expression(this, "tag"); + }, +}); + var AST_Constant = DEFNODE("Constant", null, { $documentation: "Base class for all constants", }); |