Skip to content

Commit 391803e

Browse files
authored
Merge pull request #214 from tree-sitter/object_is
Support predefined types in type predicates
2 parents 22167c2 + 5caf25e commit 391803e

File tree

9 files changed

+237863
-236121
lines changed

9 files changed

+237863
-236121
lines changed

common/corpus/types.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,30 @@ function isT(t: T): t is T {
876876
(type_identifier)))
877877
(statement_block (return_statement (true)))))
878878

879+
==================================
880+
Type predicate and predefined types
881+
==================================
882+
883+
function isFish(pet: Fish): pet is Fish {
884+
}
885+
886+
function isFish(object: Fish): object is Fish {
887+
}
888+
889+
---
890+
(program
891+
(function_declaration (identifier)
892+
(formal_parameters
893+
(required_parameter (identifier) (type_annotation (type_identifier))))
894+
(type_predicate_annotation (type_predicate (identifier) (type_identifier)))
895+
(statement_block))
896+
(function_declaration (identifier)
897+
(formal_parameters
898+
(required_parameter (identifier) (type_annotation (type_identifier))))
899+
(type_predicate_annotation (type_predicate (identifier) (type_identifier)))
900+
(statement_block))
901+
)
902+
879903
==================================
880904
Read-only arrays
881905
==================================

common/define-grammar.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,16 @@ module.exports = function defineGrammar(dialect) {
687687
)),
688688

689689
type_predicate: $ => seq(
690-
field('name', choice($.identifier, $.this)),
690+
field('name', choice(
691+
$.identifier,
692+
$.this,
693+
// Sometimes tree-sitter contextual lexing is not good enough to know
694+
// that 'object' in ':object is foo' is really an identifier and not
695+
// a predefined_type, so we must explicitely list all possibilities.
696+
// TODO: should we use '_reserved_identifier'? Should all the element in
697+
// 'predefined_type' be added to '_reserved_identifier'?
698+
alias($.predefined_type, $.identifier)
699+
)),
691700
'is',
692701
field('type', $._type)
693702
),

tsx/src/grammar.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8815,6 +8815,15 @@
88158815
{
88168816
"type": "SYMBOL",
88178817
"name": "this"
8818+
},
8819+
{
8820+
"type": "ALIAS",
8821+
"content": {
8822+
"type": "SYMBOL",
8823+
"name": "predefined_type"
8824+
},
8825+
"named": true,
8826+
"value": "identifier"
88188827
}
88198828
]
88208829
}

tsx/src/node-types.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2742,6 +2742,11 @@
27422742
}
27432743
}
27442744
},
2745+
{
2746+
"type": "identifier",
2747+
"named": true,
2748+
"fields": {}
2749+
},
27452750
{
27462751
"type": "if_statement",
27472752
"named": true,
@@ -6111,10 +6116,6 @@
61116116
"type": "hash_bang_line",
61126117
"named": true
61136118
},
6114-
{
6115-
"type": "identifier",
6116-
"named": true
6117-
},
61186119
{
61196120
"type": "if",
61206121
"named": false

0 commit comments

Comments
 (0)