11//! Hardcoded global definitions.
22
3- // FIXME: Implement inference from type definitions.
3+ // FIXME: Implement inference from type definitions: https://github.com/biomejs/biome/issues/5977
44
55use std:: {
66 borrow:: Cow ,
@@ -59,22 +59,25 @@ pub const PROMISE_RACE_ID: TypeId = TypeId::new(22);
5959pub const PROMISE_REJECT_ID : TypeId = TypeId :: new ( 23 ) ;
6060pub const PROMISE_RESOLVE_ID : TypeId = TypeId :: new ( 24 ) ;
6161pub const PROMISE_TRY_ID : TypeId = TypeId :: new ( 25 ) ;
62- pub const BIGINT_STRING_LITERAL_ID : TypeId = TypeId :: new ( 26 ) ;
63- pub const BOOLEAN_STRING_LITERAL_ID : TypeId = TypeId :: new ( 27 ) ;
64- pub const FUNCTION_STRING_LITERAL_ID : TypeId = TypeId :: new ( 28 ) ;
65- pub const NUMBER_STRING_LITERAL_ID : TypeId = TypeId :: new ( 29 ) ;
66- pub const OBJECT_STRING_LITERAL_ID : TypeId = TypeId :: new ( 30 ) ;
67- pub const STRING_STRING_LITERAL_ID : TypeId = TypeId :: new ( 31 ) ;
68- pub const SYMBOL_STRING_LITERAL_ID : TypeId = TypeId :: new ( 32 ) ;
69- pub const UNDEFINED_STRING_LITERAL_ID : TypeId = TypeId :: new ( 33 ) ;
70- pub const TYPEOF_OPERATOR_RETURN_UNION_ID : TypeId = TypeId :: new ( 34 ) ;
71- pub const T_ID : TypeId = TypeId :: new ( 35 ) ;
72- pub const U_ID : TypeId = TypeId :: new ( 36 ) ;
73- pub const CONDITIONAL_CALLBACK_ID : TypeId = TypeId :: new ( 37 ) ;
74- pub const MAP_CALLBACK_ID : TypeId = TypeId :: new ( 38 ) ;
75- pub const VOID_CALLBACK_ID : TypeId = TypeId :: new ( 39 ) ;
76- pub const FETCH_ID : TypeId = TypeId :: new ( 40 ) ;
77- pub const NUM_PREDEFINED_TYPES : usize = 41 ; // Must be one more than the highest `TypeId` above.
62+ pub const INSTANCEOF_REGEXP_ID : TypeId = TypeId :: new ( 26 ) ;
63+ pub const REGEXP_ID : TypeId = TypeId :: new ( 27 ) ;
64+ pub const REGEXP_EXEC_ID : TypeId = TypeId :: new ( 28 ) ;
65+ pub const BIGINT_STRING_LITERAL_ID : TypeId = TypeId :: new ( 29 ) ;
66+ pub const BOOLEAN_STRING_LITERAL_ID : TypeId = TypeId :: new ( 30 ) ;
67+ pub const FUNCTION_STRING_LITERAL_ID : TypeId = TypeId :: new ( 31 ) ;
68+ pub const NUMBER_STRING_LITERAL_ID : TypeId = TypeId :: new ( 32 ) ;
69+ pub const OBJECT_STRING_LITERAL_ID : TypeId = TypeId :: new ( 33 ) ;
70+ pub const STRING_STRING_LITERAL_ID : TypeId = TypeId :: new ( 34 ) ;
71+ pub const SYMBOL_STRING_LITERAL_ID : TypeId = TypeId :: new ( 35 ) ;
72+ pub const UNDEFINED_STRING_LITERAL_ID : TypeId = TypeId :: new ( 36 ) ;
73+ pub const TYPEOF_OPERATOR_RETURN_UNION_ID : TypeId = TypeId :: new ( 37 ) ;
74+ pub const T_ID : TypeId = TypeId :: new ( 38 ) ;
75+ pub const U_ID : TypeId = TypeId :: new ( 39 ) ;
76+ pub const CONDITIONAL_CALLBACK_ID : TypeId = TypeId :: new ( 40 ) ;
77+ pub const MAP_CALLBACK_ID : TypeId = TypeId :: new ( 41 ) ;
78+ pub const VOID_CALLBACK_ID : TypeId = TypeId :: new ( 42 ) ;
79+ pub const FETCH_ID : TypeId = TypeId :: new ( 43 ) ;
80+ pub const NUM_PREDEFINED_TYPES : usize = 44 ; // Must be one more than the highest `TypeId` above.
7881
7982pub const GLOBAL_UNKNOWN_ID : ResolvedTypeId = ResolvedTypeId :: new ( GLOBAL_LEVEL , UNKNOWN_ID ) ;
8083pub const GLOBAL_UNDEFINED_ID : ResolvedTypeId = ResolvedTypeId :: new ( GLOBAL_LEVEL , UNDEFINED_ID ) ;
@@ -83,6 +86,9 @@ pub const GLOBAL_CONDITIONAL_ID: ResolvedTypeId = ResolvedTypeId::new(GLOBAL_LEV
8386pub const GLOBAL_NUMBER_ID : ResolvedTypeId = ResolvedTypeId :: new ( GLOBAL_LEVEL , NUMBER_ID ) ;
8487pub const GLOBAL_STRING_ID : ResolvedTypeId = ResolvedTypeId :: new ( GLOBAL_LEVEL , STRING_ID ) ;
8588pub const GLOBAL_ARRAY_ID : ResolvedTypeId = ResolvedTypeId :: new ( GLOBAL_LEVEL , ARRAY_ID ) ;
89+ pub const GLOBAL_INSTANCEOF_REGEXP_ID : ResolvedTypeId =
90+ ResolvedTypeId :: new ( GLOBAL_LEVEL , INSTANCEOF_REGEXP_ID ) ;
91+ pub const GLOBAL_REGEXP_ID : ResolvedTypeId = ResolvedTypeId :: new ( GLOBAL_LEVEL , REGEXP_ID ) ;
8692pub const GLOBAL_GLOBAL_ID /* :smirk: */ : ResolvedTypeId = ResolvedTypeId :: new ( GLOBAL_LEVEL , GLOBAL_ID ) ;
8793pub const GLOBAL_INSTANCEOF_PROMISE_ID : ResolvedTypeId =
8894 ResolvedTypeId :: new ( GLOBAL_LEVEL , INSTANCEOF_PROMISE_ID ) ;
@@ -140,24 +146,27 @@ pub fn global_type_name(id: TypeId) -> &'static str {
140146 23 => "Promise.reject" ,
141147 24 => "Promise.resolve" ,
142148 25 => "Promise.try" ,
143- 26 => "\" bigint\" " ,
144- 27 => "\" boolean\" " ,
145- 28 => "\" function\" " ,
146- 29 => "\" number\" " ,
147- 30 => "\" object\" " ,
148- 31 => "\" string\" " ,
149- 32 => "\" symbol\" " ,
150- 33 => "\" undefined\" " ,
151- 34 => {
149+ 26 => "instanceof RegExp" ,
150+ 27 => "RegExp" ,
151+ 28 => "RegExp.exec" ,
152+ 29 => "\" bigint\" " ,
153+ 30 => "\" boolean\" " ,
154+ 31 => "\" function\" " ,
155+ 32 => "\" number\" " ,
156+ 33 => "\" object\" " ,
157+ 34 => "\" string\" " ,
158+ 35 => "\" symbol\" " ,
159+ 36 => "\" undefined\" " ,
160+ 37 => {
152161 "\" bigint\" | \" boolean\" | \" function\" | \" number\" | \" object\" \
153162 | \" string\" | \" symbol\" | \" undefined\" "
154163 }
155- 35 => "T" ,
156- 36 => "U" ,
157- 37 => "() => conditional" ,
158- 38 => "<U>(item: T) => U" ,
159- 39 => "() => void" ,
160- 40 => "fetch" ,
164+ 38 => "T" ,
165+ 39 => "U" ,
166+ 40 => "() => conditional" ,
167+ 41 => "<U>(item: T) => U" ,
168+ 42 => "() => void" ,
169+ 43 => "fetch" ,
161170 _ => "inferred type" ,
162171 }
163172}
@@ -215,6 +224,16 @@ impl Default for GlobalsResolver {
215224 } )
216225 } ;
217226
227+ let regexp_method_definition = |id : TypeId | {
228+ TypeData :: from ( Function {
229+ is_async : false ,
230+ type_parameters : Default :: default ( ) ,
231+ name : Some ( Text :: new_static ( global_type_name ( id) ) ) ,
232+ parameters : Default :: default ( ) ,
233+ return_type : ReturnType :: Type ( GLOBAL_INSTANCEOF_REGEXP_ID . into ( ) ) ,
234+ } )
235+ } ;
236+
218237 let string_literal = |value : & ' static str | -> TypeData {
219238 TypeData :: from ( Literal :: String ( Text :: new_static ( value) . into ( ) ) )
220239 } ;
@@ -311,6 +330,15 @@ impl Default for GlobalsResolver {
311330 promise_method_definition( PROMISE_REJECT_ID ) ,
312331 promise_method_definition( PROMISE_RESOLVE_ID ) ,
313332 promise_method_definition( PROMISE_TRY_ID ) ,
333+ TypeData :: instance_of( TypeReference :: from( GLOBAL_REGEXP_ID ) ) ,
334+ TypeData :: Class ( Box :: new( Class {
335+ name: Some ( Text :: new_static( "RegExp" ) ) ,
336+ type_parameters: Box :: default ( ) ,
337+ extends: None ,
338+ implements: [ ] . into( ) ,
339+ members: Box :: new( [ method( "exec" , REGEXP_EXEC_ID ) ] ) ,
340+ } ) ) ,
341+ regexp_method_definition( REGEXP_EXEC_ID ) ,
314342 string_literal( "bigint" ) ,
315343 string_literal( "boolean" ) ,
316344 string_literal( "function" ) ,
@@ -454,6 +482,8 @@ impl TypeResolver for GlobalsResolver {
454482 Some ( GLOBAL_ARRAY_ID )
455483 } else if qualifier. is_promise ( ) && !qualifier. has_known_type_parameters ( ) {
456484 Some ( GLOBAL_PROMISE_ID )
485+ } else if qualifier. is_regex ( ) && !qualifier. has_known_type_parameters ( ) {
486+ Some ( GLOBAL_REGEXP_ID )
457487 } else if !qualifier. type_only
458488 && let Some ( ident) = qualifier. path . identifier ( )
459489 {
0 commit comments