Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various fixes for valid code not being parsed properly #252

Merged
merged 6 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion common/corpus/declarations.txt
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ Type casts

foo as any as Array<number>
bar satisfies number[]
"foobar" as const

---

Expand All @@ -466,7 +467,11 @@ bar satisfies number[]
(as_expression (identifier) (predefined_type))
(generic_type (type_identifier) (type_arguments (predefined_type)))))
(expression_statement
(satisfies_expression (identifier) (array_type (predefined_type)))))
(satisfies_expression (identifier) (array_type (predefined_type))))
(expression_statement
(as_expression
(string
(string_fragment)))))

=================================
Ambient export function declarations
Expand Down
19 changes: 18 additions & 1 deletion common/corpus/functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ function foo<T, U>(this: T[]): U[] {
return []
}

function foo<const T, const U extends string>(x: T, y: U) {

}

---

(program
Expand Down Expand Up @@ -57,7 +61,20 @@ function foo<T, U>(this: T[]): U[] {
pattern: (this)
type: (type_annotation (array_type (type_identifier)))))
return_type: (type_annotation (array_type (type_identifier)))
body: (statement_block (return_statement (array)))))
body: (statement_block (return_statement (array))))
(function_declaration
name: (identifier)
type_parameters: (type_parameters
(type_parameter name: (type_identifier))
(type_parameter name: (type_identifier) constraint: (constraint (predefined_type))))
parameters: (formal_parameters
(required_parameter
pattern: (identifier)
type: (type_annotation (type_identifier)))
(required_parameter
pattern: (identifier)
type: (type_annotation (type_identifier))))
body: (statement_block)))

==================================
New object with type arguments
Expand Down
113 changes: 109 additions & 4 deletions common/corpus/types.txt
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,40 @@ let x: new < T1, T2 > ( p1, p2 ) => R;
(type_identifier))))))


=======================================
Symbol types
=======================================

const symFoo: unique symbol = Symbol("foo");
const symBar: symbol = Symbol.for("bar");

---

(program
(lexical_declaration
(variable_declarator
(identifier)
(type_annotation
(predefined_type))
(call_expression
(identifier)
(arguments
(string
(string_fragment))))))
(lexical_declaration
(variable_declarator
(identifier)
(type_annotation
(predefined_type))
(call_expression
(member_expression
(identifier)
(property_identifier))
(arguments
(string
(string_fragment)))))))


=======================================
Type annotations in parenthesized expressions
=======================================
Expand Down Expand Up @@ -855,7 +889,8 @@ function f(x: any): asserts x {
(identifier)
(formal_parameters
(required_parameter (identifier) (type_annotation (predefined_type))))
(asserts (identifier))
(asserts_annotation
(asserts (identifier)))
(statement_block)))

=======================================
Expand All @@ -881,8 +916,9 @@ function isT(t: T): t is T {
(formal_parameters
(required_parameter
(identifier) (type_annotation (predefined_type))))
(asserts
(type_predicate (identifier) (predefined_type)))
(asserts_annotation
(asserts
(type_predicate (identifier) (predefined_type))))
(statement_block))
(class_declaration
(type_identifier)
Expand All @@ -908,6 +944,45 @@ function isT(t: T): t is T {
(type_identifier)))
(statement_block (return_statement (true)))))

=======================================
Type of an assertion function
=======================================

declare const f: (x: any) => asserts x;
declare const g: (x: any) => asserts x is number;

---

(program
(ambient_declaration
(lexical_declaration
(variable_declarator
(identifier)
(type_annotation
(function_type
(formal_parameters
(required_parameter
(identifier)
(type_annotation
(predefined_type))))
(asserts
(identifier)))))))
(ambient_declaration
(lexical_declaration
(variable_declarator
(identifier)
(type_annotation
(function_type
(formal_parameters
(required_parameter
(identifier)
(type_annotation
(predefined_type))))
(asserts
(type_predicate
(identifier)
(predefined_type)))))))))

==================================
Type predicate and predefined types
==================================
Expand Down Expand Up @@ -1045,6 +1120,7 @@ type F<T, X, Y> = (t: T) => X extends Y ? X : Y extends (t: T) => X extends Y ?
type T<X, Y> = T extends X<infer Y> ? Y : X
type T<X> = X extends (infer X)[] ? X : never;
type T<X> = T extends { x: infer X } ? X : never;
type T<X> = T extends { x: infer X extends number } ? X : never;

