add token match method and remove more dead code
This commit is contained in:
@@ -50,11 +50,11 @@ pp.flowParseDeclareFunction = function (node) {
|
||||
};
|
||||
|
||||
pp.flowParseDeclare = function (node) {
|
||||
if (this.state.type === tt._class) {
|
||||
if (this.match(tt._class)) {
|
||||
return this.flowParseDeclareClass(node);
|
||||
} else if (this.state.type === tt._function) {
|
||||
} else if (this.match(tt._function)) {
|
||||
return this.flowParseDeclareFunction(node);
|
||||
} else if (this.state.type === tt._var) {
|
||||
} else if (this.match(tt._var)) {
|
||||
return this.flowParseDeclareVariable(node);
|
||||
} else if (this.isContextual("module")) {
|
||||
return this.flowParseDeclareModule(node);
|
||||
@@ -73,7 +73,7 @@ pp.flowParseDeclareVariable = function (node) {
|
||||
pp.flowParseDeclareModule = function (node) {
|
||||
this.next();
|
||||
|
||||
if (this.state.type === tt.string) {
|
||||
if (this.match(tt.string)) {
|
||||
node.id = this.parseExprAtom();
|
||||
} else {
|
||||
node.id = this.parseIdent();
|
||||
@@ -82,7 +82,7 @@ pp.flowParseDeclareModule = function (node) {
|
||||
var bodyNode = node.body = this.startNode();
|
||||
var body = bodyNode.body = [];
|
||||
this.expect(tt.braceL);
|
||||
while (this.state.type !== tt.braceR) {
|
||||
while (!this.match(tt.braceR)) {
|
||||
var node2 = this.startNode();
|
||||
|
||||
// todo: declare check
|
||||
@@ -193,7 +193,7 @@ pp.flowParseTypeParameterInstantiation = function () {
|
||||
};
|
||||
|
||||
pp.flowParseObjectPropertyKey = function () {
|
||||
return (this.state.type === tt.num || this.state.type === tt.string) ? this.parseExprAtom() : this.parseIdent(true);
|
||||
return (this.match(tt.num) || this.match(tt.string)) ? this.parseExprAtom() : this.parseIdent(true);
|
||||
};
|
||||
|
||||
pp.flowParseObjectTypeIndexer = function (node, isStatic) {
|
||||
@@ -219,9 +219,9 @@ pp.flowParseObjectTypeMethodish = function (node) {
|
||||
}
|
||||
|
||||
this.expect(tt.parenL);
|
||||
while (this.state.type === tt.name) {
|
||||
while (this.match(tt.name)) {
|
||||
node.params.push(this.flowParseFunctionTypeParam());
|
||||
if (this.state.type !== tt.parenR) {
|
||||
if (!this.match(tt.parenR)) {
|
||||
this.expect(tt.comma);
|
||||
}
|
||||
}
|
||||
@@ -266,7 +266,7 @@ pp.flowParseObjectType = function (allowStatic) {
|
||||
|
||||
this.expect(tt.braceL);
|
||||
|
||||
while (this.state.type !== tt.braceR) {
|
||||
while (!this.match(tt.braceR)) {
|
||||
var startPos = this.state.start, startLoc = this.state.startLoc;
|
||||
node = this.startNode();
|
||||
if (allowStatic && this.isContextual("static")) {
|
||||
@@ -274,17 +274,17 @@ pp.flowParseObjectType = function (allowStatic) {
|
||||
isStatic = true;
|
||||
}
|
||||
|
||||
if (this.state.type === tt.bracketL) {
|
||||
if (this.match(tt.bracketL)) {
|
||||
nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic));
|
||||
} else if (this.state.type === tt.parenL || this.isRelational("<")) {
|
||||
} else if (this.match(tt.parenL) || this.isRelational("<")) {
|
||||
nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, allowStatic));
|
||||
} else {
|
||||
if (isStatic && this.state.type === tt.colon) {
|
||||
if (isStatic && this.match(tt.colon)) {
|
||||
propertyKey = this.parseIdent();
|
||||
} else {
|
||||
propertyKey = this.flowParseObjectPropertyKey();
|
||||
}
|
||||
if (this.isRelational("<") || this.state.type === tt.parenL) {
|
||||
if (this.isRelational("<") || this.match(tt.parenL)) {
|
||||
// This is a method property
|
||||
nodeStart.properties.push(this.flowParseObjectTypeMethod(startPos, startLoc, isStatic, propertyKey));
|
||||
} else {
|
||||
@@ -307,7 +307,7 @@ pp.flowParseObjectType = function (allowStatic) {
|
||||
};
|
||||
|
||||
pp.flowObjectTypeSemicolon = function () {
|
||||
if (!this.eat(tt.semi) && !this.eat(tt.comma) && this.state.type !== tt.braceR) {
|
||||
if (!this.eat(tt.semi) && !this.eat(tt.comma) && !this.match(tt.braceR)) {
|
||||
this.unexpected();
|
||||
}
|
||||
};
|
||||
@@ -344,9 +344,9 @@ pp.flowParseTupleType = function () {
|
||||
node.types = [];
|
||||
this.expect(tt.bracketL);
|
||||
// We allow trailing commas
|
||||
while (this.state.pos < this.input.length && this.state.type !== tt.bracketR) {
|
||||
while (this.state.pos < this.input.length && !this.match(tt.bracketR)) {
|
||||
node.types.push(this.flowParseType());
|
||||
if (this.state.type === tt.bracketR) break;
|
||||
if (this.match(tt.bracketR)) break;
|
||||
this.expect(tt.comma);
|
||||
}
|
||||
this.expect(tt.bracketR);
|
||||
@@ -367,9 +367,9 @@ pp.flowParseFunctionTypeParam = function () {
|
||||
|
||||
pp.flowParseFunctionTypeParams = function () {
|
||||
var ret = { params: [], rest: null };
|
||||
while (this.state.type === tt.name) {
|
||||
while (this.match(tt.name)) {
|
||||
ret.params.push(this.flowParseFunctionTypeParam());
|
||||
if (this.state.type !== tt.parenR) {
|
||||
if (!this.match(tt.parenR)) {
|
||||
this.expect(tt.comma);
|
||||
}
|
||||
}
|
||||
@@ -445,8 +445,8 @@ pp.flowParsePrimaryType = function () {
|
||||
this.next();
|
||||
|
||||
// Check to see if this is actually a grouped type
|
||||
if (this.state.type !== tt.parenR && this.state.type !== tt.ellipsis) {
|
||||
if (this.state.type === tt.name) {
|
||||
if (!this.match(tt.parenR) && !this.match(tt.ellipsis)) {
|
||||
if (this.match(tt.name)) {
|
||||
var token = this.lookahead().type;
|
||||
isGroupedType = token !== tt.question && token !== tt.colon;
|
||||
} else {
|
||||
@@ -511,7 +511,7 @@ pp.flowParsePrimaryType = function () {
|
||||
pp.flowParsePostfixType = function () {
|
||||
var node = this.startNode();
|
||||
var type = node.elementType = this.flowParsePrimaryType();
|
||||
if (this.state.type === tt.bracketL) {
|
||||
if (this.match(tt.bracketL)) {
|
||||
this.expect(tt.bracketL);
|
||||
this.expect(tt.bracketR);
|
||||
return this.finishNode(node, "ArrayTypeAnnotation");
|
||||
@@ -573,7 +573,7 @@ pp.flowParseTypeAnnotatableIdentifier = function (requireTypeAnnotation, canBeOp
|
||||
isOptionalParam = true;
|
||||
}
|
||||
|
||||
if (requireTypeAnnotation || this.state.type === tt.colon) {
|
||||
if (requireTypeAnnotation || this.match(tt.colon)) {
|
||||
ident.typeAnnotation = this.flowParseTypeAnnotation();
|
||||
this.finishNode(ident, ident.type);
|
||||
}
|
||||
@@ -590,7 +590,7 @@ export default function (instance) {
|
||||
// function name(): string {}
|
||||
instance.extend("parseFunctionBody", function (inner) {
|
||||
return function (node, allowExpression) {
|
||||
if (this.state.type === tt.colon && !allowExpression) {
|
||||
if (this.match(tt.colon) && !allowExpression) {
|
||||
// if allowExpression is true then we're parsing an arrow function and if
|
||||
// there's a return type then it's been handled elsewhere
|
||||
node.returnType = this.flowParseTypeAnnotation();
|
||||
@@ -603,7 +603,7 @@ export default function (instance) {
|
||||
instance.extend("parseStatement", function (inner) {
|
||||
return function (declaration, topLevel) {
|
||||
// strict mode handling of `interface` since it's a reserved word
|
||||
if (this.strict && this.state.type === tt.name && this.state.value === "interface") {
|
||||
if (this.strict && this.match(tt.name) && this.state.value === "interface") {
|
||||
var node = this.startNode();
|
||||
this.next();
|
||||
return this.flowParseInterface(node);
|
||||
@@ -617,10 +617,10 @@ export default function (instance) {
|
||||
return function (node, expr) {
|
||||
if (expr.type === "Identifier") {
|
||||
if (expr.name === "declare") {
|
||||
if (this.state.type === tt._class || this.state.type === tt.name || this.state.type === tt._function || this.state.type === tt._var) {
|
||||
if (this.match(tt._class) || this.match(tt.name) || this.match(tt._function) || this.match(tt._var)) {
|
||||
return this.flowParseDeclare(node);
|
||||
}
|
||||
} else if (this.state.type === tt.name) {
|
||||
} else if (this.match(tt.name)) {
|
||||
if (expr.name === "interface") {
|
||||
return this.flowParseInterface(node);
|
||||
} else if (expr.name === "type") {
|
||||
@@ -641,12 +641,12 @@ export default function (instance) {
|
||||
|
||||
instance.extend("parseParenItem", function () {
|
||||
return function (node, startLoc, startPos, forceArrow?) {
|
||||
if (this.state.type === tt.colon) {
|
||||
if (this.match(tt.colon)) {
|
||||
var typeCastNode = this.startNodeAt(startLoc, startPos);
|
||||
typeCastNode.expression = node;
|
||||
typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
|
||||
|
||||
if (forceArrow && this.state.type !== tt.arrow) {
|
||||
if (forceArrow && !this.match(tt.arrow)) {
|
||||
this.unexpected();
|
||||
}
|
||||
|
||||
@@ -735,7 +735,7 @@ export default function (instance) {
|
||||
return function (allowEmpty, refShorthandDefaultPos) {
|
||||
var container = this.startNode();
|
||||
var node = inner.call(this, allowEmpty, refShorthandDefaultPos);
|
||||
if (this.state.type === tt.colon) {
|
||||
if (this.match(tt.colon)) {
|
||||
container._exprListItem = true;
|
||||
container.expression = node;
|
||||
container.typeAnnotation = this.flowParseTypeAnnotation();
|
||||
@@ -748,7 +748,7 @@ export default function (instance) {
|
||||
|
||||
instance.extend("parseClassProperty", function (inner) {
|
||||
return function (node) {
|
||||
if (this.state.type === tt.colon) {
|
||||
if (this.match(tt.colon)) {
|
||||
node.typeAnnotation = this.flowParseTypeAnnotation();
|
||||
}
|
||||
return inner.call(this, node);
|
||||
@@ -757,7 +757,7 @@ export default function (instance) {
|
||||
|
||||
instance.extend("isClassProperty", function (inner) {
|
||||
return function () {
|
||||
return this.state.type === tt.colon || inner.call(this);
|
||||
return this.match(tt.colon) || inner.call(this);
|
||||
};
|
||||
});
|
||||
|
||||
@@ -801,7 +801,7 @@ export default function (instance) {
|
||||
var typeParameters;
|
||||
if (this.isRelational("<")) {
|
||||
typeParameters = this.flowParseTypeParameterDeclaration();
|
||||
if (this.state.type !== tt.parenL) this.unexpected();
|
||||
if (!this.match(tt.parenL)) this.unexpected();
|
||||
}
|
||||
inner.apply(this, arguments);
|
||||
prop.value.typeParameters = typeParameters;
|
||||
@@ -813,7 +813,7 @@ export default function (instance) {
|
||||
if (this.eat(tt.question)) {
|
||||
param.optional = true;
|
||||
}
|
||||
if (this.state.type === tt.colon) {
|
||||
if (this.match(tt.colon)) {
|
||||
param.typeAnnotation = this.flowParseTypeAnnotation();
|
||||
}
|
||||
this.finishNode(param, param.type);
|
||||
@@ -825,7 +825,7 @@ export default function (instance) {
|
||||
return function (node) {
|
||||
node.importKind = "value";
|
||||
|
||||
var kind = (this.state.type === tt._typeof ? "typeof" : (this.isContextual("type") ? "type" : null));
|
||||
var kind = (this.match(tt._typeof) ? "typeof" : (this.isContextual("type") ? "type" : null));
|
||||
if (kind) {
|
||||
var lh = this.lookahead();
|
||||
if ((lh.type === tt.name && lh.value !== "from") || lh.type === tt.braceL || lh.type === tt.star) {
|
||||
@@ -852,7 +852,7 @@ export default function (instance) {
|
||||
instance.extend("parseVarHead", function (inner) {
|
||||
return function (decl) {
|
||||
inner.call(this, decl);
|
||||
if (this.state.type === tt.colon) {
|
||||
if (this.match(tt.colon)) {
|
||||
decl.id.typeAnnotation = this.flowParseTypeAnnotation();
|
||||
this.finishNode(decl.id, decl.id.type);
|
||||
}
|
||||
@@ -862,7 +862,7 @@ export default function (instance) {
|
||||
// var foo = (async (): number => {});
|
||||
instance.extend("parseAsyncArrowFromCallExpression", function (inner) {
|
||||
return function (node, call) {
|
||||
if (this.state.type === tt.colon) {
|
||||
if (this.match(tt.colon)) {
|
||||
node.returnType = this.flowParseTypeAnnotation();
|
||||
}
|
||||
|
||||
@@ -881,7 +881,7 @@ export default function (instance) {
|
||||
this.expect(tt.parenR);
|
||||
|
||||
let node = this.startNodeAt(startPos, startLoc);
|
||||
if (this.state.type === tt.colon) node.returnType = this.flowParseTypeAnnotation();
|
||||
if (this.match(tt.colon)) node.returnType = this.flowParseTypeAnnotation();
|
||||
this.expect(tt.arrow);
|
||||
return this.parseArrowExpression(node, [], isAsync);
|
||||
} else {
|
||||
@@ -890,7 +890,7 @@ export default function (instance) {
|
||||
|
||||
var state = this.state.clone();
|
||||
|
||||
if (this.state.type === tt.colon) {
|
||||
if (this.match(tt.colon)) {
|
||||
try {
|
||||
return this.parseParenItem(node, startPos, startLoc, true);
|
||||
} catch (err) {
|
||||
|
||||
@@ -186,7 +186,7 @@ function getQualifiedJSXName(object) {
|
||||
|
||||
pp.jsxParseIdentifier = function() {
|
||||
var node = this.startNode();
|
||||
if (this.state.type === tt.jsxName) {
|
||||
if (this.match(tt.jsxName)) {
|
||||
node.name = this.state.value;
|
||||
} else if (this.state.type.keyword) {
|
||||
node.name = this.state.type.keyword;
|
||||
@@ -268,7 +268,7 @@ pp.jsxParseEmptyExpression = function() {
|
||||
pp.jsxParseExpressionContainer = function() {
|
||||
var node = this.startNode();
|
||||
this.next();
|
||||
if (this.state.type === tt.braceR) {
|
||||
if (this.match(tt.braceR)) {
|
||||
node.expression = this.jsxParseEmptyExpression();
|
||||
} else {
|
||||
node.expression = this.parseExpression();
|
||||
@@ -298,7 +298,7 @@ pp.jsxParseOpeningElementAt = function(startPos, startLoc) {
|
||||
var node = this.startNodeAt(startPos, startLoc);
|
||||
node.attributes = [];
|
||||
node.name = this.jsxParseElementName();
|
||||
while (this.state.type !== tt.slash && this.state.type !== tt.jsxTagEnd) {
|
||||
while (!this.match(tt.slash) && !this.match(tt.jsxTagEnd)) {
|
||||
node.attributes.push(this.jsxParseAttribute());
|
||||
}
|
||||
node.selfClosing = this.eat(tt.slash);
|
||||
@@ -360,7 +360,7 @@ pp.jsxParseElementAt = function(startPos, startLoc) {
|
||||
node.openingElement = openingElement;
|
||||
node.closingElement = closingElement;
|
||||
node.children = children;
|
||||
if (this.state.type === tt.relational && this.state.value === "<") {
|
||||
if (this.match(tt.relational) && this.state.value === "<") {
|
||||
this.raise(this.state.start, "Adjacent JSX elements must be wrapped in an enclosing tag");
|
||||
}
|
||||
return this.finishNode(node, "JSXElement");
|
||||
@@ -377,9 +377,9 @@ pp.jsxParseElement = function() {
|
||||
export default function(instance) {
|
||||
instance.extend("parseExprAtom", function(inner) {
|
||||
return function(refShortHandDefaultPos) {
|
||||
if (this.state.type === tt.jsxText)
|
||||
if (this.match(tt.jsxText))
|
||||
return this.parseLiteral(this.state.value);
|
||||
else if (this.state.type === tt.jsxTagStart)
|
||||
else if (this.match(tt.jsxTagStart))
|
||||
return this.jsxParseElement();
|
||||
else
|
||||
return inner.call(this, refShortHandDefaultPos);
|
||||
@@ -414,13 +414,13 @@ export default function(instance) {
|
||||
|
||||
instance.extend("updateContext", function(inner) {
|
||||
return function(prevType) {
|
||||
if (this.state.type === tt.braceL) {
|
||||
if (this.match(tt.braceL)) {
|
||||
var curContext = this.curContext();
|
||||
if (curContext === tc.j_oTag) this.state.context.push(tc.b_expr);
|
||||
else if (curContext === tc.j_expr) this.state.context.push(tc.b_tmpl);
|
||||
else inner.call(this, prevType);
|
||||
this.state.exprAllowed = true;
|
||||
} else if (this.state.type === tt.slash && prevType === tt.jsxTagStart) {
|
||||
} else if (this.match(tt.slash) && prevType === tt.jsxTagStart) {
|
||||
this.state.context.length -= 2; // do not consider JSX expr -> JSX open tag -> ... anymore
|
||||
this.state.context.push(tc.j_cTag); // reconsider as closing tag context
|
||||
this.state.exprAllowed = false;
|
||||
|
||||
Reference in New Issue
Block a user