diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2020-12-23 22:22:55 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-24 06:22:55 +0800 |
commit | 2390fae5c4b008aa1028ffdddaa071e4084ef8ac (patch) | |
tree | 962a3258aa0d53d66dda3c6736a3750b46f3f0a3 /lib/parse.js | |
parent | 56fce2131c86ed58ac00bb27b308054b1c2a669f (diff) | |
download | tracifyjs-2390fae5c4b008aa1028ffdddaa071e4084ef8ac.tar.gz tracifyjs-2390fae5c4b008aa1028ffdddaa071e4084ef8ac.zip |
support default values (#4442)
Diffstat (limited to 'lib/parse.js')
-rw-r--r-- | lib/parse.js | 109 |
1 files changed, 91 insertions, 18 deletions
diff --git a/lib/parse.js b/lib/parse.js index 87293cab..c8ad531f 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1041,11 +1041,30 @@ function parse($TEXT, options) { function to_funarg(node) { if (node instanceof AST_Array) return new AST_DestructuredArray({ start: node.start, - elements: node.elements.map(function(node) { - return node instanceof AST_Hole ? node : to_funarg(node); - }), + elements: node.elements.map(to_funarg), + end: node.end, + }); + if (node instanceof AST_Assign) return new AST_DefaultValue({ + start: node.start, + name: to_funarg(node.left), + value: node.right, end: node.end, }); + if (node instanceof AST_DefaultValue) { + node.name = to_funarg(node.name); + return node; + } + if (node instanceof AST_DestructuredArray) { + node.elements = node.elements.map(to_funarg); + return node; + } + if (node instanceof AST_DestructuredObject) { + node.properties.forEach(function(prop) { + prop.value = to_funarg(prop.value); + }); + return node; + } + if (node instanceof AST_Hole) return node; if (node instanceof AST_Object) return new AST_DestructuredObject({ start: node.start, properties: node.properties.map(function(prop) { @@ -1122,7 +1141,7 @@ function parse($TEXT, options) { var was_funarg = S.in_funarg; S.in_funarg = S.in_function; var argnames = expr_list(")", !options.strict, false, function() { - return maybe_destructured(AST_SymbolFunarg); + return maybe_default(AST_SymbolFunarg); }); S.in_funarg = was_funarg; var loop = S.in_loop; @@ -1468,6 +1487,32 @@ function parse($TEXT, options) { })); continue; } + if (is_token(peek(), "operator", "=")) { + var name = as_symbol(AST_SymbolRef); + next(); + a.push(new AST_ObjectKeyVal({ + start: start, + key: start.value, + value: new AST_Assign({ + start: start, + left: name, + operator: "=", + right: maybe_assign(), + end: prev(), + }), + end: prev(), + })); + continue; + } + if (is_token(peek(), "punc", ",") || is_token(peek(), "punc", "}")) { + a.push(new AST_ObjectKeyVal({ + start: start, + key: start.value, + value: as_symbol(AST_SymbolRef), + end: prev(), + })); + continue; + } var key = as_property_key(); if (is("punc", "(")) { var func_start = S.token; @@ -1492,15 +1537,6 @@ function parse($TEXT, options) { })); continue; } - if (is("punc", ",") || is("punc", "}")) { - a.push(new AST_ObjectKeyVal({ - start: start, - key: key, - value: _make_symbol(AST_SymbolRef, start), - end: prev(), - })); - continue; - } if (start.type == "name") switch (key) { case "async": key = as_property_key(); @@ -1601,7 +1637,7 @@ function parse($TEXT, options) { return new AST_DestructuredArray({ start: start, elements: expr_list("]", !options.strict, true, function() { - return maybe_destructured(type); + return maybe_default(type); }), end: prev(), }); @@ -1620,15 +1656,25 @@ function parse($TEXT, options) { a.push(new AST_DestructuredKeyVal({ start: key_start, key: key, - value: maybe_destructured(type), + value: maybe_default(type), end: prev(), })); continue; } + var name = as_symbol(type); + if (is("operator", "=")) { + next(); + name = new AST_DefaultValue({ + start: name.start, + name: name, + value: maybe_assign(), + end: prev(), + }); + } a.push(new AST_DestructuredKeyVal({ start: key_start, key: key_start.value, - value: as_symbol(type), + value: name, end: prev(), })); } @@ -1642,6 +1688,19 @@ function parse($TEXT, options) { return as_symbol(type); } + function maybe_default(type) { + var start = S.token; + var name = maybe_destructured(type); + if (!is("operator", "=")) return name; + next(); + return new AST_DefaultValue({ + start: start, + name: name, + value: maybe_assign(), + end: prev(), + }); + } + function mark_pure(call) { var start = call.start; var comments = start.comments_before; @@ -1788,20 +1847,34 @@ function parse($TEXT, options) { if (node instanceof AST_Array) { var elements = node.elements.map(to_destructured); return all(elements, function(node) { - return node instanceof AST_Destructured || node instanceof AST_Hole || is_assignable(node); + return node instanceof AST_DefaultValue + || node instanceof AST_Destructured + || node instanceof AST_Hole + || is_assignable(node); }) ? new AST_DestructuredArray({ start: node.start, elements: elements, end: node.end, }) : node; } + if (node instanceof AST_Assign) { + var name = to_destructured(node.left); + return name instanceof AST_Destructured || is_assignable(name) ? new AST_DefaultValue({ + start: node.start, + name: name, + value: node.right, + end: node.end, + }) : node; + } if (!(node instanceof AST_Object)) return node; var props = []; for (var i = 0; i < node.properties.length; i++) { var prop = node.properties[i]; if (!(prop instanceof AST_ObjectKeyVal)) return node; var value = to_destructured(prop.value); - if (!(value instanceof AST_Destructured || is_assignable(value))) return node; + if (!(value instanceof AST_DefaultValue || value instanceof AST_Destructured || is_assignable(value))) { + return node; + } props.push(new AST_DestructuredKeyVal({ start: prop.start, key: prop.key, |