From b46b53e1494611856bf4ca35cc9a6cf22ac23453 Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Mon, 27 Oct 2014 00:55:49 +0200 Subject: [PATCH] Loose: implement object methods; expose processed options from acorn. --- acorn.js | 1 + acorn_loose.js | 31 ++++++++++++++++++++++++------- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/acorn.js b/acorn.js index ea4121ec88..ab23dcd278 100644 --- a/acorn.js +++ b/acorn.js @@ -234,6 +234,7 @@ tokRegexpAllowed = reAllowed; skipSpace(); }; + getToken.options = options; return getToken; }; diff --git a/acorn_loose.js b/acorn_loose.js index 9bc5320465..0546d19882 100644 --- a/acorn_loose.js +++ b/acorn_loose.js @@ -40,14 +40,16 @@ var options, input, fetchToken, context; + acorn.defaultOptions.tabSize = 4; + exports.parse_dammit = function(inpt, opts) { if (!opts) opts = {}; input = String(inpt); if (/^#!.*/.test(input)) input = "//" + input.slice(2); - options = opts; if (!opts.tabSize) opts.tabSize = 4; fetchToken = acorn.tokenize(input, opts); + options = fetchToken.options; sourceFile = options.sourceFile || null; context = []; nextLineStart = 0; @@ -750,18 +752,22 @@ isGenerator = eat(tt.star); } parsePropertyName(prop); - if (!prop.key) { if (isDummy(parseExpression(true))) next(); eat(tt.comma); continue; } + if (isDummy(prop.key)) { if (isDummy(parseExpression(true))) next(); eat(tt.comma); continue; } if (eat(tt.colon)) { - prop.value = parseExpression(true); prop.kind = "init"; + prop.value = parseExpression(true); + } else if (options.ecmaVersion >= 6 && (token.type === tt.parenL || token.type === tt.braceL)) { + prop.kind = "init"; + prop.method = true; + prop.value = parseMethod(isGenerator); } else if (options.ecmaVersion >= 5 && prop.key.type === "Identifier" && (prop.key.name === "get" || prop.key.name === "set")) { prop.kind = prop.key.name; - prop.key = parsePropertyName() || dummyIdent(); - prop.value = parseFunction(startNode(), false); + parsePropertyName(prop); + prop.value = parseMethod(false); } else { - prop.value = options.ecmaVersion >= 6 ? prop.key : dummyIdent(); prop.kind = "init"; + prop.value = options.ecmaVersion >= 6 ? prop.key : dummyIdent(); prop.shorthand = true; } @@ -784,7 +790,8 @@ prop.computed = false; } } - prop.key = (token.type === tt.num || token.type === tt.string) ? parseExprAtom() : parseIdent(); + var key = (token.type === tt.num || token.type === tt.string) ? parseExprAtom() : parseIdent(); + prop.key = key || dummyIdent(); } function parsePropertyAccessor() { @@ -876,6 +883,16 @@ return finishNode(node, isStatement ? "FunctionDeclaration" : "FunctionExpression"); } + function parseMethod(isGenerator) { + var node = startNode(); + initFunction(node); + parseFunctionParams(node); + node.generator = isGenerator; + node.expression = options.ecmaVersion >= 6 && token.type !== tt.braceL; + node.body = node.expression ? parseExpression(true) : parseBlock(); + return finishNode(node, "FunctionExpression"); + } + function parseExprList(close, allowEmpty) { var indent = curIndent, line = curLineStart, elts = [], continuedLine = nextLineStart; next(); // Opening bracket