Skip to content

Commit

Permalink
[loose parser] Fix interpretation of f."
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ConradIrwin authored and marijnh committed Apr 17, 2014
1 parent 6f41a22 commit 2de16b8
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion acorn_loose.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 2de16b8

Please sign in to comment.