From 2de16b8cb0bec3eeb57881581f2a7dc43f5e2e2e Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Thu, 17 Apr 2014 12:16:29 -0700 Subject: [PATCH] [loose parser] Fix interpretation of `f."` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this the ast produced by parse_dammit crashed in the following code, as Uglify correctly noticed that f."" is invalid. sample = 'f."'; loose = require('acorn/acorn_loose'); uglify = require('uglify-js'); out = new uglify.OutputStream(); ast = loose.parse_dammit(sample); ast = uglify.AST_Node.from_mozilla_ast(ast); ast.print(out); // TypeError: Cannot call method 'toString' of undefined // member_exp.computed = false && member_exp.property == "" console.log(out.toString()); After this the round-tripped AST looks like: `t.✖;"";`, which is consistent with how `foo.{` is parsed. I also considered making it parse as t[""], but as this only turns up in the wild when people try to use multiline strings, I felt it was better to be obviously wrong. --- acorn_loose.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/acorn_loose.js b/acorn_loose.js index 1c6fb46fd6..b7bc56dd4f 100644 --- a/acorn_loose.js +++ b/acorn_loose.js @@ -606,7 +606,7 @@ if (curLineStart != line && curIndent <= startIndent && tokenStartsLine()) node.property = dummyIdent(); else - node.property = parsePropertyName() || dummyIdent(); + node.property = parsePropertyAccessor() || dummyIdent(); node.computed = false; base = finishNode(node, "MemberExpression"); } else if (token.type == tt.bracketL) { @@ -735,6 +735,10 @@ if (token.type === tt.name || token.type.keyword) return parseIdent(); } + function parsePropertyAccessor() { + if (token.type === tt.name || token.type.keyword) return parseIdent(); + } + function parseIdent() { var node = startNode(); node.name = token.type === tt.name ? token.value : token.type.keyword;