Skip to content

Commit

Permalink
fix bug in one-sided "this" type guard
Browse files Browse the repository at this point in the history
Summary:
"this" is a valid type guard parameter, but it was not being parsed properly. This caused the following to error:
```
type T = () => implies this is number;
```
This diff fixes this

Reviewed By: avp

Differential Revision: D68593722

fbshipit-source-id: e93b0a693e6826dfa73946e3a7e53e5ce7cda084
  • Loading branch information
panagosg7 authored and facebook-github-bot committed Jan 25, 2025
1 parent ee1ac30 commit 7af61db
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/Parser/JSParserImpl-flow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2345,7 +2345,7 @@ Optional<ESTree::Node *> JSParserImpl::parseReturnTypeAnnotationFlow(
if (!optType)
return None;

if (check(TokenKind::identifier)) {
if (check(TokenKind::identifier, TokenKind::rw_this)) {
// Validate the "implies" token was an identifier not a more complex type.
if (auto *generic = dyn_cast<ESTree::GenericTypeAnnotationNode>(*optType);
!(generic && !generic->_typeParameters)) {
Expand All @@ -2360,8 +2360,8 @@ Optional<ESTree::Node *> JSParserImpl::parseReturnTypeAnnotationFlow(
ESTree::Node *id = setLocation(
tok_,
tok_,
new (context_)
ESTree::IdentifierNode(tok_->getIdentifier(), nullptr, false));
new (context_) ESTree::IdentifierNode(
tok_->getResWordOrIdentifier(), nullptr, false));
advance(JSLexer::GrammarContext::Type);

// implies IdentifierName is TypeAnnotation
Expand Down
30 changes: 29 additions & 1 deletion test/Parser/flow/predicate-implies.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,35 @@ type T = (empty: mixed) => implies empty is number;
// CHECK-NEXT: "rest": null,
// CHECK-NEXT: "typeParameters": null
// CHECK-NEXT: }
// CHECK-NEXT: }
// CHECK-NEXT: },

type T = () => implies this is number;
// CHECK-NEXT: {
// CHECK-NEXT: "type": "TypeAlias",
// CHECK-NEXT: "id": {
// CHECK-NEXT: "type": "Identifier",
// CHECK-NEXT: "name": "T"
// CHECK-NEXT: },
// CHECK-NEXT: "typeParameters": null,
// CHECK-NEXT: "right": {
// CHECK-NEXT: "type": "FunctionTypeAnnotation",
// CHECK-NEXT: "params": [],
// CHECK-NEXT: "this": null,
// CHECK-NEXT: "returnType": {
// CHECK-NEXT: "type": "TypePredicate",
// CHECK-NEXT: "parameterName": {
// CHECK-NEXT: "type": "Identifier",
// CHECK-NEXT: "name": "this"
// CHECK-NEXT: },
// CHECK-NEXT: "typeAnnotation": {
// CHECK-NEXT: "type": "NumberTypeAnnotation"
// CHECK-NEXT: },
// CHECK-NEXT: "kind": "implies"
// CHECK-NEXT: },
// CHECK-NEXT: "rest": null,
// CHECK-NEXT: "typeParameters": null
// CHECK-NEXT: }
// CHECK-NEXT: }

// CHECK-NEXT: ]
// CHECK-NEXT: }
110 changes: 110 additions & 0 deletions test/Parser/flow/predicate-method-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,116 @@ class C { m(): this is T {} }
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK-NEXT: }
// CHECK-NEXT: },

class C { m(): implies x is T {} }
// CHECK-NEXT: {
// CHECK-NEXT: "type": "ClassDeclaration",
// CHECK-NEXT: "id": {
// CHECK-NEXT: "type": "Identifier",
// CHECK-NEXT: "name": "C"
// CHECK-NEXT: },
// CHECK-NEXT: "superClass": null,
// CHECK-NEXT: "body": {
// CHECK-NEXT: "type": "ClassBody",
// CHECK-NEXT: "body": [
// CHECK-NEXT: {
// CHECK-NEXT: "type": "MethodDefinition",
// CHECK-NEXT: "key": {
// CHECK-NEXT: "type": "Identifier",
// CHECK-NEXT: "name": "m"
// CHECK-NEXT: },
// CHECK-NEXT: "value": {
// CHECK-NEXT: "type": "FunctionExpression",
// CHECK-NEXT: "id": null,
// CHECK-NEXT: "params": [],
// CHECK-NEXT: "body": {
// CHECK-NEXT: "type": "BlockStatement",
// CHECK-NEXT: "body": []
// CHECK-NEXT: },
// CHECK-NEXT: "returnType": {
// CHECK-NEXT: "type": "TypeAnnotation",
// CHECK-NEXT: "typeAnnotation": {
// CHECK-NEXT: "type": "TypePredicate",
// CHECK-NEXT: "parameterName": {
// CHECK-NEXT: "type": "Identifier",
// CHECK-NEXT: "name": "x"
// CHECK-NEXT: },
// CHECK-NEXT: "typeAnnotation": {
// CHECK-NEXT: "type": "GenericTypeAnnotation",
// CHECK-NEXT: "id": {
// CHECK-NEXT: "type": "Identifier",
// CHECK-NEXT: "name": "T"
// CHECK-NEXT: },
// CHECK-NEXT: "typeParameters": null
// CHECK-NEXT: },
// CHECK-NEXT: "kind": "implies"
// CHECK-NEXT: }
// CHECK-NEXT: },
// CHECK-NEXT: "generator": false,
// CHECK-NEXT: "async": false
// CHECK-NEXT: },
// CHECK-NEXT: "kind": "method",
// CHECK-NEXT: "computed": false,
// CHECK-NEXT: "static": false
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK-NEXT: }
// CHECK-NEXT: },

class C { m(): implies this is T {} }
// CHECK-NEXT: {
// CHECK-NEXT: "type": "ClassDeclaration",
// CHECK-NEXT: "id": {
// CHECK-NEXT: "type": "Identifier",
// CHECK-NEXT: "name": "C"
// CHECK-NEXT: },
// CHECK-NEXT: "superClass": null,
// CHECK-NEXT: "body": {
// CHECK-NEXT: "type": "ClassBody",
// CHECK-NEXT: "body": [
// CHECK-NEXT: {
// CHECK-NEXT: "type": "MethodDefinition",
// CHECK-NEXT: "key": {
// CHECK-NEXT: "type": "Identifier",
// CHECK-NEXT: "name": "m"
// CHECK-NEXT: },
// CHECK-NEXT: "value": {
// CHECK-NEXT: "type": "FunctionExpression",
// CHECK-NEXT: "id": null,
// CHECK-NEXT: "params": [],
// CHECK-NEXT: "body": {
// CHECK-NEXT: "type": "BlockStatement",
// CHECK-NEXT: "body": []
// CHECK-NEXT: },
// CHECK-NEXT: "returnType": {
// CHECK-NEXT: "type": "TypeAnnotation",
// CHECK-NEXT: "typeAnnotation": {
// CHECK-NEXT: "type": "TypePredicate",
// CHECK-NEXT: "parameterName": {
// CHECK-NEXT: "type": "Identifier",
// CHECK-NEXT: "name": "this"
// CHECK-NEXT: },
// CHECK-NEXT: "typeAnnotation": {
// CHECK-NEXT: "type": "GenericTypeAnnotation",
// CHECK-NEXT: "id": {
// CHECK-NEXT: "type": "Identifier",
// CHECK-NEXT: "name": "T"
// CHECK-NEXT: },
// CHECK-NEXT: "typeParameters": null
// CHECK-NEXT: },
// CHECK-NEXT: "kind": "implies"
// CHECK-NEXT: }
// CHECK-NEXT: },
// CHECK-NEXT: "generator": false,
// CHECK-NEXT: "async": false
// CHECK-NEXT: },
// CHECK-NEXT: "kind": "method",
// CHECK-NEXT: "computed": false,
// CHECK-NEXT: "static": false
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK-NEXT: }
// CHECK-NEXT: }

// CHECK-NEXT: ]
Expand Down

0 comments on commit 7af61db

Please sign in to comment.