function Parser::parseMethodDefinition
Parses a method definition
Return value
Node\MethodDefinition|null
2 calls to Parser::parseMethodDefinition()
- Parser::parseClassElement in vendor/
mck89/ peast/ lib/ Peast/ Syntax/ Parser.php - Parses a class elements
- Parser::parsePropertyDefinition in vendor/
mck89/ peast/ lib/ Peast/ Syntax/ Parser.php - Parses a property in an object literal
File
-
vendor/
mck89/ peast/ lib/ Peast/ Syntax/ Parser.php, line 2591
Class
- Parser
- Parser class
Namespace
Peast\SyntaxCode
protected function parseMethodDefinition() {
$state = $this->scanner
->getState();
$generator = $error = $async = false;
$position = null;
$kind = Node\MethodDefinition::KIND_METHOD;
if ($token = $this->scanner
->consume("get")) {
$position = $token;
$kind = Node\MethodDefinition::KIND_GET;
}
elseif ($token = $this->scanner
->consume("set")) {
$position = $token;
$kind = Node\MethodDefinition::KIND_SET;
}
elseif ($token = $this->scanner
->consume("*")) {
$position = $token;
$error = true;
$generator = true;
}
elseif ($this->features->asyncAwait && ($token = $this->checkAsyncFunctionStart(false))) {
$this->scanner
->consumeToken();
$position = $token;
$error = true;
$async = true;
if ($this->features->asyncIterationGenerators && $this->scanner
->consume("*")) {
$generator = true;
}
}
//Handle the case where get, set and async are methods name and not the
//definition of a getter/setter or the start of an async function
if (($kind !== Node\MethodDefinition::KIND_METHOD || $async && !$generator) && $this->scanner
->consume("(")) {
$this->scanner
->setState($state);
$kind = Node\MethodDefinition::KIND_METHOD;
$error = $async = false;
}
if ($prop = $this->parseClassElementName()) {
if (!$position) {
$position = isset($prop[2]) ? $prop[2] : $prop[0];
}
if ($tokenFn = $this->scanner
->consume("(")) {
if ($generator || $async) {
$flags = array(
null,
);
if ($generator) {
$flags["allowYield"] = true;
}
if ($async) {
$flags["allowAwait"] = true;
}
}
else {
$flags = null;
}
$error = true;
$params = array();
if ($kind === Node\MethodDefinition::KIND_SET) {
$params = $this->isolateContext(null, "parseBindingElement");
if ($params) {
$params = array(
$params,
);
}
}
elseif ($kind === Node\MethodDefinition::KIND_METHOD) {
$params = $this->isolateContext($flags, "parseFormalParameterList");
}
if ($params !== null && $this->scanner
->consume(")") && ($tokenBodyStart = $this->scanner
->consume("{")) && (($body = $this->isolateContext($flags, "parseFunctionBody")) || true) && $this->scanner
->consume("}")) {
if ($prop[0] instanceof Node\Identifier && $prop[0]->getName() === "constructor") {
$kind = Node\MethodDefinition::KIND_CONSTRUCTOR;
}
$body->location->start = $tokenBodyStart->location->start;
$body->location->end = $this->scanner
->getPosition();
$nodeFn = $this->createNode("FunctionExpression", $tokenFn);
$nodeFn->setParams($params);
$nodeFn->setBody($body);
$nodeFn->setGenerator($generator);
$nodeFn->setAsync($async);
$node = $this->createNode("MethodDefinition", $position);
$node->setKey($prop[0]);
$node->setValue($this->completeNode($nodeFn));
$node->setKind($kind);
$node->setComputed($prop[1]);
return $this->completeNode($node);
}
}
}
elseif ($this->features->classFields && $async && !$generator) {
$this->scanner
->setState($state);
$error = $async = false;
}
if ($error) {
$this->error();
}
else {
$this->scanner
->setState($state);
}
return null;
}