Merge pull request #214 from tree-sitter/object_is

Support predefined types in type predicates
pull/315/head
Yoann Padioleau 2022-06-24 15:05:21 +07:00 committed by GitHub
commit 391803ed80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 237863 additions and 236121 deletions

@ -876,6 +876,30 @@ function isT(t: T): t is T {
(type_identifier)))
(statement_block (return_statement (true)))))
==================================
Type predicate and predefined types
==================================
function isFish(pet: Fish): pet is Fish {
}
function isFish(object: Fish): object is Fish {
}
---
(program
(function_declaration (identifier)
(formal_parameters
(required_parameter (identifier) (type_annotation (type_identifier))))
(type_predicate_annotation (type_predicate (identifier) (type_identifier)))
(statement_block))
(function_declaration (identifier)
(formal_parameters
(required_parameter (identifier) (type_annotation (type_identifier))))
(type_predicate_annotation (type_predicate (identifier) (type_identifier)))
(statement_block))
)
==================================
Read-only arrays
==================================

@ -687,7 +687,16 @@ module.exports = function defineGrammar(dialect) {
)),
type_predicate: $ => seq(
field('name', choice($.identifier, $.this)),
field('name', choice(
$.identifier,
$.this,
// Sometimes tree-sitter contextual lexing is not good enough to know
// that 'object' in ':object is foo' is really an identifier and not
// a predefined_type, so we must explicitely list all possibilities.
// TODO: should we use '_reserved_identifier'? Should all the element in
// 'predefined_type' be added to '_reserved_identifier'?
alias($.predefined_type, $.identifier)
)),
'is',
field('type', $._type)
),

@ -8815,6 +8815,15 @@
{
"type": "SYMBOL",
"name": "this"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "predefined_type"
},
"named": true,
"value": "identifier"
}
]
}

@ -2742,6 +2742,11 @@
}
}
},
{
"type": "identifier",
"named": true,
"fields": {}
},
{
"type": "if_statement",
"named": true,
@ -6111,10 +6116,6 @@
"type": "hash_bang_line",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "if",
"named": false

249618
tsx/src/parser.c vendored

File diff suppressed because it is too large Load Diff

@ -8811,6 +8811,15 @@
{
"type": "SYMBOL",
"name": "this"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "predefined_type"
},
"named": true,
"value": "identifier"
}
]
}

@ -2734,6 +2734,11 @@
}
}
},
{
"type": "identifier",
"named": true,
"fields": {}
},
{
"type": "if_statement",
"named": true,
@ -6122,10 +6127,6 @@
"type": "hash_bang_line",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "if",
"named": false

224284
typescript/src/parser.c vendored

File diff suppressed because it is too large Load Diff

@ -7,13 +7,14 @@
"resolved" "https://registry.npmjs.org/nan/-/nan-2.15.0.tgz"
"version" "2.15.0"
"tree-sitter-cli@^0.20.0":
"integrity" "sha512-4D1qapWbJXZ5rrSUGM5rcw5Vuq/smzn9KbiFRhlON6KeuuXjra+KAtDYVrDgAoLIG4ku+jbEEGrJxCptUGi3dg=="
"resolved" "https://registry.npmjs.org/tree-sitter-cli/-/tree-sitter-cli-0.20.0.tgz"
"version" "0.20.0"
"tree-sitter-cli@^0.20.6":
"integrity" "sha512-tjbAeuGSMhco/EnsThjWkQbDIYMDmdkWsTPsa/NJAW7bjaki9P7oM9TkLxfdlnm4LXd1wR5wVSM2/RTLtZbm6A=="
"resolved" "https://registry.npmjs.org/tree-sitter-cli/-/tree-sitter-cli-0.20.6.tgz"
"version" "0.20.6"
"tree-sitter-javascript@github:tree-sitter/tree-sitter-javascript#fdeb68a":
"resolved" "git+ssh://git@github.com/tree-sitter/tree-sitter-javascript.git#fdeb68ac8d2bd5a78b943528bb68ceda3aade2eb"
"integrity" "sha512-pJzc45yGS62TRS8isYd4GLhzq3jvkBZo5qdIRCk+P2sgqnQ7UCIx8og9G8Ca0DSJTNN7UeNWeLvtTTBSODDH6Q=="
"resolved" "git+ssh://git@github.com/tree-sitter/tree-sitter-javascript.git"
"version" "0.20.0"
dependencies:
"nan" "^2.12.1"