---
(program
Expand Down Expand Up @@ -1154,6 +1230,18 @@ type T<X> = T extends { x: infer X } ? X : never;
(property_identifier)
(type_annotation (infer_type (type_identifier)))))
(type_identifier)
(predefined_type)))
(type_alias_declaration
(type_identifier)
(type_parameters
(type_parameter (type_identifier)))
(conditional_type
(type_identifier)
(object_type
(property_signature
(property_identifier)
(type_annotation (infer_type (type_identifier) (predefined_type)))))
(type_identifier)
(predefined_type))))

==================================
Expand All @@ -1170,6 +1258,7 @@ type A<B, C> = B extends C
: never
type Trim<S extends string> = S extends `${infer R}` ? Trim<R> : S;
type A = `${true & ('foo' | false)}`;
type StringToNumber<S extends string> = S extends `${infer N extends number}` ? N : never;
---
(program
(type_alias_declaration
Expand Down Expand Up @@ -1271,7 +1360,23 @@ type A = `${true & ('foo' | false)}`;
(string
(string_fragment)))
(literal_type
(false)))))))))
(false))))))))
(type_alias_declaration
(type_identifier)
(type_parameters
(type_parameter
(type_identifier)
(constraint
(predefined_type))))
(conditional_type
(type_identifier)
(template_literal_type
(template_type
(infer_type
(type_identifier)
(predefined_type))))
(type_identifier)
(predefined_type))))

==================================
Mapped type 'as' clauses
Expand Down
22 changes: 17 additions & 5 deletions common/define-grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ module.exports = function defineGrammar(dialect) {
as_expression: $ => prec.left('binary', seq(
$.expression,
'as',
$._type
choice('const', $._type)
)),

satisfies_expression: $ => prec.left('binary', seq(
Expand Down Expand Up @@ -627,11 +627,14 @@ module.exports = function defineGrammar(dialect) {
type_annotation: $ => seq(':', $._type),

asserts: $ => seq(
':',
'asserts',
choice($.type_predicate, $.identifier, $.this)
),

asserts_annotation: $ => seq(
amaanq marked this conversation as resolved.
Show resolved Hide resolved
seq(':', $.asserts)
),

_type: $ => choice(
$._primary_type,
$.function_type,
Expand Down Expand Up @@ -704,7 +707,14 @@ module.exports = function defineGrammar(dialect) {
'`'
),

infer_type: $ => seq("infer", $._type_identifier),
infer_type: $ => prec.right(seq(
'infer',
$._type_identifier,
optional(seq(
'extends',
$._type
))
)),

conditional_type: $ => prec.left(seq(
field('left', $._type),
Expand Down Expand Up @@ -834,6 +844,7 @@ module.exports = function defineGrammar(dialect) {
'boolean',
'string',
'symbol',
alias(seq('unique', 'symbol'), 'unique symbol'),
'void',
'unknown',
'string',
Expand Down Expand Up @@ -881,7 +892,7 @@ module.exports = function defineGrammar(dialect) {
field('type_parameters', optional($.type_parameters)),
field('parameters', $.formal_parameters),
field('return_type', optional(
choice($.type_annotation, $.asserts, $.type_predicate_annotation)
choice($.type_annotation, $.asserts_annotation, $.type_predicate_annotation)
))
amaanq marked this conversation as resolved.
Show resolved Hide resolved
),

Expand All @@ -890,6 +901,7 @@ module.exports = function defineGrammar(dialect) {
),

type_parameter: $ => seq(
optional('const'),
field('name', $._type_identifier),
field('constraint', optional($.constraint)),
field('value', optional($.default_type))
Expand Down Expand Up @@ -953,7 +965,7 @@ module.exports = function defineGrammar(dialect) {
field('type_parameters', optional($.type_parameters)),
field('parameters', $.formal_parameters),
'=>',
field('return_type', choice($._type, $.type_predicate)),
field('return_type', choice($._type, $.asserts, $.type_predicate)),
amaanq marked this conversation as resolved.
Show resolved Hide resolved
)),

_type_identifier: $ => alias($.identifier, $.type_identifier),
Expand Down
Loading