diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2017-05-15 02:37:53 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-15 02:37:53 +0800 |
commit | e005099fb1b9a1b87ac50ba8223255e52cec452d (patch) | |
tree | 5edef6298d3c79fc5352476c2fbfc236b626a9ee /lib | |
parent | 504a436e9daac89f5226280e01ae2818fe4e8436 (diff) | |
download | tracifyjs-e005099fb1b9a1b87ac50ba8223255e52cec452d.tar.gz tracifyjs-e005099fb1b9a1b87ac50ba8223255e52cec452d.zip |
fix & improve coverage of `estree` (#1935)
- fix `estree` conversion of getter/setter
- fix non-directive literal in `to_mozilla_ast()`
- revamp `test/mozilla-ast.js`
- reuse `test/ufuzz.js` for code generation
- use `acorn.parse()` for creating `estree`
- extend `test/ufuzz.js` for `acorn` workaround
- catch variable redefinition
- non-trivial literal as directive
- adjust options for tolerance
Miscellaneous
- optional semi-colon when parsing directives
fixes #1914
closes #1915
Diffstat (limited to 'lib')
-rw-r--r-- | lib/mozilla-ast.js | 52 | ||||
-rw-r--r-- | lib/parse.js | 20 |
2 files changed, 35 insertions, 37 deletions
diff --git a/lib/mozilla-ast.js b/lib/mozilla-ast.js index 7d246758..8d7ee4b8 100644 --- a/lib/mozilla-ast.js +++ b/lib/mozilla-ast.js @@ -111,23 +111,19 @@ }, Property: function(M) { var key = M.key; - var name = key.type == "Identifier" ? key.name : key.value; var args = { start : my_start_token(key), end : my_end_token(M.value), - key : name, + key : key.type == "Identifier" ? key.name : key.value, value : from_moz(M.value) }; - switch (M.kind) { - case "init": - return new AST_ObjectKeyVal(args); - case "set": - args.value.name = from_moz(key); - return new AST_ObjectSetter(args); - case "get": - args.value.name = from_moz(key); - return new AST_ObjectGetter(args); - } + if (M.kind == "init") return new AST_ObjectKeyVal(args); + args.key = new AST_SymbolAccessor({ + name: args.key + }); + args.value = new AST_Accessor(args.value); + if (M.kind == "get") return new AST_ObjectGetter(args); + if (M.kind == "set") return new AST_ObjectSetter(args); }, ArrayExpression: function(M) { return new AST_Array({ @@ -260,10 +256,7 @@ map("CallExpression", AST_Call, "callee>expression, arguments@args"); def_to_moz(AST_Toplevel, function To_Moz_Program(M) { - return { - type: "Program", - body: M.body.map(to_moz) - }; + return to_moz_scope("Program", M); }); def_to_moz(AST_Defun, function To_Moz_FunctionDeclaration(M) { @@ -271,7 +264,7 @@ type: "FunctionDeclaration", id: to_moz(M.name), params: M.argnames.map(to_moz), - body: to_moz_block(M) + body: to_moz_scope("BlockStatement", M) } }); @@ -280,7 +273,7 @@ type: "FunctionExpression", id: to_moz(M.name), params: M.argnames.map(to_moz), - body: to_moz_block(M) + body: to_moz_scope("BlockStatement", M) } }); @@ -386,11 +379,10 @@ }); def_to_moz(AST_ObjectProperty, function To_Moz_Property(M) { - var key = ( - is_identifier(M.key) - ? {type: "Identifier", name: M.key} - : {type: "Literal", value: M.key} - ); + var key = { + type: "Literal", + value: M.key instanceof AST_SymbolAccessor ? M.key.name : M.key + }; var kind; if (M instanceof AST_ObjectKeyVal) { kind = "init"; @@ -551,8 +543,8 @@ moz_to_me = new Function("U2", "my_start_token", "my_end_token", "from_moz", "return(" + moz_to_me + ")")( exports, my_start_token, my_end_token, from_moz ); - me_to_moz = new Function("to_moz", "to_moz_block", "return(" + me_to_moz + ")")( - to_moz, to_moz_block + me_to_moz = new Function("to_moz", "to_moz_block", "to_moz_scope", "return(" + me_to_moz + ")")( + to_moz, to_moz_block, to_moz_scope ); MOZ_TO_ME[moztype] = moz_to_me; def_to_moz(mytype, me_to_moz); @@ -610,4 +602,14 @@ }; }; + function to_moz_scope(type, node) { + var body = node.body.map(to_moz); + if (node.body[0] instanceof AST_SimpleStatement && node.body[0].body instanceof AST_String) { + body.unshift(to_moz(new AST_EmptyStatement(node.body[0]))); + } + return { + type: type, + body: body + }; + }; })(); diff --git a/lib/parse.js b/lib/parse.js index f1089501..74c00b74 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -805,24 +805,20 @@ function parse($TEXT, options) { handle_regexp(); switch (S.token.type) { case "string": - var dir = false; - if (S.in_directives === true) { - if ((is_token(peek(), "punc", ";") || peek().nlb) && S.token.raw.indexOf("\\") === -1) { + if (S.in_directives) { + tmp = peek(); + if (S.token.raw.indexOf("\\") == -1 + && (tmp.nlb + || is_token(tmp, "eof") + || is_token(tmp, "punc", ";") + || is_token(tmp, "punc", "}"))) { S.input.add_directive(S.token.value); } else { S.in_directives = false; } } var dir = S.in_directives, stat = simple_statement(); - if (dir) { - return new AST_Directive({ - start : stat.body.start, - end : stat.body.end, - quote : stat.body.quote, - value : stat.body.value, - }); - } - return stat; + return dir ? new AST_Directive(stat.body) : stat; case "num": case "regexp": case "operator": |