MCPcopy
hub / github.com/microsoft/pyright / _parseFunctionDef

Method _parseFunctionDef

server/src/parser/parser.ts:507–551  ·  view source on GitHub ↗
(asyncToken?: KeywordToken, decorators?: DecoratorNode[])

Source from the content-addressed store, hash-verified

505 // funcdef: 'def' NAME parameters ['->' test] ':' suite
506 // parameters: '(' [typedargslist] ')'
507 private _parseFunctionDef(asyncToken?: KeywordToken, decorators?: DecoratorNode[]): FunctionNode {
508 let defToken = this._getKeywordToken(KeywordType.Def);
509
510 let nameToken = this._getTokenIfIdentifier();
511 if (!nameToken) {
512 this._addError('Expected function name after "def"', defToken);
513 nameToken = new IdentifierToken(0, 0, '');
514 }
515
516 if (!this._consumeTokenIfType(TokenType.OpenParenthesis)) {
517 this._addError('Expected "("', this._peekToken());
518 }
519
520 let paramList = this._parseVarArgsList(TokenType.CloseParenthesis, true);
521
522 if (!this._consumeTokenIfType(TokenType.CloseParenthesis)) {
523 this._addError('Expected ")"', this._peekToken());
524 }
525
526 let returnType: ExpressionNode | undefined;
527 if (this._consumeTokenIfType(TokenType.Arrow)) {
528 returnType = this._parseTestExpression();
529 }
530
531 let suite = this._parseSuite();
532
533 let functionNode = new FunctionNode(defToken, new NameNode(nameToken), suite);
534 if (asyncToken) {
535 functionNode.isAsync = true;
536 functionNode.extend(asyncToken);
537 }
538 functionNode.parameters = paramList;
539 if (decorators) {
540 functionNode.decorators = decorators;
541 if (decorators.length > 0) {
542 functionNode.extend(decorators[0]);
543 }
544 }
545 if (returnType) {
546 functionNode.returnTypeAnnotation = this._parseTypeAnnotation(returnType);
547 functionNode.extend(functionNode.returnTypeAnnotation.rawExpression);
548 }
549
550 return functionNode;
551 }
552
553 // typedargslist: (
554 // tfpdef ['=' test] (',' tfpdef ['=' test])*

Callers 3

_parseStatementMethod · 0.95
_parseAsyncStatementMethod · 0.95
_parseDecoratedMethod · 0.95

Calls 10

_getKeywordTokenMethod · 0.95
_getTokenIfIdentifierMethod · 0.95
_addErrorMethod · 0.95
_consumeTokenIfTypeMethod · 0.95
_peekTokenMethod · 0.95
_parseVarArgsListMethod · 0.95
_parseTestExpressionMethod · 0.95
_parseSuiteMethod · 0.95
_parseTypeAnnotationMethod · 0.95
extendMethod · 0.80

Tested by

no test coverage detected