tools: update ESLint to 5.8.0

Update ESLint to 5.8.0.

PR-URL: https://github.com/nodejs/node/pull/23904
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
This commit is contained in:
cjihrig 2018-10-26 12:35:42 -04:00 committed by Daniel Bevenius
parent 9697c0820f
commit 2f1c356d7a
33 changed files with 1581 additions and 6384 deletions

View File

@ -888,6 +888,15 @@ module.exports = class Linter {
this.environments = new Environments();
}
/**
* Getter for package version.
* @static
* @returns {string} The version from package.json.
*/
static get version() {
return pkg.version;
}
/**
* Configuration object for the `verify` API. A JS representation of the eslintrc files.
* @typedef {Object} ESLintConfig

View File

@ -81,9 +81,7 @@ module.exports = {
const locStart = node.loc.start;
const locEnd = node.loc.end;
errors = errors.filter(error => {
const errorLoc = error[1];
errors = errors.filter(({ loc: errorLoc }) => {
if (errorLoc.line >= locStart.line && errorLoc.line <= locEnd.line) {
if (errorLoc.column >= locStart.column && (errorLoc.column <= locEnd.column || errorLoc.line < locEnd.line)) {
return false;
@ -157,7 +155,7 @@ module.exports = {
column: match.index
};
errors.push([node, location, "Irregular whitespace not allowed."]);
errors.push({ node, message: "Irregular whitespace not allowed.", loc: location });
}
});
}
@ -182,7 +180,7 @@ module.exports = {
column: sourceLines[lineIndex].length
};
errors.push([node, location, "Irregular whitespace not allowed."]);
errors.push({ node, message: "Irregular whitespace not allowed.", loc: location });
lastLineIndex = lineIndex;
}
}
@ -224,9 +222,7 @@ module.exports = {
}
// If we have any errors remaining report on them
errors.forEach(error => {
context.report(...error);
});
errors.forEach(error => context.report(error));
};
} else {
nodes.Program = noop;

View File

@ -57,6 +57,7 @@ function canBecomeVariableDeclaration(identifier) {
* @returns {boolean} Indicates if the variable is from outer scope or function parameters.
*/
function isOuterVariableInDestructing(name, initScope) {
if (initScope.through.find(ref => ref.resolved && ref.resolved.name === name)) {
return true;
}
@ -96,6 +97,54 @@ function getDestructuringHost(reference) {
return node;
}
/**
* Determines if a destructuring assignment node contains
* any MemberExpression nodes. This is used to determine if a
* variable that is only written once using destructuring can be
* safely converted into a const declaration.
* @param {ASTNode} node The ObjectPattern or ArrayPattern node to check.
* @returns {boolean} True if the destructuring pattern contains
* a MemberExpression, false if not.
*/
function hasMemberExpressionAssignment(node) {
switch (node.type) {
case "ObjectPattern":
return node.properties.some(prop => {
if (prop) {
/*
* Spread elements have an argument property while
* others have a value property. Because different
* parsers use different node types for spread elements,
* we just check if there is an argument property.
*/
return hasMemberExpressionAssignment(prop.argument || prop.value);
}
return false;
});
case "ArrayPattern":
return node.elements.some(element => {
if (element) {
return hasMemberExpressionAssignment(element);
}
return false;
});
case "AssignmentPattern":
return hasMemberExpressionAssignment(node.left);
case "MemberExpression":
return true;
// no default
}
return false;
}
/**
* Gets an identifier node of a given variable.
*
@ -148,7 +197,8 @@ function getIdentifierIfShouldBeConst(variable, ignoreReadBeforeAssign) {
if (destructuringHost !== null && destructuringHost.left !== void 0) {
const leftNode = destructuringHost.left;
let hasOuterVariables = false;
let hasOuterVariables = false,
hasNonIdentifiers = false;
if (leftNode.type === "ObjectPattern") {
const properties = leftNode.properties;
@ -157,16 +207,23 @@ function getIdentifierIfShouldBeConst(variable, ignoreReadBeforeAssign) {
.filter(prop => prop.value)
.map(prop => prop.value.name)
.some(name => isOuterVariableInDestructing(name, variable.scope));
hasNonIdentifiers = hasMemberExpressionAssignment(leftNode);
} else if (leftNode.type === "ArrayPattern") {
const elements = leftNode.elements;
hasOuterVariables = elements
.map(element => element && element.name)
.some(name => isOuterVariableInDestructing(name, variable.scope));
hasNonIdentifiers = hasMemberExpressionAssignment(leftNode);
}
if (hasOuterVariables) {
if (hasOuterVariables || hasNonIdentifiers) {
return null;
}
}
writer = reference;
@ -192,9 +249,11 @@ function getIdentifierIfShouldBeConst(variable, ignoreReadBeforeAssign) {
if (!shouldBeConst) {
return null;
}
if (isReadBeforeInit) {
return variable.defs[0].name;
}
return writer.identifier;
}
@ -295,7 +354,7 @@ module.exports = {
create(context) {
const options = context.options[0] || {};
const sourceCode = context.getSourceCode();
const checkingMixedDestructuring = options.destructuring !== "all";
const shouldMatchAnyDestructuredVariable = options.destructuring !== "all";
const ignoreReadBeforeAssign = options.ignoreReadBeforeAssign === true;
const variables = [];
@ -316,7 +375,7 @@ module.exports = {
function checkGroup(nodes) {
const nodesToReport = nodes.filter(Boolean);
if (nodes.length && (checkingMixedDestructuring || nodesToReport.length === nodes.length)) {
if (nodes.length && (shouldMatchAnyDestructuredVariable || nodesToReport.length === nodes.length)) {
const varDeclParent = findUp(nodes[0], "VariableDeclaration", parentNode => parentNode.type.endsWith("Statement"));
const shouldFix = varDeclParent &&

View File

@ -397,7 +397,7 @@ class RuleTester {
*/
function assertASTDidntChange(beforeAST, afterAST) {
if (!lodash.isEqual(beforeAST, afterAST)) {
assert.fail(null, null, "Rule should not modify AST.");
assert.fail("Rule should not modify AST.");
}
}
@ -551,7 +551,7 @@ class RuleTester {
} else {
// Message was an unexpected type
assert.fail(message, null, "Error should be a string, object, or RegExp.");
assert.fail(`Error should be a string, object, or RegExp, but found (${util.inspect(message)})`);
}
}
}

View File

@ -17,44 +17,24 @@ Please note that this tool only parses source code to JSX AST, which is useful f
## Usage
You can use module directly in order to get Acorn instance with plugin installed:
Requiring this module provides you with an Acorn plugin that you can use like this:
```javascript
var acorn = require('acorn-jsx');
```
Or you can use `inject.js` for injecting plugin into your own version of Acorn like following:
```javascript
var acorn = require('acorn-jsx/inject')(require('./custom-acorn'));
```
Then, use `plugins` option whenever you need to support JSX while parsing:
```javascript
var ast = acorn.parse(code, {
plugins: { jsx: true }
});
var acorn = require("acorn");
var jsx = require("acorn-jsx");
acorn.Parser.extend(jsx()).parse("my(<jsx/>, 'code');");
```
Note that official spec doesn't support mix of XML namespaces and object-style access in tag names (#27) like in `<namespace:Object.Property />`, so it was deprecated in `acorn-jsx@3.0`. If you still want to opt-in to support of such constructions, you can pass the following option:
```javascript
var ast = acorn.parse(code, {
plugins: {
jsx: { allowNamespacedObjects: true }
}
});
acorn.Parser.extend(jsx({ allowNamespacedObjects: true }))
```
Also, since most apps use pure React transformer, a new option was introduced that allows to prohibit namespaces completely:
```javascript
var ast = acorn.parse(code, {
plugins: {
jsx: { allowNamespaces: false }
}
});
acorn.Parser.extend(jsx({ allowNamespaces: false }))
```
Note that by default `allowNamespaces` is enabled for spec compliancy.

View File

@ -1,3 +1,431 @@
'use strict';
const XHTMLEntities = require('./xhtml');
module.exports = require('./inject')(require('acorn'));
const hexNumber = /^[\da-fA-F]+$/;
const decimalNumber = /^\d+$/;
const {tokTypes: tt, TokContext, tokContexts, TokenType, isNewLine, isIdentifierStart, isIdentifierChar} = require("acorn");
const tc_oTag = new TokContext('<tag', false);
const tc_cTag = new TokContext('</tag', false);
const tc_expr = new TokContext('<tag>...</tag>', true, true);
const tok = {
jsxName: new TokenType('jsxName'),
jsxText: new TokenType('jsxText', {beforeExpr: true}),
jsxTagStart: new TokenType('jsxTagStart'),
jsxTagEnd: new TokenType('jsxTagEnd')
}
tok.jsxTagStart.updateContext = function() {
this.context.push(tc_expr); // treat as beginning of JSX expression
this.context.push(tc_oTag); // start opening tag context
this.exprAllowed = false;
};
tok.jsxTagEnd.updateContext = function(prevType) {
let out = this.context.pop();
if (out === tc_oTag && prevType === tt.slash || out === tc_cTag) {
this.context.pop();
this.exprAllowed = this.curContext() === tc_expr;
} else {
this.exprAllowed = true;
}
};
// Transforms JSX element name to string.
function getQualifiedJSXName(object) {
if (!object)
return object;
if (object.type === 'JSXIdentifier')
return object.name;
if (object.type === 'JSXNamespacedName')
return object.namespace.name + ':' + object.name.name;
if (object.type === 'JSXMemberExpression')
return getQualifiedJSXName(object.object) + '.' +
getQualifiedJSXName(object.property);
}
module.exports = function(options = {}) {
return function(Parser) {
return plugin({
allowNamespaces: options.allowNamespaces !== false,
allowNamespacedObjects: !!options.allowNamespacedObjects
}, Parser);
}
}
module.exports.tokTypes = tok
function plugin(options, Parser) {
return class extends Parser {
// Reads inline JSX contents token.
jsx_readToken() {
let out = '', chunkStart = this.pos;
for (;;) {
if (this.pos >= this.input.length)
this.raise(this.start, 'Unterminated JSX contents');
let ch = this.input.charCodeAt(this.pos);
switch (ch) {
case 60: // '<'
case 123: // '{'
if (this.pos === this.start) {
if (ch === 60 && this.exprAllowed) {
++this.pos;
return this.finishToken(tok.jsxTagStart);
}
return this.getTokenFromCode(ch);
}
out += this.input.slice(chunkStart, this.pos);
return this.finishToken(tok.jsxText, out);
case 38: // '&'
out += this.input.slice(chunkStart, this.pos);
out += this.jsx_readEntity();
chunkStart = this.pos;
break;
default:
if (isNewLine(ch)) {
out += this.input.slice(chunkStart, this.pos);
out += this.jsx_readNewLine(true);
chunkStart = this.pos;
} else {
++this.pos;
}
}
}
}
jsx_readNewLine(normalizeCRLF) {
let ch = this.input.charCodeAt(this.pos);
let out;
++this.pos;
if (ch === 13 && this.input.charCodeAt(this.pos) === 10) {
++this.pos;
out = normalizeCRLF ? '\n' : '\r\n';
} else {
out = String.fromCharCode(ch);
}
if (this.options.locations) {
++this.curLine;
this.lineStart = this.pos;
}
return out;
};
jsx_readString(quote) {
let out = '', chunkStart = ++this.pos;
for (;;) {
if (this.pos >= this.input.length)
this.raise(this.start, 'Unterminated string constant');
let ch = this.input.charCodeAt(this.pos);
if (ch === quote) break;
if (ch === 38) { // '&'
out += this.input.slice(chunkStart, this.pos);
out += this.jsx_readEntity();
chunkStart = this.pos;
} else if (isNewLine(ch)) {
out += this.input.slice(chunkStart, this.pos);
out += this.jsx_readNewLine(false);
chunkStart = this.pos;
} else {
++this.pos;
}
}
out += this.input.slice(chunkStart, this.pos++);
return this.finishToken(tt.string, out);
}
jsx_readEntity() {
let str = '', count = 0, entity;
let ch = this.input[this.pos];
if (ch !== '&')
this.raise(this.pos, 'Entity must start with an ampersand');
let startPos = ++this.pos;
while (this.pos < this.input.length && count++ < 10) {
ch = this.input[this.pos++];
if (ch === ';') {
if (str[0] === '#') {
if (str[1] === 'x') {
str = str.substr(2);
if (hexNumber.test(str))
entity = String.fromCharCode(parseInt(str, 16));
} else {
str = str.substr(1);
if (decimalNumber.test(str))
entity = String.fromCharCode(parseInt(str, 10));
}
} else {
entity = XHTMLEntities[str];
}
break;
}
str += ch;
}
if (!entity) {
this.pos = startPos;
return '&';
}
return entity;
}
// Read a JSX identifier (valid tag or attribute name).
//
// Optimized version since JSX identifiers can't contain
// escape characters and so can be read as single slice.
// Also assumes that first character was already checked
// by isIdentifierStart in readToken.
jsx_readWord() {
let ch, start = this.pos;
do {
ch = this.input.charCodeAt(++this.pos);
} while (isIdentifierChar(ch) || ch === 45); // '-'
return this.finishToken(tok.jsxName, this.input.slice(start, this.pos));
}
// Parse next token as JSX identifier
jsx_parseIdentifier() {
let node = this.startNode();
if (this.type === tok.jsxName)
node.name = this.value;
else if (this.type.keyword)
node.name = this.type.keyword;
else
this.unexpected();
this.next();
return this.finishNode(node, 'JSXIdentifier');
}
// Parse namespaced identifier.
jsx_parseNamespacedName() {
let startPos = this.start, startLoc = this.startLoc;
let name = this.jsx_parseIdentifier();
if (!options.allowNamespaces || !this.eat(tt.colon)) return name;
var node = this.startNodeAt(startPos, startLoc);
node.namespace = name;
node.name = this.jsx_parseIdentifier();
return this.finishNode(node, 'JSXNamespacedName');
}
// Parses element name in any form - namespaced, member
// or single identifier.
jsx_parseElementName() {
if (this.type === tok.jsxTagEnd) return '';
let startPos = this.start, startLoc = this.startLoc;
let node = this.jsx_parseNamespacedName();
if (this.type === tt.dot && node.type === 'JSXNamespacedName' && !options.allowNamespacedObjects) {
this.unexpected();
}
while (this.eat(tt.dot)) {
let newNode = this.startNodeAt(startPos, startLoc);
newNode.object = node;
newNode.property = this.jsx_parseIdentifier();
node = this.finishNode(newNode, 'JSXMemberExpression');
}
return node;
}
// Parses any type of JSX attribute value.
jsx_parseAttributeValue() {
switch (this.type) {
case tt.braceL:
let node = this.jsx_parseExpressionContainer();
if (node.expression.type === 'JSXEmptyExpression')
this.raise(node.start, 'JSX attributes must only be assigned a non-empty expression');
return node;
case tok.jsxTagStart:
case tt.string:
return this.parseExprAtom();
default:
this.raise(this.start, 'JSX value should be either an expression or a quoted JSX text');
}
}
// JSXEmptyExpression is unique type since it doesn't actually parse anything,
// and so it should start at the end of last read token (left brace) and finish
// at the beginning of the next one (right brace).
jsx_parseEmptyExpression() {
let node = this.startNodeAt(this.lastTokEnd, this.lastTokEndLoc);
return this.finishNodeAt(node, 'JSXEmptyExpression', this.start, this.startLoc);
}
// Parses JSX expression enclosed into curly brackets.
jsx_parseExpressionContainer() {
let node = this.startNode();
this.next();
node.expression = this.type === tt.braceR
? this.jsx_parseEmptyExpression()
: this.parseExpression();
this.expect(tt.braceR);
return this.finishNode(node, 'JSXExpressionContainer');
}
// Parses following JSX attribute name-value pair.
jsx_parseAttribute() {
let node = this.startNode();
if (this.eat(tt.braceL)) {
this.expect(tt.ellipsis);
node.argument = this.parseMaybeAssign();
this.expect(tt.braceR);
return this.finishNode(node, 'JSXSpreadAttribute');
}
node.name = this.jsx_parseNamespacedName();
node.value = this.eat(tt.eq) ? this.jsx_parseAttributeValue() : null;
return this.finishNode(node, 'JSXAttribute');
}
// Parses JSX opening tag starting after '<'.
jsx_parseOpeningElementAt(startPos, startLoc) {
let node = this.startNodeAt(startPos, startLoc);
node.attributes = [];
let nodeName = this.jsx_parseElementName();
if (nodeName) node.name = nodeName;
while (this.type !== tt.slash && this.type !== tok.jsxTagEnd)
node.attributes.push(this.jsx_parseAttribute());
node.selfClosing = this.eat(tt.slash);
this.expect(tok.jsxTagEnd);
return this.finishNode(node, nodeName ? 'JSXOpeningElement' : 'JSXOpeningFragment');
}
// Parses JSX closing tag starting after '</'.
jsx_parseClosingElementAt(startPos, startLoc) {
let node = this.startNodeAt(startPos, startLoc);
let nodeName = this.jsx_parseElementName();
if (nodeName) node.name = nodeName;
this.expect(tok.jsxTagEnd);
return this.finishNode(node, nodeName ? 'JSXClosingElement' : 'JSXClosingFragment');
}
// Parses entire JSX element, including it's opening tag
// (starting after '<'), attributes, contents and closing tag.
jsx_parseElementAt(startPos, startLoc) {
let node = this.startNodeAt(startPos, startLoc);
let children = [];
let openingElement = this.jsx_parseOpeningElementAt(startPos, startLoc);
let closingElement = null;
if (!openingElement.selfClosing) {
contents: for (;;) {
switch (this.type) {
case tok.jsxTagStart:
startPos = this.start; startLoc = this.startLoc;
this.next();
if (this.eat(tt.slash)) {
closingElement = this.jsx_parseClosingElementAt(startPos, startLoc);
break contents;
}
children.push(this.jsx_parseElementAt(startPos, startLoc));
break;
case tok.jsxText:
children.push(this.parseExprAtom());
break;
case tt.braceL:
children.push(this.jsx_parseExpressionContainer());
break;
default:
this.unexpected();
}
}
if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
this.raise(
closingElement.start,
'Expected corresponding JSX closing tag for <' + getQualifiedJSXName(openingElement.name) + '>');
}
}
let fragmentOrElement = openingElement.name ? 'Element' : 'Fragment';
node['opening' + fragmentOrElement] = openingElement;
node['closing' + fragmentOrElement] = closingElement;
node.children = children;
if (this.type === tt.relational && this.value === "<") {
this.raise(this.start, "Adjacent JSX elements must be wrapped in an enclosing tag");
}
return this.finishNode(node, 'JSX' + fragmentOrElement);
}
// Parse JSX text
jsx_parseText(value) {
let node = this.parseLiteral(value);
node.type = "JSXText";
return node;
}
// Parses entire JSX element from current position.
jsx_parseElement() {
let startPos = this.start, startLoc = this.startLoc;
this.next();
return this.jsx_parseElementAt(startPos, startLoc);
}
parseExprAtom(refShortHandDefaultPos) {
if (this.type === tok.jsxText)
return this.jsx_parseText(this.value);
else if (this.type === tok.jsxTagStart)
return this.jsx_parseElement();
else
return super.parseExprAtom(refShortHandDefaultPos);
}
readToken(code) {
let context = this.curContext();
if (context === tc_expr) return this.jsx_readToken();
if (context === tc_oTag || context === tc_cTag) {
if (isIdentifierStart(code)) return this.jsx_readWord();
if (code == 62) {
++this.pos;
return this.finishToken(tok.jsxTagEnd);
}
if ((code === 34 || code === 39) && context == tc_oTag)
return this.jsx_readString(code);
}
if (code === 60 && this.exprAllowed && this.input.charCodeAt(this.pos + 1) !== 33) {
++this.pos;
return this.finishToken(tok.jsxTagStart);
}
return super.readToken(code)
}
updateContext(prevType) {
if (this.type == tt.braceL) {
var curContext = this.curContext();
if (curContext == tc_oTag) this.context.push(tokContexts.b_expr);
else if (curContext == tc_expr) this.context.push(tokContexts.b_tmpl);
else super.updateContext(prevType)
this.exprAllowed = true;
} else if (this.type === tt.slash && prevType === tok.jsxTagStart) {
this.context.length -= 2; // do not consider JSX expr -> JSX open tag -> ... anymore
this.context.push(tc_cTag); // reconsider as closing tag context
this.exprAllowed = false;
} else {
return super.updateContext(prevType);
}
}
};
};

View File

@ -1,449 +0,0 @@
'use strict';
var XHTMLEntities = require('./xhtml');
var hexNumber = /^[\da-fA-F]+$/;
var decimalNumber = /^\d+$/;
module.exports = function(acorn) {
var tt = acorn.tokTypes;
var tc = acorn.tokContexts;
tc.j_oTag = new acorn.TokContext('<tag', false);
tc.j_cTag = new acorn.TokContext('</tag', false);
tc.j_expr = new acorn.TokContext('<tag>...</tag>', true, true);
tt.jsxName = new acorn.TokenType('jsxName');
tt.jsxText = new acorn.TokenType('jsxText', {beforeExpr: true});
tt.jsxTagStart = new acorn.TokenType('jsxTagStart');
tt.jsxTagEnd = new acorn.TokenType('jsxTagEnd');
tt.jsxTagStart.updateContext = function() {
this.context.push(tc.j_expr); // treat as beginning of JSX expression
this.context.push(tc.j_oTag); // start opening tag context
this.exprAllowed = false;
};
tt.jsxTagEnd.updateContext = function(prevType) {
var out = this.context.pop();
if (out === tc.j_oTag && prevType === tt.slash || out === tc.j_cTag) {
this.context.pop();
this.exprAllowed = this.curContext() === tc.j_expr;
} else {
this.exprAllowed = true;
}
};
var pp = acorn.Parser.prototype;
// Reads inline JSX contents token.
pp.jsx_readToken = function() {
var out = '', chunkStart = this.pos;
for (;;) {
if (this.pos >= this.input.length)
this.raise(this.start, 'Unterminated JSX contents');
var ch = this.input.charCodeAt(this.pos);
switch (ch) {
case 60: // '<'
case 123: // '{'
if (this.pos === this.start) {
if (ch === 60 && this.exprAllowed) {
++this.pos;
return this.finishToken(tt.jsxTagStart);
}
return this.getTokenFromCode(ch);
}
out += this.input.slice(chunkStart, this.pos);
return this.finishToken(tt.jsxText, out);
case 38: // '&'
out += this.input.slice(chunkStart, this.pos);
out += this.jsx_readEntity();
chunkStart = this.pos;
break;
default:
if (acorn.isNewLine(ch)) {
out += this.input.slice(chunkStart, this.pos);
out += this.jsx_readNewLine(true);
chunkStart = this.pos;
} else {
++this.pos;
}
}
}
};
pp.jsx_readNewLine = function(normalizeCRLF) {
var ch = this.input.charCodeAt(this.pos);
var out;
++this.pos;
if (ch === 13 && this.input.charCodeAt(this.pos) === 10) {
++this.pos;
out = normalizeCRLF ? '\n' : '\r\n';
} else {
out = String.fromCharCode(ch);
}
if (this.options.locations) {
++this.curLine;
this.lineStart = this.pos;
}
return out;
};
pp.jsx_readString = function(quote) {
var out = '', chunkStart = ++this.pos;
for (;;) {
if (this.pos >= this.input.length)
this.raise(this.start, 'Unterminated string constant');
var ch = this.input.charCodeAt(this.pos);
if (ch === quote) break;
if (ch === 38) { // '&'
out += this.input.slice(chunkStart, this.pos);
out += this.jsx_readEntity();
chunkStart = this.pos;
} else if (acorn.isNewLine(ch)) {
out += this.input.slice(chunkStart, this.pos);
out += this.jsx_readNewLine(false);
chunkStart = this.pos;
} else {
++this.pos;
}
}
out += this.input.slice(chunkStart, this.pos++);
return this.finishToken(tt.string, out);
};
pp.jsx_readEntity = function() {
var str = '', count = 0, entity;
var ch = this.input[this.pos];
if (ch !== '&')
this.raise(this.pos, 'Entity must start with an ampersand');
var startPos = ++this.pos;
while (this.pos < this.input.length && count++ < 10) {
ch = this.input[this.pos++];
if (ch === ';') {
if (str[0] === '#') {
if (str[1] === 'x') {
str = str.substr(2);
if (hexNumber.test(str))
entity = String.fromCharCode(parseInt(str, 16));
} else {
str = str.substr(1);
if (decimalNumber.test(str))
entity = String.fromCharCode(parseInt(str, 10));
}
} else {
entity = XHTMLEntities[str];
}
break;
}
str += ch;
}
if (!entity) {
this.pos = startPos;
return '&';
}
return entity;
};
// Read a JSX identifier (valid tag or attribute name).
//
// Optimized version since JSX identifiers can't contain
// escape characters and so can be read as single slice.
// Also assumes that first character was already checked
// by isIdentifierStart in readToken.
pp.jsx_readWord = function() {
var ch, start = this.pos;
do {
ch = this.input.charCodeAt(++this.pos);
} while (acorn.isIdentifierChar(ch) || ch === 45); // '-'
return this.finishToken(tt.jsxName, this.input.slice(start, this.pos));
};
// Transforms JSX element name to string.
function getQualifiedJSXName(object) {
if (!object)
return object;
if (object.type === 'JSXIdentifier')
return object.name;
if (object.type === 'JSXNamespacedName')
return object.namespace.name + ':' + object.name.name;
if (object.type === 'JSXMemberExpression')
return getQualifiedJSXName(object.object) + '.' +
getQualifiedJSXName(object.property);
}
// Parse next token as JSX identifier
pp.jsx_parseIdentifier = function() {
var node = this.startNode();
if (this.type === tt.jsxName)
node.name = this.value;
else if (this.type.keyword)
node.name = this.type.keyword;
else
this.unexpected();
this.next();
return this.finishNode(node, 'JSXIdentifier');
};
// Parse namespaced identifier.
pp.jsx_parseNamespacedName = function() {
var startPos = this.start, startLoc = this.startLoc;
var name = this.jsx_parseIdentifier();
if (!this.options.plugins.jsx.allowNamespaces || !this.eat(tt.colon)) return name;
var node = this.startNodeAt(startPos, startLoc);
node.namespace = name;
node.name = this.jsx_parseIdentifier();
return this.finishNode(node, 'JSXNamespacedName');
};
// Parses element name in any form - namespaced, member
// or single identifier.
pp.jsx_parseElementName = function() {
if (this.type === tt.jsxTagEnd)
return '';
var startPos = this.start, startLoc = this.startLoc;
var node = this.jsx_parseNamespacedName();
if (this.type === tt.dot && node.type === 'JSXNamespacedName' && !this.options.plugins.jsx.allowNamespacedObjects) {
this.unexpected();
}
while (this.eat(tt.dot)) {
var newNode = this.startNodeAt(startPos, startLoc);
newNode.object = node;
newNode.property = this.jsx_parseIdentifier();
node = this.finishNode(newNode, 'JSXMemberExpression');
}
return node;
};
// Parses any type of JSX attribute value.
pp.jsx_parseAttributeValue = function() {
switch (this.type) {
case tt.braceL:
var node = this.jsx_parseExpressionContainer();
if (node.expression.type === 'JSXEmptyExpression')
this.raise(node.start, 'JSX attributes must only be assigned a non-empty expression');
return node;
case tt.jsxTagStart:
case tt.string:
return this.parseExprAtom();
default:
this.raise(this.start, 'JSX value should be either an expression or a quoted JSX text');
}
};
// JSXEmptyExpression is unique type since it doesn't actually parse anything,
// and so it should start at the end of last read token (left brace) and finish
// at the beginning of the next one (right brace).
pp.jsx_parseEmptyExpression = function() {
var node = this.startNodeAt(this.lastTokEnd, this.lastTokEndLoc);
return this.finishNodeAt(node, 'JSXEmptyExpression', this.start, this.startLoc);
};
// Parses JSX expression enclosed into curly brackets.
pp.jsx_parseExpressionContainer = function() {
var node = this.startNode();
this.next();
node.expression = this.type === tt.braceR
? this.jsx_parseEmptyExpression()
: this.parseExpression();
this.expect(tt.braceR);
return this.finishNode(node, 'JSXExpressionContainer');
};
// Parses following JSX attribute name-value pair.
pp.jsx_parseAttribute = function() {
var node = this.startNode();
if (this.eat(tt.braceL)) {
this.expect(tt.ellipsis);
node.argument = this.parseMaybeAssign();
this.expect(tt.braceR);
return this.finishNode(node, 'JSXSpreadAttribute');
}
node.name = this.jsx_parseNamespacedName();
node.value = this.eat(tt.eq) ? this.jsx_parseAttributeValue() : null;
return this.finishNode(node, 'JSXAttribute');
};
// Parses JSX opening tag starting after '<'.
pp.jsx_parseOpeningElementAt = function(startPos, startLoc) {
var node = this.startNodeAt(startPos, startLoc);
node.attributes = [];
var nodeName = this.jsx_parseElementName();
if (nodeName) node.name = nodeName;
while (this.type !== tt.slash && this.type !== tt.jsxTagEnd)
node.attributes.push(this.jsx_parseAttribute());
node.selfClosing = this.eat(tt.slash);
this.expect(tt.jsxTagEnd);
return this.finishNode(node, nodeName ? 'JSXOpeningElement' : 'JSXOpeningFragment');
};
// Parses JSX closing tag starting after '</'.
pp.jsx_parseClosingElementAt = function(startPos, startLoc) {
var node = this.startNodeAt(startPos, startLoc);
var nodeName = this.jsx_parseElementName();
if (nodeName) node.name = nodeName;
this.expect(tt.jsxTagEnd);
return this.finishNode(node, nodeName ? 'JSXClosingElement' : 'JSXClosingFragment');
};
// Parses entire JSX element, including it's opening tag
// (starting after '<'), attributes, contents and closing tag.
pp.jsx_parseElementAt = function(startPos, startLoc) {
var node = this.startNodeAt(startPos, startLoc);
var children = [];
var openingElement = this.jsx_parseOpeningElementAt(startPos, startLoc);
var closingElement = null;
if (!openingElement.selfClosing) {
contents: for (;;) {
switch (this.type) {
case tt.jsxTagStart:
startPos = this.start; startLoc = this.startLoc;
this.next();
if (this.eat(tt.slash)) {
closingElement = this.jsx_parseClosingElementAt(startPos, startLoc);
break contents;
}
children.push(this.jsx_parseElementAt(startPos, startLoc));
break;
case tt.jsxText:
children.push(this.parseExprAtom());
break;
case tt.braceL:
children.push(this.jsx_parseExpressionContainer());
break;
default:
this.unexpected();
}
}
if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
this.raise(
closingElement.start,
'Expected corresponding JSX closing tag for <' + getQualifiedJSXName(openingElement.name) + '>');
}
}
node.openingElement = openingElement;
node.closingElement = closingElement;
node.children = children;
if (this.type === tt.relational && this.value === "<") {
this.raise(this.start, "Adjacent JSX elements must be wrapped in an enclosing tag");
}
return this.finishNode(node, openingElement.name ? 'JSXElement' : 'JSXFragment');
};
// Parse JSX text
pp.jsx_parseText = function(value) {
var node = this.parseLiteral(value);
node.type = "JSXText";
return node;
};
// Parses entire JSX element from current position.
pp.jsx_parseElement = function() {
var startPos = this.start, startLoc = this.startLoc;
this.next();
return this.jsx_parseElementAt(startPos, startLoc);
};
acorn.plugins.jsx = function(instance, opts) {
if (!opts) {
return;
}
if (typeof opts !== 'object') {
opts = {};
}
instance.options.plugins.jsx = {
allowNamespaces: opts.allowNamespaces !== false,
allowNamespacedObjects: !!opts.allowNamespacedObjects
};
instance.extend('parseExprAtom', function(inner) {
return function(refShortHandDefaultPos) {
if (this.type === tt.jsxText)
return this.jsx_parseText(this.value);
else if (this.type === tt.jsxTagStart)
return this.jsx_parseElement();
else
return inner.call(this, refShortHandDefaultPos);
};
});
instance.extend('readToken', function(inner) {
return function(code) {
var context = this.curContext();
if (context === tc.j_expr) return this.jsx_readToken();
if (context === tc.j_oTag || context === tc.j_cTag) {
if (acorn.isIdentifierStart(code)) return this.jsx_readWord();
if (code == 62) {
++this.pos;
return this.finishToken(tt.jsxTagEnd);
}
if ((code === 34 || code === 39) && context == tc.j_oTag)
return this.jsx_readString(code);
}
if (code === 60 && this.exprAllowed && this.input.charCodeAt(this.pos + 1) !== 33) {
++this.pos;
return this.finishToken(tt.jsxTagStart);
}
return inner.call(this, code);
};
});
instance.extend('updateContext', function(inner) {
return function(prevType) {
if (this.type == tt.braceL) {
var curContext = this.curContext();
if (curContext == tc.j_oTag) this.context.push(tc.b_expr);
else if (curContext == tc.j_expr) this.context.push(tc.b_tmpl);
else inner.call(this, prevType);
this.exprAllowed = true;
} else if (this.type === tt.slash && prevType === tt.jsxTagStart) {
this.context.length -= 2; // do not consider JSX expr -> JSX open tag -> ... anymore
this.context.push(tc.j_cTag); // reconsider as closing tag context
this.exprAllowed = false;
} else {
return inner.call(this, prevType);
}
};
});
};
return acorn;
};

