Socket
Socket
Sign inDemoInstall

acorn

Package Overview
Dependencies
0
Maintainers
1
Versions
131
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.9.0 to 0.10.0

.editorconfig

585

acorn_loose.js

@@ -43,8 +43,10 @@ // Acorn: Loose parser

acorn.defaultOptions.tabSize = 4;
exports.parse_dammit = function(inpt, opts) {
if (!opts) opts = {};
input = String(inpt);
options = opts;
if (!opts.tabSize) opts.tabSize = 4;
fetchToken = acorn.tokenize(inpt, opts);
if (/^#!.*/.test(input)) input = "//" + input.slice(2);
fetchToken = acorn.tokenize(input, opts);
options = fetchToken.options;
sourceFile = options.sourceFile || null;

@@ -61,11 +63,10 @@ context = [];

function next() {
function next(forceRegexp) {
lastEnd = token.end;
if (options.locations)
lastEndLoc = token.endLoc;
if (forceRegexp)
ahead.length = 0;
if (ahead.length)
token = ahead.shift();
else
token = readToken();
token = ahead.shift() || readToken(forceRegexp);

@@ -81,6 +82,12 @@ if (token.start >= nextLineStart) {

function readToken() {
function readToken(forceRegexp) {
for (;;) {
try {
return fetchToken();
var tok = fetchToken(forceRegexp);
if (tok.type === tt.dot && input.substr(tok.end, 1) === '.') {
tok = fetchToken();
tok.start--;
tok.type = tt.ellipsis;
}
return tok;
} catch(e) {

@@ -99,2 +106,6 @@ if (!(e instanceof SyntaxError)) throw e;

replace = {start: e.pos, end: pos, type: tt.regexp, value: re};
} else if (/template/.test(msg)) {
replace = {start: e.pos, end: pos,
type: input.charAt(e.pos) == "`" ? tt.template : tt.templateContinued,
value: input.slice(e.pos + 1, pos)};
} else {

@@ -216,9 +227,23 @@ replace = false;

node.sourceFile = options.directSourceFile;
if (options.ranges)
node.range = [token.start, 0];
return node;
}
function startNodeFrom(other) {
var node = new Node(other.start);
if (options.locations)
node.loc = new SourceLocation(other.loc.start);
function storeCurrentPos() {
return options.locations ? [token.start, token.startLoc] : token.start;
}
function startNodeAt(pos) {
var node;
if (options.locations) {
node = new Node(pos[0]);
node.loc = new SourceLocation(pos[1]);
} else {
node = new Node(pos);
}
if (options.directSourceFile)
node.sourceFile = options.directSourceFile;
if (options.ranges)
node.range = [pos[0], 0];
return node;

@@ -232,20 +257,11 @@ }

node.loc.end = lastEndLoc;
if (options.ranges)
node.range[1] = lastEnd;
return node;
}
function getDummyLoc() {
if (options.locations) {
var loc = new SourceLocation();
loc.end = loc.start;
return loc;
}
};
function dummyIdent() {
var dummy = new Node(token.start);
dummy.type = "Identifier";
dummy.end = token.start;
var dummy = startNode();
dummy.name = "✖";
dummy.loc = getDummyLoc();
return dummy;
return finishNode(dummy, "Identifier");
}

@@ -258,2 +274,4 @@ function isDummy(node) { return node.name == "✖"; }

return true;
} else {
return false;
}

@@ -266,3 +284,3 @@ }

function semicolon() {
eat(tt.semi);
return eat(tt.semi);
}

@@ -283,10 +301,22 @@

function checkLVal(expr) {
if (expr.type === "Identifier" || expr.type === "MemberExpression") return expr;
return dummyIdent();
if (!expr) return expr;
switch (expr.type) {
case "Identifier":
case "MemberExpression":
case "ObjectPattern":
case "ArrayPattern":
case "SpreadElement":
return expr;
default:
return dummyIdent();
}
}
function parseTopLevel() {
var node = startNode();
var node = startNodeAt(options.locations ? [0, acorn.getLineInfo(input, 0)] : 0);
node.body = [];
while (token.type !== tt.eof) node.body.push(parseStatement());
lastEnd = token.end;
lastEndLoc = token.endLoc;
return finishNode(node, "Program");

@@ -296,2 +326,5 @@ }

function parseStatement() {
if (token.type === tt.slash || token.type === tt.assign && token.value === "/=")
next(true);
var starttype = token.type, node = startNode();

@@ -303,4 +336,8 @@

var isBreak = starttype === tt._break;
node.label = token.type === tt.name ? parseIdent() : null;
semicolon();
if (semicolon() || canInsertSemicolon()) {
node.label = null;
} else {
node.label = token.type === tt.name ? parseIdent() : null;
semicolon();
}
return finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");

@@ -325,12 +362,13 @@

if (token.type === tt.semi) return parseFor(node, null);
if (token.type === tt._var) {
var init = startNode();
next();
parseVar(init, true);
if (init.declarations.length === 1 && eat(tt._in))
if (token.type === tt._var || token.type === tt._let) {
var init = parseVar(true);
if (init.declarations.length === 1 && (token.type === tt._in || token.type === tt.name && token.value === "of")) {
return parseForIn(node, init);
}
return parseFor(node, init);
}
var init = parseExpression(false, true);
if (eat(tt._in)) {return parseForIn(node, checkLVal(init));}
if (token.type === tt._in || token.type === tt.name && token.value === "of") {
return parseForIn(node, checkLVal(init));
}
return parseFor(node, init);

@@ -412,6 +450,5 @@

case tt._var:
next();
node = parseVar(node);
semicolon();
return node;
case tt._let:
case tt._const:
return parseVar();

@@ -437,2 +474,11 @@ case tt._while:

case tt._class:
return parseObj(true, true);
case tt._import:
return parseImport();
case tt._export:
return parseExport();
default:

@@ -481,2 +527,4 @@ var expr = parseExpression();

function parseForIn(node, init) {
var type = token.type === tt._in ? "ForInStatement" : "ForOfStatement";
next();
node.left = init;

@@ -487,15 +535,16 @@ node.right = parseExpression();

node.body = parseStatement();
return finishNode(node, "ForInStatement");
return finishNode(node, type);
}
function parseVar(node, noIn) {
function parseVar(noIn) {
var node = startNode();
node.kind = token.type.keyword;
next();
node.declarations = [];
node.kind = "var";
while (token.type === tt.name) {
do {
var decl = startNode();
decl.id = parseIdent();
decl.id = options.ecmaVersion >= 6 ? toAssignable(parseExprAtom()) : parseIdent();
decl.init = eat(tt.eq) ? parseExpression(true, noIn) : null;
node.declarations.push(finishNode(decl, "VariableDeclarator"));
if (!eat(tt.comma)) break;
}
} while (eat(tt.comma));
if (!node.declarations.length) {

@@ -506,2 +555,3 @@ var decl = startNode();

}
if (!noIn) semicolon();
return finishNode(node, "VariableDeclaration");

@@ -511,5 +561,6 @@ }

function parseExpression(noComma, noIn) {
var start = storeCurrentPos();
var expr = parseMaybeAssign(noIn);
if (!noComma && token.type === tt.comma) {
var node = startNodeFrom(expr);
var node = startNodeAt(start);
node.expressions = [expr];

@@ -532,7 +583,8 @@ while (eat(tt.comma)) node.expressions.push(parseMaybeAssign(noIn));

function parseMaybeAssign(noIn) {
var start = storeCurrentPos();
var left = parseMaybeConditional(noIn);
if (token.type.isAssign) {
var node = startNodeFrom(left);
var node = startNodeAt(start);
node.operator = token.value;
node.left = checkLVal(left);
node.left = token.type === tt.eq ? toAssignable(left) : checkLVal(left);
next();

@@ -546,5 +598,6 @@ node.right = parseMaybeAssign(noIn);

function parseMaybeConditional(noIn) {
var start = storeCurrentPos();
var expr = parseExprOps(noIn);
if (eat(tt.question)) {
var node = startNodeFrom(expr);
var node = startNodeAt(start);
node.test = expr;

@@ -559,7 +612,8 @@ node.consequent = parseExpression(true);

function parseExprOps(noIn) {
var start = storeCurrentPos();
var indent = curIndent, line = curLineStart;
return parseExprOp(parseMaybeUnary(noIn), -1, noIn, indent, line);
return parseExprOp(parseMaybeUnary(noIn), start, -1, noIn, indent, line);
}
function parseExprOp(left, minPrec, noIn, indent, line) {
function parseExprOp(left, start, minPrec, noIn, indent, line) {
if (curLineStart != line && curIndent < indent && tokenStartsLine()) return left;

@@ -569,12 +623,14 @@ var prec = token.type.binop;

if (prec > minPrec) {
var node = startNodeFrom(left);
var node = startNodeAt(start);
node.left = left;
node.operator = token.value;
next();
if (curLineStart != line && curIndent < indent && tokenStartsLine())
if (curLineStart != line && curIndent < indent && tokenStartsLine()) {
node.right = dummyIdent();
else
node.right = parseExprOp(parseMaybeUnary(noIn), prec, noIn, indent, line);
var node = finishNode(node, /&&|\|\|/.test(node.operator) ? "LogicalExpression" : "BinaryExpression");
return parseExprOp(node, minPrec, noIn, indent, line);
} else {
var rightStart = storeCurrentPos();
node.right = parseExprOp(parseMaybeUnary(noIn), rightStart, prec, noIn, indent, line);
}
finishNode(node, /&&|\|\|/.test(node.operator) ? "LogicalExpression" : "BinaryExpression");
return parseExprOp(node, start, minPrec, noIn, indent, line);
}

@@ -587,3 +643,10 @@ }

if (token.type.prefix) {
var node = startNode(), update = token.type.isUpdate;
var node = startNode(), update = token.type.isUpdate, nodeType;
if (token.type === tt.ellipsis) {
nodeType = "SpreadElement";
} else {
nodeType = update ? "UpdateExpression" : "UnaryExpression";
node.operator = token.value;
node.prefix = true;
}
node.operator = token.value;

@@ -594,7 +657,8 @@ node.prefix = true;

if (update) node.argument = checkLVal(node.argument);
return finishNode(node, update ? "UpdateExpression" : "UnaryExpression");
return finishNode(node, nodeType);
}
var start = storeCurrentPos();
var expr = parseExprSubscripts();
while (token.type.postfix && !canInsertSemicolon()) {
var node = startNodeFrom(expr);
var node = startNodeAt(start);
node.operator = token.value;

@@ -610,6 +674,7 @@ node.prefix = false;

function parseExprSubscripts() {
return parseSubscripts(parseExprAtom(), false, curIndent, curLineStart);
var start = storeCurrentPos();
return parseSubscripts(parseExprAtom(), start, false, curIndent, curLineStart);
}
function parseSubscripts(base, noCalls, startIndent, line) {
function parseSubscripts(base, start, noCalls, startIndent, line) {
for (;;) {

@@ -624,3 +689,3 @@ if (curLineStart != line && curIndent <= startIndent && tokenStartsLine()) {

if (eat(tt.dot)) {
var node = startNodeFrom(base);
var node = startNodeAt(start);
node.object = base;

@@ -636,3 +701,3 @@ if (curLineStart != line && curIndent <= startIndent && tokenStartsLine())

next();
var node = startNodeFrom(base);
var node = startNodeAt(start);
node.object = base;

@@ -646,6 +711,11 @@ node.property = parseExpression();

pushCx();
var node = startNodeFrom(base);
var node = startNodeAt(start);
node.callee = base;
node.arguments = parseExprList(tt.parenR);
base = finishNode(node, "CallExpression");
} else if (token.type == tt.template) {
var node = startNodeAt(start);
node.tag = base;
node.quasi = parseTemplate();
base = finishNode(node, "TaggedTemplateExpression");
} else {

@@ -663,6 +733,19 @@ return base;

return finishNode(node, "ThisExpression");
case tt.name:
return parseIdent();
case tt.num: case tt.string: case tt.regexp:
var start = storeCurrentPos();
var id = parseIdent();
return eat(tt.arrow) ? parseArrowExpression(startNodeAt(start), [id]) : id;
case tt.regexp:
var node = startNode();
var val = token.value;
node.regex = {pattern: val.pattern, flags: val.flags};
node.value = val.value;
node.raw = input.slice(token.start, token.end);
next();
return finishNode(node, "Literal");
case tt.num: case tt.string:
var node = startNode();
node.value = token.value;

@@ -681,8 +764,14 @@ node.raw = input.slice(token.start, token.end);

case tt.parenL:
var tokStart1 = token.start;
var start = storeCurrentPos();
next();
var val = parseExpression();
val.start = tokStart1;
val.end = token.end;
expect(tt.parenR);
if (eat(tt.arrow)) {
return parseArrowExpression(startNodeAt(start), val.expressions || (isDummy(val) ? [] : [val]));
}
if (options.preserveParens) {
var par = startNodeAt(start);
par.expression = val;
val = finishNode(par, "ParenthesizedExpression");
}
return val;

@@ -693,3 +782,3 @@

pushCx();
node.elements = parseExprList(tt.bracketR);
node.elements = parseExprList(tt.bracketR, true);
return finishNode(node, "ArrayExpression");

@@ -700,2 +789,5 @@

case tt._class:
return parseObj(true);
case tt._function:

@@ -709,2 +801,17 @@ var node = startNode();

case tt._yield:
var node = startNode();
next();
if (semicolon() || canInsertSemicolon()) {
node.delegate = false;
node.argument = null;
} else {
node.delegate = eat(tt.star);
node.argument = parseExpression(true);
}
return finishNode(node, "YieldExpression");
case tt.template:
return parseTemplate();
default:

@@ -718,3 +825,4 @@ return dummyIdent();

next();
node.callee = parseSubscripts(parseExprAtom(), true, startIndent, line);
var start = storeCurrentPos();
node.callee = parseSubscripts(parseExprAtom(), start, true, startIndent, line);
if (token.type == tt.parenL) {

@@ -729,37 +837,120 @@ pushCx();

function parseObj() {
function parseTemplateElement() {
var elem = startNode();
elem.value = token.value;
elem.tail = input.charCodeAt(token.end - 1) !== 123; // '{'
next();
return finishNode(elem, "TemplateElement");
}
function parseTemplate() {
var node = startNode();
node.properties = [];
node.expressions = [];
var curElt = parseTemplateElement();
node.quasis = [curElt];
while (!curElt.tail) {
var next = parseExpression();
if (isDummy(next)) {
node.quasis[node.quasis.length - 1].tail = true;
break;
}
node.expressions.push(next);
if (token.type === tt.templateContinued) {
node.quasis.push(curElt = parseTemplateElement());
} else {
curElt = startNode();
curElt.value = {cooked: "", raw: ""};
curElt.tail = true;
node.quasis.push(curElt);
}
}
return finishNode(node, "TemplateLiteral");
}
function parseObj(isClass, isStatement) {
var node = startNode();
if (isClass) {
next();
if (token.type === tt.name) node.id = parseIdent();
else if (isStatement) node.id = dummyIdent();
node.superClass = eat(tt._extends) ? parseExpression() : null;
node.body = startNode();
node.body.body = [];
} else {
node.properties = [];
}
pushCx();
var indent = curIndent + 1, line = curLineStart;
next();
eat(tt.braceL);
if (curIndent + 1 < indent) { indent = curIndent; line = curLineStart; }
while (!closes(tt.braceR, indent, line)) {
var name = parsePropertyName();
if (!name) { if (isDummy(parseExpression(true))) next(); eat(tt.comma); continue; }
var prop = startNode();
prop.key = name;
if (eat(tt.colon)) {
var prop = startNode(), isGenerator;
if (options.ecmaVersion >= 6) {
if (isClass) {
if (prop['static'] = (token.type === tt.name && token.value === "static")) next();
} else {
prop.method = false;
prop.shorthand = false;
}
isGenerator = eat(tt.star);
}
parsePropertyName(prop);
if (isDummy(prop.key)) { if (isDummy(parseExpression(true))) next(); eat(tt.comma); continue; }
if (!isClass && eat(tt.colon)) {
prop.kind = "init";
prop.value = parseExpression(true);
prop.kind = "init";
} else if (options.ecmaVersion >= 6 && (token.type === tt.parenL || token.type === tt.braceL)) {
if (isClass) {
prop.kind = "";
} else {
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 if (isClass) {
prop.kind = "";
prop.value = parseMethod(isGenerator);
} else {
prop.value = dummyIdent();
prop.kind = "init";
prop.value = options.ecmaVersion >= 6 ? prop.key : dummyIdent();
prop.shorthand = true;
}
node.properties.push(finishNode(prop, "Property"));
eat(tt.comma);
if (isClass) {
node.body.body.push(finishNode(prop, "MethodDefinition"));
semicolon();
} else {
node.properties.push(finishNode(prop, "Property"));
eat(tt.comma);
}
}
popCx();
eat(tt.braceR);
return finishNode(node, "ObjectExpression");
if (isClass) {
semicolon();
finishNode(node.body, "ClassBody");
return finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
} else {
return finishNode(node, "ObjectExpression");
}
}
function parsePropertyName() {
if (token.type === tt.num || token.type === tt.string) return parseExprAtom();
if (token.type === tt.name || token.type.keyword) return parseIdent();
function parsePropertyName(prop) {
if (options.ecmaVersion >= 6) {
if (eat(tt.bracketL)) {
prop.computed = true;
prop.key = parseExpression();
expect(tt.bracketR);
return;
} else {
prop.computed = false;
}
}
var key = (token.type === tt.num || token.type === tt.string) ? parseExprAtom() : parseIdent();
prop.key = key || dummyIdent();
}

@@ -774,2 +965,3 @@

node.name = token.type === tt.name ? token.value : token.type.keyword;
fetchToken.noRegexp();
next();

@@ -779,15 +971,80 @@ return finishNode(node, "Identifier");

function initFunction(node) {
node.id = null;
node.params = [];
if (options.ecmaVersion >= 6) {
node.defaults = [];
node.rest = null;
node.generator = false;
node.expression = false;
}
}
// Convert existing expression atom to assignable pattern
// if possible.
function toAssignable(node) {
if (options.ecmaVersion >= 6 && node) {
switch (node.type) {
case "ObjectExpression":
node.type = "ObjectPattern";
var props = node.properties;
for (var i = 0; i < props.length; i++) {
props[i].value = toAssignable(props[i].value);
}
break;
case "ArrayExpression":
node.type = "ArrayPattern";
var elms = node.elements;
for (var i = 0; i < elms.length; i++) {
elms[i] = toAssignable(elms[i]);
}
break;
case "SpreadElement":
node.argument = toAssignable(node.argument);
break;
}
}
return checkLVal(node);
}
function parseFunctionParams(node, params) {
var defaults = [], hasDefaults = false;
if (!params) {
pushCx();
params = parseExprList(tt.parenR);
}
for (var i = 0; i < params.length; i++) {
var param = params[i], defValue = null;
if (param.type === "AssignmentExpression") {
defValue = param.right;
param = param.left;
}
param = toAssignable(param);
if (param.type === "SpreadElement") {
param = param.argument;
if (i === params.length - 1) {
node.rest = param;
continue;
}
}
node.params.push(param);
defaults.push(defValue);
if (defValue) hasDefaults = true;
}
if (hasDefaults) node.defaults = defaults;
}
function parseFunction(node, isStatement) {
initFunction(node);
if (options.ecmaVersion >= 6) {
node.generator = eat(tt.star);
}
if (token.type === tt.name) node.id = parseIdent();
else if (isStatement) node.id = dummyIdent();
else node.id = null;
node.params = [];
pushCx();
expect(tt.parenL);
while (token.type == tt.name) {
node.params.push(parseIdent());
eat(tt.comma);
}
popCx();
eat(tt.parenR);
parseFunctionParams(node);
node.body = parseBlock();

@@ -797,3 +1054,113 @@ return finishNode(node, isStatement ? "FunctionDeclaration" : "FunctionExpression");

function parseExprList(close) {
function parseMethod(isGenerator) {
var node = startNode();
initFunction(node);
parseFunctionParams(node);
node.generator = isGenerator || false;
node.expression = options.ecmaVersion >= 6 && token.type !== tt.braceL;
node.body = node.expression ? parseExpression(true) : parseBlock();
return finishNode(node, "FunctionExpression");
}
function parseArrowExpression(node, params) {
initFunction(node);
parseFunctionParams(node, params);
node.expression = token.type !== tt.braceL;
node.body = node.expression ? parseExpression(true) : parseBlock();
return finishNode(node, "ArrowFunctionExpression");
}
function parseExport() {
var node = startNode();
next();
node['default'] = eat(tt._default);
node.specifiers = node.source = null;
if (node['default']) {
node.declaration = parseExpression();
semicolon();
} else if (token.type.keyword) {
node.declaration = parseStatement();
} else {
node.declaration = null;
parseSpecifierList(node, "Export");
}
semicolon();
return finishNode(node, "ExportDeclaration");
}
function parseImport() {
var node = startNode();
next();
if (token.type === tt.string) {
node.specifiers = [];
node.source = parseExprAtom();
node.kind = '';
} else {
if (token.type === tt.name && token.value !== "from") {
var elt = startNode();
elt.id = parseIdent();
elt.name = null;
elt['default'] = true;
finishNode(elt, "ImportSpecifier");
eat(tt.comma);
}
parseSpecifierList(node, "Import");
var specs = node.specifiers;
for (var i = 0; i < specs.length; i++) specs[i]['default'] = false;
if (elt) node.specifiers.unshift(elt);
}
semicolon();
return finishNode(node, "ImportDeclaration");
}
function parseSpecifierList(node, prefix) {
var elts = node.specifiers = [];
if (token.type === tt.star) {
var elt = startNode();
next();
if (token.type === tt.name && token.value === "as") {
next();
elt.name = parseIdent();
}
elts.push(finishNode(elt, prefix + "BatchSpecifier"));
} else {
var indent = curIndent, line = curLineStart, continuedLine = nextLineStart;
pushCx();
eat(tt.braceL);
if (curLineStart > continuedLine) continuedLine = curLineStart;
while (!closes(tt.braceR, indent + (curLineStart <= continuedLine ? 1 : 0), line)) {
var elt = startNode();
if (token.type === tt.star) {
next();
if (token.type === tt.name && token.value === "as") {
next();
elt.name = parseIdent();
}
finishNode(elt, prefix + "BatchSpecifier");
} else {
if (token.type === tt.name && token.value === "from") break;
elt.id = parseIdent();
if (token.type === tt.name && token.value === "as") {
next();
elt.name = parseIdent();
} else {
elt.name = null;
}
finishNode(elt, prefix + "Specifier");
}
elts.push(elt);
eat(tt.comma);
}
eat(tt.braceR);
popCx();
}
if (token.type === tt.name && token.value === "from") {
next();
node.source = parseExprAtom();
} else {
node.source = null;
}
}
function parseExprList(close, allowEmpty) {
var indent = curIndent, line = curLineStart, elts = [], continuedLine = nextLineStart;

@@ -803,2 +1170,6 @@ next(); // Opening bracket

while (!closes(close, indent + (curLineStart <= continuedLine ? 1 : 0), line)) {
if (eat(tt.comma)) {
elts.push(allowEmpty ? null : dummyIdent());
continue;
}
var elt = parseExpression(true);

@@ -811,3 +1182,3 @@ if (isDummy(elt)) {

}
while (eat(tt.comma)) {}
eat(tt.comma);
}

@@ -814,0 +1185,0 @@ popCx();

2

package.json

@@ -6,3 +6,3 @@ {

"main": "acorn.js",
"version": "0.9.0",
"version": "0.10.0",
"engines": {"node": ">=0.4.0"},

@@ -9,0 +9,0 @@ "maintainers": [{"name": "Marijn Haverbeke",

@@ -71,2 +71,6 @@ # Acorn

- **allowImportExportEverywhere**: By default, `import` and `export`
declarations can only appear at a program's top level. Setting this
option to `true` allows them anywhere where a statement is allowed.
- **locations**: When `true`, each node has a `loc` object attached

@@ -136,2 +140,7 @@ with `start` and `end` subobjects, each of which contains the

- **preserveParens**: If this option is `true`, parenthesized expressions
are represented by (non-standard) `ParenthesizedExpression` nodes
that have a single `expression` property containing the expression
inside parentheses.
[range]: https://bugzilla.mozilla.org/show_bug.cgi?id=745678

@@ -211,3 +220,3 @@

sense of the input. Depends on `acorn.js`, because it uses the same
tokenizer.
tokenizer. The loose parser does not support ECMAScript 6 syntax yet.

@@ -214,0 +223,0 @@ ### util/walk.js ###

(function(exports) {
var tests = [];
var acorn = typeof require == "undefined" ? window.acorn : require("../acorn.js");
exports.test = function(code, ast, options, comments) {
tests.push({code: code, ast: ast, options: options, comments: comments});
exports.test = function(code, ast, options) {
tests.push({code: code, ast: ast, options: options});
};

@@ -15,26 +14,25 @@ exports.testFail = function(code, message, options) {

exports.runTests = function(callback) {
var comments;
exports.runTests = function(config, callback) {
var parse = config.parse;
function onComment(block, text, start, end, startLoc, endLoc) {
comments.push({
block: block,
text: text,
start: start,
end: end,
startLoc: { line: startLoc.line, column: startLoc.column },
endLoc: { line: endLoc.line, column: endLoc.column }
});
}
var opts = {locations: true, onComment: onComment};
for (var i = 0; i < tests.length; ++i) {
var test = tests[i];
if (config.filter && !config.filter(test)) continue;
try {
comments = [];
if (test.options && !test.options.onComment) test.options.onComment = onComment;
var ast = acorn.parse(test.code, test.options || opts);
if (test.error) callback("fail", test.code,
"Expected error message: " + test.error + "\nBut parsing succeeded.");
var testOpts = test.options || {locations: true};
var expected = {};
if (expected.onComment = testOpts.onComment) {
testOpts.onComment = []
}
if (expected.onToken = testOpts.onToken) {
testOpts.onToken = [];
}
var ast = parse(test.code, testOpts);
if (test.error) {
if (config.loose) {
callback("ok", test.code);
} else {
callback("fail", test.code, "Expected error message: " + test.error + "\nBut parsing succeeded.");
}
}
else if (test.assert) {

@@ -47,9 +45,17 @@ var error = test.assert(ast);

var mis = misMatch(test.ast, ast);
for (var name in expected) {
if (mis) break;
if (expected[name]) {
mis = misMatch(expected[name], testOpts[name]);
testOpts[name] = expected[name];
}
}
if (mis) callback("fail", test.code, mis);
if (test.comments) mis = misMatch(test.comments, comments);
if (!mis) callback("ok", test.code);
else callback("fail", test.code, mis);
else callback("ok", test.code);
}
} catch(e) {
if (test.error && e instanceof SyntaxError) {
if (!(e instanceof SyntaxError)) {
throw e;
}
if (test.error) {
if (e.message == test.error) callback("ok", test.code);

@@ -72,3 +78,3 @@ else callback("fail", test.code,

function misMatch(exp, act) {
var misMatch = exports.misMatch = function(exp, act) {
if (!exp || !act || (typeof exp != "object") || (typeof act != "object")) {

@@ -89,3 +95,3 @@ if (exp !== act) return ppJSON(exp) + " !== " + ppJSON(act);

}
}
};

@@ -92,0 +98,0 @@ function mangle(ast) {

@@ -1,22 +0,109 @@

var driver = require("./driver.js");
require("./tests.js");
require("./tests-harmony.js");
(function() {
var driver;
var testsRun = 0, failed = 0;
function report(state, code, message) {
if (state != "ok") {++failed; console.log(code, message);}
++testsRun;
}
if (typeof require !== "undefined") {
driver = require("./driver.js");
require("./tests.js");
require("./tests-harmony.js");
} else {
driver = window;
}
var t0 = +new Date;
driver.runTests(report);
console.log(testsRun + " tests run in " + (+new Date - t0) + "ms");
var htmlLog = typeof document === "object" && document.getElementById('log');
var htmlGroup = htmlLog;
if (failed) {
console.log(failed + " failures.");
process.stdout.write("", function() {
process.exit(1);
});
} else {
console.log("All passed.");
}
function group(name) {
if (htmlGroup) {
var parentGroup = htmlGroup;
htmlGroup = document.createElement("ul");
var item = document.createElement("li");
item.textContent = name;
item.appendChild(htmlGroup);
parentGroup.appendChild(item);
}
if (typeof console === "object" && console.group) {
console.group(name);
}
}
function groupEnd() {
if (htmlGroup) {
htmlGroup = htmlGroup.parentElement.parentElement;
}
if (typeof console === "object" && console.groupEnd) {
console.groupEnd(name);
}
}
function log(title, message) {
if (htmlGroup) {
var elem = document.createElement("li");
elem.innerHTML = "<b>" + title + "</b> " + message;
htmlGroup.appendChild(elem);
}
if (typeof console === "object") console.log(title, message);
}
var stats, modes = {
Normal: {
config: {
parse: (typeof require === "undefined" ? window.acorn : require("../acorn.js")).parse
}
},
Loose: {
config: {
parse: (typeof require === "undefined" ? window.acorn : require("../acorn_loose")).parse_dammit,
loose: true,
filter: function (test) {
var opts = test.options || {};
if (opts.loose === false) return false;
return (opts.ecmaVersion || 5) <= 6;
}
}
}
};
function report(state, code, message) {
if (state != "ok") {++stats.failed; log(code, message);}
++stats.testsRun;
}
group("Errors");
for (var name in modes) {
group(name);
var mode = modes[name];
stats = mode.stats = {testsRun: 0, failed: 0};
var t0 = +new Date;
driver.runTests(mode.config, report);
mode.stats.duration = +new Date - t0;
groupEnd();
}
groupEnd();
function outputStats(name, stats) {
log(name + ":", stats.testsRun + " tests run in " + stats.duration + "ms; " +
(stats.failed ? stats.failed + " failures." : "all passed."));
}
var total = {testsRun: 0, failed: 0, duration: 0};
group("Stats");
for (var name in modes) {
var stats = modes[name].stats;
outputStats(name + " parser", stats);
for (var key in stats) total[key] += stats[key];
}
outputStats("Total", total);
groupEnd();
if (total.failed && typeof process === "object") {
process.stdout.write("", function() {
process.exit(1);
});
}
})();

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc