tree-sitter-typescript
Advanced tools
Comparing version 0.20.3 to 0.20.4
@@ -0,4 +1,3 @@ | ||
# Checklist | ||
Checklist: | ||
- [ ] All tests pass in CI. | ||
@@ -5,0 +4,0 @@ - [ ] There are sufficient tests for the new fix/feature. |
@@ -0,12 +1,8 @@ | ||
const JavaScript = require('tree-sitter-javascript/grammar'); | ||
module.exports = function defineGrammar(dialect) { | ||
return grammar(require('tree-sitter-javascript/grammar'), { | ||
return grammar(JavaScript, { | ||
name: dialect, | ||
externals: ($, previous) => previous.concat([ | ||
// Allow the external scanner to tell whether it is parsing an expression | ||
// or a type by checking the validity of this binary operator. This is | ||
// needed because the rules for automatic semicolon insertion are | ||
// slightly different when parsing types. Any binary-only operator would | ||
// work. | ||
'||', | ||
$._function_signature_automatic_semicolon, | ||
@@ -39,3 +35,3 @@ ]), | ||
$.type_predicate, | ||
$.readonly_type | ||
$.readonly_type, | ||
], | ||
@@ -58,2 +54,3 @@ [$.mapped_type_clause, $.primary_expression], | ||
[$._type_query_member_expression, $.member_expression], | ||
[$.member_expression, $._type_query_member_expression_in_type_annotation], | ||
[$._type_query_member_expression, $.primary_expression], | ||
@@ -68,3 +65,8 @@ [$._type_query_subscript_expression, $.subscript_expression], | ||
[$.literal_type, $.pattern], | ||
[$.predefined_type, $.pattern] | ||
[$.predefined_type, $.pattern], | ||
[$.call_expression, $._type_query_call_expression], | ||
[$.call_expression, $._type_query_call_expression_in_type_annotation], | ||
[$.new_expression, $.primary_expression], | ||
[$.meta_property, $.primary_expression], | ||
[$.construct_signature, $._property_name], | ||
]), | ||
@@ -76,3 +78,2 @@ | ||
[$.call_expression, $.instantiation_expression, $.binary_expression, $.update_expression], | ||
[$.call_expression, $.binary_expression, $.type_assertion], | ||
[$.call_expression, $.instantiation_expression, $.binary_expression, $.await_expression], | ||
@@ -85,7 +86,3 @@ | ||
[$.nested_identifier, $.nested_type_identifier], | ||
[$.nested_identifier, $.member_expression], | ||
[$.primary_expression, $.array_type], | ||
[$.primary_expression, $.array_type, $.tuple_type], | ||
[$._call_signature, $.function_type], | ||
@@ -96,12 +93,7 @@ [$._call_signature, $.constructor_type], | ||
[$.jsx_opening_element, $.type_parameter], | ||
[$.jsx_opening_element, $.type_parameter, $._primary_type], | ||
[$.jsx_opening_element, $.generic_type], | ||
[$.jsx_namespace_name, $._primary_type], | ||
[$.primary_expression, $._parameter_name], | ||
[$.primary_expression, $._parameter_name, $.predefined_type], | ||
[$.primary_expression, $._parameter_name, $._primary_type], | ||
[$.primary_expression, $._parameter_name, $.array_type, $.tuple_type], | ||
[$.primary_expression, $.literal_type], | ||
[$.primary_expression, $.literal_type, $.pattern], | ||
[$.primary_expression, $.literal_type, $.rest_pattern], | ||
@@ -113,17 +105,9 @@ [$.primary_expression, $.predefined_type, $.rest_pattern], | ||
[$.primary_expression, $.pattern, $._primary_type], | ||
[$.primary_expression, $.pattern, $.predefined_type], | ||
[$._parameter_name, $.predefined_type], | ||
[$._parameter_name, $._primary_type], | ||
[$._parameter_name, $.assignment_expression], | ||
[$._parameter_name, $.pattern], | ||
[$.pattern, $._primary_type], | ||
[$.pattern, $.predefined_type], | ||
[$.optional_tuple_parameter, $._primary_type], | ||
[$.optional_tuple_parameter, $._primary_type, $.primary_expression], | ||
[$.rest_pattern, $._primary_type, $.primary_expression], | ||
[$.rest_pattern, $._primary_type], | ||
[$.object, $.object_type], | ||
[$.object, $._property_name], | ||
[$.object, $.object_pattern, $.object_type], | ||
@@ -133,3 +117,2 @@ [$.object, $.object_pattern, $._property_name], | ||
[$.object_pattern, $.object_type], | ||
[$.object_pattern, $._property_name], | ||
@@ -140,9 +123,9 @@ [$.array, $.tuple_type], | ||
[$.template_literal_type, $.template_string] | ||
[$.template_literal_type, $.template_string], | ||
]), | ||
inline: ($, previous) => previous | ||
.filter(rule => ![ | ||
.filter((rule) => ![ | ||
'_formal_parameter', | ||
'_call_signature' | ||
'_call_signature', | ||
].includes(rule.name)) | ||
@@ -155,5 +138,8 @@ .concat([ | ||
rules: { | ||
public_field_definition: $ => seq( | ||
optional('declare'), | ||
optional($.accessibility_modifier), | ||
public_field_definition: ($) => seq( | ||
repeat(field('decorator', $.decorator)), | ||
optional(choice( | ||
seq('declare', optional($.accessibility_modifier)), | ||
seq($.accessibility_modifier, optional('declare')), | ||
)), | ||
choice( | ||
@@ -167,7 +153,7 @@ seq(optional('static'), optional($.override_modifier), optional('readonly')), | ||
field('type', optional($.type_annotation)), | ||
optional($._initializer) | ||
optional($._initializer), | ||
), | ||
// override original catch_clause, add optional type annotation | ||
catch_clause: $ => seq( | ||
catch_clause: ($) => seq( | ||
'catch', | ||
@@ -179,3 +165,3 @@ optional( | ||
'parameter', | ||
choice($.identifier, $._destructuring_pattern) | ||
choice($.identifier, $._destructuring_pattern), | ||
), | ||
@@ -187,13 +173,13 @@ optional( | ||
), | ||
')' | ||
) | ||
')', | ||
), | ||
), | ||
field('body', $.statement_block) | ||
field('body', $.statement_block), | ||
), | ||
call_expression: $ => choice( | ||
call_expression: ($) => choice( | ||
prec('call', seq( | ||
field('function', $.expression), | ||
field('function', choice($.expression, $.import)), | ||
field('type_arguments', optional($.type_arguments)), | ||
field('arguments', choice($.arguments, $.template_string)) | ||
field('arguments', choice($.arguments, $.template_string)), | ||
)), | ||
@@ -204,13 +190,20 @@ prec('member', seq( | ||
field('type_arguments', optional($.type_arguments)), | ||
field('arguments', $.arguments) | ||
)) | ||
field('arguments', $.arguments), | ||
)), | ||
), | ||
new_expression: $ => prec.right('new', seq( | ||
new_expression: ($) => prec.right('new', seq( | ||
'new', | ||
field('constructor', $.primary_expression), | ||
field('type_arguments', optional($.type_arguments)), | ||
field('arguments', optional($.arguments)) | ||
field('arguments', optional($.arguments)), | ||
)), | ||
assignment_expression: ($) => prec.right('assign', seq( | ||
optional('using'), | ||
field('left', choice($.parenthesized_expression, $._lhs_expression)), | ||
'=', | ||
field('right', $.expression), | ||
)), | ||
_augmented_assignment_lhs: ($, previous) => choice(previous, $.non_null_expression), | ||
@@ -237,4 +230,4 @@ | ||
choices.push($.type_assertion); | ||
choices.push(...previous.members.filter(member => | ||
member.name !== '_jsx_element' | ||
choices.push(...previous.members.filter((member) => | ||
member.name !== '_jsx_element', | ||
)); | ||
@@ -250,3 +243,3 @@ } else if (dialect === 'tsx') { | ||
_jsx_start_opening_element: $ => seq( | ||
_jsx_start_opening_element: ($) => seq( | ||
'<', | ||
@@ -258,3 +251,3 @@ optional( | ||
$._jsx_identifier, | ||
$.jsx_namespace_name | ||
$.jsx_namespace_name, | ||
)), | ||
@@ -266,4 +259,4 @@ seq( | ||
)), | ||
field('type_arguments', optional($.type_arguments)) | ||
) | ||
field('type_arguments', optional($.type_arguments)), | ||
), | ||
), | ||
@@ -276,32 +269,32 @@ repeat(field('attribute', $._jsx_attribute)), | ||
// This rule is only referenced by expression when the dialect is 'tsx' | ||
jsx_opening_element: $ => prec.dynamic(-1, seq( | ||
jsx_opening_element: ($) => prec.dynamic(-1, seq( | ||
$._jsx_start_opening_element, | ||
'>' | ||
'>', | ||
)), | ||
// tsx only. See jsx_opening_element. | ||
jsx_self_closing_element: $ => prec.dynamic(-1, seq( | ||
jsx_self_closing_element: ($) => prec.dynamic(-1, seq( | ||
$._jsx_start_opening_element, | ||
'/>' | ||
'/>', | ||
)), | ||
export_specifier: ($, previous) => seq( | ||
export_specifier: (_, previous) => seq( | ||
optional(choice('type', 'typeof')), | ||
previous | ||
previous, | ||
), | ||
_import_identifier: $ => choice($.identifier, alias('type', $.identifier)), | ||
_import_identifier: ($) => choice($.identifier, alias('type', $.identifier)), | ||
import_specifier: ($, previous) => seq( | ||
import_specifier: ($) => seq( | ||
optional(choice('type', 'typeof')), | ||
choice( | ||
field('name', $._import_identifier), | ||
seq( | ||
field('name', choice($._module_export_name, alias('type', $.identifier))), | ||
'as', | ||
field('alias', $._import_identifier) | ||
), | ||
)), | ||
field('name', $._import_identifier), | ||
seq( | ||
field('name', choice($._module_export_name, alias('type', $.identifier))), | ||
'as', | ||
field('alias', $._import_identifier), | ||
), | ||
)), | ||
import_clause: ($, previous) => choice( | ||
import_clause: ($) => choice( | ||
$.namespace_import, | ||
@@ -315,9 +308,9 @@ $.named_imports, | ||
$.namespace_import, | ||
$.named_imports | ||
) | ||
)) | ||
) | ||
$.named_imports, | ||
), | ||
)), | ||
), | ||
), | ||
import_statement: $ => seq( | ||
import_statement: ($) => seq( | ||
'import', | ||
@@ -328,5 +321,5 @@ optional(choice('type', 'typeof')), | ||
$.import_require_clause, | ||
field('source', $.string) | ||
field('source', $.string), | ||
), | ||
$._semicolon | ||
$._semicolon, | ||
), | ||
@@ -341,3 +334,3 @@ | ||
optional($._from_clause), | ||
$._semicolon | ||
$._semicolon, | ||
), | ||
@@ -348,11 +341,11 @@ seq('export', '=', $.expression, $._semicolon), | ||
non_null_expression: $ => prec.left('unary', seq( | ||
$.expression, '!' | ||
non_null_expression: ($) => prec.left('unary', seq( | ||
$.expression, '!', | ||
)), | ||
variable_declarator: $ => choice( | ||
variable_declarator: ($) => choice( | ||
seq( | ||
field('name', choice($.identifier, $._destructuring_pattern)), | ||
field('type', optional($.type_annotation)), | ||
optional($._initializer) | ||
optional($._initializer), | ||
), | ||
@@ -362,7 +355,7 @@ prec('declaration', seq( | ||
'!', | ||
field('type', $.type_annotation) | ||
)) | ||
field('type', $.type_annotation), | ||
)), | ||
), | ||
method_signature: $ => seq( | ||
method_signature: ($) => seq( | ||
optional($.accessibility_modifier), | ||
@@ -376,29 +369,30 @@ optional('static'), | ||
optional('?'), | ||
$._call_signature | ||
$._call_signature, | ||
), | ||
abstract_method_signature: $ => seq( | ||
abstract_method_signature: ($) => seq( | ||
optional($.accessibility_modifier), | ||
'abstract', | ||
optional($.override_modifier), | ||
optional(choice('get', 'set', '*')), | ||
field('name', $._property_name), | ||
optional('?'), | ||
$._call_signature | ||
$._call_signature, | ||
), | ||
parenthesized_expression: ($, previous) => seq( | ||
parenthesized_expression: ($) => seq( | ||
'(', | ||
choice( | ||
seq($.expression, field('type', optional($.type_annotation))), | ||
$.sequence_expression | ||
$.sequence_expression, | ||
), | ||
')' | ||
')', | ||
), | ||
_formal_parameter: $ => choice( | ||
_formal_parameter: ($) => choice( | ||
$.required_parameter, | ||
$.optional_parameter | ||
$.optional_parameter, | ||
), | ||
function_signature: $ => seq( | ||
function_signature: ($) => seq( | ||
optional('async'), | ||
@@ -411,7 +405,10 @@ 'function', | ||
class_body: $ => seq( | ||
class_body: ($) => seq( | ||
'{', | ||
repeat(choice( | ||
$.decorator, | ||
seq($.method_definition, optional($._semicolon)), | ||
seq( | ||
repeat(field('decorator', $.decorator)), | ||
$.method_definition, | ||
optional($._semicolon), | ||
), | ||
// As it happens for functions, the semicolon insertion should not | ||
@@ -432,11 +429,12 @@ // happen if a block follows the closing paren, because then it's a | ||
$.method_signature, | ||
$.public_field_definition | ||
$.public_field_definition, | ||
), | ||
choice($._semicolon, ',') | ||
) | ||
choice($._semicolon, ','), | ||
), | ||
';', | ||
)), | ||
'}' | ||
'}', | ||
), | ||
method_definition: $ => prec.left(seq( | ||
method_definition: ($) => prec.left(seq( | ||
optional($.accessibility_modifier), | ||
@@ -451,3 +449,3 @@ optional('static'), | ||
$._call_signature, | ||
field('body', $.statement_block) | ||
field('body', $.statement_block), | ||
)), | ||
@@ -465,23 +463,23 @@ | ||
$.import_alias, | ||
$.ambient_declaration | ||
$.ambient_declaration, | ||
), | ||
type_assertion: $ => prec.left('unary', seq( | ||
type_assertion: ($) => prec.left('unary', seq( | ||
$.type_arguments, | ||
$.expression | ||
$.expression, | ||
)), | ||
as_expression: $ => prec.left('binary', seq( | ||
as_expression: ($) => prec.left('binary', seq( | ||
$.expression, | ||
'as', | ||
choice('const', $._type) | ||
choice('const', $._type), | ||
)), | ||
satisfies_expression: $ => prec.left('binary', seq( | ||
satisfies_expression: ($) => prec.left('binary', seq( | ||
$.expression, | ||
'satisfies', | ||
$._type | ||
$._type, | ||
)), | ||
instantiation_expression: $ => prec('instantiation', seq( | ||
instantiation_expression: ($) => prec('instantiation', seq( | ||
$.expression, | ||
@@ -491,8 +489,8 @@ field('type_arguments', $.type_arguments), | ||
class_heritage: $ => choice( | ||
class_heritage: ($) => choice( | ||
seq($.extends_clause, optional($.implements_clause)), | ||
$.implements_clause | ||
$.implements_clause, | ||
), | ||
import_require_clause: $ => seq( | ||
import_require_clause: ($) => seq( | ||
$.identifier, | ||
@@ -503,6 +501,6 @@ '=', | ||
field('source', $.string), | ||
')' | ||
')', | ||
), | ||
extends_clause: $ => seq( | ||
extends_clause: ($) => seq( | ||
'extends', | ||
@@ -512,13 +510,13 @@ commaSep1($._extends_clause_single), | ||
_extends_clause_single: $ => prec('extends', seq( | ||
field('value', $.expression), | ||
field('type_arguments', optional($.type_arguments)) | ||
_extends_clause_single: ($) => prec('extends', seq( | ||
field('value', $.expression), | ||
field('type_arguments', optional($.type_arguments)), | ||
)), | ||
implements_clause: $ => seq( | ||
implements_clause: ($) => seq( | ||
'implements', | ||
commaSep1($._type) | ||
commaSep1($._type), | ||
), | ||
ambient_declaration: $ => seq( | ||
ambient_declaration: ($) => seq( | ||
'declare', | ||
@@ -528,7 +526,7 @@ choice( | ||
seq('global', $.statement_block), | ||
seq('module', '.', alias($.identifier, $.property_identifier), ':', $._type, $._semicolon) | ||
) | ||
seq('module', '.', alias($.identifier, $.property_identifier), ':', $._type, $._semicolon), | ||
), | ||
), | ||
class: $ => prec('literal', seq( | ||
class: ($) => prec('literal', seq( | ||
repeat(field('decorator', $.decorator)), | ||
@@ -539,6 +537,6 @@ 'class', | ||
optional($.class_heritage), | ||
field('body', $.class_body) | ||
field('body', $.class_body), | ||
)), | ||
abstract_class_declaration: $ => prec('declaration', seq( | ||
abstract_class_declaration: ($) => prec('declaration', seq( | ||
repeat(field('decorator', $.decorator)), | ||
@@ -550,6 +548,6 @@ 'abstract', | ||
optional($.class_heritage), | ||
field('body', $.class_body) | ||
field('body', $.class_body), | ||
)), | ||
class_declaration: $ => prec.left('declaration', seq( | ||
class_declaration: ($) => prec.left('declaration', seq( | ||
repeat(field('decorator', $.decorator)), | ||
@@ -561,23 +559,23 @@ 'class', | ||
field('body', $.class_body), | ||
optional($._automatic_semicolon) | ||
optional($._automatic_semicolon), | ||
)), | ||
module: $ => seq( | ||
module: ($) => seq( | ||
'module', | ||
$._module | ||
$._module, | ||
), | ||
internal_module: $ => seq( | ||
internal_module: ($) => seq( | ||
'namespace', | ||
$._module | ||
$._module, | ||
), | ||
_module: $ => prec.right(seq( | ||
_module: ($) => prec.right(seq( | ||
field('name', choice($.string, $.identifier, $.nested_identifier)), | ||
// On .d.ts files "declare module foo" desugars to "declare module foo {}", | ||
// hence why it is optional here | ||
field('body', optional($.statement_block)) | ||
field('body', optional($.statement_block)), | ||
)), | ||
import_alias: $ => seq( | ||
import_alias: ($) => seq( | ||
'import', | ||
@@ -587,12 +585,12 @@ $.identifier, | ||
choice($.identifier, $.nested_identifier), | ||
$._semicolon | ||
$._semicolon, | ||
), | ||
nested_type_identifier: $ => prec('member', seq( | ||
nested_type_identifier: ($) => prec('member', seq( | ||
field('module', choice($.identifier, $.nested_identifier)), | ||
'.', | ||
field('name', $._type_identifier) | ||
field('name', $._type_identifier), | ||
)), | ||
interface_declaration: $ => seq( | ||
interface_declaration: ($) => seq( | ||
'interface', | ||
@@ -602,6 +600,6 @@ field('name', $._type_identifier), | ||
optional($.extends_type_clause), | ||
field('body', $.object_type) | ||
field('body', alias($.object_type, $.interface_body)), | ||
), | ||
extends_type_clause: $ => seq( | ||
extends_type_clause: ($) => seq( | ||
'extends', | ||
@@ -612,13 +610,13 @@ commaSep1(field('type', choice( | ||
$.generic_type, | ||
))) | ||
))), | ||
), | ||
enum_declaration: $ => seq( | ||
enum_declaration: ($) => seq( | ||
optional('const'), | ||
'enum', | ||
field('name', $.identifier), | ||
field('body', $.enum_body) | ||
field('body', $.enum_body), | ||
), | ||
enum_body: $ => seq( | ||
enum_body: ($) => seq( | ||
'{', | ||
@@ -628,15 +626,15 @@ optional(seq( | ||
field('name', $._property_name), | ||
$.enum_assignment | ||
$.enum_assignment, | ||
)), | ||
optional(',') | ||
optional(','), | ||
)), | ||
'}' | ||
'}', | ||
), | ||
enum_assignment: $ => seq( | ||
enum_assignment: ($) => seq( | ||
field('name', $._property_name), | ||
$._initializer | ||
$._initializer, | ||
), | ||
type_alias_declaration: $ => seq( | ||
type_alias_declaration: ($) => seq( | ||
'type', | ||
@@ -647,27 +645,27 @@ field('name', $._type_identifier), | ||
field('value', $._type), | ||
$._semicolon | ||
$._semicolon, | ||
), | ||
accessibility_modifier: $ => choice( | ||
accessibility_modifier: (_) => choice( | ||
'public', | ||
'private', | ||
'protected' | ||
'protected', | ||
), | ||
override_modifier: _ => 'override', | ||
override_modifier: (_) => 'override', | ||
required_parameter: $ => seq( | ||
required_parameter: ($) => seq( | ||
$._parameter_name, | ||
field('type', optional($.type_annotation)), | ||
optional($._initializer) | ||
optional($._initializer), | ||
), | ||
optional_parameter: $ => seq( | ||
optional_parameter: ($) => seq( | ||
$._parameter_name, | ||
'?', | ||
field('type', optional($.type_annotation)), | ||
optional($._initializer) | ||
optional($._initializer), | ||
), | ||
_parameter_name: $ => seq( | ||
_parameter_name: ($) => seq( | ||
repeat(field('decorator', $.decorator)), | ||
@@ -677,19 +675,53 @@ optional($.accessibility_modifier), | ||
optional('readonly'), | ||
field('pattern', choice($.pattern, $.this)) | ||
field('pattern', choice($.pattern, $.this)), | ||
), | ||
omitting_type_annotation: $ => seq('-?:', $._type), | ||
opting_type_annotation: $ => seq('?:', $._type), | ||
type_annotation: $ => seq(':', $._type), | ||
omitting_type_annotation: ($) => seq('-?:', $._type), | ||
adding_type_annotation: ($) => seq('+?:', $._type), | ||
opting_type_annotation: ($) => seq('?:', $._type), | ||
type_annotation: ($) => seq( | ||
':', | ||
choice( | ||
$._type, | ||
alias($._type_query_member_expression_in_type_annotation, $.member_expression), | ||
alias($._type_query_call_expression_in_type_annotation, $.call_expression), | ||
), | ||
), | ||
asserts: $ => seq( | ||
// Oh boy | ||
// The issue is these special type queries need a lower relative precedence than the normal ones, | ||
// since these are used in type annotations whereas the other ones are used where `typeof` is | ||
// required beforehand. This allows for parsing of annotations such as | ||
// foo: import('x').y.z; | ||
// but was a nightmare to get working. | ||
_type_query_member_expression_in_type_annotation: ($) => seq( | ||
field('object', choice( | ||
$.import, | ||
alias($._type_query_member_expression_in_type_annotation, $.member_expression), | ||
alias($._type_query_call_expression_in_type_annotation, $.call_expression), | ||
)), | ||
'.', | ||
field('property', choice( | ||
$.private_property_identifier, | ||
alias($.identifier, $.property_identifier), | ||
)), | ||
), | ||
_type_query_call_expression_in_type_annotation: ($) => seq( | ||
field('function', choice( | ||
$.import, | ||
alias($._type_query_member_expression_in_type_annotation, $.member_expression), | ||
)), | ||
field('arguments', $.arguments), | ||
), | ||
asserts: ($) => seq( | ||
'asserts', | ||
choice($.type_predicate, $.identifier, $.this) | ||
choice($.type_predicate, $.identifier, $.this), | ||
), | ||
asserts_annotation: $ => seq( | ||
seq(':', $.asserts) | ||
asserts_annotation: ($) => seq( | ||
seq(':', $.asserts), | ||
), | ||
_type: $ => choice( | ||
_type: ($) => choice( | ||
$._primary_type, | ||
@@ -699,20 +731,20 @@ $.function_type, | ||
$.constructor_type, | ||
$.infer_type | ||
$.infer_type, | ||
), | ||
tuple_parameter: $ => seq( | ||
tuple_parameter: ($) => seq( | ||
field('name', choice($.identifier, $.rest_pattern)), | ||
field('type', $.type_annotation) | ||
field('type', $.type_annotation), | ||
), | ||
optional_tuple_parameter: $ => seq( | ||
optional_tuple_parameter: ($) => seq( | ||
field('name', $.identifier), | ||
'?', | ||
field('type', $.type_annotation) | ||
field('type', $.type_annotation), | ||
), | ||
optional_type: $ => seq($._type, '?'), | ||
rest_type: $ => seq('...', $._type), | ||
optional_type: ($) => seq($._type, '?'), | ||
rest_type: ($) => seq('...', $._type), | ||
_tuple_type_member: $ => choice( | ||
_tuple_type_member: ($) => choice( | ||
alias($.tuple_parameter, $.required_parameter), | ||
@@ -725,3 +757,3 @@ alias($.optional_tuple_parameter, $.optional_parameter), | ||
constructor_type: $ => prec.left(seq( | ||
constructor_type: ($) => prec.left(seq( | ||
optional('abstract'), | ||
@@ -732,6 +764,6 @@ 'new', | ||
'=>', | ||
field('type', $._type) | ||
field('type', $._type), | ||
)), | ||
_primary_type: $ => choice( | ||
_primary_type: ($) => choice( | ||
$.parenthesized_type, | ||
@@ -755,17 +787,18 @@ $.predefined_type, | ||
$.intersection_type, | ||
$.union_type | ||
$.union_type, | ||
'const', | ||
), | ||
template_type: $ => seq('${', choice($._primary_type, $.infer_type), '}'), | ||
template_type: ($) => seq('${', choice($._primary_type, $.infer_type), '}'), | ||
template_literal_type: $ => seq( | ||
template_literal_type: ($) => seq( | ||
'`', | ||
repeat(choice( | ||
$._template_chars, | ||
$.template_type | ||
$.template_type, | ||
)), | ||
'`' | ||
'`', | ||
), | ||
infer_type: $ => prec.right(seq( | ||
infer_type: ($) => prec.right(seq( | ||
'infer', | ||
@@ -775,7 +808,7 @@ $._type_identifier, | ||
'extends', | ||
$._type | ||
)) | ||
$._type, | ||
)), | ||
)), | ||
conditional_type: $ => prec.right(seq( | ||
conditional_type: ($) => prec.right(seq( | ||
field('left', $._type), | ||
@@ -787,34 +820,34 @@ 'extends', | ||
':', | ||
field('alternative', $._type) | ||
field('alternative', $._type), | ||
)), | ||
generic_type: $ => prec('call', seq( | ||
generic_type: ($) => prec('call', seq( | ||
field('name', choice( | ||
$._type_identifier, | ||
$.nested_type_identifier | ||
$.nested_type_identifier, | ||
)), | ||
field('type_arguments', $.type_arguments) | ||
field('type_arguments', $.type_arguments), | ||
)), | ||
type_predicate: $ => seq( | ||
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) | ||
)), | ||
type_predicate: ($) => seq( | ||
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) | ||
field('type', $._type), | ||
), | ||
type_predicate_annotation: $ => seq( | ||
seq(':', $.type_predicate) | ||
type_predicate_annotation: ($) => seq( | ||
seq(':', $.type_predicate), | ||
), | ||
// Type query expressions are more restrictive than regular expressions | ||
_type_query_member_expression: $ => seq( | ||
_type_query_member_expression: ($) => seq( | ||
field('object', choice( | ||
@@ -824,3 +857,3 @@ $.identifier, | ||
alias($._type_query_member_expression, $.member_expression), | ||
alias($._type_query_call_expression, $.call_expression) | ||
alias($._type_query_call_expression, $.call_expression), | ||
)), | ||
@@ -830,6 +863,6 @@ choice('.', '?.'), | ||
$.private_property_identifier, | ||
alias($.identifier, $.property_identifier) | ||
)) | ||
alias($.identifier, $.property_identifier), | ||
)), | ||
), | ||
_type_query_subscript_expression: $ => seq( | ||
_type_query_subscript_expression: ($) => seq( | ||
field('object', choice( | ||
@@ -839,8 +872,8 @@ $.identifier, | ||
alias($._type_query_member_expression, $.member_expression), | ||
alias($._type_query_call_expression, $.call_expression) | ||
alias($._type_query_call_expression, $.call_expression), | ||
)), | ||
optional('?.'), | ||
'[', field('index', choice($.predefined_type, $.string, $.number)), ']' | ||
'[', field('index', choice($.predefined_type, $.string, $.number)), ']', | ||
), | ||
_type_query_call_expression: $ => seq( | ||
_type_query_call_expression: ($) => seq( | ||
field('function', choice( | ||
@@ -850,7 +883,7 @@ $.import, | ||
alias($._type_query_member_expression, $.member_expression), | ||
alias($._type_query_subscript_expression, $.subscript_expression) | ||
alias($._type_query_subscript_expression, $.subscript_expression), | ||
)), | ||
field('arguments', $.arguments) | ||
field('arguments', $.arguments), | ||
), | ||
_type_query_instantiation_expression: $ => seq( | ||
_type_query_instantiation_expression: ($) => seq( | ||
field('function', choice( | ||
@@ -860,7 +893,7 @@ $.import, | ||
alias($._type_query_member_expression, $.member_expression), | ||
alias($._type_query_subscript_expression, $.subscript_expression) | ||
alias($._type_query_subscript_expression, $.subscript_expression), | ||
)), | ||
field('type_arguments', $.type_arguments) | ||
field('type_arguments', $.type_arguments), | ||
), | ||
type_query: $ => prec.right(seq( | ||
type_query: ($) => prec.right(seq( | ||
'typeof', | ||
@@ -872,7 +905,7 @@ choice( | ||
alias($._type_query_instantiation_expression, $.instantiation_expression), | ||
$.identifier | ||
$.identifier, | ||
), | ||
)), | ||
index_type_query: $ => seq( | ||
index_type_query: ($) => seq( | ||
'keyof', | ||
@@ -882,17 +915,17 @@ $._primary_type, | ||
lookup_type: $ => seq( | ||
lookup_type: ($) => seq( | ||
$._primary_type, | ||
'[', | ||
$._type, | ||
']' | ||
']', | ||
), | ||
mapped_type_clause: $ => seq( | ||
mapped_type_clause: ($) => seq( | ||
field('name', $._type_identifier), | ||
'in', | ||
field('type', $._type), | ||
optional(seq('as', field('alias', $._type))) | ||
optional(seq('as', field('alias', $._type))), | ||
), | ||
literal_type: $ => choice( | ||
literal_type: ($) => choice( | ||
alias($._number, $.unary_expression), | ||
@@ -907,16 +940,14 @@ $.number, | ||
_number: $ => prec.left(1, seq( | ||
_number: ($) => prec.left(1, seq( | ||
field('operator', choice('-', '+')), | ||
field('argument', $.number) | ||
field('argument', $.number), | ||
)), | ||
existential_type: $ => '*', | ||
existential_type: (_) => '*', | ||
flow_maybe_type: $ => prec.right(seq( '?', $._primary_type)), | ||
flow_maybe_type: ($) => prec.right(seq('?', $._primary_type)), | ||
parenthesized_type: $ => seq( | ||
'(', $._type, ')' | ||
), | ||
parenthesized_type: ($) => seq('(', $._type, ')'), | ||
predefined_type: $ => choice( | ||
predefined_type: (_) => choice( | ||
'any', | ||
@@ -932,10 +963,17 @@ 'number', | ||
'never', | ||
'object' | ||
'object', | ||
), | ||
type_arguments: $ => seq( | ||
'<', commaSep1($._type), optional(','), '>' | ||
type_arguments: ($) => seq( | ||
'<', | ||
commaSep1(choice( | ||
$._type, | ||
alias($._type_query_member_expression_in_type_annotation, $.member_expression), | ||
alias($._type_query_call_expression_in_type_annotation, $.call_expression), | ||
)), | ||
optional(','), | ||
'>', | ||
), | ||
object_type: $ => seq( | ||
object_type: ($) => seq( | ||
choice('{', '{|'), | ||
@@ -952,13 +990,13 @@ optional(seq( | ||
$.index_signature, | ||
$.method_signature | ||
) | ||
$.method_signature, | ||
), | ||
), | ||
optional(choice(',', $._semicolon)) | ||
optional(choice(',', $._semicolon)), | ||
)), | ||
choice('}', '|}') | ||
choice('}', '|}'), | ||
), | ||
call_signature: $ => $._call_signature, | ||
call_signature: ($) => $._call_signature, | ||
property_signature: $ => seq( | ||
property_signature: ($) => seq( | ||
optional($.accessibility_modifier), | ||
@@ -970,35 +1008,35 @@ optional('static'), | ||
optional('?'), | ||
field('type', optional($.type_annotation)) | ||
field('type', optional($.type_annotation)), | ||
), | ||
_call_signature: $ => seq( | ||
_call_signature: ($) => seq( | ||
field('type_parameters', optional($.type_parameters)), | ||
field('parameters', $.formal_parameters), | ||
field('return_type', optional( | ||
choice($.type_annotation, $.asserts_annotation, $.type_predicate_annotation) | ||
)) | ||
choice($.type_annotation, $.asserts_annotation, $.type_predicate_annotation), | ||
)), | ||
), | ||
type_parameters: $ => seq( | ||
'<', commaSep1($.type_parameter), optional(','), '>' | ||
type_parameters: ($) => seq( | ||
'<', commaSep1($.type_parameter), optional(','), '>', | ||
), | ||
type_parameter: $ => seq( | ||
type_parameter: ($) => seq( | ||
optional('const'), | ||
field('name', $._type_identifier), | ||
field('constraint', optional($.constraint)), | ||
field('value', optional($.default_type)) | ||
field('value', optional($.default_type)), | ||
), | ||
default_type: $ => seq( | ||
default_type: ($) => seq( | ||
'=', | ||
$._type | ||
$._type, | ||
), | ||
constraint: $ => seq( | ||
constraint: ($) => seq( | ||
choice('extends', ':'), | ||
$._type | ||
$._type, | ||
), | ||
construct_signature: $ => seq( | ||
construct_signature: ($) => seq( | ||
optional('abstract'), | ||
@@ -1008,11 +1046,11 @@ 'new', | ||
field('parameters', $.formal_parameters), | ||
field('type', optional($.type_annotation)) | ||
field('type', optional($.type_annotation)), | ||
), | ||
index_signature: $ => seq( | ||
index_signature: ($) => seq( | ||
optional( | ||
seq( | ||
field("sign", optional("-")), | ||
'readonly' | ||
) | ||
field('sign', optional(choice('-', '+'))), | ||
'readonly', | ||
), | ||
), | ||
@@ -1024,3 +1062,3 @@ '[', | ||
$.identifier, | ||
alias($._reserved_identifier, $.identifier) | ||
alias($._reserved_identifier, $.identifier), | ||
)), | ||
@@ -1030,3 +1068,3 @@ ':', | ||
), | ||
$.mapped_type_clause | ||
$.mapped_type_clause, | ||
), | ||
@@ -1037,16 +1075,17 @@ ']', | ||
$.omitting_type_annotation, | ||
$.opting_type_annotation | ||
)) | ||
$.adding_type_annotation, | ||
$.opting_type_annotation, | ||
)), | ||
), | ||
array_type: $ => seq($._primary_type, '[', ']'), | ||
tuple_type: $ => seq( | ||
'[', commaSep($._tuple_type_member), optional(','), ']' | ||
array_type: ($) => seq($._primary_type, '[', ']'), | ||
tuple_type: ($) => seq( | ||
'[', commaSep($._tuple_type_member), optional(','), ']', | ||
), | ||
readonly_type: $ => seq('readonly', $._type), | ||
readonly_type: ($) => seq('readonly', $._type), | ||
union_type: $ => prec.left(seq(optional($._type), '|', $._type)), | ||
intersection_type: $ => prec.left(seq(optional($._type), '&', $._type)), | ||
union_type: ($) => prec.left(seq(optional($._type), '|', $._type)), | ||
intersection_type: ($) => prec.left(seq(optional($._type), '&', $._type)), | ||
function_type: $ => prec.left(seq( | ||
function_type: ($) => prec.left(seq( | ||
field('type_parameters', optional($.type_parameters)), | ||
@@ -1058,5 +1097,5 @@ field('parameters', $.formal_parameters), | ||
_type_identifier: $ => alias($.identifier, $.type_identifier), | ||
_type_identifier: ($) => alias($.identifier, $.type_identifier), | ||
_reserved_identifier: ($, previous) => choice( | ||
_reserved_identifier: (_, previous) => choice( | ||
'declare', | ||
@@ -1077,22 +1116,58 @@ 'namespace', | ||
'export', | ||
previous | ||
'object', | ||
'new', | ||
previous, | ||
), | ||
}, | ||
}); | ||
} | ||
}; | ||
function commaSep1 (rule) { | ||
/** | ||
* Creates a rule to match one or more of the rules separated by a comma | ||
* | ||
* @param {RuleOrLiteral} rule | ||
* | ||
* @return {SeqRule} | ||
* | ||
*/ | ||
function commaSep1(rule) { | ||
return sepBy1(',', rule); | ||
} | ||
function commaSep (rule) { | ||
/** | ||
* Creates a rule to optionally match one or more of the rules separated by a comma | ||
* | ||
* @param {RuleOrLiteral} rule | ||
* | ||
* @return {SeqRule} | ||
* | ||
*/ | ||
function commaSep(rule) { | ||
return sepBy(',', rule); | ||
} | ||
function sepBy (sep, rule) { | ||
return optional(sepBy1(sep, rule)) | ||
/** | ||
* Creates a rule to optionally match one or more of the rules separated by a separator | ||
* | ||
* @param {RuleOrLiteral} sep | ||
* | ||
* @param {RuleOrLiteral} rule | ||
* | ||
* @return {ChoiceRule} | ||
*/ | ||
function sepBy(sep, rule) { | ||
return optional(sepBy1(sep, rule)); | ||
} | ||
function sepBy1 (sep, rule) { | ||
/** | ||
* Creates a rule to match one or more of the rules separated by a separator | ||
* | ||
* @param {RuleOrLiteral} sep | ||
* | ||
* @param {RuleOrLiteral} rule | ||
* | ||
* @return {SeqRule} | ||
*/ | ||
function sepBy1(sep, rule) { | ||
return seq(rule, repeat(seq(sep, rule))); | ||
} |
{ | ||
"name": "tree-sitter-typescript", | ||
"version": "0.20.3", | ||
"description": "Typescript grammar for tree-sitter", | ||
"version": "0.20.4", | ||
"description": "TypeScript grammar for tree-sitter", | ||
"main": "./bindings/node", | ||
"keywords": [ | ||
"parser", | ||
"tree-sitter", | ||
"typescript" | ||
"typescript", | ||
"tsx" | ||
], | ||
@@ -17,8 +19,10 @@ "repository": { | ||
"dependencies": { | ||
"nan": "^2.14.0" | ||
"nan": "^2.18.0", | ||
"tree-sitter": "^0.20.6" | ||
}, | ||
"main": "./bindings/node", | ||
"devDependencies": { | ||
"tree-sitter-cli": "^0.20.6", | ||
"tree-sitter-javascript": "github:tree-sitter/tree-sitter-javascript#f772967" | ||
"eslint": ">=8.56.0", | ||
"eslint-config-google": "^0.14.0", | ||
"tree-sitter-cli": "^0.20.8", | ||
"tree-sitter-javascript": "^0.20.2" | ||
}, | ||
@@ -29,2 +33,3 @@ "scripts": { | ||
"build-tsx": "cd tsx && npx tree-sitter generate --no-bindings", | ||
"lint": "eslint common/define-grammar.js", | ||
"test-load": "node -e \"console.log(require('./typescript').name, require('./tsx').name)\"", | ||
@@ -31,0 +36,0 @@ "test": "npm run test-typescript && npm run test-tsx && npm run test-load && script/parse-examples", |
@@ -1,6 +0,7 @@ | ||
tree-sitter-typescript | ||
=========================== | ||
# tree-sitter-typescript | ||
[](https://github.com/tree-sitter/tree-sitter-typescript/actions?query=workflow%3Abuild) | ||
[](https://ci.appveyor.com/project/maxbrunsfeld/tree-sitter-typescript/branch/master) | ||
[](https://github.com/tree-sitter/tree-sitter-typescript/actions/workflows/ci.yml) | ||
[](https://discord.gg/w7nTvsVJhm) | ||
[](https://crates.io/crates/tree-sitter-typescript) | ||
[](https://www.npmjs.com/package/tree-sitter-typescript) | ||
@@ -12,4 +13,4 @@ TypeScript and TSX grammars for [tree-sitter][]. | ||
```js | ||
require('tree-sitter-typescript').typescript; // TypeScript grammar | ||
require('tree-sitter-typescript').tsx; // TSX grammar | ||
require("tree-sitter-typescript").typescript; // TypeScript grammar | ||
require("tree-sitter-typescript").tsx; // TSX grammar | ||
``` | ||
@@ -24,2 +25,2 @@ | ||
* [TypeScript Language Spec](https://github.com/microsoft/TypeScript/blob/main/doc/spec-ARCHIVED.md) | ||
- [TypeScript Language Spec](https://github.com/microsoft/TypeScript/blob/main/doc/spec-ARCHIVED.md) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
26652754
49
37959
25
2
4
+ Addedtree-sitter@^0.20.6
+ Addedbase64-js@1.5.1(transitive)
+ Addedbl@4.1.0(transitive)
+ Addedbuffer@5.7.1(transitive)
+ Addedchownr@1.1.4(transitive)
+ Addeddecompress-response@6.0.0(transitive)
+ Addeddeep-extend@0.6.0(transitive)
+ Addeddetect-libc@2.0.3(transitive)
+ Addedend-of-stream@1.4.4(transitive)
+ Addedexpand-template@2.0.3(transitive)
+ Addedfs-constants@1.0.0(transitive)
+ Addedgithub-from-package@0.0.0(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedini@1.3.8(transitive)
+ Addedmimic-response@3.1.0(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedmkdirp-classic@0.5.3(transitive)
+ Addednapi-build-utils@2.0.0(transitive)
+ Addednode-abi@3.74.0(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedprebuild-install@7.1.3(transitive)
+ Addedpump@3.0.2(transitive)
+ Addedrc@1.2.8(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsemver@7.7.1(transitive)
+ Addedsimple-concat@1.0.1(transitive)
+ Addedsimple-get@4.0.1(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addedstrip-json-comments@2.0.1(transitive)
+ Addedtar-fs@2.1.2(transitive)
+ Addedtar-stream@2.2.0(transitive)
+ Addedtree-sitter@0.20.6(transitive)
+ Addedtunnel-agent@0.6.0(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedwrappy@1.0.2(transitive)
Updatednan@^2.18.0