View File

@ -3,14 +3,10 @@
"url": "https://github.com/RReverser/acorn-jsx/issues"
},
"bundleDependencies": false,
"dependencies": {
"acorn": "^5.0.3"
},
"deprecated": false,
"description": "Alternative, faster React.js JSX parser",
"devDependencies": {
"chai": "^3.0.0",
"mocha": "^3.3.0"
"acorn": "^6.0.0"
},
"homepage": "https://github.com/RReverser/acorn-jsx",
"license": "MIT",
@ -22,6 +18,9 @@
}
],
"name": "acorn-jsx",
"peerDependencies": {
"acorn": "^6.0.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/RReverser/acorn-jsx.git"
@ -29,5 +28,5 @@
"scripts": {
"test": "node test/run.js"
},
"version": "4.1.1"
"version": "5.0.0"
}

View File

@ -1,79 +0,0 @@
List of Acorn contributors. Updated before every release.
Adrian Heine
Adrian Rakovsky
Alistair Braidwood
Amila Welihinda
Andres Suarez
Angelo
Aparajita Fishman
Arian Stolwijk
Artem Govorov
Boopesh Mahendran
Bradley Heinz
Brandon Mills
Charles Hughes
Charmander
Chris McKnight
Conrad Irwin
Daniel Tschinder
David Bonnet
Domenico Matteo
ehmicky
Eugene Obrezkov
Felix Maier
Forbes Lindesay
Gilad Peleg
impinball
Ingvar Stepanyan
Jackson Ray Hamilton
Jesse McCarthy
Jiaxing Wang
Joel Kemp
Johannes Herr
John-David Dalton
Jordan Klassen
Jürg Lehni
Kai Cataldo
keeyipchan
Keheliya Gallaba
Kevin Irish
Kevin Kwok
krator
laosb
luckyzeng
Marek
Marijn Haverbeke
Martin Carlberg
Mat Garcia
Mathias Bynens
Mathieu 'p01' Henri
Matthew Bastien
Max Schaefer
Max Zerzouri
Mihai Bazon
Mike Rennie
naoh
Nicholas C. Zakas
Nick Fitzgerald
Olivier Thomann
Oskar Schöldström
Paul Harper
Peter Rust
PlNG
Prayag Verma
ReadmeCritic
r-e-d
Renée Kooi
Richard Gibson
Rich Harris
Sebastian McKenzie
Shahar Soel
Sheel Bedi
Simen Bekkhus
Teddy Katz
Timothy Gu
Toru Nagashima
Victor Homyakov
Wexpo Lyu
zsjforcn

View File

@ -1,11 +1,6 @@
# Acorn
[![Build Status](https://travis-ci.org/acornjs/acorn.svg?branch=master)](https://travis-ci.org/acornjs/acorn)
[![NPM version](https://img.shields.io/npm/v/acorn.svg)](https://www.npmjs.com/package/acorn)
[![CDNJS](https://img.shields.io/cdnjs/v/acorn.svg)](https://cdnjs.com/libraries/acorn)
[Author funding status: ![maintainer happiness](https://marijnhaverbeke.nl/fund/status_s.png?force)](https://marijnhaverbeke.nl/fund/)
A tiny, fast JavaScript parser, written completely in JavaScript.
A tiny, fast JavaScript parser written in JavaScript.
## Community
@ -20,9 +15,7 @@ and discussion, please use the
## Installation
The easiest way to install acorn is with [`npm`][npm].
[npm]: https://www.npmjs.com/
The easiest way to install acorn is from [`npm`](https://www.npmjs.com/):
```sh
npm install acorn
@ -34,36 +27,29 @@ Alternately, you can download the source and build acorn yourself:
git clone https://github.com/acornjs/acorn.git
cd acorn
npm install
npm run build
```
## Components
## Interface
When run in a CommonJS (node.js) or AMD environment, exported values
appear in the interfaces exposed by the individual files, as usual.
When loaded in the browser (Acorn works in any JS-enabled browser more
recent than IE5) without any kind of module management, a single
global object `acorn` will be defined, and all the exported properties
will be added to that.
**parse**`(input, options)` is the main interface to the library. The
`input` parameter is a string, `options` can be undefined or an object
setting some of the options listed below. The return value will be an
abstract syntax tree object as specified by the [ESTree
spec](https://github.com/estree/estree).
### Main parser
```javascript
let acorn = require("acorn");
console.log(acorn.parse("1 + 1"));
```
This is implemented in `dist/acorn.js`, and is what you get when you
`require("acorn")` in node.js.
**parse**`(input, options)` is used to parse a JavaScript program.
The `input` parameter is a string, `options` can be undefined or an
object setting some of the options listed below. The return value will
be an abstract syntax tree object as specified by the
[ESTree spec][estree].
When encountering a syntax error, the parser will raise a
`SyntaxError` object with a meaningful message. The error object will
have a `pos` property that indicates the character offset at which the
error occurred, and a `loc` object that contains a `{line, column}`
When encountering a syntax error, the parser will raise a
`SyntaxError` object with a meaningful message. The error object will
have a `pos` property that indicates the string offset at which the
error occurred, and a `loc` object that contains a `{line, column}`
object referring to that same position.
[estree]: https://github.com/estree/estree
Options can be provided by passing a second argument, which should be
an object containing any of these fields:
- **ecmaVersion**: Indicates the ECMAScript version to parse. Must be
either 3, 5, 6 (2015), 7 (2016), 8 (2017), 9 (2018) or 10 (2019, partial
@ -71,7 +57,8 @@ object referring to that same position.
reserved words, and support for new syntax features. Default is 7.
**NOTE**: Only 'stage 4' (finalized) ECMAScript features are being
implemented by Acorn.
implemented by Acorn. Other proposed new features can be implemented
through plugins.
- **sourceType**: Indicate the mode the code should be parsed in. Can be
either `"script"` or `"module"`. This influences global strict mode
@ -100,7 +87,10 @@ object referring to that same position.
declarations can only appear at a program's top level. Setting this
option to `true` allows them anywhere where a statement is allowed.
- **allowAwaitOutsideFunction**: By default, `await` expressions can only appear inside `async` functions. Setting this option to `true` allows to have top-level `await` expressions. They are still not allowed in non-`async` functions, though.
- **allowAwaitOutsideFunction**: By default, `await` expressions can
only appear inside `async` functions. Setting this option to
`true` allows to have top-level `await` expressions. They are
still not allowed in non-`async` functions, though.
- **allowHashBang**: When this is enabled (off by default), if the
code starts with the characters `#!` (as in a shellscript), the
@ -159,9 +149,10 @@ object referring to that same position.
- **ranges**: Nodes have their start and end characters offsets
recorded in `start` and `end` properties (directly on the node,
rather than the `loc` object, which holds line/column data. To also
add a [semi-standardized][range] `range` property holding a
`[start, end]` array with the same numbers, set the `ranges` option
to `true`.
add a
[semi-standardized](https://bugzilla.mozilla.org/show_bug.cgi?id=745678)
`range` property holding a `[start, end]` array with the same
numbers, set the `ranges` option to `true`.
- **program**: It is possible to parse multiple files into a single
AST by passing the tree produced by parsing the first file as the
@ -184,15 +175,10 @@ object referring to that same position.
that have a single `expression` property containing the expression
inside parentheses.
[range]: https://bugzilla.mozilla.org/show_bug.cgi?id=745678
**parseExpressionAt**`(input, offset, options)` will parse a single
expression in a string, and return its AST. It will not complain if
there is more of the string left after the expression.
**getLineInfo**`(input, offset)` can be used to get a `{line,
column}` object for a given program string and character offset.
**tokenizer**`(input, options)` returns an object with a `getToken`
method that can be called repeatedly to get the next token, a `{start,
end, type, value}` object (with added `loc` property when the
@ -216,139 +202,30 @@ var tokens = [...acorn.tokenizer(str)];
**tokTypes** holds an object mapping names to the token type objects
that end up in the `type` properties of tokens.
#### Note on using with [Escodegen][escodegen]
**getLineInfo**`(input, offset)` can be used to get a `{line,
column}` object for a given program string and offset.
Escodegen supports generating comments from AST, attached in
Esprima-specific format. In order to simulate same format in
Acorn, consider following example:
### The `Parser` class
Instances of the **`Parser`** class contain all the state and logic
that drives a parse. It has static methods `parse`,
`parseExpressionAt`, and `tokenizer` that match the top-level
functions by the same name.
When extending the parser with plugins, you need to call these methods
on the extended version of the class. To extend a parser with plugins,
you can use its static `extend` method.
```javascript
var comments = [], tokens = [];
var ast = acorn.parse('var x = 42; // answer', {
// collect ranges for each node
ranges: true,
// collect comments in Esprima's format
onComment: comments,
// collect token ranges
onToken: tokens
});
// attach comments using collected information
escodegen.attachComments(ast, comments, tokens);
// generate code
console.log(escodegen.generate(ast, {comment: true}));
// > 'var x = 42; // answer'
var acorn = require("acorn");
var jsx = require("acorn-jsx");
var JSXParser = acorn.Parser.extend(jsx());
JSXParser.parse("foo(<bar/>)");
```
[escodegen]: https://github.com/estools/escodegen
### dist/acorn_loose.js ###
This file implements an error-tolerant parser. It exposes a single
function. The loose parser is accessible in node.js via `require("acorn/dist/acorn_loose")`.
**parse_dammit**`(input, options)` takes the same arguments and
returns the same syntax tree as the `parse` function in `acorn.js`,
but never raises an error, and will do its best to parse syntactically
invalid code in as meaningful a way as it can. It'll insert identifier
nodes with name `"✖"` as placeholders in places where it can't make
sense of the input. Depends on `acorn.js`, because it uses the same
tokenizer.
### dist/walk.js ###
Implements an abstract syntax tree walker. Will store its interface in
`acorn.walk` when loaded without a module system.
**simple**`(node, visitors, base, state)` does a 'simple' walk over
a tree. `node` should be the AST node to walk, and `visitors` an
object with properties whose names correspond to node types in the
[ESTree spec][estree]. The properties should contain functions
that will be called with the node object and, if applicable the state
at that point. The last two arguments are optional. `base` is a walker
algorithm, and `state` is a start state. The default walker will
simply visit all statements and expressions and not produce a
meaningful state. (An example of a use of state is to track scope at
each point in the tree.)
```js
const acorn = require("acorn")
const walk = require("acorn/dist/walk")
walk.simple(acorn.parse("let x = 10"), {
Literal(node) {
console.log(`Found a literal: ${node.value}`)
}
})
```
**ancestor**`(node, visitors, base, state)` does a 'simple' walk over
a tree, building up an array of ancestor nodes (including the current node)
and passing the array to the callbacks as a third parameter.
```js
const acorn = require("acorn")
const walk = require("acorn/dist/walk")
walk.ancestor(acorn.parse("foo('hi')"), {
Literal(_, ancestors) {
console.log("This literal's ancestors are:",
ancestors.map(n => n.type))
}
})
```
**recursive**`(node, state, functions, base)` does a 'recursive'
walk, where the walker functions are responsible for continuing the
walk on the child nodes of their target node. `state` is the start
state, and `functions` should contain an object that maps node types
to walker functions. Such functions are called with `(node, state, c)`
arguments, and can cause the walk to continue on a sub-node by calling
the `c` argument on it with `(node, state)` arguments. The optional
`base` argument provides the fallback walker functions for node types
that aren't handled in the `functions` object. If not given, the
default walkers will be used.
**make**`(functions, base)` builds a new walker object by using the
walker functions in `functions` and filling in the missing ones by
taking defaults from `base`.
**full**`(node, callback, base, state)` does a 'full'
walk over a tree, calling the callback with the arguments (node, state, type)
for each node
**fullAncestor**`(node, callback, base, state)` does a 'full' walk over
a tree, building up an array of ancestor nodes (including the current node)
and passing the array to the callbacks as a third parameter.
```js
const acorn = require("acorn")
const walk = require("acorn/dist/walk")
walk.full(acorn.parse("1 + 1"), node => {
console.log(`There's a ${node.type} node at ${node.ch}`)
})
```
**findNodeAt**`(node, start, end, test, base, state)` tries to
locate a node in a tree at the given start and/or end offsets, which
satisfies the predicate `test`. `start` and `end` can be either `null`
(as wildcard) or a number. `test` may be a string (indicating a node
type) or a function that takes `(nodeType, node)` arguments and
returns a boolean indicating whether this node is interesting. `base`
and `state` are optional, and can be used to specify a custom walker.
Nodes are tested from inner to outer, so if two nodes match the
boundaries, the inner one will be preferred.
**findNodeAround**`(node, pos, test, base, state)` is a lot like
`findNodeAt`, but will match any node that exists 'around' (spanning)
the given position.
**findNodeAfter**`(node, pos, test, base, state)` is similar to
`findNodeAround`, but will match all nodes *after* the given position
(testing outer nodes before inner nodes).
The `extend` method takes any number of plugin values, and returns a
new `Parser` class that includes the extra parser logic provided by
the plugins.
## Command line interface
@ -357,7 +234,7 @@ line. It accepts as arguments its input file and the following
options:
- `--ecma3|--ecma5|--ecma6|--ecma7|--ecma8|--ecma9|--ecma10`: Sets the ECMAScript version
to parse. Default is version 7.
to parse. Default is version 9.
- `--module`: Sets the parsing mode to `"module"`. Is set to `"script"` otherwise.
@ -365,7 +242,8 @@ options:
"end" subobjects, each of which contains the one-based line and
zero-based column numbers in `{line, column}` form.
- `--allow-hash-bang`: If the code starts with the characters #! (as in a shellscript), the first line will be treated as a comment.
- `--allow-hash-bang`: If the code starts with the characters #! (as
in a shellscript), the first line will be treated as a comment.
- `--compact`: No whitespace is used in the AST output.
@ -375,82 +253,11 @@ options:
The utility spits out the syntax tree as JSON data.
## Build system
Acorn is written in ECMAScript 6, as a set of small modules, in the
project's `src` directory, and compiled down to bigger ECMAScript 3
files in `dist` using [Browserify](http://browserify.org) and
[Babel](http://babeljs.io/). If you are already using Babel, you can
consider including the modules directly.
The command-line test runner (`npm test`) uses the ES6 modules. The
browser-based test page (`test/index.html`) uses the compiled modules.
The `bin/build-acorn.js` script builds the latter from the former.
If you are working on Acorn, you'll probably want to try the code out
directly, without an intermediate build step. In your scripts, you can
register the Babel require shim like this:
require("babel-core/register")
That will allow you to directly `require` the ES6 modules.
## Plugins
Acorn is designed support allow plugins which, within reasonable
bounds, redefine the way the parser works. Plugins can add new token
types and new tokenizer contexts (if necessary), and extend methods in
the parser object. This is not a clean, elegant API—using it requires
an understanding of Acorn's internals, and plugins are likely to break
whenever those internals are significantly changed. But still, it is
_possible_, in this way, to create parsers for JavaScript dialects
without forking all of Acorn. And in principle it is even possible to
combine such plugins, so that if you have, for example, a plugin for
parsing types and a plugin for parsing JSX-style XML literals, you
could load them both and parse code with both JSX tags and types.
A plugin should register itself by adding a property to
`acorn.plugins`, which holds a function. Calling `acorn.parse`, a
`plugins` option can be passed, holding an object mapping plugin names
to configuration values (or just `true` for plugins that don't take
options). After the parser object has been created, the initialization
functions for the chosen plugins are called with `(parser,
configValue)` arguments. They are expected to use the `parser.extend`
method to extend parser methods. For example, the `readToken` method
could be extended like this:
```javascript
parser.extend("readToken", function(nextMethod) {
return function(code) {
console.log("Reading a token!")
return nextMethod.call(this, code)
}
})
```
The `nextMethod` argument passed to `extend`'s second argument is the
previous value of this method, and should usually be called through to
whenever the extended method does not handle the call itself.
Similarly, the loose parser allows plugins to register themselves via
`acorn.pluginsLoose`. The extension mechanism is the same as for the
normal parser:
```javascript
looseParser.extend("readToken", function(nextMethod) {
return function() {
console.log("Reading a token in the loose parser!")
return nextMethod.call(this)
}
})
```
### Existing plugins
## Existing plugins
- [`acorn-jsx`](https://github.com/RReverser/acorn-jsx): Parse [Facebook JSX syntax extensions](https://github.com/facebook/jsx)
- [`acorn-objj`](https://github.com/cappuccino/acorn-objj): [Objective-J](http://www.cappuccino-project.org/learn/objective-j.html) language parser built as Acorn plugin
Plugins for ECMAScript proposals:
Plugins for ECMAScript proposals:
- [`acorn-stage3`](https://github.com/acornjs/acorn-stage3): Parse most stage 3 proposals, bundling:
- [`acorn-async-iteration`](https://github.com/acornjs/acorn-async-iteration): Parse [async iteration proposal](https://github.com/tc39/proposal-async-iteration)
@ -459,9 +266,4 @@ looseParser.extend("readToken", function(nextMethod) {
- [`acorn-dynamic-import`](https://github.com/kesne/acorn-dynamic-import): Parse [import() proposal](https://github.com/tc39/proposal-dynamic-import)
- [`acorn-import-meta`](https://github.com/acornjs/acorn-import-meta): Parse [import.meta proposal](https://github.com/tc39/proposal-import-meta)
- [`acorn-numeric-separator`](https://github.com/acornjs/acorn-numeric-separator): Parse [numeric separator proposal](https://github.com/tc39/proposal-numeric-separator)
- [`acorn-optional-catch-binding`](https://github.com/acornjs/acorn-optional-catch-binding): Parse [optional catch binding proposal](https://github.com/tc39/proposal-optional-catch-binding)
- [`acorn-private-methods`](https://github.com/acornjs/acorn-private-methods): parse [private methods, getters and setters proposal](https://github.com/tc39/proposal-private-methods)
- [`acorn5-object-spread`](https://github.com/adrianheine/acorn5-object-spread): Parse [Object Rest/Spread Properties proposal](https://github.com/tc39/proposal-object-rest-spread)
- [`acorn-object-rest-spread`](https://github.com/victor-homyakov/acorn-object-rest-spread): Parse [Object Rest/Spread Properties proposal](https://github.com/tc39/proposal-object-rest-spread)
- [`acorn-es7`](https://github.com/angelozerr/acorn-es7): Parse [decorator syntax proposal](https://github.com/wycats/javascript-decorators)
- [`acorn-static-class-property-initializer`](https://github.com/victor-homyakov/acorn-static-class-property-initializer): Partial support for static class properties from [ES Class Fields & Static Properties Proposal](https://github.com/tc39/proposal-class-public-fields) to support static property initializers in [React components written as ES6+ classes](https://babeljs.io/blog/2015/07/07/react-on-es6-plus)
- [`acorn-private-methods`](https://github.com/acornjs/acorn-private-methods): parse [private methods, getters and setters proposal](https://github.com/tc39/proposal-private-methods)n

View File

@ -1,4 +1,4 @@
#!/usr/bin/env node
'use strict';
require('./_acorn.js');
require('../dist/bin.js');

View File

@ -1,21 +0,0 @@
const fs = require("fs")
const path = require("path")
const run = require("test262-parser-runner")
const parse = require("..").parse
const unsupportedFeatures = [
"BigInt",
"class-fields",
"class-fields-private",
"class-fields-public",
"numeric-separator-literal"
];
run(
(content, {sourceType}) => parse(content, {sourceType, ecmaVersion: 10}),
{
testsDirectory: path.dirname(require.resolve("test262/package.json")),
skip: test => (test.attrs.features && unsupportedFeatures.some(f => test.attrs.features.includes(f))),
whitelist: fs.readFileSync("./bin/test262.whitelist", "utf8").split("\n").filter(v => v)
}
)

View File

@ -1,404 +0,0 @@
annexB/language/function-code/block-decl-func-no-skip-try.js (default)
annexB/language/function-code/block-decl-func-skip-early-err-block.js (default)
annexB/language/function-code/block-decl-func-skip-early-err.js (default)
annexB/language/function-code/block-decl-func-skip-early-err-switch.js (default)
annexB/language/function-code/block-decl-func-skip-early-err-try.js (default)
annexB/language/function-code/if-decl-else-decl-a-func-no-skip-try.js (default)
annexB/language/function-code/if-decl-else-decl-a-func-skip-early-err-block.js (default)
annexB/language/function-code/if-decl-else-decl-a-func-skip-early-err.js (default)
annexB/language/function-code/if-decl-else-decl-a-func-skip-early-err-switch.js (default)
annexB/language/function-code/if-decl-else-decl-a-func-skip-early-err-try.js (default)
annexB/language/function-code/if-decl-else-decl-b-func-no-skip-try.js (default)
annexB/language/function-code/if-decl-else-decl-b-func-skip-early-err-block.js (default)
annexB/language/function-code/if-decl-else-decl-b-func-skip-early-err.js (default)
annexB/language/function-code/if-decl-else-decl-b-func-skip-early-err-switch.js (default)
annexB/language/function-code/if-decl-else-decl-b-func-skip-early-err-try.js (default)
annexB/language/function-code/if-decl-else-stmt-func-no-skip-try.js (default)
annexB/language/function-code/if-decl-else-stmt-func-skip-early-err-block.js (default)
annexB/language/function-code/if-decl-else-stmt-func-skip-early-err.js (default)
annexB/language/function-code/if-decl-else-stmt-func-skip-early-err-switch.js (default)
annexB/language/function-code/if-decl-else-stmt-func-skip-early-err-try.js (default)
annexB/language/function-code/if-decl-no-else-func-no-skip-try.js (default)
annexB/language/function-code/if-decl-no-else-func-skip-early-err-block.js (default)
annexB/language/function-code/if-decl-no-else-func-skip-early-err.js (default)
annexB/language/function-code/if-decl-no-else-func-skip-early-err-switch.js (default)
annexB/language/function-code/if-decl-no-else-func-skip-early-err-try.js (default)
annexB/language/function-code/if-stmt-else-decl-func-no-skip-try.js (default)
annexB/language/function-code/if-stmt-else-decl-func-skip-early-err-block.js (default)
annexB/language/function-code/if-stmt-else-decl-func-skip-early-err.js (default)
annexB/language/function-code/if-stmt-else-decl-func-skip-early-err-switch.js (default)
annexB/language/function-code/if-stmt-else-decl-func-skip-early-err-try.js (default)
annexB/language/function-code/switch-case-func-no-skip-try.js (default)
annexB/language/function-code/switch-case-func-skip-early-err-block.js (default)
annexB/language/function-code/switch-case-func-skip-early-err.js (default)
annexB/language/function-code/switch-case-func-skip-early-err-switch.js (default)
annexB/language/function-code/switch-case-func-skip-early-err-try.js (default)
annexB/language/function-code/switch-dflt-func-no-skip-try.js (default)
annexB/language/function-code/switch-dflt-func-skip-early-err-block.js (default)
annexB/language/function-code/switch-dflt-func-skip-early-err.js (default)
annexB/language/function-code/switch-dflt-func-skip-early-err-switch.js (default)
annexB/language/function-code/switch-dflt-func-skip-early-err-try.js (default)
annexB/language/global-code/block-decl-global-no-skip-try.js (default)
annexB/language/global-code/block-decl-global-skip-early-err-block.js (default)
annexB/language/global-code/block-decl-global-skip-early-err.js (default)
annexB/language/global-code/block-decl-global-skip-early-err-switch.js (default)
annexB/language/global-code/block-decl-global-skip-early-err-try.js (default)
annexB/language/global-code/if-decl-else-decl-a-global-no-skip-try.js (default)
annexB/language/global-code/if-decl-else-decl-a-global-skip-early-err-block.js (default)
annexB/language/global-code/if-decl-else-decl-a-global-skip-early-err.js (default)
annexB/language/global-code/if-decl-else-decl-a-global-skip-early-err-switch.js (default)
annexB/language/global-code/if-decl-else-decl-a-global-skip-early-err-try.js (default)
annexB/language/global-code/if-decl-else-decl-b-global-no-skip-try.js (default)
annexB/language/global-code/if-decl-else-decl-b-global-skip-early-err-block.js (default)
annexB/language/global-code/if-decl-else-decl-b-global-skip-early-err.js (default)
annexB/language/global-code/if-decl-else-decl-b-global-skip-early-err-switch.js (default)
annexB/language/global-code/if-decl-else-decl-b-global-skip-early-err-try.js (default)
annexB/language/global-code/if-decl-else-stmt-global-no-skip-try.js (default)
annexB/language/global-code/if-decl-else-stmt-global-skip-early-err-block.js (default)
annexB/language/global-code/if-decl-else-stmt-global-skip-early-err.js (default)
annexB/language/global-code/if-decl-else-stmt-global-skip-early-err-switch.js (default)
annexB/language/global-code/if-decl-else-stmt-global-skip-early-err-try.js (default)
annexB/language/global-code/if-decl-no-else-global-no-skip-try.js (default)
annexB/language/global-code/if-decl-no-else-global-skip-early-err-block.js (default)
annexB/language/global-code/if-decl-no-else-global-skip-early-err.js (default)
annexB/language/global-code/if-decl-no-else-global-skip-early-err-switch.js (default)
annexB/language/global-code/if-decl-no-else-global-skip-early-err-try.js (default)
annexB/language/global-code/if-stmt-else-decl-global-no-skip-try.js (default)
annexB/language/global-code/if-stmt-else-decl-global-skip-early-err-block.js (default)
annexB/language/global-code/if-stmt-else-decl-global-skip-early-err.js (default)
annexB/language/global-code/if-stmt-else-decl-global-skip-early-err-switch.js (default)
annexB/language/global-code/if-stmt-else-decl-global-skip-early-err-try.js (default)
annexB/language/global-code/switch-case-global-no-skip-try.js (default)
annexB/language/global-code/switch-case-global-skip-early-err-block.js (default)
annexB/language/global-code/switch-case-global-skip-early-err.js (default)
annexB/language/global-code/switch-case-global-skip-early-err-switch.js (default)
annexB/language/global-code/switch-case-global-skip-early-err-try.js (default)
annexB/language/global-code/switch-dflt-global-no-skip-try.js (default)
annexB/language/global-code/switch-dflt-global-skip-early-err-block.js (default)
annexB/language/global-code/switch-dflt-global-skip-early-err.js (default)
annexB/language/global-code/switch-dflt-global-skip-early-err-switch.js (default)
annexB/language/global-code/switch-dflt-global-skip-early-err-try.js (default)
annexB/language/statements/try/catch-redeclared-for-in-var.js (default)
annexB/language/statements/try/catch-redeclared-for-in-var.js (strict mode)
annexB/language/statements/try/catch-redeclared-for-var.js (default)
annexB/language/statements/try/catch-redeclared-for-var.js (strict mode)
annexB/language/statements/try/catch-redeclared-var-statement-captured.js (default)
annexB/language/statements/try/catch-redeclared-var-statement-captured.js (strict mode)
annexB/language/statements/try/catch-redeclared-var-statement.js (default)
annexB/language/statements/try/catch-redeclared-var-statement.js (strict mode)
language/block-scope/syntax/redeclaration/async-function-declaration-attempt-to-redeclare-with-async-function-declaration.js (default)
language/block-scope/syntax/redeclaration/async-function-declaration-attempt-to-redeclare-with-async-function-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/async-function-declaration-attempt-to-redeclare-with-class-declaration.js (default)
language/block-scope/syntax/redeclaration/async-function-declaration-attempt-to-redeclare-with-class-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/async-function-declaration-attempt-to-redeclare-with-function-declaration.js (default)
language/block-scope/syntax/redeclaration/async-function-declaration-attempt-to-redeclare-with-function-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/async-function-declaration-attempt-to-redeclare-with-generator-declaration.js (default)
language/block-scope/syntax/redeclaration/async-function-declaration-attempt-to-redeclare-with-generator-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/async-function-declaration-attempt-to-redeclare-with-var-declaration.js (default)
language/block-scope/syntax/redeclaration/async-function-declaration-attempt-to-redeclare-with-var-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/async-function-declaration-attempt-to-redeclare-with-async-generator-declaration.js (default)
language/block-scope/syntax/redeclaration/async-function-declaration-attempt-to-redeclare-with-async-generator-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/async-generator-declaration-attempt-to-redeclare-with-async-function-declaration.js (default)
language/block-scope/syntax/redeclaration/async-generator-declaration-attempt-to-redeclare-with-async-function-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/async-generator-declaration-attempt-to-redeclare-with-async-generator-declaration.js (default)
language/block-scope/syntax/redeclaration/async-generator-declaration-attempt-to-redeclare-with-async-generator-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/async-generator-declaration-attempt-to-redeclare-with-class-declaration.js (default)
language/block-scope/syntax/redeclaration/async-generator-declaration-attempt-to-redeclare-with-class-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/async-generator-declaration-attempt-to-redeclare-with-function-declaration.js (default)
language/block-scope/syntax/redeclaration/async-generator-declaration-attempt-to-redeclare-with-function-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/async-generator-declaration-attempt-to-redeclare-with-generator-declaration.js (default)
language/block-scope/syntax/redeclaration/async-generator-declaration-attempt-to-redeclare-with-generator-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/async-generator-declaration-attempt-to-redeclare-with-var-declaration.js (default)
language/block-scope/syntax/redeclaration/async-generator-declaration-attempt-to-redeclare-with-var-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-async-generator-declaration.js (default)
language/block-scope/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-async-generator-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/function-declaration-attempt-to-redeclare-with-async-generator-declaration.js (default)
language/block-scope/syntax/redeclaration/function-declaration-attempt-to-redeclare-with-async-generator-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/generator-declaration-attempt-to-redeclare-with-async-generator-declaration.js (default)
language/block-scope/syntax/redeclaration/generator-declaration-attempt-to-redeclare-with-async-generator-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/var-declaration-attempt-to-redeclare-with-async-generator-declaration.js (default)
language/block-scope/syntax/redeclaration/var-declaration-attempt-to-redeclare-with-async-generator-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-async-function-declaration.js (default)
language/block-scope/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-async-function-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-class-declaration.js (default)
language/block-scope/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-class-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-const-declaration.js (default)
language/block-scope/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-const-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-function-declaration.js (default)
language/block-scope/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-function-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-generator-declaration.js (default)
language/block-scope/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-generator-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-let-declaration.js (default)
language/block-scope/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-let-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-var-declaration.js (default)
language/block-scope/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-var-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/const-declaration-attempt-to-redeclare-with-class-declaration.js (default)
language/block-scope/syntax/redeclaration/const-declaration-attempt-to-redeclare-with-class-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/function-declaration-attempt-to-redeclare-with-async-function-declaration.js (default)
language/block-scope/syntax/redeclaration/function-declaration-attempt-to-redeclare-with-async-function-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/function-declaration-attempt-to-redeclare-with-class-declaration.js (default)
language/block-scope/syntax/redeclaration/function-declaration-attempt-to-redeclare-with-class-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/function-declaration-attempt-to-redeclare-with-function-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/function-declaration-attempt-to-redeclare-with-generator-declaration.js (default)
language/block-scope/syntax/redeclaration/function-declaration-attempt-to-redeclare-with-generator-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/function-declaration-attempt-to-redeclare-with-var-declaration.js (default)
language/block-scope/syntax/redeclaration/function-declaration-attempt-to-redeclare-with-var-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/generator-declaration-attempt-to-redeclare-with-async-function-declaration.js (default)
language/block-scope/syntax/redeclaration/generator-declaration-attempt-to-redeclare-with-async-function-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/generator-declaration-attempt-to-redeclare-with-class-declaration.js (default)
language/block-scope/syntax/redeclaration/generator-declaration-attempt-to-redeclare-with-class-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/generator-declaration-attempt-to-redeclare-with-function-declaration.js (default)
language/block-scope/syntax/redeclaration/generator-declaration-attempt-to-redeclare-with-function-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/generator-declaration-attempt-to-redeclare-with-generator-declaration.js (default)
language/block-scope/syntax/redeclaration/generator-declaration-attempt-to-redeclare-with-generator-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/generator-declaration-attempt-to-redeclare-with-var-declaration.js (default)
language/block-scope/syntax/redeclaration/generator-declaration-attempt-to-redeclare-with-var-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/let-declaration-attempt-to-redeclare-with-class-declaration.js (default)
language/block-scope/syntax/redeclaration/let-declaration-attempt-to-redeclare-with-class-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/var-declaration-attempt-to-redeclare-with-async-function-declaration.js (default)
language/block-scope/syntax/redeclaration/var-declaration-attempt-to-redeclare-with-async-function-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/var-declaration-attempt-to-redeclare-with-class-declaration.js (default)
language/block-scope/syntax/redeclaration/var-declaration-attempt-to-redeclare-with-class-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/var-declaration-attempt-to-redeclare-with-function-declaration.js (default)
language/block-scope/syntax/redeclaration/var-declaration-attempt-to-redeclare-with-function-declaration.js (strict mode)
language/block-scope/syntax/redeclaration/var-declaration-attempt-to-redeclare-with-generator-declaration.js (default)
language/block-scope/syntax/redeclaration/var-declaration-attempt-to-redeclare-with-generator-declaration.js (strict mode)
language/expressions/async-arrow-function/early-errors-arrow-await-in-formals-default.js (default)
language/expressions/async-arrow-function/early-errors-arrow-await-in-formals-default.js (strict mode)
language/expressions/async-arrow-function/early-errors-arrow-body-contains-super-call.js (default)
language/expressions/async-arrow-function/early-errors-arrow-body-contains-super-call.js (strict mode)
language/expressions/async-arrow-function/early-errors-arrow-body-contains-super-property.js (default)
language/expressions/async-arrow-function/early-errors-arrow-body-contains-super-property.js (strict mode)
language/expressions/async-function/early-errors-expression-body-contains-super-call.js (default)
language/expressions/async-function/early-errors-expression-body-contains-super-call.js (strict mode)
language/expressions/async-function/early-errors-expression-body-contains-super-property.js (default)
language/expressions/async-function/early-errors-expression-body-contains-super-property.js (strict mode)
language/expressions/async-function/early-errors-expression-formals-contains-super-call.js (default)
language/expressions/async-function/early-errors-expression-formals-contains-super-call.js (strict mode)
language/expressions/async-function/early-errors-expression-formals-contains-super-property.js (default)
language/expressions/async-function/early-errors-expression-formals-contains-super-property.js (strict mode)
language/expressions/class/method-param-dflt-yield.js (default)
language/expressions/class/static-method-param-dflt-yield.js (default)
language/expressions/function/early-body-super-call.js (default)
language/expressions/function/early-body-super-call.js (strict mode)
language/expressions/function/early-body-super-prop.js (default)
language/expressions/function/early-body-super-prop.js (strict mode)
language/expressions/function/early-params-super-call.js (default)
language/expressions/function/early-params-super-call.js (strict mode)
language/expressions/function/early-params-super-prop.js (default)
language/expressions/function/early-params-super-prop.js (strict mode)
language/expressions/object/method-definition/early-errors-object-method-body-contains-super-call.js (default)
language/expressions/object/method-definition/early-errors-object-method-body-contains-super-call.js (strict mode)
language/expressions/object/method-definition/early-errors-object-method-duplicate-parameters.js (default)
language/expressions/object/method-definition/early-errors-object-method-formals-contains-super-call.js (default)
language/expressions/object/method-definition/early-errors-object-method-formals-contains-super-call.js (strict mode)
language/expressions/object/method-definition/generator-super-call-body.js (default)
language/expressions/object/method-definition/generator-super-call-body.js (strict mode)
language/expressions/object/method-definition/generator-super-call-param.js (default)
language/expressions/object/method-definition/generator-super-call-param.js (strict mode)
language/expressions/object/prop-def-invalid-async-prefix.js (default)
language/expressions/object/prop-def-invalid-async-prefix.js (strict mode)
language/expressions/yield/in-iteration-stmt.js (default)
language/expressions/yield/in-iteration-stmt.js (strict mode)
language/expressions/yield/star-in-iteration-stmt.js (default)
language/expressions/yield/star-in-iteration-stmt.js (strict mode)
language/global-code/new.target-arrow.js (default)
language/global-code/new.target-arrow.js (strict mode)
language/global-code/super-call-arrow.js (default)
language/global-code/super-call-arrow.js (strict mode)
language/global-code/super-prop-arrow.js (default)
language/global-code/super-prop-arrow.js (strict mode)
language/module-code/early-export-global.js (default)
language/module-code/early-export-global.js (strict mode)
language/module-code/early-export-unresolvable.js (default)
language/module-code/early-export-unresolvable.js (strict mode)
language/statements/async-function/early-errors-declaration-body-contains-super-call.js (default)
language/statements/async-function/early-errors-declaration-body-contains-super-call.js (strict mode)
language/statements/async-function/early-errors-declaration-body-contains-super-property.js (default)
language/statements/async-function/early-errors-declaration-body-contains-super-property.js (strict mode)
language/statements/async-function/early-errors-declaration-formals-contains-super-call.js (default)
language/statements/async-function/early-errors-declaration-formals-contains-super-call.js (strict mode)
language/statements/async-function/early-errors-declaration-formals-contains-super-property.js (default)
language/statements/async-function/early-errors-declaration-formals-contains-super-property.js (strict mode)
language/expressions/async-generator/early-errors-expression-body-contains-super-call.js (default)
language/expressions/async-generator/early-errors-expression-body-contains-super-call.js (strict mode)
language/expressions/async-generator/early-errors-expression-body-contains-super-property.js (default)
language/expressions/async-generator/early-errors-expression-body-contains-super-property.js (strict mode)
language/expressions/async-generator/early-errors-expression-formals-contains-super-call.js (default)
language/expressions/async-generator/early-errors-expression-formals-contains-super-call.js (strict mode)
language/expressions/async-generator/early-errors-expression-formals-contains-super-property.js (default)
language/expressions/async-generator/early-errors-expression-formals-contains-super-property.js (strict mode)
language/statements/class/definition/early-errors-class-method-arguments-in-formal-parameters.js (default)
language/statements/class/definition/early-errors-class-method-body-contains-super-call.js (default)
language/statements/class/definition/early-errors-class-method-body-contains-super-call.js (strict mode)
language/statements/class/definition/early-errors-class-method-duplicate-parameters.js (default)
language/statements/class/definition/early-errors-class-method-eval-in-formal-parameters.js (default)
language/statements/class/definition/early-errors-class-method-formals-contains-super-call.js (default)
language/statements/class/definition/early-errors-class-method-formals-contains-super-call.js (strict mode)
language/statements/class/definition/methods-gen-yield-as-function-expression-binding-identifier.js (default)
language/statements/class/definition/methods-gen-yield-as-identifier-in-nested-function.js (default)
language/statements/class/method-param-yield.js (default)
language/statements/class/static-method-param-yield.js (default)
language/statements/class/strict-mode/with.js (default)
language/statements/class/syntax/early-errors/class-body-has-direct-super-missing-class-heritage.js (default)
language/statements/class/syntax/early-errors/class-body-has-direct-super-missing-class-heritage.js (strict mode)
language/statements/class/syntax/early-errors/class-body-method-contains-direct-super.js (default)
language/statements/class/syntax/early-errors/class-body-method-contains-direct-super.js (strict mode)
language/statements/class/syntax/early-errors/class-body-special-method-generator-contains-direct-super.js (default)
language/statements/class/syntax/early-errors/class-body-special-method-generator-contains-direct-super.js (strict mode)
language/statements/class/syntax/early-errors/class-body-special-method-get-contains-direct-super.js (default)
language/statements/class/syntax/early-errors/class-body-special-method-get-contains-direct-super.js (strict mode)
language/statements/class/syntax/early-errors/class-body-special-method-set-contains-direct-super.js (default)
language/statements/class/syntax/early-errors/class-body-special-method-set-contains-direct-super.js (strict mode)
language/statements/class/syntax/early-errors/class-body-static-method-contains-direct-super.js (default)
language/statements/class/syntax/early-errors/class-body-static-method-contains-direct-super.js (strict mode)
language/statements/class/syntax/early-errors/class-body-static-method-get-contains-direct-super.js (default)
language/statements/class/syntax/early-errors/class-body-static-method-get-contains-direct-super.js (strict mode)
language/statements/class/syntax/early-errors/class-body-static-method-set-contains-direct-super.js (default)
language/statements/class/syntax/early-errors/class-body-static-method-set-contains-direct-super.js (strict mode)
language/statements/class/syntax/early-errors/class-definition-evaluation-block-duplicate-binding.js (default)
language/statements/class/syntax/early-errors/class-definition-evaluation-block-duplicate-binding.js (strict mode)
language/statements/class/syntax/early-errors/class-definition-evaluation-scriptbody-duplicate-binding.js (default)
language/statements/class/syntax/early-errors/class-definition-evaluation-scriptbody-duplicate-binding.js (strict mode)
language/statements/const/syntax/const-declaring-let-split-across-two-lines.js (default)
language/statements/do-while/labelled-fn-stmt.js (default)
language/statements/for/head-let-bound-names-in-stmt.js (default)
language/statements/for/head-let-bound-names-in-stmt.js (strict mode)
language/statements/for-in/head-const-bound-names-in-stmt.js (default)
language/statements/for-in/head-const-bound-names-in-stmt.js (strict mode)
language/statements/for-in/head-const-bound-names-let.js (default)
language/statements/for-in/head-let-bound-names-in-stmt.js (default)
language/statements/for-in/head-let-bound-names-in-stmt.js (strict mode)
language/statements/for-in/head-let-bound-names-let.js (default)
language/statements/for-in/labelled-fn-stmt-const.js (default)
language/statements/for-in/labelled-fn-stmt-let.js (default)
language/statements/for-in/labelled-fn-stmt-lhs.js (default)
language/statements/for-in/labelled-fn-stmt-var.js (default)
language/statements/for-in/let-block-with-newline.js (default)
language/statements/for-in/let-identifier-with-newline.js (default)
language/statements/for/labelled-fn-stmt-expr.js (default)
language/statements/for/labelled-fn-stmt-let.js (default)
language/statements/for/labelled-fn-stmt-var.js (default)
language/statements/for/let-block-with-newline.js (default)
language/statements/for/let-identifier-with-newline.js (default)
language/statements/for-of/head-const-bound-names-in-stmt.js (default)
language/statements/for-of/head-const-bound-names-in-stmt.js (strict mode)
language/statements/for-of/head-const-bound-names-let.js (default)
language/statements/for-of/head-let-bound-names-in-stmt.js (default)
language/statements/for-of/head-let-bound-names-in-stmt.js (strict mode)
language/statements/for-of/head-let-bound-names-let.js (default)
language/statements/for-of/labelled-fn-stmt-const.js (default)
language/statements/for-of/labelled-fn-stmt-let.js (default)
language/statements/for-of/labelled-fn-stmt-lhs.js (default)
language/statements/for-of/labelled-fn-stmt-var.js (default)
language/statements/for-of/let-block-with-newline.js (default)
language/statements/for-of/let-identifier-with-newline.js (default)
language/statements/for-await-of/let-block-with-newline.js (default)
language/statements/for-await-of/let-identifier-with-newline.js (default)
language/statements/function/early-body-super-call.js (default)
language/statements/function/early-body-super-call.js (strict mode)
language/statements/function/early-body-super-prop.js (default)
language/statements/function/early-body-super-prop.js (strict mode)
language/statements/function/early-params-super-call.js (default)
language/statements/function/early-params-super-call.js (strict mode)
language/statements/function/early-params-super-prop.js (default)
language/statements/function/early-params-super-prop.js (strict mode)
language/statements/if/if-gen-else-gen.js (default)
language/statements/if/if-gen-else-stmt.js (default)
language/statements/if/if-gen-no-else.js (default)
language/statements/if/if-stmt-else-gen.js (default)
language/statements/if/labelled-fn-stmt-first.js (default)
language/statements/if/labelled-fn-stmt-lone.js (default)
language/statements/if/labelled-fn-stmt-second.js (default)
language/statements/if/let-block-with-newline.js (default)
language/statements/if/let-identifier-with-newline.js (default)
language/statements/labeled/let-block-with-newline.js (default)
language/statements/labeled/let-identifier-with-newline.js (default)
language/statements/let/syntax/identifier-let-disallowed-as-boundname.js (default)
language/statements/let/syntax/let-let-declaration-split-across-two-lines.js (default)
language/statements/let/syntax/let-let-declaration-with-initializer-split-across-two-lines.js (default)
language/statements/switch/syntax/redeclaration/async-function-declaration-attempt-to-redeclare-with-async-function-declaration.js (default)
language/statements/switch/syntax/redeclaration/async-function-declaration-attempt-to-redeclare-with-async-function-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/async-function-declaration-attempt-to-redeclare-with-class-declaration.js (default)
language/statements/switch/syntax/redeclaration/async-function-declaration-attempt-to-redeclare-with-class-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/async-function-declaration-attempt-to-redeclare-with-function-declaration.js (default)
language/statements/switch/syntax/redeclaration/async-function-declaration-attempt-to-redeclare-with-function-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/async-function-declaration-attempt-to-redeclare-with-generator-declaration.js (default)
language/statements/switch/syntax/redeclaration/async-function-declaration-attempt-to-redeclare-with-generator-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/async-function-declaration-attempt-to-redeclare-with-var-declaration.js (default)
language/statements/switch/syntax/redeclaration/async-function-declaration-attempt-to-redeclare-with-var-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/async-function-declaration-attempt-to-redeclare-with-async-generator-declaration.js (default)
language/statements/switch/syntax/redeclaration/async-function-declaration-attempt-to-redeclare-with-async-generator-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/async-generator-declaration-attempt-to-redeclare-with-async-function-declaration.js (default)
language/statements/switch/syntax/redeclaration/async-generator-declaration-attempt-to-redeclare-with-async-function-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/async-generator-declaration-attempt-to-redeclare-with-async-generator-declaration.js (default)
language/statements/switch/syntax/redeclaration/async-generator-declaration-attempt-to-redeclare-with-async-generator-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/async-generator-declaration-attempt-to-redeclare-with-class-declaration.js (default)
language/statements/switch/syntax/redeclaration/async-generator-declaration-attempt-to-redeclare-with-class-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/async-generator-declaration-attempt-to-redeclare-with-function-declaration.js (default)
language/statements/switch/syntax/redeclaration/async-generator-declaration-attempt-to-redeclare-with-function-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/async-generator-declaration-attempt-to-redeclare-with-generator-declaration.js (default)
language/statements/switch/syntax/redeclaration/async-generator-declaration-attempt-to-redeclare-with-generator-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/async-generator-declaration-attempt-to-redeclare-with-var-declaration.js (default)
language/statements/switch/syntax/redeclaration/async-generator-declaration-attempt-to-redeclare-with-var-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-async-generator-declaration.js (default)
language/statements/switch/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-async-generator-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/function-declaration-attempt-to-redeclare-with-async-generator-declaration.js (default)
language/statements/switch/syntax/redeclaration/function-declaration-attempt-to-redeclare-with-async-generator-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/generator-declaration-attempt-to-redeclare-with-async-generator-declaration.js (default)
language/statements/switch/syntax/redeclaration/generator-declaration-attempt-to-redeclare-with-async-generator-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/var-declaration-attempt-to-redeclare-with-async-generator-declaration.js (default)
language/statements/switch/syntax/redeclaration/var-declaration-attempt-to-redeclare-with-async-generator-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-async-function-declaration.js (default)
language/statements/switch/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-async-function-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-class-declaration.js (default)
language/statements/switch/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-class-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-const-declaration.js (default)
language/statements/switch/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-const-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-function-declaration.js (default)
language/statements/switch/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-function-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-generator-declaration.js (default)
language/statements/switch/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-generator-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-let-declaration.js (default)
language/statements/switch/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-let-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-var-declaration.js (default)
language/statements/switch/syntax/redeclaration/class-declaration-attempt-to-redeclare-with-var-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/const-declaration-attempt-to-redeclare-with-class-declaration.js (default)
language/statements/switch/syntax/redeclaration/const-declaration-attempt-to-redeclare-with-class-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/function-declaration-attempt-to-redeclare-with-async-function-declaration.js (default)
language/statements/switch/syntax/redeclaration/function-declaration-attempt-to-redeclare-with-async-function-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/function-declaration-attempt-to-redeclare-with-class-declaration.js (default)
language/statements/switch/syntax/redeclaration/function-declaration-attempt-to-redeclare-with-class-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/function-declaration-attempt-to-redeclare-with-function-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/function-declaration-attempt-to-redeclare-with-generator-declaration.js (default)
language/statements/switch/syntax/redeclaration/function-declaration-attempt-to-redeclare-with-generator-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/function-declaration-attempt-to-redeclare-with-var-declaration.js (default)
language/statements/switch/syntax/redeclaration/function-declaration-attempt-to-redeclare-with-var-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/generator-declaration-attempt-to-redeclare-with-async-function-declaration.js (default)
language/statements/switch/syntax/redeclaration/generator-declaration-attempt-to-redeclare-with-async-function-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/generator-declaration-attempt-to-redeclare-with-class-declaration.js (default)
language/statements/switch/syntax/redeclaration/generator-declaration-attempt-to-redeclare-with-class-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/generator-declaration-attempt-to-redeclare-with-function-declaration.js (default)
language/statements/switch/syntax/redeclaration/generator-declaration-attempt-to-redeclare-with-function-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/generator-declaration-attempt-to-redeclare-with-generator-declaration.js (default)
language/statements/switch/syntax/redeclaration/generator-declaration-attempt-to-redeclare-with-generator-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/generator-declaration-attempt-to-redeclare-with-var-declaration.js (default)
language/statements/switch/syntax/redeclaration/generator-declaration-attempt-to-redeclare-with-var-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/let-declaration-attempt-to-redeclare-with-class-declaration.js (default)
language/statements/switch/syntax/redeclaration/let-declaration-attempt-to-redeclare-with-class-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/var-declaration-attempt-to-redeclare-with-async-function-declaration.js (default)
language/statements/switch/syntax/redeclaration/var-declaration-attempt-to-redeclare-with-async-function-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/var-declaration-attempt-to-redeclare-with-class-declaration.js (default)
language/statements/switch/syntax/redeclaration/var-declaration-attempt-to-redeclare-with-class-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/var-declaration-attempt-to-redeclare-with-function-declaration.js (default)
language/statements/switch/syntax/redeclaration/var-declaration-attempt-to-redeclare-with-function-declaration.js (strict mode)
language/statements/switch/syntax/redeclaration/var-declaration-attempt-to-redeclare-with-generator-declaration.js (default)
language/statements/switch/syntax/redeclaration/var-declaration-attempt-to-redeclare-with-generator-declaration.js (strict mode)
language/statements/while/labelled-fn-stmt.js (default)
language/statements/while/let-block-with-newline.js (default)
language/statements/while/let-identifier-with-newline.js (default)
language/statements/with/labelled-fn-stmt.js (default)
language/statements/with/let-block-with-newline.js (default)
language/statements/with/let-identifier-with-newline.js (default)
language/white-space/mongolian-vowel-separator.js (default)
language/white-space/mongolian-vowel-separator.js (strict mode)

View File

@ -0,0 +1,208 @@
export as namespace acorn
export = acorn
declare namespace acorn {
function parse(input: string, options?: Options): Node
function parseExpressionAt(input: string, pos?: number, options?: Options): Node
function tokenizer(input: string, options?: Options): {
getToken(): Token
[Symbol.iterator](): Iterator<Token>
}
interface Options {
ecmaVersion?: 3 | 5 | 6 | 7 | 8 | 9 | 10 | 2015 | 2016 | 2017 | 2018 | 2019
sourceType?: 'script' | 'module'
onInsertedSemicolon?: (lastTokEnd: number, lastTokEndLoc?: Position) => void
onTrailingComma?: (lastTokEnd: number, lastTokEndLoc?: Position) => void
allowReserved?: boolean
allowReturnOutsideFunction?: boolean
allowImportExportEverywhere?: boolean
allowHashBang?: boolean
locations?: boolean
onToken?: ((token: Token) => any) | Token[]
onComment?: ((
isBlock: boolean, text: string, start: number, end: number, startLoc?: Position,
endLoc?: Position
) => void) | Comment[]
ranges?: boolean
program?: Node
sourceFile?: string
directSourceFile?: string
preserveParens?: boolean
}
class Parser {
constructor(options: Options, input: string, startPos?: number)
parse(): Node
static parse(input: string, options?: Options): Node
static parseExpressionAt(input: string, pos: number, options?: Options): Node
static tokenizer(input: string, options?: Options): {
getToken(): Token
[Symbol.iterator](): Iterator<Token>
}
static extend(...plugins: (typeof Parser)[]): typeof Parser
}
interface Position { line: number; column: number; offset: number }
const defaultOptions: Options
function getLineInfo(input: string, offset: number): Position
class SourceLocation {
start: Position
end: Position
source?: string | null
constructor(p: Parser, start: Position, end: Position)
}
class Node {
type: string
start: number
end: number
loc?: SourceLocation
sourceFile?: string
range?: [number, number]
constructor(parser: Parser, pos: number, loc?: SourceLocation)
}
class TokenType {
label: string
keyword: string
beforeExpr: boolean
startsExpr: boolean
isLoop: boolean
isAssign: boolean
prefix: boolean
postfix: boolean
binop: number
updateContext?: (prevType: TokenType) => void
constructor(label: string, conf?: any)
}
const tokTypes: {
num: TokenType
regexp: TokenType
string: TokenType
name: TokenType
eof: TokenType
bracketL: TokenType
bracketR: TokenType
braceL: TokenType
braceR: TokenType
parenL: TokenType
parenR: TokenType
comma: TokenType
semi: TokenType
colon: TokenType
dot: TokenType
question: TokenType
arrow: TokenType
template: TokenType
ellipsis: TokenType
backQuote: TokenType
dollarBraceL: TokenType
eq: TokenType
assign: TokenType
incDec: TokenType
prefix: TokenType
logicalOR: TokenType
logicalAND: TokenType
bitwiseOR: TokenType
bitwiseXOR: TokenType
bitwiseAND: TokenType
equality: TokenType
relational: TokenType
bitShift: TokenType
plusMin: TokenType
modulo: TokenType
star: TokenType
slash: TokenType
starstar: TokenType
_break: TokenType
_case: TokenType
_catch: TokenType
_continue: TokenType
_debugger: TokenType
_default: TokenType
_do: TokenType
_else: TokenType
_finally: TokenType
_for: TokenType
_function: TokenType
_if: TokenType
_return: TokenType
_switch: TokenType
_throw: TokenType
_try: TokenType
_var: TokenType
_const: TokenType
_while: TokenType
_with: TokenType
_new: TokenType
_this: TokenType
_super: TokenType
_class: TokenType
_extends: TokenType
_export: TokenType
_import: TokenType
_null: TokenType
_true: TokenType
_false: TokenType
_in: TokenType
_instanceof: TokenType
_typeof: TokenType
_void: TokenType
_delete: TokenType
}
class TokContext {
constructor(token: string, isExpr: boolean, preserveSpace: boolean, override?: (p: Parser) => void)
}
const tokContexts: {
b_stat: TokContext
b_expr: TokContext
b_tmpl: TokContext
p_stat: TokContext
p_expr: TokContext
q_tmpl: TokContext
f_expr: TokContext
}
function isIdentifierStart(code: number, astral?: boolean): boolean
function isIdentifierChar(code: number, astral?: boolean): boolean
interface AbstractToken {
}
interface Comment extends AbstractToken {
type: string
value: string
start: number
end: number
loc?: SourceLocation
range?: [number, number]
}
class Token {
type: TokenType
value: any
start: number
end: number
loc?: SourceLocation
range?: [number, number]
constructor(p: Parser)
}
function isNewLine(code: number): boolean
const lineBreak: RegExp
const lineBreakG: RegExp
const version: string
}

View File

@ -313,11 +313,12 @@ function getLineInfo(input, offset) {
// the parser process. These options are recognized:
var defaultOptions = {
// `ecmaVersion` indicates the ECMAScript version to parse. Must
// be either 3, 5, 6 (2015), 7 (2016), or 8 (2017). This influences support
// for strict mode, the set of reserved words, and support for
// new syntax features. The default is 7.
ecmaVersion: 7,
// `ecmaVersion` indicates the ECMAScript version to parse. Must be
// either 3, 5, 6 (2015), 7 (2016), 8 (2017), 9 (2018), or 10
// (2019). This influences support for strict mode, the set of
// reserved words, and support for new syntax features. The default
// is 9.
ecmaVersion: 9,
// `sourceType` indicates the mode the code should be parsed in.
// Can be either `"script"` or `"module"`. This influences global
// strict mode and parsing of `import` and `export` declarations.
@ -393,8 +394,7 @@ var defaultOptions = {
directSourceFile: null,
// When enabled, parenthesized expressions are represented by
// (non-standard) ParenthesizedExpression nodes
preserveParens: false,
plugins: {}
preserveParens: false
};
// Interpret and default an options object
@ -437,8 +437,26 @@ function pushComment(options, array) {
}
}
// Registered plugins
var plugins = {};
// Each scope gets a bitset that may contain these flags
var SCOPE_TOP = 1;
var SCOPE_FUNCTION = 2;
var SCOPE_VAR = SCOPE_TOP | SCOPE_FUNCTION;
var SCOPE_ASYNC = 4;
var SCOPE_GENERATOR = 8;
var SCOPE_ARROW = 16;
var SCOPE_SIMPLE_CATCH = 32;
function functionFlags(async, generator) {
return SCOPE_FUNCTION | (async ? SCOPE_ASYNC : 0) | (generator ? SCOPE_GENERATOR : 0)
}
// Used in checkLVal and declareName to determine the type of a binding
var BIND_NONE = 0;
var BIND_VAR = 1;
var BIND_LEXICAL = 2;
var BIND_FUNCTION = 3;
var BIND_SIMPLE_CATCH = 4;
var BIND_OUTSIDE = 5; // Special case for function names as bound inside the function
function keywordRegexp(words) {
return new RegExp("^(?:" + words.replace(/ /g, "|") + ")$")
@ -465,9 +483,6 @@ var Parser = function Parser(options, input, startPos) {
// escape sequences must not be interpreted as keywords.
this.containsEsc = false;
// Load plugins
this.loadPlugins(options.plugins);
// Set up token state
// The current position of the tokenizer in the input.
@ -508,8 +523,6 @@ var Parser = function Parser(options, input, startPos) {
// Used to signify the start of a potential arrow function
this.potentialArrowAt = -1;
// Flags to track whether we are in a function, a generator, an async function.
this.inFunction = this.inGenerator = this.inAsync = false;
// Positions to delayed-check that yield/await does not exist in default parameters.
this.yieldPos = this.awaitPos = 0;
// Labels in scope.
@ -521,29 +534,13 @@ var Parser = function Parser(options, input, startPos) {
// Scope tracking for duplicate variable names (see scope.js)
this.scopeStack = [];
this.enterFunctionScope();
this.enterScope(SCOPE_TOP);
// For RegExp validation
this.regexpState = null;
};
// DEPRECATED Kept for backwards compatibility until 3.0 in case a plugin uses them
Parser.prototype.isKeyword = function isKeyword (word) { return this.keywords.test(word) };
Parser.prototype.isReservedWord = function isReservedWord (word) { return this.reservedWords.test(word) };
Parser.prototype.extend = function extend (name, f) {
this[name] = f(this[name]);
};
Parser.prototype.loadPlugins = function loadPlugins (pluginConfigs) {
var this$1 = this;
for (var name in pluginConfigs) {
var plugin = plugins[name];
if (!plugin) { throw new Error("Plugin '" + name + "' not found") }
plugin(this$1, pluginConfigs[name]);
}
};
var prototypeAccessors = { inFunction: { configurable: true },inGenerator: { configurable: true },inAsync: { configurable: true } };
Parser.prototype.parse = function parse () {
var node = this.options.program || this.startNode();
@ -551,6 +548,35 @@ Parser.prototype.parse = function parse () {
return this.parseTopLevel(node)
};
prototypeAccessors.inFunction.get = function () { return (this.currentVarScope().flags & SCOPE_FUNCTION) > 0 };
prototypeAccessors.inGenerator.get = function () { return (this.currentVarScope().flags & SCOPE_GENERATOR) > 0 };
prototypeAccessors.inAsync.get = function () { return (this.currentVarScope().flags & SCOPE_ASYNC) > 0 };
Parser.extend = function extend () {
var plugins = [], len = arguments.length;
while ( len-- ) plugins[ len ] = arguments[ len ];
var cls = this;
for (var i = 0; i < plugins.length; i++) { cls = plugins[i](cls); }
return cls
};
Parser.parse = function parse (input, options) {
return new this(options, input).parse()
};
Parser.parseExpressionAt = function parseExpressionAt (input, pos, options) {
var parser = new this(options, input, pos);
parser.nextToken();
return parser.parseExpression()
};
Parser.tokenizer = function tokenizer (input, options) {
return new this(options, input)
};
Object.defineProperties( Parser.prototype, prototypeAccessors );
var pp = Parser.prototype;
// ## Parser utilities
@ -703,7 +729,7 @@ pp$1.parseTopLevel = function(node) {
var exports = {};
if (!node.body) { node.body = []; }
while (this.type !== types.eof) {
var stmt = this$1.parseStatement(true, true, exports);
var stmt = this$1.parseStatement(null, true, exports);
node.body.push(stmt);
}
this.adaptDirectivePrologue(node.body);
@ -754,7 +780,7 @@ pp$1.isAsyncFunction = function() {
// `if (foo) /blah/.exec(foo)`, where looking at the previous token
// does not help.
pp$1.parseStatement = function(declaration, topLevel, exports) {
pp$1.parseStatement = function(context, topLevel, exports) {
var starttype = this.type, node = this.startNode(), kind;
if (this.isLet()) {
@ -772,10 +798,10 @@ pp$1.parseStatement = function(declaration, topLevel, exports) {
case types._do: return this.parseDoStatement(node)
case types._for: return this.parseForStatement(node)
case types._function:
if (!declaration && this.options.ecmaVersion >= 6) { this.unexpected(); }
return this.parseFunctionStatement(node, false)
if ((context && (this.strict || context !== "if")) && this.options.ecmaVersion >= 6) { this.unexpected(); }
return this.parseFunctionStatement(node, false, !context)
case types._class:
if (!declaration) { this.unexpected(); }
if (context) { this.unexpected(); }
return this.parseClass(node, true)
case types._if: return this.parseIfStatement(node)
case types._return: return this.parseReturnStatement(node)
@ -784,11 +810,11 @@ pp$1.parseStatement = function(declaration, topLevel, exports) {
case types._try: return this.parseTryStatement(node)
case types._const: case types._var:
kind = kind || this.value;
if (!declaration && kind !== "var") { this.unexpected(); }
if (context && kind !== "var") { this.unexpected(); }
return this.parseVarStatement(node, kind)
case types._while: return this.parseWhileStatement(node)
case types._with: return this.parseWithStatement(node)
case types.braceL: return this.parseBlock()
case types.braceL: return this.parseBlock(true, node)
case types.semi: return this.parseEmptyStatement(node)
case types._export:
case types._import:
@ -807,14 +833,14 @@ pp$1.parseStatement = function(declaration, topLevel, exports) {
// Identifier node, we switch to interpreting it as a label.
default:
if (this.isAsyncFunction()) {
if (!declaration) { this.unexpected(); }
if (context) { this.unexpected(); }
this.next();
return this.parseFunctionStatement(node, true)
return this.parseFunctionStatement(node, true, !context)
}
var maybeName = this.value, expr = this.parseExpression();
if (starttype === types.name && expr.type === "Identifier" && this.eat(types.colon))
{ return this.parseLabeledStatement(node, maybeName, expr) }
{ return this.parseLabeledStatement(node, maybeName, expr, context) }
else { return this.parseExpressionStatement(node, expr) }
}
};
@ -854,7 +880,7 @@ pp$1.parseDebuggerStatement = function(node) {
pp$1.parseDoStatement = function(node) {
this.next();
this.labels.push(loopLabel);
node.body = this.parseStatement(false);
node.body = this.parseStatement("do");
this.labels.pop();
this.expect(types._while);
node.test = this.parseParenExpression();
@ -877,7 +903,7 @@ pp$1.parseForStatement = function(node) {
this.next();
var awaitAt = (this.options.ecmaVersion >= 9 && (this.inAsync || (!this.inFunction && this.options.allowAwaitOutsideFunction)) && this.eatContextual("await")) ? this.lastTokStart : -1;
this.labels.push(loopLabel);
this.enterLexicalScope();
this.enterScope(0);
this.expect(types.parenL);
if (this.type === types.semi) {
if (awaitAt > -1) { this.unexpected(awaitAt); }
@ -919,17 +945,17 @@ pp$1.parseForStatement = function(node) {
return this.parseFor(node, init)
};
pp$1.parseFunctionStatement = function(node, isAsync) {
pp$1.parseFunctionStatement = function(node, isAsync, declarationPosition) {
this.next();
return this.parseFunction(node, true, false, isAsync)
return this.parseFunction(node, FUNC_STATEMENT | (declarationPosition ? 0 : FUNC_HANGING_STATEMENT), false, isAsync)
};
pp$1.parseIfStatement = function(node) {
this.next();
node.test = this.parseParenExpression();
// allow function declarations in branches, but only in non-strict mode
node.consequent = this.parseStatement(!this.strict && this.type === types._function);
node.alternate = this.eat(types._else) ? this.parseStatement(!this.strict && this.type === types._function) : null;
node.consequent = this.parseStatement("if");
node.alternate = this.eat(types._else) ? this.parseStatement("if") : null;
return this.finishNode(node, "IfStatement")
};
@ -955,7 +981,7 @@ pp$1.parseSwitchStatement = function(node) {
node.cases = [];
this.expect(types.braceL);
this.labels.push(switchLabel);
this.enterLexicalScope();
this.enterScope(0);
// Statements under must be grouped (by label) in SwitchCase
// nodes. `cur` is used to keep the node that we are currently
@ -979,10 +1005,10 @@ pp$1.parseSwitchStatement = function(node) {
this$1.expect(types.colon);
} else {
if (!cur) { this$1.unexpected(); }
cur.consequent.push(this$1.parseStatement(true));
cur.consequent.push(this$1.parseStatement(null));
}
}
this.exitLexicalScope();
this.exitScope();
if (cur) { this.finishNode(cur, "SwitchCase"); }
this.next(); // Closing brace
this.labels.pop();
@ -1011,16 +1037,17 @@ pp$1.parseTryStatement = function(node) {
this.next();
if (this.eat(types.parenL)) {
clause.param = this.parseBindingAtom();
this.enterLexicalScope();
this.checkLVal(clause.param, "let");
var simple = clause.param.type === "Identifier";
this.enterScope(simple ? SCOPE_SIMPLE_CATCH : 0);
this.checkLVal(clause.param, simple ? BIND_SIMPLE_CATCH : BIND_LEXICAL);
this.expect(types.parenR);
} else {
if (this.options.ecmaVersion < 10) { this.unexpected(); }
clause.param = null;
this.enterLexicalScope();
this.enterScope(0);
}
clause.body = this.parseBlock(false);
this.exitLexicalScope();
this.exitScope();
node.handler = this.finishNode(clause, "CatchClause");
}
node.finalizer = this.eat(types._finally) ? this.parseBlock() : null;
@ -1040,7 +1067,7 @@ pp$1.parseWhileStatement = function(node) {
this.next();
node.test = this.parseParenExpression();
this.labels.push(loopLabel);
node.body = this.parseStatement(false);
node.body = this.parseStatement("while");
this.labels.pop();
return this.finishNode(node, "WhileStatement")
};
@ -1049,7 +1076,7 @@ pp$1.parseWithStatement = function(node) {
if (this.strict) { this.raise(this.start, "'with' in strict mode"); }
this.next();
node.object = this.parseParenExpression();
node.body = this.parseStatement(false);
node.body = this.parseStatement("with");
return this.finishNode(node, "WithStatement")
};
@ -1058,7 +1085,7 @@ pp$1.parseEmptyStatement = function(node) {
return this.finishNode(node, "EmptyStatement")
};
pp$1.parseLabeledStatement = function(node, maybeName, expr) {
pp$1.parseLabeledStatement = function(node, maybeName, expr, context) {
var this$1 = this;
for (var i$1 = 0, list = this$1.labels; i$1 < list.length; i$1 += 1)
@ -1078,7 +1105,7 @@ pp$1.parseLabeledStatement = function(node, maybeName, expr) {
} else { break }
}
this.labels.push({name: maybeName, kind: kind, statementStart: this.start});
node.body = this.parseStatement(true);
node.body = this.parseStatement(context);
if (node.body.type === "ClassDeclaration" ||
node.body.type === "VariableDeclaration" && node.body.kind !== "var" ||
node.body.type === "FunctionDeclaration" && (this.strict || node.body.generator || node.body.async))
@ -1098,23 +1125,19 @@ pp$1.parseExpressionStatement = function(node, expr) {
// strict"` declarations when `allowStrict` is true (used for
// function bodies).
pp$1.parseBlock = function(createNewLexicalScope) {
pp$1.parseBlock = function(createNewLexicalScope, node) {
var this$1 = this;
if ( createNewLexicalScope === void 0 ) createNewLexicalScope = true;
if ( node === void 0 ) node = this.startNode();
var node = this.startNode();
node.body = [];
this.expect(types.braceL);
if (createNewLexicalScope) {
this.enterLexicalScope();
}
if (createNewLexicalScope) { this.enterScope(0); }
while (!this.eat(types.braceR)) {
var stmt = this$1.parseStatement(true);
var stmt = this$1.parseStatement(null);
node.body.push(stmt);
}
if (createNewLexicalScope) {
this.exitLexicalScope();
}
if (createNewLexicalScope) { this.exitScope(); }
return this.finishNode(node, "BlockStatement")
};
@ -1129,8 +1152,8 @@ pp$1.parseFor = function(node, init) {
this.expect(types.semi);
node.update = this.type === types.parenR ? null : this.parseExpression();
this.expect(types.parenR);
this.exitLexicalScope();
node.body = this.parseStatement(false);
this.exitScope();
node.body = this.parseStatement("for");
this.labels.pop();
return this.finishNode(node, "ForStatement")
};
@ -1150,8 +1173,8 @@ pp$1.parseForIn = function(node, init) {
node.left = init;
node.right = type === "ForInStatement" ? this.parseExpression() : this.parseMaybeAssign();
this.expect(types.parenR);
this.exitLexicalScope();
node.body = this.parseStatement(false);
this.exitScope();
node.body = this.parseStatement("for");
this.labels.pop();
return this.finishNode(node, type)
};
@ -1183,47 +1206,43 @@ pp$1.parseVar = function(node, isFor, kind) {
pp$1.parseVarId = function(decl, kind) {
decl.id = this.parseBindingAtom(kind);
this.checkLVal(decl.id, kind, false);
this.checkLVal(decl.id, kind === "var" ? BIND_VAR : BIND_LEXICAL, false);
};
var FUNC_STATEMENT = 1;
var FUNC_HANGING_STATEMENT = 2;
var FUNC_NULLABLE_ID = 4;
// Parse a function declaration or literal (depending on the
// `isStatement` parameter).
pp$1.parseFunction = function(node, isStatement, allowExpressionBody, isAsync) {
pp$1.parseFunction = function(node, statement, allowExpressionBody, isAsync) {
this.initFunction(node);
if (this.options.ecmaVersion >= 9 || this.options.ecmaVersion >= 6 && !isAsync)
{ node.generator = this.eat(types.star); }
if (this.options.ecmaVersion >= 8)
{ node.async = !!isAsync; }
if (isStatement) {
node.id = isStatement === "nullableID" && this.type !== types.name ? null : this.parseIdent();
if (node.id) {
this.checkLVal(node.id, this.inModule && !this.inFunction ? "let" : "var");
}
if (statement & FUNC_STATEMENT) {
node.id = (statement & FUNC_NULLABLE_ID) && this.type !== types.name ? null : this.parseIdent();
if (node.id && !(statement & FUNC_HANGING_STATEMENT))
{ this.checkLVal(node.id, this.inModule && !this.inFunction ? BIND_LEXICAL : BIND_FUNCTION); }
}
var oldInGen = this.inGenerator, oldInAsync = this.inAsync,
oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos, oldInFunc = this.inFunction;
this.inGenerator = node.generator;
this.inAsync = node.async;
var oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos;
this.yieldPos = 0;
this.awaitPos = 0;
this.inFunction = true;
this.enterFunctionScope();
this.enterScope(functionFlags(node.async, node.generator));
if (!isStatement)
if (!(statement & FUNC_STATEMENT))
{ node.id = this.type === types.name ? this.parseIdent() : null; }
this.parseFunctionParams(node);
this.parseFunctionBody(node, allowExpressionBody);
this.inGenerator = oldInGen;
this.inAsync = oldInAsync;
this.yieldPos = oldYieldPos;
this.awaitPos = oldAwaitPos;
this.inFunction = oldInFunc;
return this.finishNode(node, isStatement ? "FunctionDeclaration" : "FunctionExpression")
return this.finishNode(node, (statement & FUNC_STATEMENT) ? "FunctionDeclaration" : "FunctionExpression")
};
pp$1.parseFunctionParams = function(node) {
@ -1247,17 +1266,20 @@ pp$1.parseClass = function(node, isStatement) {
classBody.body = [];
this.expect(types.braceL);
while (!this.eat(types.braceR)) {
var member = this$1.parseClassMember(classBody);
if (member && member.type === "MethodDefinition" && member.kind === "constructor") {
if (hadConstructor) { this$1.raise(member.start, "Duplicate constructor in the same class"); }
hadConstructor = true;
var element = this$1.parseClassElement();
if (element) {
classBody.body.push(element);
if (element.type === "MethodDefinition" && element.kind === "constructor") {
if (hadConstructor) { this$1.raise(element.start, "Duplicate constructor in the same class"); }
hadConstructor = true;
}
}
}
node.body = this.finishNode(classBody, "ClassBody");
return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression")
};
pp$1.parseClassMember = function(classBody) {
pp$1.parseClassElement = function() {
var this$1 = this;
if (this.eat(types.semi)) { return null }
@ -1302,7 +1324,7 @@ pp$1.parseClassMember = function(classBody) {
} else if (method.static && key.type === "Identifier" && key.name === "prototype") {
this.raise(key.start, "Classes may not have a static property named prototype");
}
this.parseClassMethod(classBody, method, isGenerator, isAsync);
this.parseClassMethod(method, isGenerator, isAsync);
if (method.kind === "get" && method.value.params.length !== 0)
{ this.raiseRecoverable(method.value.start, "getter should have no params"); }
if (method.kind === "set" && method.value.params.length !== 1)
@ -1312,9 +1334,9 @@ pp$1.parseClassMember = function(classBody) {
return method
};
pp$1.parseClassMethod = function(classBody, method, isGenerator, isAsync) {
pp$1.parseClassMethod = function(method, isGenerator, isAsync) {
method.value = this.parseMethod(isGenerator, isAsync);
classBody.body.push(this.finishNode(method, "MethodDefinition"));
return this.finishNode(method, "MethodDefinition")
};
pp$1.parseClassId = function(node, isStatement) {
@ -1346,7 +1368,7 @@ pp$1.parseExport = function(node, exports) {
var fNode = this.startNode();
this.next();
if (isAsync) { this.next(); }
node.declaration = this.parseFunction(fNode, "nullableID", false, isAsync);
node.declaration = this.parseFunction(fNode, FUNC_STATEMENT | FUNC_NULLABLE_ID, false, isAsync, true);
} else if (this.type === types._class) {
var cNode = this.startNode();
node.declaration = this.parseClass(cNode, "nullableID");
@ -1358,7 +1380,7 @@ pp$1.parseExport = function(node, exports) {
}
// export var|const|let|function|class ...
if (this.shouldParseExportStatement()) {
node.declaration = this.parseStatement(true);
node.declaration = this.parseStatement(null);
if (node.declaration.type === "VariableDeclaration")
{ this.checkVariableExport(exports, node.declaration.declarations); }
else
@ -1493,7 +1515,7 @@ pp$1.parseImportSpecifiers = function() {
// import defaultObj, { x, y as z } from '...'
var node = this.startNode();
node.local = this.parseIdent();
this.checkLVal(node.local, "let");
this.checkLVal(node.local, BIND_LEXICAL);
nodes.push(this.finishNode(node, "ImportDefaultSpecifier"));
if (!this.eat(types.comma)) { return nodes }
}
@ -1502,7 +1524,7 @@ pp$1.parseImportSpecifiers = function() {
this.next();
this.expectContextual("as");
node$1.local = this.parseIdent();
this.checkLVal(node$1.local, "let");
this.checkLVal(node$1.local, BIND_LEXICAL);
nodes.push(this.finishNode(node$1, "ImportNamespaceSpecifier"));
return nodes
}
@ -1521,7 +1543,7 @@ pp$1.parseImportSpecifiers = function() {
this$1.checkUnreserved(node$2.imported);
node$2.local = node$2.imported;
}
this$1.checkLVal(node$2.local, "let");
this$1.checkLVal(node$2.local, BIND_LEXICAL);
nodes.push(this$1.finishNode(node$2, "ImportSpecifier"));
}
return nodes
@ -1736,6 +1758,7 @@ pp$2.parseMaybeDefault = function(startPos, startLoc, left) {
pp$2.checkLVal = function(expr, bindingType, checkClashes) {
var this$1 = this;
if ( bindingType === void 0 ) bindingType = BIND_NONE;
switch (expr.type) {
case "Identifier":
@ -1746,19 +1769,7 @@ pp$2.checkLVal = function(expr, bindingType, checkClashes) {
{ this.raiseRecoverable(expr.start, "Argument name clash"); }
checkClashes[expr.name] = true;
}
if (bindingType && bindingType !== "none") {
if (
bindingType === "var" && !this.canDeclareVarName(expr.name) ||
bindingType !== "var" && !this.canDeclareLexicalName(expr.name)
) {
this.raiseRecoverable(expr.start, ("Identifier '" + (expr.name) + "' has already been declared"));
}
if (bindingType === "var") {
this.declareVarName(expr.name);
} else {
this.declareLexicalName(expr.name);
}
}
if (bindingType !== BIND_NONE && bindingType !== BIND_OUTSIDE) { this.declareName(expr.name, bindingType, expr.start); }
break
case "MemberExpression":
@ -1907,13 +1918,19 @@ pp$3.parseExpression = function(noIn, refDestructuringErrors) {
// operators like `+=`.
pp$3.parseMaybeAssign = function(noIn, refDestructuringErrors, afterLeftParse) {
if (this.inGenerator && this.isContextual("yield")) { return this.parseYield() }
if (this.isContextual("yield")) {
if (this.inGenerator) { return this.parseYield() }
// The tokenizer will assume an expression is allowed after
// `yield`, but this isn't that kind of yield
else { this.exprAllowed = false; }
}
var ownDestructuringErrors = false, oldParenAssign = -1, oldTrailingComma = -1;
var ownDestructuringErrors = false, oldParenAssign = -1, oldTrailingComma = -1, oldShorthandAssign = -1;
if (refDestructuringErrors) {
oldParenAssign = refDestructuringErrors.parenthesizedAssign;
oldTrailingComma = refDestructuringErrors.trailingComma;
refDestructuringErrors.parenthesizedAssign = refDestructuringErrors.trailingComma = -1;
oldShorthandAssign = refDestructuringErrors.shorthandAssign;
refDestructuringErrors.parenthesizedAssign = refDestructuringErrors.trailingComma = refDestructuringErrors.shorthandAssign = -1;
} else {
refDestructuringErrors = new DestructuringErrors;
ownDestructuringErrors = true;
@ -1939,6 +1956,7 @@ pp$3.parseMaybeAssign = function(noIn, refDestructuringErrors, afterLeftParse) {
}
if (oldParenAssign > -1) { refDestructuringErrors.parenthesizedAssign = oldParenAssign; }
if (oldTrailingComma > -1) { refDestructuringErrors.trailingComma = oldTrailingComma; }
if (oldShorthandAssign > -1) { refDestructuringErrors.shorthandAssign = oldShorthandAssign; }
return left
};
@ -2130,7 +2148,7 @@ pp$3.parseExprAtom = function(refDestructuringErrors) {
var startPos = this.start, startLoc = this.startLoc, containsEsc = this.containsEsc;
var id = this.parseIdent(this.type !== types.name);
if (this.options.ecmaVersion >= 8 && !containsEsc && id.name === "async" && !this.canInsertSemicolon() && this.eat(types._function))
{ return this.parseFunction(this.startNodeAt(startPos, startLoc), false, false, true) }
{ return this.parseFunction(this.startNodeAt(startPos, startLoc), 0, false, true) }
if (canBeArrow && !this.canInsertSemicolon()) {
if (this.eat(types.arrow))
{ return this.parseArrowExpression(this.startNodeAt(startPos, startLoc), [id], false) }
@ -2181,7 +2199,7 @@ pp$3.parseExprAtom = function(refDestructuringErrors) {
case types._function:
node = this.startNode();
this.next();
return this.parseFunction(node, false)
return this.parseFunction(node, 0)
case types._class:
return this.parseClass(this.startNode(), false)
@ -2300,7 +2318,7 @@ pp$3.parseNew = function() {
node.property = this.parseIdent(true);
if (node.property.name !== "target" || containsEsc)
{ this.raiseRecoverable(node.property.start, "The only valid meta property for new is new.target"); }
if (!this.inFunction)
if (!this.inNonArrowFunction())
{ this.raiseRecoverable(node.start, "new.target can only be used in functions"); }
return this.finishNode(node, "MetaProperty")
}
@ -2500,19 +2518,14 @@ pp$3.parsePropertyName = function(prop) {
pp$3.initFunction = function(node) {
node.id = null;
if (this.options.ecmaVersion >= 6) {
node.generator = false;
node.expression = false;
}
if (this.options.ecmaVersion >= 8)
{ node.async = false; }
if (this.options.ecmaVersion >= 6) { node.generator = node.expression = false; }
if (this.options.ecmaVersion >= 8) { node.async = false; }
};
// Parse object or class method.
pp$3.parseMethod = function(isGenerator, isAsync) {
var node = this.startNode(), oldInGen = this.inGenerator, oldInAsync = this.inAsync,
oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos, oldInFunc = this.inFunction;
var node = this.startNode(), oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos;
this.initFunction(node);
if (this.options.ecmaVersion >= 6)
@ -2520,51 +2533,37 @@ pp$3.parseMethod = function(isGenerator, isAsync) {
if (this.options.ecmaVersion >= 8)
{ node.async = !!isAsync; }
this.inGenerator = node.generator;
this.inAsync = node.async;
this.yieldPos = 0;
this.awaitPos = 0;
this.inFunction = true;
this.enterFunctionScope();
this.enterScope(functionFlags(isAsync, node.generator));
this.expect(types.parenL);
node.params = this.parseBindingList(types.parenR, false, this.options.ecmaVersion >= 8);
this.checkYieldAwaitInDefaultParams();
this.parseFunctionBody(node, false);
this.inGenerator = oldInGen;
this.inAsync = oldInAsync;
this.yieldPos = oldYieldPos;
this.awaitPos = oldAwaitPos;
this.inFunction = oldInFunc;
return this.finishNode(node, "FunctionExpression")
};
// Parse arrow function expression with given parameters.
pp$3.parseArrowExpression = function(node, params, isAsync) {
var oldInGen = this.inGenerator, oldInAsync = this.inAsync,
oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos, oldInFunc = this.inFunction;
var oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos;
this.enterFunctionScope();
this.enterScope(functionFlags(isAsync, false) | SCOPE_ARROW);
this.initFunction(node);
if (this.options.ecmaVersion >= 8)
{ node.async = !!isAsync; }
if (this.options.ecmaVersion >= 8) { node.async = !!isAsync; }
this.inGenerator = false;
this.inAsync = node.async;
this.yieldPos = 0;
this.awaitPos = 0;
this.inFunction = true;
node.params = this.toAssignableList(params, true);
this.parseFunctionBody(node, true);
this.inGenerator = oldInGen;
this.inAsync = oldInAsync;
this.yieldPos = oldYieldPos;
this.awaitPos = oldAwaitPos;
this.inFunction = oldInFunc;
return this.finishNode(node, "ArrowFunctionExpression")
};
@ -2602,12 +2601,10 @@ pp$3.parseFunctionBody = function(node, isArrowFunction) {
this.adaptDirectivePrologue(node.body.body);
this.labels = oldLabels;
}
this.exitFunctionScope();
this.exitScope();
if (this.strict && node.id) {
// Ensure the function name isn't a forbidden identifier in strict mode, e.g. 'eval'
this.checkLVal(node.id, "none");
}
// Ensure the function name isn't a forbidden identifier in strict mode, e.g. 'eval'
if (this.strict && node.id) { this.checkLVal(node.id, BIND_OUTSIDE); }
this.strict = oldStrict;
};
@ -2632,7 +2629,7 @@ pp$3.checkParams = function(node, allowDuplicates) {
{
var param = list[i];
this$1.checkLVal(param, "var", allowDuplicates ? null : nameHash);
this$1.checkLVal(param, BIND_VAR, allowDuplicates ? null : nameHash);
}
};
@ -2676,7 +2673,7 @@ pp$3.checkUnreserved = function(ref) {
{ this.raiseRecoverable(start, "Can not use 'yield' as identifier inside a generator"); }
if (this.inAsync && name === "await")
{ this.raiseRecoverable(start, "Can not use 'await' as identifier inside an async function"); }
if (this.isKeyword(name))
if (this.keywords.test(name))
{ this.raise(start, ("Unexpected keyword '" + name + "'")); }
if (this.options.ecmaVersion < 6 &&
this.input.slice(start, end).indexOf("\\") !== -1) { return }
@ -2769,79 +2766,69 @@ pp$4.curPosition = function() {
var pp$5 = Parser.prototype;
// Object.assign polyfill
var assign = Object.assign || function(target) {
var sources = [], len = arguments.length - 1;
while ( len-- > 0 ) sources[ len ] = arguments[ len + 1 ];
for (var i = 0, list = sources; i < list.length; i += 1) {
var source = list[i];
for (var key in source) {
if (has(source, key)) {
target[key] = source[key];
}
}
}
return target
var Scope = function Scope(flags) {
this.flags = flags;
// A list of var-declared names in the current lexical scope
this.var = [];
// A list of lexically-declared names in the current lexical scope
this.lexical = [];
};
// The functions in this module keep track of declared variables in the current scope in order to detect duplicate variable names.
pp$5.enterFunctionScope = function() {
// var: a hash of var-declared names in the current lexical scope
// lexical: a hash of lexically-declared names in the current lexical scope
// childVar: a hash of var-declared names in all child lexical scopes of the current lexical scope (within the current function scope)
// parentLexical: a hash of lexically-declared names in all parent lexical scopes of the current lexical scope (within the current function scope)
this.scopeStack.push({var: {}, lexical: {}, childVar: {}, parentLexical: {}});
pp$5.enterScope = function(flags) {
this.scopeStack.push(new Scope(flags));
};
pp$5.exitFunctionScope = function() {
pp$5.exitScope = function() {
this.scopeStack.pop();
};
pp$5.enterLexicalScope = function() {
var parentScope = this.scopeStack[this.scopeStack.length - 1];
var childScope = {var: {}, lexical: {}, childVar: {}, parentLexical: {}};
pp$5.declareName = function(name, bindingType, pos) {
var this$1 = this;
this.scopeStack.push(childScope);
assign(childScope.parentLexical, parentScope.lexical, parentScope.parentLexical);
var redeclared = false;
if (bindingType === BIND_LEXICAL) {
var scope = this.currentScope();
redeclared = scope.lexical.indexOf(name) > -1 || scope.var.indexOf(name) > -1;
scope.lexical.push(name);
} else if (bindingType === BIND_SIMPLE_CATCH) {
var scope$1 = this.currentScope();
scope$1.lexical.push(name);
} else if (bindingType === BIND_FUNCTION) {
var scope$2 = this.currentScope();
redeclared = scope$2.lexical.indexOf(name) > -1;
scope$2.var.push(name);
} else {
for (var i = this.scopeStack.length - 1; i >= 0; --i) {
var scope$3 = this$1.scopeStack[i];
if (scope$3.lexical.indexOf(name) > -1 && !(scope$3.flags & SCOPE_SIMPLE_CATCH) && scope$3.lexical[0] === name) { redeclared = true; }
scope$3.var.push(name);
if (scope$3.flags & SCOPE_VAR) { break }
}
}
if (redeclared) { this.raiseRecoverable(pos, ("Identifier '" + name + "' has already been declared")); }
};
pp$5.exitLexicalScope = function() {
var childScope = this.scopeStack.pop();
var parentScope = this.scopeStack[this.scopeStack.length - 1];
assign(parentScope.childVar, childScope.var, childScope.childVar);
pp$5.currentScope = function() {
return this.scopeStack[this.scopeStack.length - 1]
};
/**
* A name can be declared with `var` if there are no variables with the same name declared with `let`/`const`
* in the current lexical scope or any of the parent lexical scopes in this function.
*/
pp$5.canDeclareVarName = function(name) {
var currentScope = this.scopeStack[this.scopeStack.length - 1];
pp$5.currentVarScope = function() {
var this$1 = this;
return !has(currentScope.lexical, name) && !has(currentScope.parentLexical, name)
for (var i = this.scopeStack.length - 1;; i--) {
var scope = this$1.scopeStack[i];
if (scope.flags & SCOPE_VAR) { return scope }
}
};
/**
* A name can be declared with `let`/`const` if there are no variables with the same name declared with `let`/`const`
* in the current scope, and there are no variables with the same name declared with `var` in the current scope or in
* any child lexical scopes in this function.
*/
pp$5.canDeclareLexicalName = function(name) {
var currentScope = this.scopeStack[this.scopeStack.length - 1];
pp$5.inNonArrowFunction = function() {
var this$1 = this;
return !has(currentScope.lexical, name) && !has(currentScope.var, name) && !has(currentScope.childVar, name)
};
pp$5.declareVarName = function(name) {
this.scopeStack[this.scopeStack.length - 1].var[name] = true;
};
pp$5.declareLexicalName = function(name) {
this.scopeStack[this.scopeStack.length - 1].lexical[name] = true;
for (var i = this.scopeStack.length - 1; i >= 0; i--)
{ if (this$1.scopeStack[i].flags & SCOPE_FUNCTION && !(this$1.scopeStack[i].flags & SCOPE_ARROW)) { return true } }
return false
};
var Node = function Node(parser, pos, loc) {
@ -5268,14 +5255,9 @@ pp$8.readWord = function() {
//
// [ghbt]: https://github.com/acornjs/acorn/issues
//
// This file defines the main parser interface. The library also comes
// with a [error-tolerant parser][dammit] and an
// [abstract syntax tree walker][walk], defined in other files.
//
// [dammit]: acorn_loose.js
// [walk]: util/walk.js
var version = "5.7.3";
var version = "6.0.2";
// The main exported interface (under `self.acorn` when in the
// browser) is a `parse` function that takes a code string and
@ -5285,7 +5267,7 @@ var version = "5.7.3";
// [api]: https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API
function parse(input, options) {
return new Parser(options, input).parse()
return Parser.parse(input, options)
}
// This function tries to parse a single expression at a given
@ -5293,35 +5275,21 @@ function parse(input, options) {
// that embed JavaScript expressions.
function parseExpressionAt(input, pos, options) {
var p = new Parser(options, input, pos);
p.nextToken();
return p.parseExpression()
return Parser.parseExpressionAt(input, pos, options)
}
// Acorn is organized as a tokenizer and a recursive-descent parser.
// The `tokenizer` export provides an interface to the tokenizer.
function tokenizer(input, options) {
return new Parser(options, input)
}
// This is a terrible kludge to support the existing, pre-ES6
// interface where the loose parser module retroactively adds exports
// to this module.
// eslint-disable-line camelcase
function addLooseExports(parse, Parser$$1, plugins$$1) {
exports.parse_dammit = parse; // eslint-disable-line camelcase
exports.LooseParser = Parser$$1;
exports.pluginsLoose = plugins$$1;
return Parser.tokenizer(input, options)
}
exports.version = version;
exports.parse = parse;
exports.parseExpressionAt = parseExpressionAt;
exports.tokenizer = tokenizer;
exports.addLooseExports = addLooseExports;
exports.Parser = Parser;
exports.plugins = plugins;
exports.defaultOptions = defaultOptions;
exports.Position = Position;
exports.SourceLocation = SourceLocation;
@ -5343,3 +5311,4 @@ exports.nonASCIIwhitespace = nonASCIIwhitespace;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=acorn.js.map

File diff suppressed because one or more lines are too long

View File

@ -307,11 +307,12 @@ function getLineInfo(input, offset) {
// the parser process. These options are recognized:
var defaultOptions = {
// `ecmaVersion` indicates the ECMAScript version to parse. Must
// be either 3, 5, 6 (2015), 7 (2016), or 8 (2017). This influences support
// for strict mode, the set of reserved words, and support for
// new syntax features. The default is 7.
ecmaVersion: 7,
// `ecmaVersion` indicates the ECMAScript version to parse. Must be
// either 3, 5, 6 (2015), 7 (2016), 8 (2017), 9 (2018), or 10
// (2019). This influences support for strict mode, the set of
// reserved words, and support for new syntax features. The default
// is 9.
ecmaVersion: 9,
// `sourceType` indicates the mode the code should be parsed in.
// Can be either `"script"` or `"module"`. This influences global
// strict mode and parsing of `import` and `export` declarations.
@ -387,8 +388,7 @@ var defaultOptions = {
directSourceFile: null,
// When enabled, parenthesized expressions are represented by
// (non-standard) ParenthesizedExpression nodes
preserveParens: false,
plugins: {}
preserveParens: false
};
// Interpret and default an options object
@ -431,8 +431,26 @@ function pushComment(options, array) {
}
}
// Registered plugins
var plugins = {};
// Each scope gets a bitset that may contain these flags
var SCOPE_TOP = 1;
var SCOPE_FUNCTION = 2;
var SCOPE_VAR = SCOPE_TOP | SCOPE_FUNCTION;
var SCOPE_ASYNC = 4;
var SCOPE_GENERATOR = 8;
var SCOPE_ARROW = 16;
var SCOPE_SIMPLE_CATCH = 32;
function functionFlags(async, generator) {
return SCOPE_FUNCTION | (async ? SCOPE_ASYNC : 0) | (generator ? SCOPE_GENERATOR : 0)
}
// Used in checkLVal and declareName to determine the type of a binding
var BIND_NONE = 0;
var BIND_VAR = 1;
var BIND_LEXICAL = 2;
var BIND_FUNCTION = 3;
var BIND_SIMPLE_CATCH = 4;
var BIND_OUTSIDE = 5; // Special case for function names as bound inside the function
function keywordRegexp(words) {
return new RegExp("^(?:" + words.replace(/ /g, "|") + ")$")
@ -459,9 +477,6 @@ var Parser = function Parser(options, input, startPos) {
// escape sequences must not be interpreted as keywords.
this.containsEsc = false;
// Load plugins
this.loadPlugins(options.plugins);
// Set up token state
// The current position of the tokenizer in the input.
@ -502,8 +517,6 @@ var Parser = function Parser(options, input, startPos) {
// Used to signify the start of a potential arrow function
this.potentialArrowAt = -1;
// Flags to track whether we are in a function, a generator, an async function.
this.inFunction = this.inGenerator = this.inAsync = false;
// Positions to delayed-check that yield/await does not exist in default parameters.
this.yieldPos = this.awaitPos = 0;
// Labels in scope.
@ -515,29 +528,13 @@ var Parser = function Parser(options, input, startPos) {
// Scope tracking for duplicate variable names (see scope.js)
this.scopeStack = [];
this.enterFunctionScope();
this.enterScope(SCOPE_TOP);
// For RegExp validation
this.regexpState = null;
};
// DEPRECATED Kept for backwards compatibility until 3.0 in case a plugin uses them
Parser.prototype.isKeyword = function isKeyword (word) { return this.keywords.test(word) };
Parser.prototype.isReservedWord = function isReservedWord (word) { return this.reservedWords.test(word) };
Parser.prototype.extend = function extend (name, f) {
this[name] = f(this[name]);
};
Parser.prototype.loadPlugins = function loadPlugins (pluginConfigs) {
var this$1 = this;
for (var name in pluginConfigs) {
var plugin = plugins[name];
if (!plugin) { throw new Error("Plugin '" + name + "' not found") }
plugin(this$1, pluginConfigs[name]);
}
};
var prototypeAccessors = { inFunction: { configurable: true },inGenerator: { configurable: true },inAsync: { configurable: true } };
Parser.prototype.parse = function parse () {
var node = this.options.program || this.startNode();
@ -545,6 +542,35 @@ Parser.prototype.parse = function parse () {
return this.parseTopLevel(node)
};
prototypeAccessors.inFunction.get = function () { return (this.currentVarScope().flags & SCOPE_FUNCTION) > 0 };
prototypeAccessors.inGenerator.get = function () { return (this.currentVarScope().flags & SCOPE_GENERATOR) > 0 };
prototypeAccessors.inAsync.get = function () { return (this.currentVarScope().flags & SCOPE_ASYNC) > 0 };
Parser.extend = function extend () {
var plugins = [], len = arguments.length;
while ( len-- ) plugins[ len ] = arguments[ len ];
var cls = this;
for (var i = 0; i < plugins.length; i++) { cls = plugins[i](cls); }
return cls
};
Parser.parse = function parse (input, options) {
return new this(options, input).parse()
};
Parser.parseExpressionAt = function parseExpressionAt (input, pos, options) {
var parser = new this(options, input, pos);
parser.nextToken();
return parser.parseExpression()
};
Parser.tokenizer = function tokenizer (input, options) {
return new this(options, input)
};
Object.defineProperties( Parser.prototype, prototypeAccessors );
var pp = Parser.prototype;
// ## Parser utilities
@ -697,7 +723,7 @@ pp$1.parseTopLevel = function(node) {
var exports = {};
if (!node.body) { node.body = []; }
while (this.type !== types.eof) {
var stmt = this$1.parseStatement(true, true, exports);
var stmt = this$1.parseStatement(null, true, exports);
node.body.push(stmt);
}
this.adaptDirectivePrologue(node.body);
@ -748,7 +774,7 @@ pp$1.isAsyncFunction = function() {
// `if (foo) /blah/.exec(foo)`, where looking at the previous token
// does not help.
pp$1.parseStatement = function(declaration, topLevel, exports) {
pp$1.parseStatement = function(context, topLevel, exports) {
var starttype = this.type, node = this.startNode(), kind;
if (this.isLet()) {
@ -766,10 +792,10 @@ pp$1.parseStatement = function(declaration, topLevel, exports) {
case types._do: return this.parseDoStatement(node)
case types._for: return this.parseForStatement(node)
case types._function:
if (!declaration && this.options.ecmaVersion >= 6) { this.unexpected(); }
return this.parseFunctionStatement(node, false)
if ((context && (this.strict || context !== "if")) && this.options.ecmaVersion >= 6) { this.unexpected(); }
return this.parseFunctionStatement(node, false, !context)
case types._class:
if (!declaration) { this.unexpected(); }
if (context) { this.unexpected(); }
return this.parseClass(node, true)
case types._if: return this.parseIfStatement(node)
case types._return: return this.parseReturnStatement(node)
@ -778,11 +804,11 @@ pp$1.parseStatement = function(declaration, topLevel, exports) {
case types._try: return this.parseTryStatement(node)
case types._const: case types._var:
kind = kind || this.value;
if (!declaration && kind !== "var") { this.unexpected(); }
if (context && kind !== "var") { this.unexpected(); }
return this.parseVarStatement(node, kind)
case types._while: return this.parseWhileStatement(node)
case types._with: return this.parseWithStatement(node)
case types.braceL: return this.parseBlock()
case types.braceL: return this.parseBlock(true, node)
case types.semi: return this.parseEmptyStatement(node)
case types._export:
case types._import:
@ -801,14 +827,14 @@ pp$1.parseStatement = function(declaration, topLevel, exports) {
// Identifier node, we switch to interpreting it as a label.
default:
if (this.isAsyncFunction()) {
if (!declaration) { this.unexpected(); }
if (context) { this.unexpected(); }
this.next();
return this.parseFunctionStatement(node, true)
return this.parseFunctionStatement(node, true, !context)
}
var maybeName = this.value, expr = this.parseExpression();
if (starttype === types.name && expr.type === "Identifier" && this.eat(types.colon))
{ return this.parseLabeledStatement(node, maybeName, expr) }
{ return this.parseLabeledStatement(node, maybeName, expr, context) }
else { return this.parseExpressionStatement(node, expr) }
}
};
@ -848,7 +874,7 @@ pp$1.parseDebuggerStatement = function(node) {
pp$1.parseDoStatement = function(node) {
this.next();
this.labels.push(loopLabel);
node.body = this.parseStatement(false);
node.body = this.parseStatement("do");
this.labels.pop();
this.expect(types._while);
node.test = this.parseParenExpression();
@ -871,7 +897,7 @@ pp$1.parseForStatement = function(node) {
this.next();
var awaitAt = (this.options.ecmaVersion >= 9 && (this.inAsync || (!this.inFunction && this.options.allowAwaitOutsideFunction)) && this.eatContextual("await")) ? this.lastTokStart : -1;
this.labels.push(loopLabel);
this.enterLexicalScope();
this.enterScope(0);
this.expect(types.parenL);
if (this.type === types.semi) {
if (awaitAt > -1) { this.unexpected(awaitAt); }
@ -913,17 +939,17 @@ pp$1.parseForStatement = function(node) {
return this.parseFor(node, init)
};
pp$1.parseFunctionStatement = function(node, isAsync) {
pp$1.parseFunctionStatement = function(node, isAsync, declarationPosition) {
this.next();
return this.parseFunction(node, true, false, isAsync)
return this.parseFunction(node, FUNC_STATEMENT | (declarationPosition ? 0 : FUNC_HANGING_STATEMENT), false, isAsync)
};
pp$1.parseIfStatement = function(node) {
this.next();
node.test = this.parseParenExpression();
// allow function declarations in branches, but only in non-strict mode
node.consequent = this.parseStatement(!this.strict && this.type === types._function);
node.alternate = this.eat(types._else) ? this.parseStatement(!this.strict && this.type === types._function) : null;
node.consequent = this.parseStatement("if");
node.alternate = this.eat(types._else) ? this.parseStatement("if") : null;
return this.finishNode(node, "IfStatement")
};
@ -949,7 +975,7 @@ pp$1.parseSwitchStatement = function(node) {
node.cases = [];
this.expect(types.braceL);
this.labels.push(switchLabel);
this.enterLexicalScope();
this.enterScope(0);
// Statements under must be grouped (by label) in SwitchCase
// nodes. `cur` is used to keep the node that we are currently
@ -973,10 +999,10 @@ pp$1.parseSwitchStatement = function(node) {
this$1.expect(types.colon);
} else {
if (!cur) { this$1.unexpected(); }
cur.consequent.push(this$1.parseStatement(true));
cur.consequent.push(this$1.parseStatement(null));
}
}
this.exitLexicalScope();
this.exitScope();
if (cur) { this.finishNode(cur, "SwitchCase"); }
this.next(); // Closing brace
this.labels.pop();
@ -1005,16 +1031,17 @@ pp$1.parseTryStatement = function(node) {
this.next();
if (this.eat(types.parenL)) {
clause.param = this.parseBindingAtom();
this.enterLexicalScope();
this.checkLVal(clause.param, "let");
var simple = clause.param.type === "Identifier";
this.enterScope(simple ? SCOPE_SIMPLE_CATCH : 0);
this.checkLVal(clause.param, simple ? BIND_SIMPLE_CATCH : BIND_LEXICAL);
this.expect(types.parenR);
} else {
if (this.options.ecmaVersion < 10) { this.unexpected(); }
clause.param = null;
this.enterLexicalScope();
this.enterScope(0);
}
clause.body = this.parseBlock(false);
this.exitLexicalScope();
this.exitScope();
node.handler = this.finishNode(clause, "CatchClause");
}
node.finalizer = this.eat(types._finally) ? this.parseBlock() : null;
@ -1034,7 +1061,7 @@ pp$1.parseWhileStatement = function(node) {
this.next();
node.test = this.parseParenExpression();
this.labels.push(loopLabel);
node.body = this.parseStatement(false);
node.body = this.parseStatement("while");
this.labels.pop();
return this.finishNode(node, "WhileStatement")
};
@ -1043,7 +1070,7 @@ pp$1.parseWithStatement = function(node) {
if (this.strict) { this.raise(this.start, "'with' in strict mode"); }
this.next();
node.object = this.parseParenExpression();
node.body = this.parseStatement(false);
node.body = this.parseStatement("with");
return this.finishNode(node, "WithStatement")
};
@ -1052,7 +1079,7 @@ pp$1.parseEmptyStatement = function(node) {
return this.finishNode(node, "EmptyStatement")
};
pp$1.parseLabeledStatement = function(node, maybeName, expr) {
pp$1.parseLabeledStatement = function(node, maybeName, expr, context) {
var this$1 = this;
for (var i$1 = 0, list = this$1.labels; i$1 < list.length; i$1 += 1)
@ -1072,7 +1099,7 @@ pp$1.parseLabeledStatement = function(node, maybeName, expr) {
} else { break }
}
this.labels.push({name: maybeName, kind: kind, statementStart: this.start});
node.body = this.parseStatement(true);
node.body = this.parseStatement(context);
if (node.body.type === "ClassDeclaration" ||
node.body.type === "VariableDeclaration" && node.body.kind !== "var" ||
node.body.type === "FunctionDeclaration" && (this.strict || node.body.generator || node.body.async))
@ -1092,23 +1119,19 @@ pp$1.parseExpressionStatement = function(node, expr) {
// strict"` declarations when `allowStrict` is true (used for
// function bodies).
pp$1.parseBlock = function(createNewLexicalScope) {
pp$1.parseBlock = function(createNewLexicalScope, node) {
var this$1 = this;
if ( createNewLexicalScope === void 0 ) createNewLexicalScope = true;
if ( node === void 0 ) node = this.startNode();
var node = this.startNode();
node.body = [];
this.expect(types.braceL);
if (createNewLexicalScope) {
this.enterLexicalScope();
}
if (createNewLexicalScope) { this.enterScope(0); }
while (!this.eat(types.braceR)) {
var stmt = this$1.parseStatement(true);
var stmt = this$1.parseStatement(null);
node.body.push(stmt);
}
if (createNewLexicalScope) {
this.exitLexicalScope();
}
if (createNewLexicalScope) { this.exitScope(); }
return this.finishNode(node, "BlockStatement")
};
@ -1123,8 +1146,8 @@ pp$1.parseFor = function(node, init) {
this.expect(types.semi);
node.update = this.type === types.parenR ? null : this.parseExpression();
this.expect(types.parenR);
this.exitLexicalScope();
node.body = this.parseStatement(false);
this.exitScope();
node.body = this.parseStatement("for");
this.labels.pop();
return this.finishNode(node, "ForStatement")
};
@ -1144,8 +1167,8 @@ pp$1.parseForIn = function(node, init) {
node.left = init;
node.right = type === "ForInStatement" ? this.parseExpression() : this.parseMaybeAssign();
this.expect(types.parenR);
this.exitLexicalScope();
node.body = this.parseStatement(false);
this.exitScope();
node.body = this.parseStatement("for");
this.labels.pop();
return this.finishNode(node, type)
};
@ -1177,47 +1200,43 @@ pp$1.parseVar = function(node, isFor, kind) {
pp$1.parseVarId = function(decl, kind) {
decl.id = this.parseBindingAtom(kind);
this.checkLVal(decl.id, kind, false);
this.checkLVal(decl.id, kind === "var" ? BIND_VAR : BIND_LEXICAL, false);
};
var FUNC_STATEMENT = 1;
var FUNC_HANGING_STATEMENT = 2;
var FUNC_NULLABLE_ID = 4;
// Parse a function declaration or literal (depending on the
// `isStatement` parameter).
pp$1.parseFunction = function(node, isStatement, allowExpressionBody, isAsync) {
pp$1.parseFunction = function(node, statement, allowExpressionBody, isAsync) {
this.initFunction(node);
if (this.options.ecmaVersion >= 9 || this.options.ecmaVersion >= 6 && !isAsync)
{ node.generator = this.eat(types.star); }
if (this.options.ecmaVersion >= 8)
{ node.async = !!isAsync; }
if (isStatement) {
node.id = isStatement === "nullableID" && this.type !== types.name ? null : this.parseIdent();
if (node.id) {
this.checkLVal(node.id, this.inModule && !this.inFunction ? "let" : "var");
}
if (statement & FUNC_STATEMENT) {
node.id = (statement & FUNC_NULLABLE_ID) && this.type !== types.name ? null : this.parseIdent();
if (node.id && !(statement & FUNC_HANGING_STATEMENT))
{ this.checkLVal(node.id, this.inModule && !this.inFunction ? BIND_LEXICAL : BIND_FUNCTION); }
}
var oldInGen = this.inGenerator, oldInAsync = this.inAsync,
oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos, oldInFunc = this.inFunction;
this.inGenerator = node.generator;
this.inAsync = node.async;
var oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos;
this.yieldPos = 0;
this.awaitPos = 0;
this.inFunction = true;
this.enterFunctionScope();
this.enterScope(functionFlags(node.async, node.generator));
if (!isStatement)
if (!(statement & FUNC_STATEMENT))
{ node.id = this.type === types.name ? this.parseIdent() : null; }
this.parseFunctionParams(node);
this.parseFunctionBody(node, allowExpressionBody);
this.inGenerator = oldInGen;
this.inAsync = oldInAsync;
this.yieldPos = oldYieldPos;
this.awaitPos = oldAwaitPos;
this.inFunction = oldInFunc;
return this.finishNode(node, isStatement ? "FunctionDeclaration" : "FunctionExpression")
return this.finishNode(node, (statement & FUNC_STATEMENT) ? "FunctionDeclaration" : "FunctionExpression")
};
pp$1.parseFunctionParams = function(node) {
@ -1241,17 +1260,20 @@ pp$1.parseClass = function(node, isStatement) {
classBody.body = [];
this.expect(types.braceL);
while (!this.eat(types.braceR)) {
var member = this$1.parseClassMember(classBody);
if (member && member.type === "MethodDefinition" && member.kind === "constructor") {
if (hadConstructor) { this$1.raise(member.start, "Duplicate constructor in the same class"); }
hadConstructor = true;
var element = this$1.parseClassElement();
if (element) {
classBody.body.push(element);
if (element.type === "MethodDefinition" && element.kind === "constructor") {
if (hadConstructor) { this$1.raise(element.start, "Duplicate constructor in the same class"); }
hadConstructor = true;
}
}
}
node.body = this.finishNode(classBody, "ClassBody");
return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression")
};
pp$1.parseClassMember = function(classBody) {
pp$1.parseClassElement = function() {
var this$1 = this;
if (this.eat(types.semi)) { return null }
@ -1296,7 +1318,7 @@ pp$1.parseClassMember = function(classBody) {
} else if (method.static && key.type === "Identifier" && key.name === "prototype") {
this.raise(key.start, "Classes may not have a static property named prototype");
}
this.parseClassMethod(classBody, method, isGenerator, isAsync);
this.parseClassMethod(method, isGenerator, isAsync);
if (method.kind === "get" && method.value.params.length !== 0)
{ this.raiseRecoverable(method.value.start, "getter should have no params"); }
if (method.kind === "set" && method.value.params.length !== 1)
@ -1306,9 +1328,9 @@ pp$1.parseClassMember = function(classBody) {
return method
};
pp$1.parseClassMethod = function(classBody, method, isGenerator, isAsync) {
pp$1.parseClassMethod = function(method, isGenerator, isAsync) {
method.value = this.parseMethod(isGenerator, isAsync);
classBody.body.push(this.finishNode(method, "MethodDefinition"));
return this.finishNode(method, "MethodDefinition")
};
pp$1.parseClassId = function(node, isStatement) {
@ -1340,7 +1362,7 @@ pp$1.parseExport = function(node, exports) {
var fNode = this.startNode();
this.next();
if (isAsync) { this.next(); }
node.declaration = this.parseFunction(fNode, "nullableID", false, isAsync);
node.declaration = this.parseFunction(fNode, FUNC_STATEMENT | FUNC_NULLABLE_ID, false, isAsync, true);
} else if (this.type === types._class) {
var cNode = this.startNode();
node.declaration = this.parseClass(cNode, "nullableID");
@ -1352,7 +1374,7 @@ pp$1.parseExport = function(node, exports) {
}
// export var|const|let|function|class ...
if (this.shouldParseExportStatement()) {
node.declaration = this.parseStatement(true);
node.declaration = this.parseStatement(null);
if (node.declaration.type === "VariableDeclaration")
{ this.checkVariableExport(exports, node.declaration.declarations); }
else
@ -1487,7 +1509,7 @@ pp$1.parseImportSpecifiers = function() {
// import defaultObj, { x, y as z } from '...'
var node = this.startNode();
node.local = this.parseIdent();
this.checkLVal(node.local, "let");
this.checkLVal(node.local, BIND_LEXICAL);
nodes.push(this.finishNode(node, "ImportDefaultSpecifier"));
if (!this.eat(types.comma)) { return nodes }
}
@ -1496,7 +1518,7 @@ pp$1.parseImportSpecifiers = function() {
this.next();
this.expectContextual("as");
node$1.local = this.parseIdent();
this.checkLVal(node$1.local, "let");
this.checkLVal(node$1.local, BIND_LEXICAL);
nodes.push(this.finishNode(node$1, "ImportNamespaceSpecifier"));
return nodes
}
@ -1515,7 +1537,7 @@ pp$1.parseImportSpecifiers = function() {
this$1.checkUnreserved(node$2.imported);
node$2.local = node$2.imported;
}
this$1.checkLVal(node$2.local, "let");
this$1.checkLVal(node$2.local, BIND_LEXICAL);
nodes.push(this$1.finishNode(node$2, "ImportSpecifier"));
}
return nodes
@ -1730,6 +1752,7 @@ pp$2.parseMaybeDefault = function(startPos, startLoc, left) {
pp$2.checkLVal = function(expr, bindingType, checkClashes) {
var this$1 = this;
if ( bindingType === void 0 ) bindingType = BIND_NONE;
switch (expr.type) {
case "Identifier":
@ -1740,19 +1763,7 @@ pp$2.checkLVal = function(expr, bindingType, checkClashes) {
{ this.raiseRecoverable(expr.start, "Argument name clash"); }
checkClashes[expr.name] = true;
}
if (bindingType && bindingType !== "none") {
if (
bindingType === "var" && !this.canDeclareVarName(expr.name) ||
bindingType !== "var" && !this.canDeclareLexicalName(expr.name)
) {
this.raiseRecoverable(expr.start, ("Identifier '" + (expr.name) + "' has already been declared"));
}
if (bindingType === "var") {
this.declareVarName(expr.name);
} else {
this.declareLexicalName(expr.name);
}
}
if (bindingType !== BIND_NONE && bindingType !== BIND_OUTSIDE) { this.declareName(expr.name, bindingType, expr.start); }
break
case "MemberExpression":
@ -1901,13 +1912,19 @@ pp$3.parseExpression = function(noIn, refDestructuringErrors) {
// operators like `+=`.
pp$3.parseMaybeAssign = function(noIn, refDestructuringErrors, afterLeftParse) {
if (this.inGenerator && this.isContextual("yield")) { return this.parseYield() }
if (this.isContextual("yield")) {
if (this.inGenerator) { return this.parseYield() }
// The tokenizer will assume an expression is allowed after
// `yield`, but this isn't that kind of yield
else { this.exprAllowed = false; }
}
var ownDestructuringErrors = false, oldParenAssign = -1, oldTrailingComma = -1;
var ownDestructuringErrors = false, oldParenAssign = -1, oldTrailingComma = -1, oldShorthandAssign = -1;
if (refDestructuringErrors) {
oldParenAssign = refDestructuringErrors.parenthesizedAssign;
oldTrailingComma = refDestructuringErrors.trailingComma;
refDestructuringErrors.parenthesizedAssign = refDestructuringErrors.trailingComma = -1;
oldShorthandAssign = refDestructuringErrors.shorthandAssign;
refDestructuringErrors.parenthesizedAssign = refDestructuringErrors.trailingComma = refDestructuringErrors.shorthandAssign = -1;
} else {
refDestructuringErrors = new DestructuringErrors;
ownDestructuringErrors = true;
@ -1933,6 +1950,7 @@ pp$3.parseMaybeAssign = function(noIn, refDestructuringErrors, afterLeftParse) {
}
if (oldParenAssign > -1) { refDestructuringErrors.parenthesizedAssign = oldParenAssign; }
if (oldTrailingComma > -1) { refDestructuringErrors.trailingComma = oldTrailingComma; }
if (oldShorthandAssign > -1) { refDestructuringErrors.shorthandAssign = oldShorthandAssign; }
return left
};
@ -2124,7 +2142,7 @@ pp$3.parseExprAtom = function(refDestructuringErrors) {
var startPos = this.start, startLoc = this.startLoc, containsEsc = this.containsEsc;
var id = this.parseIdent(this.type !== types.name);
if (this.options.ecmaVersion >= 8 && !containsEsc && id.name === "async" && !this.canInsertSemicolon() && this.eat(types._function))
{ return this.parseFunction(this.startNodeAt(startPos, startLoc), false, false, true) }
{ return this.parseFunction(this.startNodeAt(startPos, startLoc), 0, false, true) }
if (canBeArrow && !this.canInsertSemicolon()) {
if (this.eat(types.arrow))
{ return this.parseArrowExpression(this.startNodeAt(startPos, startLoc), [id], false) }
@ -2175,7 +2193,7 @@ pp$3.parseExprAtom = function(refDestructuringErrors) {
case types._function:
node = this.startNode();
this.next();
return this.parseFunction(node, false)
return this.parseFunction(node, 0)
case types._class:
return this.parseClass(this.startNode(), false)
@ -2294,7 +2312,7 @@ pp$3.parseNew = function() {
node.property = this.parseIdent(true);
if (node.property.name !== "target" || containsEsc)
{ this.raiseRecoverable(node.property.start, "The only valid meta property for new is new.target"); }
if (!this.inFunction)
if (!this.inNonArrowFunction())
{ this.raiseRecoverable(node.start, "new.target can only be used in functions"); }
return this.finishNode(node, "MetaProperty")
}
@ -2494,19 +2512,14 @@ pp$3.parsePropertyName = function(prop) {
pp$3.initFunction = function(node) {
node.id = null;
if (this.options.ecmaVersion >= 6) {
node.generator = false;
node.expression = false;
}
if (this.options.ecmaVersion >= 8)
{ node.async = false; }
if (this.options.ecmaVersion >= 6) { node.generator = node.expression = false; }
if (this.options.ecmaVersion >= 8) { node.async = false; }
};
// Parse object or class method.
pp$3.parseMethod = function(isGenerator, isAsync) {
var node = this.startNode(), oldInGen = this.inGenerator, oldInAsync = this.inAsync,
oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos, oldInFunc = this.inFunction;
var node = this.startNode(), oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos;
this.initFunction(node);
if (this.options.ecmaVersion >= 6)
@ -2514,51 +2527,37 @@ pp$3.parseMethod = function(isGenerator, isAsync) {
if (this.options.ecmaVersion >= 8)
{ node.async = !!isAsync; }
this.inGenerator = node.generator;
this.inAsync = node.async;
this.yieldPos = 0;
this.awaitPos = 0;
this.inFunction = true;
this.enterFunctionScope();
this.enterScope(functionFlags(isAsync, node.generator));
this.expect(types.parenL);
node.params = this.parseBindingList(types.parenR, false, this.options.ecmaVersion >= 8);
this.checkYieldAwaitInDefaultParams();
this.parseFunctionBody(node, false);
this.inGenerator = oldInGen;
this.inAsync = oldInAsync;
this.yieldPos = oldYieldPos;
this.awaitPos = oldAwaitPos;
this.inFunction = oldInFunc;
return this.finishNode(node, "FunctionExpression")
};
// Parse arrow function expression with given parameters.
pp$3.parseArrowExpression = function(node, params, isAsync) {
var oldInGen = this.inGenerator, oldInAsync = this.inAsync,
oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos, oldInFunc = this.inFunction;
var oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos;
this.enterFunctionScope();
this.enterScope(functionFlags(isAsync, false) | SCOPE_ARROW);
this.initFunction(node);
if (this.options.ecmaVersion >= 8)
{ node.async = !!isAsync; }
if (this.options.ecmaVersion >= 8) { node.async = !!isAsync; }
this.inGenerator = false;
this.inAsync = node.async;
this.yieldPos = 0;
this.awaitPos = 0;
this.inFunction = true;
node.params = this.toAssignableList(params, true);
this.parseFunctionBody(node, true);
this.inGenerator = oldInGen;
this.inAsync = oldInAsync;
this.yieldPos = oldYieldPos;
this.awaitPos = oldAwaitPos;
this.inFunction = oldInFunc;
return this.finishNode(node, "ArrowFunctionExpression")
};
@ -2596,12 +2595,10 @@ pp$3.parseFunctionBody = function(node, isArrowFunction) {
this.adaptDirectivePrologue(node.body.body);
this.labels = oldLabels;
}
this.exitFunctionScope();
this.exitScope();
if (this.strict && node.id) {
// Ensure the function name isn't a forbidden identifier in strict mode, e.g. 'eval'
this.checkLVal(node.id, "none");
}
// Ensure the function name isn't a forbidden identifier in strict mode, e.g. 'eval'
if (this.strict && node.id) { this.checkLVal(node.id, BIND_OUTSIDE); }
this.strict = oldStrict;
};
@ -2626,7 +2623,7 @@ pp$3.checkParams = function(node, allowDuplicates) {
{
var param = list[i];
this$1.checkLVal(param, "var", allowDuplicates ? null : nameHash);
this$1.checkLVal(param, BIND_VAR, allowDuplicates ? null : nameHash);
}
};
@ -2670,7 +2667,7 @@ pp$3.checkUnreserved = function(ref) {
{ this.raiseRecoverable(start, "Can not use 'yield' as identifier inside a generator"); }
if (this.inAsync && name === "await")
{ this.raiseRecoverable(start, "Can not use 'await' as identifier inside an async function"); }
if (this.isKeyword(name))
if (this.keywords.test(name))
{ this.raise(start, ("Unexpected keyword '" + name + "'")); }
if (this.options.ecmaVersion < 6 &&
this.input.slice(start, end).indexOf("\\") !== -1) { return }
@ -2763,79 +2760,69 @@ pp$4.curPosition = function() {
var pp$5 = Parser.prototype;
// Object.assign polyfill
var assign = Object.assign || function(target) {
var sources = [], len = arguments.length - 1;
while ( len-- > 0 ) sources[ len ] = arguments[ len + 1 ];
for (var i = 0, list = sources; i < list.length; i += 1) {
var source = list[i];
for (var key in source) {
if (has(source, key)) {
target[key] = source[key];
}
}
}
return target
var Scope = function Scope(flags) {
this.flags = flags;
// A list of var-declared names in the current lexical scope
this.var = [];
// A list of lexically-declared names in the current lexical scope
this.lexical = [];
};
// The functions in this module keep track of declared variables in the current scope in order to detect duplicate variable names.
pp$5.enterFunctionScope = function() {
// var: a hash of var-declared names in the current lexical scope
// lexical: a hash of lexically-declared names in the current lexical scope
// childVar: a hash of var-declared names in all child lexical scopes of the current lexical scope (within the current function scope)
// parentLexical: a hash of lexically-declared names in all parent lexical scopes of the current lexical scope (within the current function scope)
this.scopeStack.push({var: {}, lexical: {}, childVar: {}, parentLexical: {}});
pp$5.enterScope = function(flags) {
this.scopeStack.push(new Scope(flags));
};
pp$5.exitFunctionScope = function() {
pp$5.exitScope = function() {
this.scopeStack.pop();
};
pp$5.enterLexicalScope = function() {
var parentScope = this.scopeStack[this.scopeStack.length - 1];
var childScope = {var: {}, lexical: {}, childVar: {}, parentLexical: {}};
pp$5.declareName = function(name, bindingType, pos) {
var this$1 = this;
this.scopeStack.push(childScope);
assign(childScope.parentLexical, parentScope.lexical, parentScope.parentLexical);
var redeclared = false;
if (bindingType === BIND_LEXICAL) {
var scope = this.currentScope();
redeclared = scope.lexical.indexOf(name) > -1 || scope.var.indexOf(name) > -1;
scope.lexical.push(name);
} else if (bindingType === BIND_SIMPLE_CATCH) {
var scope$1 = this.currentScope();
scope$1.lexical.push(name);
} else if (bindingType === BIND_FUNCTION) {
var scope$2 = this.currentScope();
redeclared = scope$2.lexical.indexOf(name) > -1;
scope$2.var.push(name);
} else {
for (var i = this.scopeStack.length - 1; i >= 0; --i) {
var scope$3 = this$1.scopeStack[i];
if (scope$3.lexical.indexOf(name) > -1 && !(scope$3.flags & SCOPE_SIMPLE_CATCH) && scope$3.lexical[0] === name) { redeclared = true; }
scope$3.var.push(name);
if (scope$3.flags & SCOPE_VAR) { break }
}
}
if (redeclared) { this.raiseRecoverable(pos, ("Identifier '" + name + "' has already been declared")); }
};
pp$5.exitLexicalScope = function() {
var childScope = this.scopeStack.pop();
var parentScope = this.scopeStack[this.scopeStack.length - 1];
assign(parentScope.childVar, childScope.var, childScope.childVar);
pp$5.currentScope = function() {
return this.scopeStack[this.scopeStack.length - 1]
};
/**
* A name can be declared with `var` if there are no variables with the same name declared with `let`/`const`
* in the current lexical scope or any of the parent lexical scopes in this function.
*/
pp$5.canDeclareVarName = function(name) {
var currentScope = this.scopeStack[this.scopeStack.length - 1];
pp$5.currentVarScope = function() {
var this$1 = this;
return !has(currentScope.lexical, name) && !has(currentScope.parentLexical, name)
for (var i = this.scopeStack.length - 1;; i--) {
var scope = this$1.scopeStack[i];
if (scope.flags & SCOPE_VAR) { return scope }
}
};
/**
* A name can be declared with `let`/`const` if there are no variables with the same name declared with `let`/`const`
* in the current scope, and there are no variables with the same name declared with `var` in the current scope or in
* any child lexical scopes in this function.
*/
pp$5.canDeclareLexicalName = function(name) {
var currentScope = this.scopeStack[this.scopeStack.length - 1];
pp$5.inNonArrowFunction = function() {
var this$1 = this;
return !has(currentScope.lexical, name) && !has(currentScope.var, name) && !has(currentScope.childVar, name)
};
pp$5.declareVarName = function(name) {
this.scopeStack[this.scopeStack.length - 1].var[name] = true;
};
pp$5.declareLexicalName = function(name) {
this.scopeStack[this.scopeStack.length - 1].lexical[name] = true;
for (var i = this.scopeStack.length - 1; i >= 0; i--)
{ if (this$1.scopeStack[i].flags & SCOPE_FUNCTION && !(this$1.scopeStack[i].flags & SCOPE_ARROW)) { return true } }
return false
};
var Node = function Node(parser, pos, loc) {
@ -5262,14 +5249,9 @@ pp$8.readWord = function() {
//
// [ghbt]: https://github.com/acornjs/acorn/issues
//
// This file defines the main parser interface. The library also comes
// with a [error-tolerant parser][dammit] and an
// [abstract syntax tree walker][walk], defined in other files.
//
// [dammit]: acorn_loose.js
// [walk]: util/walk.js
var version = "5.7.3";
var version = "6.0.2";
// The main exported interface (under `self.acorn` when in the
// browser) is a `parse` function that takes a code string and
@ -5279,7 +5261,7 @@ var version = "5.7.3";
// [api]: https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API
function parse(input, options) {
return new Parser(options, input).parse()
return Parser.parse(input, options)
}
// This function tries to parse a single expression at a given
@ -5287,28 +5269,15 @@ function parse(input, options) {
// that embed JavaScript expressions.
function parseExpressionAt(input, pos, options) {
var p = new Parser(options, input, pos);
p.nextToken();
return p.parseExpression()
return Parser.parseExpressionAt(input, pos, options)
}
// Acorn is organized as a tokenizer and a recursive-descent parser.
// The `tokenizer` export provides an interface to the tokenizer.
function tokenizer(input, options) {
return new Parser(options, input)
return Parser.tokenizer(input, options)
}
// This is a terrible kludge to support the existing, pre-ES6
// interface where the loose parser module retroactively adds exports
// to this module.
var parse_dammit;
var LooseParser;
var pluginsLoose; // eslint-disable-line camelcase
function addLooseExports(parse, Parser$$1, plugins$$1) {
parse_dammit = parse; // eslint-disable-line camelcase
LooseParser = Parser$$1;
pluginsLoose = plugins$$1;
}
export { version, parse, parseExpressionAt, tokenizer, parse_dammit, LooseParser, pluginsLoose, addLooseExports, Parser, plugins, defaultOptions, Position, SourceLocation, getLineInfo, Node, TokenType, types as tokTypes, keywords$1 as keywordTypes, TokContext, types$1 as tokContexts, isIdentifierChar, isIdentifierStart, Token, isNewLine, lineBreak, lineBreakG, nonASCIIwhitespace };
export { version, parse, parseExpressionAt, tokenizer, Parser, defaultOptions, Position, SourceLocation, getLineInfo, Node, TokenType, types as tokTypes, keywords$1 as keywordTypes, TokContext, types$1 as tokContexts, isIdentifierChar, isIdentifierStart, Token, isNewLine, lineBreak, lineBreakG, nonASCIIwhitespace };
//# sourceMappingURL=acorn.mjs.map

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,9 +1,8 @@
#!/usr/bin/env node
'use strict';
var path = require('path');
var fs = require('fs');
var acorn = require('../dist/acorn.js');
var acorn = require('./acorn.js');
var infile;
var forceFile;

View File

@ -1,423 +0,0 @@
// AST walker module for Mozilla Parser API compatible trees
// A simple walk is one where you simply specify callbacks to be
// called on specific nodes. The last two arguments are optional. A
// simple use would be
//
// walk.simple(myTree, {
// Expression: function(node) { ... }
// });
//
// to do something with all expressions. All Parser API node types
// can be used to identify node types, as well as Expression,
// Statement, and ScopeBody, which denote categories of nodes.
//
// The base argument can be used to pass a custom (recursive)
// walker, and state can be used to give this walked an initial
// state.
function simple(node, visitors, baseVisitor, state, override) {
if (!baseVisitor) { baseVisitor = base
; }(function c(node, st, override) {
var type = override || node.type, found = visitors[type];
baseVisitor[type](node, st, c);
if (found) { found(node, st); }
})(node, state, override);
}
// An ancestor walk keeps an array of ancestor nodes (including the
// current node) and passes them to the callback as third parameter
// (and also as state parameter when no other state is present).
function ancestor(node, visitors, baseVisitor, state) {
var ancestors = [];
if (!baseVisitor) { baseVisitor = base
; }(function c(node, st, override) {
var type = override || node.type, found = visitors[type];
var isNew = node !== ancestors[ancestors.length - 1];
if (isNew) { ancestors.push(node); }
baseVisitor[type](node, st, c);
if (found) { found(node, st || ancestors, ancestors); }
if (isNew) { ancestors.pop(); }
})(node, state);
}
// A recursive walk is one where your functions override the default
// walkers. They can modify and replace the state parameter that's
// threaded through the walk, and can opt how and whether to walk
// their child nodes (by calling their third argument on these
// nodes).
function recursive(node, state, funcs, baseVisitor, override) {
var visitor = funcs ? make(funcs, baseVisitor || undefined) : baseVisitor;(function c(node, st, override) {
visitor[override || node.type](node, st, c);
})(node, state, override);
}
function makeTest(test) {
if (typeof test === "string")
{ return function (type) { return type === test; } }
else if (!test)
{ return function () { return true; } }
else
{ return test }
}
var Found = function Found(node, state) { this.node = node; this.state = state; };
// A full walk triggers the callback on each node
function full(node, callback, baseVisitor, state, override) {
if (!baseVisitor) { baseVisitor = base
; }(function c(node, st, override) {
var type = override || node.type;
baseVisitor[type](node, st, c);
if (!override) { callback(node, st, type); }
})(node, state, override);
}
// An fullAncestor walk is like an ancestor walk, but triggers
// the callback on each node
function fullAncestor(node, callback, baseVisitor, state) {
if (!baseVisitor) { baseVisitor = base; }
var ancestors = [];(function c(node, st, override) {
var type = override || node.type;
var isNew = node !== ancestors[ancestors.length - 1];
if (isNew) { ancestors.push(node); }
baseVisitor[type](node, st, c);
if (!override) { callback(node, st || ancestors, ancestors, type); }
if (isNew) { ancestors.pop(); }
})(node, state);
}
// Find a node with a given start, end, and type (all are optional,
// null can be used as wildcard). Returns a {node, state} object, or
// undefined when it doesn't find a matching node.
function findNodeAt(node, start, end, test, baseVisitor, state) {
if (!baseVisitor) { baseVisitor = base; }
test = makeTest(test);
try {
(function c(node, st, override) {
var type = override || node.type;
if ((start == null || node.start <= start) &&
(end == null || node.end >= end))
{ baseVisitor[type](node, st, c); }
if ((start == null || node.start === start) &&
(end == null || node.end === end) &&
test(type, node))
{ throw new Found(node, st) }
})(node, state);
} catch (e) {
if (e instanceof Found) { return e }
throw e
}
}
// Find the innermost node of a given type that contains the given
// position. Interface similar to findNodeAt.
function findNodeAround(node, pos, test, baseVisitor, state) {
test = makeTest(test);
if (!baseVisitor) { baseVisitor = base; }
try {
(function c(node, st, override) {
var type = override || node.type;
if (node.start > pos || node.end < pos) { return }
baseVisitor[type](node, st, c);
if (test(type, node)) { throw new Found(node, st) }
})(node, state);
} catch (e) {
if (e instanceof Found) { return e }
throw e
}
}
// Find the outermost matching node after a given position.
function findNodeAfter(node, pos, test, baseVisitor, state) {
test = makeTest(test);
if (!baseVisitor) { baseVisitor = base; }
try {
(function c(node, st, override) {
if (node.end < pos) { return }
var type = override || node.type;
if (node.start >= pos && test(type, node)) { throw new Found(node, st) }
baseVisitor[type](node, st, c);
})(node, state);
} catch (e) {
if (e instanceof Found) { return e }
throw e
}
}
// Find the outermost matching node before a given position.
function findNodeBefore(node, pos, test, baseVisitor, state) {
test = makeTest(test);
if (!baseVisitor) { baseVisitor = base; }
var max;(function c(node, st, override) {
if (node.start > pos) { return }
var type = override || node.type;
if (node.end <= pos && (!max || max.node.end < node.end) && test(type, node))
{ max = new Found(node, st); }
baseVisitor[type](node, st, c);
})(node, state);
return max
}
// Fallback to an Object.create polyfill for older environments.
var create = Object.create || function(proto) {
function Ctor() {}
Ctor.prototype = proto;
return new Ctor
};
// Used to create a custom walker. Will fill in all missing node
// type properties with the defaults.
function make(funcs, baseVisitor) {
var visitor = create(baseVisitor || base);
for (var type in funcs) { visitor[type] = funcs[type]; }
return visitor
}
function skipThrough(node, st, c) { c(node, st); }
function ignore(_node, _st, _c) {}
// Node walkers.
var base = {};
base.Program = base.BlockStatement = function (node, st, c) {
for (var i = 0, list = node.body; i < list.length; i += 1)
{
var stmt = list[i];
c(stmt, st, "Statement");
}
};
base.Statement = skipThrough;
base.EmptyStatement = ignore;
base.ExpressionStatement = base.ParenthesizedExpression =
function (node, st, c) { return c(node.expression, st, "Expression"); };
base.IfStatement = function (node, st, c) {
c(node.test, st, "Expression");
c(node.consequent, st, "Statement");
if (node.alternate) { c(node.alternate, st, "Statement"); }
};
base.LabeledStatement = function (node, st, c) { return c(node.body, st, "Statement"); };
base.BreakStatement = base.ContinueStatement = ignore;
base.WithStatement = function (node, st, c) {
c(node.object, st, "Expression");
c(node.body, st, "Statement");
};
base.SwitchStatement = function (node, st, c) {
c(node.discriminant, st, "Expression");
for (var i = 0, list = node.cases; i < list.length; i += 1) {
var cs = list[i];
if (cs.test) { c(cs.test, st, "Expression"); }
for (var i$1 = 0, list$1 = cs.consequent; i$1 < list$1.length; i$1 += 1)
{
var cons = list$1[i$1];
c(cons, st, "Statement");
}
}
};
base.SwitchCase = function (node, st, c) {
if (node.test) { c(node.test, st, "Expression"); }
for (var i = 0, list = node.consequent; i < list.length; i += 1)
{
var cons = list[i];
c(cons, st, "Statement");
}
};
base.ReturnStatement = base.YieldExpression = base.AwaitExpression = function (node, st, c) {
if (node.argument) { c(node.argument, st, "Expression"); }
};
base.ThrowStatement = base.SpreadElement =
function (node, st, c) { return c(node.argument, st, "Expression"); };
base.TryStatement = function (node, st, c) {
c(node.block, st, "Statement");
if (node.handler) { c(node.handler, st); }
if (node.finalizer) { c(node.finalizer, st, "Statement"); }
};
base.CatchClause = function (node, st, c) {
if (node.param) { c(node.param, st, "Pattern"); }
c(node.body, st, "ScopeBody");
};
base.WhileStatement = base.DoWhileStatement = function (node, st, c) {
c(node.test, st, "Expression");
c(node.body, st, "Statement");
};
base.ForStatement = function (node, st, c) {
if (node.init) { c(node.init, st, "ForInit"); }
if (node.test) { c(node.test, st, "Expression"); }
if (node.update) { c(node.update, st, "Expression"); }
c(node.body, st, "Statement");
};
base.ForInStatement = base.ForOfStatement = function (node, st, c) {
c(node.left, st, "ForInit");
c(node.right, st, "Expression");
c(node.body, st, "Statement");
};
base.ForInit = function (node, st, c) {
if (node.type === "VariableDeclaration") { c(node, st); }
else { c(node, st, "Expression"); }
};
base.DebuggerStatement = ignore;
base.FunctionDeclaration = function (node, st, c) { return c(node, st, "Function"); };
base.VariableDeclaration = function (node, st, c) {
for (var i = 0, list = node.declarations; i < list.length; i += 1)
{
var decl = list[i];
c(decl, st);
}
};
base.VariableDeclarator = function (node, st, c) {
c(node.id, st, "Pattern");
if (node.init) { c(node.init, st, "Expression"); }
};
base.Function = function (node, st, c) {
if (node.id) { c(node.id, st, "Pattern"); }
for (var i = 0, list = node.params; i < list.length; i += 1)
{
var param = list[i];
c(param, st, "Pattern");
}
c(node.body, st, node.expression ? "ScopeExpression" : "ScopeBody");
};
// FIXME drop these node types in next major version
// (They are awkward, and in ES6 every block can be a scope.)
base.ScopeBody = function (node, st, c) { return c(node, st, "Statement"); };
base.ScopeExpression = function (node, st, c) { return c(node, st, "Expression"); };
base.Pattern = function (node, st, c) {
if (node.type === "Identifier")
{ c(node, st, "VariablePattern"); }
else if (node.type === "MemberExpression")
{ c(node, st, "MemberPattern"); }
else
{ c(node, st); }
};
base.VariablePattern = ignore;
base.MemberPattern = skipThrough;
base.RestElement = function (node, st, c) { return c(node.argument, st, "Pattern"); };
base.ArrayPattern = function (node, st, c) {
for (var i = 0, list = node.elements; i < list.length; i += 1) {
var elt = list[i];
if (elt) { c(elt, st, "Pattern"); }
}
};
base.ObjectPattern = function (node, st, c) {
for (var i = 0, list = node.properties; i < list.length; i += 1) {
var prop = list[i];
if (prop.type === "Property") {
if (prop.computed) { c(prop.key, st, "Expression"); }
c(prop.value, st, "Pattern");
} else if (prop.type === "RestElement") {
c(prop.argument, st, "Pattern");
}
}
};
base.Expression = skipThrough;
base.ThisExpression = base.Super = base.MetaProperty = ignore;
base.ArrayExpression = function (node, st, c) {
for (var i = 0, list = node.elements; i < list.length; i += 1) {
var elt = list[i];
if (elt) { c(elt, st, "Expression"); }
}
};
base.ObjectExpression = function (node, st, c) {
for (var i = 0, list = node.properties; i < list.length; i += 1)
{
var prop = list[i];
c(prop, st);
}
};
base.FunctionExpression = base.ArrowFunctionExpression = base.FunctionDeclaration;
base.SequenceExpression = base.TemplateLiteral = function (node, st, c) {
for (var i = 0, list = node.expressions; i < list.length; i += 1)
{
var expr = list[i];
c(expr, st, "Expression");
}
};
base.UnaryExpression = base.UpdateExpression = function (node, st, c) {
c(node.argument, st, "Expression");
};
base.BinaryExpression = base.LogicalExpression = function (node, st, c) {
c(node.left, st, "Expression");
c(node.right, st, "Expression");
};
base.AssignmentExpression = base.AssignmentPattern = function (node, st, c) {
c(node.left, st, "Pattern");
c(node.right, st, "Expression");
};
base.ConditionalExpression = function (node, st, c) {
c(node.test, st, "Expression");
c(node.consequent, st, "Expression");
c(node.alternate, st, "Expression");
};
base.NewExpression = base.CallExpression = function (node, st, c) {
c(node.callee, st, "Expression");
if (node.arguments)
{ for (var i = 0, list = node.arguments; i < list.length; i += 1)
{
var arg = list[i];
c(arg, st, "Expression");
} }
};
base.MemberExpression = function (node, st, c) {
c(node.object, st, "Expression");
if (node.computed) { c(node.property, st, "Expression"); }
};
base.ExportNamedDeclaration = base.ExportDefaultDeclaration = function (node, st, c) {
if (node.declaration)
{ c(node.declaration, st, node.type === "ExportNamedDeclaration" || node.declaration.id ? "Statement" : "Expression"); }
if (node.source) { c(node.source, st, "Expression"); }
};
base.ExportAllDeclaration = function (node, st, c) {
c(node.source, st, "Expression");
};
base.ImportDeclaration = function (node, st, c) {
for (var i = 0, list = node.specifiers; i < list.length; i += 1)
{
var spec = list[i];
c(spec, st);
}
c(node.source, st, "Expression");
};
base.ImportSpecifier = base.ImportDefaultSpecifier = base.ImportNamespaceSpecifier = base.Identifier = base.Literal = ignore;
base.TaggedTemplateExpression = function (node, st, c) {
c(node.tag, st, "Expression");
c(node.quasi, st, "Expression");
};
base.ClassDeclaration = base.ClassExpression = function (node, st, c) { return c(node, st, "Class"); };
base.Class = function (node, st, c) {
if (node.id) { c(node.id, st, "Pattern"); }
if (node.superClass) { c(node.superClass, st, "Expression"); }
c(node.body, st);
};
base.ClassBody = function (node, st, c) {
for (var i = 0, list = node.body; i < list.length; i += 1)
{
var elt = list[i];
c(elt, st);
}
};
base.MethodDefinition = base.Property = function (node, st, c) {
if (node.computed) { c(node.key, st, "Expression"); }
c(node.value, st, "Expression");
};
export { simple, ancestor, recursive, full, fullAncestor, findNodeAt, findNodeAround, findNodeAfter, findNodeBefore, make, base };

View File

@ -1,443 +0,0 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.acorn = global.acorn || {}, global.acorn.walk = {})));
}(this, (function (exports) { 'use strict';
// AST walker module for Mozilla Parser API compatible trees
// A simple walk is one where you simply specify callbacks to be
// called on specific nodes. The last two arguments are optional. A
// simple use would be
//
// walk.simple(myTree, {
// Expression: function(node) { ... }
// });
//
// to do something with all expressions. All Parser API node types
// can be used to identify node types, as well as Expression,
// Statement, and ScopeBody, which denote categories of nodes.
//
// The base argument can be used to pass a custom (recursive)
// walker, and state can be used to give this walked an initial
// state.
function simple(node, visitors, baseVisitor, state, override) {
if (!baseVisitor) { baseVisitor = base
; }(function c(node, st, override) {
var type = override || node.type, found = visitors[type];
baseVisitor[type](node, st, c);
if (found) { found(node, st); }
})(node, state, override);
}
// An ancestor walk keeps an array of ancestor nodes (including the
// current node) and passes them to the callback as third parameter
// (and also as state parameter when no other state is present).
function ancestor(node, visitors, baseVisitor, state) {
var ancestors = [];
if (!baseVisitor) { baseVisitor = base
; }(function c(node, st, override) {
var type = override || node.type, found = visitors[type];
var isNew = node !== ancestors[ancestors.length - 1];
if (isNew) { ancestors.push(node); }
baseVisitor[type](node, st, c);
if (found) { found(node, st || ancestors, ancestors); }
if (isNew) { ancestors.pop(); }
})(node, state);
}
// A recursive walk is one where your functions override the default
// walkers. They can modify and replace the state parameter that's
// threaded through the walk, and can opt how and whether to walk
// their child nodes (by calling their third argument on these
// nodes).
function recursive(node, state, funcs, baseVisitor, override) {
var visitor = funcs ? make(funcs, baseVisitor || undefined) : baseVisitor;(function c(node, st, override) {
visitor[override || node.type](node, st, c);
})(node, state, override);
}
function makeTest(test) {
if (typeof test === "string")
{ return function (type) { return type === test; } }
else if (!test)
{ return function () { return true; } }
else
{ return test }
}
var Found = function Found(node, state) { this.node = node; this.state = state; };
// A full walk triggers the callback on each node
function full(node, callback, baseVisitor, state, override) {
if (!baseVisitor) { baseVisitor = base
; }(function c(node, st, override) {
var type = override || node.type;
baseVisitor[type](node, st, c);
if (!override) { callback(node, st, type); }
})(node, state, override);
}
// An fullAncestor walk is like an ancestor walk, but triggers
// the callback on each node
function fullAncestor(node, callback, baseVisitor, state) {
if (!baseVisitor) { baseVisitor = base; }
var ancestors = [];(function c(node, st, override) {
var type = override || node.type;
var isNew = node !== ancestors[ancestors.length - 1];
if (isNew) { ancestors.push(node); }
baseVisitor[type](node, st, c);
if (!override) { callback(node, st || ancestors, ancestors, type); }
if (isNew) { ancestors.pop(); }
})(node, state);
}
// Find a node with a given start, end, and type (all are optional,
// null can be used as wildcard). Returns a {node, state} object, or
// undefined when it doesn't find a matching node.
function findNodeAt(node, start, end, test, baseVisitor, state) {
if (!baseVisitor) { baseVisitor = base; }
test = makeTest(test);
try {
(function c(node, st, override) {
var type = override || node.type;
if ((start == null || node.start <= start) &&
(end == null || node.end >= end))
{ baseVisitor[type](node, st, c); }
if ((start == null || node.start === start) &&
(end == null || node.end === end) &&
test(type, node))
{ throw new Found(node, st) }
})(node, state);
} catch (e) {
if (e instanceof Found) { return e }
throw e
}
}
// Find the innermost node of a given type that contains the given
// position. Interface similar to findNodeAt.
function findNodeAround(node, pos, test, baseVisitor, state) {
test = makeTest(test);
if (!baseVisitor) { baseVisitor = base; }
try {
(function c(node, st, override) {
var type = override || node.type;
if (node.start > pos || node.end < pos) { return }
baseVisitor[type](node, st, c);
if (test(type, node)) { throw new Found(node, st) }
})(node, state);
} catch (e) {
if (e instanceof Found) { return e }
throw e
}
}
// Find the outermost matching node after a given position.
function findNodeAfter(node, pos, test, baseVisitor, state) {
test = makeTest(test);
if (!baseVisitor) { baseVisitor = base; }
try {
(function c(node, st, override) {
if (node.end < pos) { return }
var type = override || node.type;
if (node.start >= pos && test(type, node)) { throw new Found(node, st) }
baseVisitor[type](node, st, c);
})(node, state);
} catch (e) {
if (e instanceof Found) { return e }
throw e
}
}
// Find the outermost matching node before a given position.
function findNodeBefore(node, pos, test, baseVisitor, state) {
test = makeTest(test);
if (!baseVisitor) { baseVisitor = base; }
var max;(function c(node, st, override) {
if (node.start > pos) { return }
var type = override || node.type;
if (node.end <= pos && (!max || max.node.end < node.end) && test(type, node))
{ max = new Found(node, st); }
baseVisitor[type](node, st, c);
})(node, state);
return max
}
// Fallback to an Object.create polyfill for older environments.
var create = Object.create || function(proto) {
function Ctor() {}
Ctor.prototype = proto;
return new Ctor
};
// Used to create a custom walker. Will fill in all missing node
// type properties with the defaults.
function make(funcs, baseVisitor) {
var visitor = create(baseVisitor || base);
for (var type in funcs) { visitor[type] = funcs[type]; }
return visitor
}
function skipThrough(node, st, c) { c(node, st); }
function ignore(_node, _st, _c) {}
// Node walkers.
var base = {};
base.Program = base.BlockStatement = function (node, st, c) {
for (var i = 0, list = node.body; i < list.length; i += 1)
{
var stmt = list[i];
c(stmt, st, "Statement");
}
};
base.Statement = skipThrough;
base.EmptyStatement = ignore;
base.ExpressionStatement = base.ParenthesizedExpression =
function (node, st, c) { return c(node.expression, st, "Expression"); };
base.IfStatement = function (node, st, c) {
c(node.test, st, "Expression");
c(node.consequent, st, "Statement");
if (node.alternate) { c(node.alternate, st, "Statement"); }
};
base.LabeledStatement = function (node, st, c) { return c(node.body, st, "Statement"); };
base.BreakStatement = base.ContinueStatement = ignore;
base.WithStatement = function (node, st, c) {
c(node.object, st, "Expression");
c(node.body, st, "Statement");
};
base.SwitchStatement = function (node, st, c) {
c(node.discriminant, st, "Expression");
for (var i = 0, list = node.cases; i < list.length; i += 1) {
var cs = list[i];
if (cs.test) { c(cs.test, st, "Expression"); }
for (var i$1 = 0, list$1 = cs.consequent; i$1 < list$1.length; i$1 += 1)
{
var cons = list$1[i$1];
c(cons, st, "Statement");
}
}
};
base.SwitchCase = function (node, st, c) {
if (node.test) { c(node.test, st, "Expression"); }
for (var i = 0, list = node.consequent; i < list.length; i += 1)
{
var cons = list[i];
c(cons, st, "Statement");
}
};
base.ReturnStatement = base.YieldExpression = base.AwaitExpression = function (node, st, c) {
if (node.argument) { c(node.argument, st, "Expression"); }
};
base.ThrowStatement = base.SpreadElement =
function (node, st, c) { return c(node.argument, st, "Expression"); };
base.TryStatement = function (node, st, c) {
c(node.block, st, "Statement");
if (node.handler) { c(node.handler, st); }
if (node.finalizer) { c(node.finalizer, st, "Statement"); }
};
base.CatchClause = function (node, st, c) {
if (node.param) { c(node.param, st, "Pattern"); }
c(node.body, st, "ScopeBody");
};
base.WhileStatement = base.DoWhileStatement = function (node, st, c) {
c(node.test, st, "Expression");
c(node.body, st, "Statement");
};
base.ForStatement = function (node, st, c) {
if (node.init) { c(node.init, st, "ForInit"); }
if (node.test) { c(node.test, st, "Expression"); }
if (node.update) { c(node.update, st, "Expression"); }
c(node.body, st, "Statement");
};
base.ForInStatement = base.ForOfStatement = function (node, st, c) {
c(node.left, st, "ForInit");
c(node.right, st, "Expression");
c(node.body, st, "Statement");
};
base.ForInit = function (node, st, c) {
if (node.type === "VariableDeclaration") { c(node, st); }
else { c(node, st, "Expression"); }
};
base.DebuggerStatement = ignore;
base.FunctionDeclaration = function (node, st, c) { return c(node, st, "Function"); };
base.VariableDeclaration = function (node, st, c) {
for (var i = 0, list = node.declarations; i < list.length; i += 1)
{
var decl = list[i];
c(decl, st);
}
};
base.VariableDeclarator = function (node, st, c) {
c(node.id, st, "Pattern");
if (node.init) { c(node.init, st, "Expression"); }
};
base.Function = function (node, st, c) {
if (node.id) { c(node.id, st, "Pattern"); }
for (var i = 0, list = node.params; i < list.length; i += 1)
{
var param = list[i];
c(param, st, "Pattern");
}
c(node.body, st, node.expression ? "ScopeExpression" : "ScopeBody");
};
// FIXME drop these node types in next major version
// (They are awkward, and in ES6 every block can be a scope.)
base.ScopeBody = function (node, st, c) { return c(node, st, "Statement"); };
base.ScopeExpression = function (node, st, c) { return c(node, st, "Expression"); };
base.Pattern = function (node, st, c) {
if (node.type === "Identifier")
{ c(node, st, "VariablePattern"); }
else if (node.type === "MemberExpression")
{ c(node, st, "MemberPattern"); }
else
{ c(node, st); }
};
base.VariablePattern = ignore;
base.MemberPattern = skipThrough;
base.RestElement = function (node, st, c) { return c(node.argument, st, "Pattern"); };
base.ArrayPattern = function (node, st, c) {
for (var i = 0, list = node.elements; i < list.length; i += 1) {
var elt = list[i];
if (elt) { c(elt, st, "Pattern"); }
}
};
base.ObjectPattern = function (node, st, c) {
for (var i = 0, list = node.properties; i < list.length; i += 1) {
var prop = list[i];
if (prop.type === "Property") {
if (prop.computed) { c(prop.key, st, "Expression"); }
c(prop.value, st, "Pattern");
} else if (prop.type === "RestElement") {
c(prop.argument, st, "Pattern");
}
}
};
base.Expression = skipThrough;
base.ThisExpression = base.Super = base.MetaProperty = ignore;
base.ArrayExpression = function (node, st, c) {
for (var i = 0, list = node.elements; i < list.length; i += 1) {
var elt = list[i];
if (elt) { c(elt, st, "Expression"); }
}
};
base.ObjectExpression = function (node, st, c) {
for (var i = 0, list = node.properties; i < list.length; i += 1)
{
var prop = list[i];
c(prop, st);
}
};
base.FunctionExpression = base.ArrowFunctionExpression = base.FunctionDeclaration;
base.SequenceExpression = base.TemplateLiteral = function (node, st, c) {
for (var i = 0, list = node.expressions; i < list.length; i += 1)
{
var expr = list[i];
c(expr, st, "Expression");
}
};
base.UnaryExpression = base.UpdateExpression = function (node, st, c) {
c(node.argument, st, "Expression");
};
base.BinaryExpression = base.LogicalExpression = function (node, st, c) {
c(node.left, st, "Expression");
c(node.right, st, "Expression");
};
base.AssignmentExpression = base.AssignmentPattern = function (node, st, c) {
c(node.left, st, "Pattern");
c(node.right, st, "Expression");
};
base.ConditionalExpression = function (node, st, c) {
c(node.test, st, "Expression");
c(node.consequent, st, "Expression");
c(node.alternate, st, "Expression");
};
base.NewExpression = base.CallExpression = function (node, st, c) {
c(node.callee, st, "Expression");
if (node.arguments)
{ for (var i = 0, list = node.arguments; i < list.length; i += 1)
{
var arg = list[i];
c(arg, st, "Expression");
} }
};
base.MemberExpression = function (node, st, c) {
c(node.object, st, "Expression");
if (node.computed) { c(node.property, st, "Expression"); }
};
base.ExportNamedDeclaration = base.ExportDefaultDeclaration = function (node, st, c) {
if (node.declaration)
{ c(node.declaration, st, node.type === "ExportNamedDeclaration" || node.declaration.id ? "Statement" : "Expression"); }
if (node.source) { c(node.source, st, "Expression"); }
};
base.ExportAllDeclaration = function (node, st, c) {
c(node.source, st, "Expression");
};
base.ImportDeclaration = function (node, st, c) {
for (var i = 0, list = node.specifiers; i < list.length; i += 1)
{
var spec = list[i];
c(spec, st);
}
c(node.source, st, "Expression");
};
base.ImportSpecifier = base.ImportDefaultSpecifier = base.ImportNamespaceSpecifier = base.Identifier = base.Literal = ignore;
base.TaggedTemplateExpression = function (node, st, c) {
c(node.tag, st, "Expression");
c(node.quasi, st, "Expression");
};
base.ClassDeclaration = base.ClassExpression = function (node, st, c) { return c(node, st, "Class"); };
base.Class = function (node, st, c) {
if (node.id) { c(node.id, st, "Pattern"); }
if (node.superClass) { c(node.superClass, st, "Expression"); }
c(node.body, st);
};
base.ClassBody = function (node, st, c) {
for (var i = 0, list = node.body; i < list.length; i += 1)
{
var elt = list[i];
c(elt, st);
}
};
base.MethodDefinition = base.Property = function (node, st, c) {
if (node.computed) { c(node.key, st, "Expression"); }
c(node.value, st, "Expression");
};
exports.simple = simple;
exports.ancestor = ancestor;
exports.recursive = recursive;
exports.full = full;
exports.fullAncestor = fullAncestor;
exports.findNodeAt = findNodeAt;
exports.findNodeAround = findNodeAround;
exports.findNodeAfter = findNodeAfter;
exports.findNodeBefore = findNodeBefore;
exports.make = make;
exports.base = base;
Object.defineProperty(exports, '__esModule', { value: true });
})));

View File

@ -6,257 +6,8 @@
"url": "https://github.com/acornjs/acorn/issues"
},
"bundleDependencies": false,
"contributors": [
{
"name": "List of Acorn contributors. Updated before every release."
},
{
"name": "Adrian Heine"
},
{
"name": "Adrian Rakovsky"
},
{
"name": "Alistair Braidwood"
},
{
"name": "Amila Welihinda"
},
{
"name": "Andres Suarez"
},
{
"name": "Angelo"
},
{
"name": "Aparajita Fishman"
},
{
"name": "Arian Stolwijk"
},
{
"name": "Artem Govorov"
},
{
"name": "Boopesh Mahendran"
},
{
"name": "Bradley Heinz"
},
{
"name": "Brandon Mills"
},
{
"name": "Charles Hughes"
},
{
"name": "Charmander"
},
{
"name": "Chris McKnight"
},
{
"name": "Conrad Irwin"
},
{
"name": "Daniel Tschinder"
},
{
"name": "David Bonnet"
},
{
"name": "Domenico Matteo"
},
{
"name": "ehmicky"
},
{
"name": "Eugene Obrezkov"
},
{
"name": "Felix Maier"
},
{
"name": "Forbes Lindesay"
},
{
"name": "Gilad Peleg"
},
{
"name": "impinball"
},
{
"name": "Ingvar Stepanyan"
},
{
"name": "Jackson Ray Hamilton"
},
{
"name": "Jesse McCarthy"
},
{
"name": "Jiaxing Wang"
},
{
"name": "Joel Kemp"
},
{
"name": "Johannes Herr"
},
{
"name": "John-David Dalton"
},
{
"name": "Jordan Klassen"
},
{
"name": "Jürg Lehni"
},
{
"name": "Kai Cataldo"
},
{
"name": "keeyipchan"
},
{
"name": "Keheliya Gallaba"
},
{
"name": "Kevin Irish"
},
{
"name": "Kevin Kwok"
},
{
"name": "krator"
},
{
"name": "laosb"
},
{
"name": "luckyzeng"
},
{
"name": "Marek"
},
{
"name": "Marijn Haverbeke"
},
{
"name": "Martin Carlberg"
},
{
"name": "Mat Garcia"
},
{
"name": "Mathias Bynens"
},
{
"name": "Mathieu 'p01' Henri"
},
{
"name": "Matthew Bastien"
},
{
"name": "Max Schaefer"
},
{
"name": "Max Zerzouri"
},
{
"name": "Mihai Bazon"
},
{
"name": "Mike Rennie"
},
{
"name": "naoh"
},
{
"name": "Nicholas C. Zakas"
},
{
"name": "Nick Fitzgerald"
},
{
"name": "Olivier Thomann"
},
{
"name": "Oskar Schöldström"
},
{
"name": "Paul Harper"
},
{
"name": "Peter Rust"
},
{
"name": "PlNG"
},
{
"name": "Prayag Verma"
},
{
"name": "ReadmeCritic"
},
{
"name": "r-e-d"
},
{
"name": "Renée Kooi"
},
{
"name": "Richard Gibson"
},
{
"name": "Rich Harris"
},
{
"name": "Sebastian McKenzie"
},
{
"name": "Shahar Soel"
},
{
"name": "Sheel Bedi"
},
{
"name": "Simen Bekkhus"
},
{
"name": "Teddy Katz"
},
{
"name": "Timothy Gu"
},
{
"name": "Toru Nagashima"
},
{
"name": "Victor Homyakov"
},
{
"name": "Wexpo Lyu"
},
{
"name": "zsjforcn"
}
],
"deprecated": false,
"description": "ECMAScript parser",
"devDependencies": {
"eslint": "^4.10.0",
"eslint-config-standard": "^10.2.1",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-node": "^5.2.1",
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-standard": "^3.0.1",
"rollup": "^0.45.0",
"rollup-plugin-buble": "^0.16.0",
"test262": "git+https://github.com/tc39/test262.git#3bfad28cc302fd4455badcfcbca7c5bb7ce41a72",
"test262-parser-runner": "^0.4.0",
"unicode-11.0.0": "^0.7.7"
},
"engines": {
"node": ">=0.4.0"
},
@ -267,35 +18,26 @@
{
"name": "Marijn Haverbeke",
"email": "marijnh@gmail.com",
"url": "http://marijnhaverbeke.nl"
"url": "https://marijnhaverbeke.nl"
},
{
"name": "Ingvar Stepanyan",
"email": "me@rreverser.com",
"url": "http://rreverser.com/"
"url": "https://rreverser.com/"
},
{
"name": "Adrian Heine",
"email": "http://adrianheine.de"
"url": "http://adrianheine.de"
}
],
"module": "dist/acorn.es.js",
"module": "dist/acorn.mjs",
"name": "acorn",
"repository": {
"type": "git",
"url": "git+https://github.com/acornjs/acorn.git"
},
"scripts": {
"build": "npm run build:main && npm run build:walk && npm run build:loose && npm run build:bin",
"build:bin": "rollup -c rollup/config.bin.js",
"build:loose": "rollup -c rollup/config.loose.js && rollup -c rollup/config.loose_es.js",
"build:main": "rollup -c rollup/config.main.js",
"build:walk": "rollup -c rollup/config.walk.js",
"lint": "eslint src/",
"prepare": "npm run build && node test/run.js && node test/lint.js",
"pretest": "npm run build:main && npm run build:loose",
"test": "node test/run.js && node test/lint.js",
"test:test262": "node bin/run_test262.js"
"prepare": "cd ..; npm run build:main && npm run build:bin"
},
"version": "5.7.3"
"version": "6.0.2"
}

View File

@ -150,8 +150,8 @@ There are seven ECMAScript 2018 syntax changes:
* Async iteration
* RegExp `s` flag
* RegExp named capture groups
* RegExp lookhehind assersions
* RegExp unicode property escapes
* RegExp lookbehind assertions
* RegExp Unicode property escapes
Espree supports all of them.

View File

@ -58,271 +58,38 @@
"use strict";
var astNodeTypes = require("./lib/ast-node-types"),
commentAttachment = require("./lib/comment-attachment"),
TokenTranslator = require("./lib/token-translator"),
acornJSX = require("acorn-jsx/inject"),
rawAcorn = require("acorn");
const acorn = require("acorn");
const jsx = require("acorn-jsx");
const astNodeTypes = require("./lib/ast-node-types");
const espree = require("./lib/espree");
// To initialize lazily.
const parsers = {
_regular: null,
_jsx: null,
var acorn = acornJSX(rawAcorn);
var DEFAULT_ECMA_VERSION = 5;
var lookahead,
extra,
lastToken;
/**
* Resets the extra object to its default.
* @returns {void}
* @private
*/
function resetExtra() {
extra = {
tokens: null,
range: false,
loc: false,
comment: false,
comments: [],
tolerant: false,
errors: [],
strict: false,
ecmaFeatures: {},
ecmaVersion: DEFAULT_ECMA_VERSION,
isModule: false
};
}
var tt = acorn.tokTypes,
getLineInfo = acorn.getLineInfo;
// custom type for JSX attribute values
tt.jsxAttrValueToken = {};
/**
* Normalize ECMAScript version from the initial config
* @param {number} ecmaVersion ECMAScript version from the initial config
* @returns {number} normalized ECMAScript version
*/
function normalizeEcmaVersion(ecmaVersion) {
if (typeof ecmaVersion === "number") {
var version = ecmaVersion;
// Calculate ECMAScript edition number from official year version starting with
// ES2015, which corresponds with ES6 (or a difference of 2009).
if (version >= 2015) {
version -= 2009;
get regular() {
if (this._regular === null) {
this._regular = acorn.Parser.extend(espree());
}
return this._regular;
},
switch (version) {
case 3:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
return version;
default:
throw new Error("Invalid ecmaVersion.");
get jsx() {
if (this._jsx === null) {
this._jsx = acorn.Parser.extend(jsx(), espree());
}
} else {
return DEFAULT_ECMA_VERSION;
return this._jsx;
},
get(options) {
const useJsx = Boolean(
options &&
options.ecmaFeatures &&
options.ecmaFeatures.jsx
);
return useJsx ? this.jsx : this.regular;
}
}
/**
* Determines if a node is valid given the set of ecmaFeatures.
* @param {ASTNode} node The node to check.
* @returns {boolean} True if the node is allowed, false if not.
* @private
*/
function isValidNode(node) {
switch (node.type) {
case "ImportDeclaration":
case "ExportNamedDeclaration":
case "ExportDefaultDeclaration":
case "ExportAllDeclaration":
return extra.isModule;
default:
return true;
}
}
/**
* Performs last-minute Esprima-specific compatibility checks and fixes.
* @param {ASTNode} result The node to check.
* @returns {ASTNode} The finished node.
* @private
* @this acorn.Parser
*/
function esprimaFinishNode(result) {
// ensure that parsed node was allowed through ecmaFeatures
if (!isValidNode(result)) {
this.unexpected(result.start);
}
// Acorn doesn't count the opening and closing backticks as part of templates
// so we have to adjust ranges/locations appropriately.
if (result.type === "TemplateElement") {
// additional adjustment needed if ${ is the last token
var terminalDollarBraceL = this.input.slice(result.end, result.end + 2) === "${";
if (result.range) {
result.range[0]--;
result.range[1] += (terminalDollarBraceL ? 2 : 1);
}
if (result.loc) {
result.loc.start.column--;
result.loc.end.column += (terminalDollarBraceL ? 2 : 1);
}
}
if (extra.attachComment) {
commentAttachment.processComment(result);
}
if (result.type.indexOf("Function") > -1 && !result.generator) {
result.generator = false;
}
return result;
}
/**
* Determines if a token is valid given the set of ecmaFeatures.
* @param {acorn.Parser} parser The parser to check.
* @returns {boolean} True if the token is allowed, false if not.
* @private
*/
function isValidToken(parser) {
var ecma = extra.ecmaFeatures;
var type = parser.type;
switch (type) {
case tt.jsxName:
case tt.jsxText:
case tt.jsxTagStart:
case tt.jsxTagEnd:
return ecma.jsx;
// https://github.com/ternjs/acorn/issues/363
case tt.regexp:
if (extra.ecmaVersion < 6 && parser.value.flags && parser.value.flags.indexOf("y") > -1) {
return false;
}
return true;
default:
return true;
}
}
/**
* Injects esprimaFinishNode into the finishNode process.
* @param {Function} finishNode Original finishNode function.
* @returns {ASTNode} The finished node.
* @private
*/
function wrapFinishNode(finishNode) {
return /** @this acorn.Parser */ function(node, type, pos, loc) {
var result = finishNode.call(this, node, type, pos, loc);
return esprimaFinishNode.call(this, result);
};
}
acorn.plugins.espree = function(instance) {
instance.extend("finishNode", wrapFinishNode);
instance.extend("finishNodeAt", wrapFinishNode);
instance.extend("next", function(next) {
return /** @this acorn.Parser */ function() {
if (!isValidToken(this)) {
this.unexpected();
}
return next.call(this);
};
});
instance.extend("parseTopLevel", function(parseTopLevel) {
return /** @this acorn.Parser */ function(node) {
if (extra.ecmaFeatures.impliedStrict && this.options.ecmaVersion >= 5) {
this.strict = true;
}
return parseTopLevel.call(this, node);
};
});
/**
* Overwrites the default raise method to throw Esprima-style errors.
* @param {int} pos The position of the error.
* @param {string} message The error message.
* @throws {SyntaxError} A syntax error.
* @returns {void}
*/
instance.raise = instance.raiseRecoverable = function(pos, message) {
var loc = getLineInfo(this.input, pos);
var err = new SyntaxError(message);
err.index = pos;
err.lineNumber = loc.line;
err.column = loc.column + 1; // acorn uses 0-based columns
throw err;
};
/**
* Overwrites the default unexpected method to throw Esprima-style errors.
* @param {int} pos The position of the error.
* @throws {SyntaxError} A syntax error.
* @returns {void}
*/
instance.unexpected = function(pos) {
var message = "Unexpected token";
if (pos !== null && pos !== undefined) {
this.pos = pos;
if (this.options.locations) {
while (this.pos < this.lineStart) {
this.lineStart = this.input.lastIndexOf("\n", this.lineStart - 2) + 1;
--this.curLine;
}
}
this.nextToken();
}
if (this.end > this.start) {
message += " " + this.input.slice(this.start, this.end);
}
this.raise(this.start, message);
};
/*
* Esprima-FB represents JSX strings as tokens called "JSXText", but Acorn-JSX
* uses regular tt.string without any distinction between this and regular JS
* strings. As such, we intercept an attempt to read a JSX string and set a flag
* on extra so that when tokens are converted, the next token will be switched
* to JSXText via onToken.
*/
instance.extend("jsx_readString", function(jsxReadString) {
return /** @this acorn.Parser */ function(quote) {
var result = jsxReadString.call(this, quote);
if (this.type === tt.string) {
extra.jsxAttrValueToken = true;
}
return result;
};
});
};
//------------------------------------------------------------------------------
@ -338,266 +105,30 @@ acorn.plugins.espree = function(instance) {
* @private
*/
function tokenize(code, options) {
var toString,
tokens,
impliedStrict,
translator = new TokenTranslator(tt, code);
const Parser = parsers.get(options);
toString = String;
if (typeof code !== "string" && !(code instanceof String)) {
code = toString(code);
// Ensure to collect tokens.
if (!options || options.tokens !== true) {
options = Object.assign({}, options, { tokens: true });
}
lookahead = null;
// Options matching.
options = Object.assign({}, options);
var acornOptions = {
ecmaVersion: DEFAULT_ECMA_VERSION,
plugins: {
espree: true
}
};
resetExtra();
// Of course we collect tokens here.
options.tokens = true;
extra.tokens = [];
extra.range = (typeof options.range === "boolean") && options.range;
acornOptions.ranges = extra.range;
extra.loc = (typeof options.loc === "boolean") && options.loc;
acornOptions.locations = extra.loc;
extra.comment = typeof options.comment === "boolean" && options.comment;
if (extra.comment) {
acornOptions.onComment = function() {
var comment = convertAcornCommentToEsprimaComment.apply(this, arguments);
extra.comments.push(comment);
};
}
extra.tolerant = typeof options.tolerant === "boolean" && options.tolerant;
acornOptions.ecmaVersion = extra.ecmaVersion = normalizeEcmaVersion(options.ecmaVersion);
// apply parsing flags
if (options.ecmaFeatures && typeof options.ecmaFeatures === "object") {
extra.ecmaFeatures = Object.assign({}, options.ecmaFeatures);
impliedStrict = extra.ecmaFeatures.impliedStrict;
extra.ecmaFeatures.impliedStrict = typeof impliedStrict === "boolean" && impliedStrict;
}
try {
var tokenizer = acorn.tokenizer(code, acornOptions);
while ((lookahead = tokenizer.getToken()).type !== tt.eof) {
translator.onToken(lookahead, extra);
}
// filterTokenLocation();
tokens = extra.tokens;
if (extra.comment) {
tokens.comments = extra.comments;
}
if (extra.tolerant) {
tokens.errors = extra.errors;
}
} catch (e) {
throw e;
}
return tokens;
return new Parser(options, code).tokenize();
}
//------------------------------------------------------------------------------
// Parser
//------------------------------------------------------------------------------
/**
* Converts an Acorn comment to a Esprima comment.
* @param {boolean} block True if it's a block comment, false if not.
* @param {string} text The text of the comment.
* @param {int} start The index at which the comment starts.
* @param {int} end The index at which the comment ends.
* @param {Location} startLoc The location at which the comment starts.
* @param {Location} endLoc The location at which the comment ends.
* @returns {Object} The comment object.
* @private
*/
function convertAcornCommentToEsprimaComment(block, text, start, end, startLoc, endLoc) {
var comment = {
type: block ? "Block" : "Line",
value: text
};
if (typeof start === "number") {
comment.start = start;
comment.end = end;
comment.range = [start, end];
}
if (typeof startLoc === "object") {
comment.loc = {
start: startLoc,
end: endLoc
};
}
return comment;
}
/**
* Parses the given code.
* @param {string} code The code to tokenize.
* @param {Object} options Options defining how to tokenize.
* @returns {ASTNode} The "Program" AST node.
* @throws {SyntaxError} If the input code is invalid.
* @private
*/
function parse(code, options) {
var program,
toString = String,
translator,
impliedStrict,
acornOptions = {
ecmaVersion: DEFAULT_ECMA_VERSION,
plugins: {
espree: true
}
};
lastToken = null;
if (typeof code !== "string" && !(code instanceof String)) {
code = toString(code);
}
resetExtra();
commentAttachment.reset();
if (typeof options !== "undefined") {
extra.range = (typeof options.range === "boolean") && options.range;
extra.loc = (typeof options.loc === "boolean") && options.loc;
extra.attachComment = (typeof options.attachComment === "boolean") && options.attachComment;
if (extra.loc && options.source !== null && options.source !== undefined) {
extra.source = toString(options.source);
}
if (typeof options.tokens === "boolean" && options.tokens) {
extra.tokens = [];
translator = new TokenTranslator(tt, code);
}
if (typeof options.comment === "boolean" && options.comment) {
extra.comment = true;
extra.comments = [];
}
if (typeof options.tolerant === "boolean" && options.tolerant) {
extra.errors = [];
}
if (extra.attachComment) {
extra.range = true;
extra.comments = [];
commentAttachment.reset();
}
acornOptions.ecmaVersion = extra.ecmaVersion = normalizeEcmaVersion(options.ecmaVersion);
if (options.sourceType === "module") {
extra.isModule = true;
// modules must be in 6 at least
if (acornOptions.ecmaVersion < 6) {
acornOptions.ecmaVersion = 6;
extra.ecmaVersion = 6;
}
acornOptions.sourceType = "module";
}
// apply parsing flags after sourceType to allow overriding
if (options.ecmaFeatures && typeof options.ecmaFeatures === "object") {
extra.ecmaFeatures = Object.assign({}, options.ecmaFeatures);
impliedStrict = extra.ecmaFeatures.impliedStrict;
extra.ecmaFeatures.impliedStrict = typeof impliedStrict === "boolean" && impliedStrict;
if (options.ecmaFeatures.globalReturn) {
acornOptions.allowReturnOutsideFunction = true;
}
}
acornOptions.onToken = function(token) {
if (extra.tokens) {
translator.onToken(token, extra);
}
if (token.type !== tt.eof) {
lastToken = token;
}
};
if (extra.attachComment || extra.comment) {
acornOptions.onComment = function() {
var comment = convertAcornCommentToEsprimaComment.apply(this, arguments);
extra.comments.push(comment);
if (extra.attachComment) {
commentAttachment.addComment(comment);
}
};
}
if (extra.range) {
acornOptions.ranges = true;
}
if (extra.loc) {
acornOptions.locations = true;
}
if (extra.ecmaFeatures.jsx) {
// Should process jsx plugin before espree plugin.
acornOptions.plugins = {
jsx: true,
espree: true
};
}
}
program = acorn.parse(code, acornOptions);
program.sourceType = extra.isModule ? "module" : "script";
if (extra.comment || extra.attachComment) {
program.comments = extra.comments;
}
if (extra.tokens) {
program.tokens = extra.tokens;
}
/*
* Adjust opening and closing position of program to match Esprima.
* Acorn always starts programs at range 0 whereas Esprima starts at the
* first AST node's start (the only real difference is when there's leading
* whitespace or leading comments). Acorn also counts trailing whitespace
* as part of the program whereas Esprima only counts up to the last token.
*/
if (program.range) {
program.range[0] = program.body.length ? program.body[0].range[0] : program.range[0];
program.range[1] = lastToken ? lastToken.range[1] : program.range[1];
}
if (program.loc) {
program.loc.start = program.body.length ? program.body[0].loc.start : program.loc.start;
program.loc.end = lastToken ? lastToken.loc.end : program.loc.end;
}
return program;
const Parser = parsers.get(options);
return new Parser(options, code).parse();
}
//------------------------------------------------------------------------------
@ -634,23 +165,5 @@ exports.Syntax = (function() {
/* istanbul ignore next */
exports.VisitorKeys = (function() {
var visitorKeys = require("./lib/visitor-keys");
var name,
keys = {};
if (typeof Object.create === "function") {
keys = Object.create(null);
}
for (name in visitorKeys) {
if (visitorKeys.hasOwnProperty(name)) {
keys[name] = visitorKeys[name];
}
}
if (typeof Object.freeze === "function") {
Object.freeze(keys);
}
return keys;
return require("eslint-visitor-keys").KEYS;
}());

View File

@ -0,0 +1,314 @@
"use strict";
const acorn = require("acorn");
const jsx = require("acorn-jsx");
const commentAttachment = require("./comment-attachment");
const TokenTranslator = require("./token-translator");
const DEFAULT_ECMA_VERSION = 5;
const STATE = Symbol("espree's internal state");
const ESPRIMA_FINISH_NODE = Symbol("espree's esprimaFinishNode");
const tokTypes = Object.assign({}, acorn.tokTypes, jsx.tokTypes);
/**
* Normalize ECMAScript version from the initial config
* @param {number} ecmaVersion ECMAScript version from the initial config
* @returns {number} normalized ECMAScript version
*/
function normalizeEcmaVersion(ecmaVersion) {
if (typeof ecmaVersion === "number") {
let version = ecmaVersion;
// Calculate ECMAScript edition number from official year version starting with
// ES2015, which corresponds with ES6 (or a difference of 2009).
if (version >= 2015) {
version -= 2009;
}
switch (version) {
case 3:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
return version;
default:
throw new Error("Invalid ecmaVersion.");
}
} else {
return DEFAULT_ECMA_VERSION;
}
}
/**
* Converts an Acorn comment to a Esprima comment.
* @param {boolean} block True if it's a block comment, false if not.
* @param {string} text The text of the comment.
* @param {int} start The index at which the comment starts.
* @param {int} end The index at which the comment ends.
* @param {Location} startLoc The location at which the comment starts.
* @param {Location} endLoc The location at which the comment ends.
* @returns {Object} The comment object.
* @private
*/
function convertAcornCommentToEsprimaComment(block, text, start, end, startLoc, endLoc) {
const comment = {
type: block ? "Block" : "Line",
value: text
};
if (typeof start === "number") {
comment.start = start;
comment.end = end;
comment.range = [start, end];
}
if (typeof startLoc === "object") {
comment.loc = {
start: startLoc,
end: endLoc
};
}
return comment;
}
module.exports = () => Parser => class Espree extends Parser {
constructor(options, code) {
if (typeof options !== "object" || options === null) {
options = {};
}
if (typeof code !== "string" && !(code instanceof String)) {
code = String(code);
}
const ecmaFeatures = options.ecmaFeatures || {};
const ecmaVersion = normalizeEcmaVersion(options.ecmaVersion);
const isModule = options.sourceType === "module";
const tokenTranslator =
options.tokens === true ?
new TokenTranslator(tokTypes, code) :
null;
// Initialize acorn parser.
super({
ecmaVersion: isModule ? Math.max(6, ecmaVersion) : ecmaVersion,
sourceType: isModule ? "module" : "script",
ranges: options.range === true || options.attachComment === true,
locations: options.loc === true,
// Truthy value is true for backward compatibility.
allowReturnOutsideFunction: Boolean(ecmaFeatures.globalReturn),
// Collect tokens
onToken: (token) => {
if (tokenTranslator) {
// Use `tokens`, `ecmaVersion`, and `jsxAttrValueToken` in the state.
tokenTranslator.onToken(token, this[STATE]);
}
if (token.type !== tokTypes.eof) {
this[STATE].lastToken = token;
}
},
// Collect comments
onComment: (block, text, start, end, startLoc, endLoc) => {
if (this[STATE].comments) {
const comment = convertAcornCommentToEsprimaComment(block, text, start, end, startLoc, endLoc);
this[STATE].comments.push(comment);
if (options.attachComment === true) {
commentAttachment.addComment(comment);
}
}
}
}, code);
// TODO: remove global state.
commentAttachment.reset();
// Initialize internal state.
this[STATE] = {
tokens: tokenTranslator ? [] : null,
comments: options.comment === true || options.attachComment === true ? [] : null,
attachComment: options.attachComment === true,
impliedStrict: ecmaFeatures.impliedStrict === true && this.options.ecmaVersion >= 5,
ecmaVersion: this.options.ecmaVersion,
jsxAttrValueToken: false,
lastToken: null
};
}
tokenize() {
do {
this.next();
} while (this.type !== tokTypes.eof);
const extra = this[STATE];
const tokens = extra.tokens;
if (extra.comments) {
tokens.comments = extra.comments;
}
return tokens;
}
finishNode(...args) {
const result = super.finishNode(...args);
return this[ESPRIMA_FINISH_NODE](result);
}
finishNodeAt(...args) {
const result = super.finishNodeAt(...args);
return this[ESPRIMA_FINISH_NODE](result);
}
parse() {
const extra = this[STATE];
const program = super.parse();
program.sourceType = this.options.sourceType;
if (extra.comments) {
program.comments = extra.comments;
}
if (extra.tokens) {
program.tokens = extra.tokens;
}
/*
* Adjust opening and closing position of program to match Esprima.
* Acorn always starts programs at range 0 whereas Esprima starts at the
* first AST node's start (the only real difference is when there's leading
* whitespace or leading comments). Acorn also counts trailing whitespace
* as part of the program whereas Esprima only counts up to the last token.
*/
if (program.range) {
program.range[0] = program.body.length ? program.body[0].range[0] : program.range[0];
program.range[1] = extra.lastToken ? extra.lastToken.range[1] : program.range[1];
}
if (program.loc) {
program.loc.start = program.body.length ? program.body[0].loc.start : program.loc.start;
program.loc.end = extra.lastToken ? extra.lastToken.loc.end : program.loc.end;
}
return program;
}
parseTopLevel(node) {
if (this[STATE].impliedStrict) {
this.strict = true;
}
return super.parseTopLevel(node);
}
/**
* Overwrites the default raise method to throw Esprima-style errors.
* @param {int} pos The position of the error.
* @param {string} message The error message.
* @throws {SyntaxError} A syntax error.
* @returns {void}
*/
raise(pos, message) {
const loc = acorn.getLineInfo(this.input, pos);
const err = new SyntaxError(message);
err.index = pos;
err.lineNumber = loc.line;
err.column = loc.column + 1; // acorn uses 0-based columns
throw err;
}
/**
* Overwrites the default raise method to throw Esprima-style errors.
* @param {int} pos The position of the error.
* @param {string} message The error message.
* @throws {SyntaxError} A syntax error.
* @returns {void}
*/
raiseRecoverable(pos, message) {
this.raise(pos, message);
}
/**
* Overwrites the default unexpected method to throw Esprima-style errors.
* @param {int} pos The position of the error.
* @throws {SyntaxError} A syntax error.
* @returns {void}
*/
unexpected(pos) {
let message = "Unexpected token";
if (pos !== null && pos !== void 0) {
this.pos = pos;
if (this.options.locations) {
while (this.pos < this.lineStart) {
this.lineStart = this.input.lastIndexOf("\n", this.lineStart - 2) + 1;
--this.curLine;
}
}
this.nextToken();
}
if (this.end > this.start) {
message += " " + this.input.slice(this.start, this.end);
}
this.raise(this.start, message);
}
/*
* Esprima-FB represents JSX strings as tokens called "JSXText", but Acorn-JSX
* uses regular tt.string without any distinction between this and regular JS
* strings. As such, we intercept an attempt to read a JSX string and set a flag
* on extra so that when tokens are converted, the next token will be switched
* to JSXText via onToken.
*/
jsx_readString(quote) { // eslint-disable-line camelcase
const result = super.jsx_readString(quote);
if (this.type === tokTypes.string) {
this[STATE].jsxAttrValueToken = true;
}
return result;
}
/**
* Performs last-minute Esprima-specific compatibility checks and fixes.
* @param {ASTNode} result The node to check.
* @returns {ASTNode} The finished node.
*/
[ESPRIMA_FINISH_NODE](result) {
// Acorn doesn't count the opening and closing backticks as part of templates
// so we have to adjust ranges/locations appropriately.
if (result.type === "TemplateElement") {
// additional adjustment needed if ${ is the last token
const terminalDollarBraceL = this.input.slice(result.end, result.end + 2) === "${";
if (result.range) {
result.range[0]--;
result.range[1] += (terminalDollarBraceL ? 2 : 1);
}
if (result.loc) {
result.loc.start.column--;
result.loc.end.column += (terminalDollarBraceL ? 2 : 1);
}
}
if (this[STATE].attachComment) {
commentAttachment.processComment(result);
}
if (result.type.indexOf("Function") > -1 && !result.generator) {
result.generator = false;
}
return result;
}
};

View File

@ -1,123 +0,0 @@
/**
* @fileoverview The visitor keys for the node types Espree supports
* @author Nicholas C. Zakas
*
* This file contains code from estraverse-fb.
*
* The MIT license. Copyright (c) 2014 Ingvar Stepanyan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
// None!
//------------------------------------------------------------------------------
// Public
//------------------------------------------------------------------------------
module.exports = {
// ECMAScript
AssignmentExpression: ["left", "right"],
AssignmentPattern: ["left", "right"],
ArrayExpression: ["elements"],
ArrayPattern: ["elements"],
ArrowFunctionExpression: ["params", "body"],
BlockStatement: ["body"],
BinaryExpression: ["left", "right"],
BreakStatement: ["label"],
CallExpression: ["callee", "arguments"],
CatchClause: ["param", "body"],
ClassBody: ["body"],
ClassDeclaration: ["id", "superClass", "body"],
ClassExpression: ["id", "superClass", "body"],
ConditionalExpression: ["test", "consequent", "alternate"],
ContinueStatement: ["label"],
DebuggerStatement: [],
DirectiveStatement: [],
DoWhileStatement: ["body", "test"],
EmptyStatement: [],
ExportAllDeclaration: ["source"],
ExportDefaultDeclaration: ["declaration"],
ExportNamedDeclaration: ["declaration", "specifiers", "source"],
ExportSpecifier: ["exported", "local"],
ExpressionStatement: ["expression"],
ForStatement: ["init", "test", "update", "body"],
ForInStatement: ["left", "right", "body"],
ForOfStatement: ["left", "right", "body"],
FunctionDeclaration: ["id", "params", "body"],
FunctionExpression: ["id", "params", "body"],
Identifier: [],
IfStatement: ["test", "consequent", "alternate"],
ImportDeclaration: ["specifiers", "source"],
ImportDefaultSpecifier: ["local"],
ImportNamespaceSpecifier: ["local"],
ImportSpecifier: ["imported", "local"],
Literal: [],
LabeledStatement: ["label", "body"],
LogicalExpression: ["left", "right"],
MemberExpression: ["object", "property"],
MetaProperty: ["meta", "property"],
MethodDefinition: ["key", "value"],
ModuleSpecifier: [],
NewExpression: ["callee", "arguments"],
ObjectExpression: ["properties"],
ObjectPattern: ["properties"],
Program: ["body"],
Property: ["key", "value"],
RestElement: [ "argument" ],
ReturnStatement: ["argument"],
SequenceExpression: ["expressions"],
SpreadElement: ["argument"],
Super: [],
SwitchStatement: ["discriminant", "cases"],
SwitchCase: ["test", "consequent"],
TaggedTemplateExpression: ["tag", "quasi"],
TemplateElement: [],
TemplateLiteral: ["quasis", "expressions"],
ThisExpression: [],
ThrowStatement: ["argument"],
TryStatement: ["block", "handler", "finalizer"],
UnaryExpression: ["argument"],
UpdateExpression: ["argument"],
VariableDeclaration: ["declarations"],
VariableDeclarator: ["id", "init"],
WhileStatement: ["test", "body"],
WithStatement: ["object", "body"],
YieldExpression: ["argument"],
// JSX
JSXIdentifier: [],
JSXNamespacedName: ["namespace", "name"],
JSXMemberExpression: ["object", "property"],
JSXEmptyExpression: [],
JSXExpressionContainer: ["expression"],
JSXElement: ["openingElement", "closingElement", "children"],
JSXClosingElement: ["name"],
JSXOpeningElement: ["name", "attributes"],
JSXAttribute: ["name", "value"],
JSXText: null,
JSXSpreadAttribute: ["argument"]
};

View File

@ -8,8 +8,9 @@
},
"bundleDependencies": false,
"dependencies": {
"acorn": "^5.6.0",
"acorn-jsx": "^4.1.1"
"acorn": "^6.0.2",
"acorn-jsx": "^5.0.0",
"eslint-visitor-keys": "^1.0.0"
},
"deprecated": false,
"description": "An Esprima-compatible JavaScript parser built on Acorn",
@ -18,13 +19,13 @@
"chai": "^1.10.0",
"eslint": "^2.13.1",
"eslint-config-eslint": "^3.0.0",
"eslint-release": "^0.11.1",
"eslint-release": "^1.0.0",
"esprima": "latest",
"esprima-fb": "^8001.2001.0-dev-harmony-fb",
"istanbul": "~0.2.6",
"json-diff": "~0.3.1",
"leche": "^1.0.1",
"mocha": "^2.0.1",
"nyc": "^13.0.1",
"regenerate": "~0.5.4",
"shelljs": "^0.3.0",
"shelljs-nodecli": "^0.1.1",
@ -54,16 +55,15 @@
"url": "git+https://github.com/eslint/espree.git"
},
"scripts": {
"alpharelease": "eslint-prerelease alpha",
"betarelease": "eslint-prerelease beta",
"browserify": "node Makefile.js browserify",
"ci-release": "eslint-ci-release",
"generate-alpharelease": "eslint-generate-prerelease alpha",
"generate-betarelease": "eslint-generate-prerelease beta",
"generate-rcrelease": "eslint-generate-prerelease rc",
"generate-regex": "node tools/generate-identifier-regex.js",
"gh-release": "eslint-gh-release",
"generate-release": "eslint-generate-release",
"lint": "node Makefile.js lint",
"rcrelease": "eslint-prerelease rc",
"release": "eslint-release",
"publish-release": "eslint-publish-release",
"test": "npm run-script lint && node Makefile.js test"
},
"version": "4.0.0"
"version": "4.1.0"
}

View File

@ -66,6 +66,7 @@ function ProgressBar(fmt, options) {
head : options.head || (options.complete || '=')
};
this.renderThrottle = options.renderThrottle !== 0 ? (options.renderThrottle || 16) : 0;
this.lastRender = -Infinity;
this.callback = options.callback || function () {};
this.tokens = {};
this.lastDraw = '';
@ -92,14 +93,12 @@ ProgressBar.prototype.tick = function(len, tokens){
this.curr += len
// schedule render
if (!this.renderThrottleTimeout) {
this.renderThrottleTimeout = setTimeout(this.render.bind(this), this.renderThrottle);
}
// try to render
this.render();
// progress complete
if (this.curr >= this.total) {
if (this.renderThrottleTimeout) this.render();
this.render();
this.complete = true;
this.terminate();
this.callback(this);
@ -116,17 +115,22 @@ ProgressBar.prototype.tick = function(len, tokens){
*/
ProgressBar.prototype.render = function (tokens) {
clearTimeout(this.renderThrottleTimeout);
this.renderThrottleTimeout = null;
if (tokens) this.tokens = tokens;
if (!this.stream.isTTY) return;
var now = Date.now();
var delta = now - this.lastRender;
if (delta < this.renderThrottle) {
return;
} else {
this.lastRender = now;
}
var ratio = this.curr / this.total;
ratio = Math.min(Math.max(ratio, 0), 1);
var percent = ratio * 100;
var percent = Math.floor(ratio * 100);
var incomplete, complete, completeLength;
var elapsed = new Date - this.start;
var eta = (percent == 100) ? 0 : elapsed * (this.total / this.curr - 1);

View File

@ -19,6 +19,10 @@
{
"name": "Andrew Rhyne",
"email": "rhyneandrew@gmail.com"
},
{
"name": "Marco Brack",
"email": "PapstDonB@Googlemail.com"
}
],
"dependencies": {},
@ -39,5 +43,5 @@
"type": "git",
"url": "git://github.com/visionmedia/node-progress.git"
},
"version": "2.0.0"
"version": "2.0.1"
}

View File

@ -69,7 +69,7 @@
"eslint-plugin-eslint-plugin": "^1.2.0",
"eslint-plugin-node": "^7.0.1",
"eslint-plugin-rulesdir": "^0.1.0",
"eslint-release": "^0.11.1",
"eslint-release": "^1.0.0",
"eslint-rule-composer": "^0.3.0",
"eslump": "^1.6.2",
"esprima": "^4.0.1",
@ -120,20 +120,20 @@
"url": "git+https://github.com/eslint/eslint.git"
},
"scripts": {
"alpharelease": "node Makefile.js prerelease -- alpha",
"betarelease": "node Makefile.js prerelease -- beta",
"browserify": "node Makefile.js browserify",
"ci-release": "node Makefile.js ciRelease",
"coveralls": "cat ./coverage/lcov.info | coveralls",
"docs": "node Makefile.js docs",
"fuzz": "node Makefile.js fuzz",
"generate-alpharelease": "node Makefile.js generatePrerelease -- alpha",
"generate-betarelease": "node Makefile.js generatePrerelease -- beta",
"generate-rcrelease": "node Makefile.js generatePrerelease -- rc",
"generate-release": "node Makefile.js generateRelease",
"gensite": "node Makefile.js gensite",
"lint": "node Makefile.js lint",
"perf": "node Makefile.js perf",
"profile": "beefy tests/bench/bench.js --open -- -t brfs -t ./tests/bench/xform-rules.js -r espree",
"rcrelease": "node Makefile.js prerelease -- rc",
"release": "node Makefile.js release",
"publish-release": "node Makefile.js publishRelease",
"test": "node Makefile.js test"
},
"version": "5.7.0"
"version": "5.8.0"
}