typescript-language-server 0.1.0

A high-performance TypeScript and JavaScript language server implemented in Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
//! Type system representations
//! Reserved for full type checking implementation

#![allow(dead_code)]

use std::collections::HashMap;

/// Unique identifier for a type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TypeId(pub u32);

impl TypeId {
    pub fn new(id: u32) -> Self {
        Self(id)
    }
}

bitflags::bitflags! {
    /// Flags describing type characteristics
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub struct TypeFlags: u32 {
        const NONE = 0;

        // Primitive types
        const ANY = 1 << 0;
        const UNKNOWN = 1 << 1;
        const STRING = 1 << 2;
        const NUMBER = 1 << 3;
        const BOOLEAN = 1 << 4;
        const VOID = 1 << 5;
        const UNDEFINED = 1 << 6;
        const NULL = 1 << 7;
        const NEVER = 1 << 8;
        const SYMBOL = 1 << 9;
        const BIGINT = 1 << 10;

        // Literal types
        const STRING_LITERAL = 1 << 11;
        const NUMBER_LITERAL = 1 << 12;
        const BOOLEAN_LITERAL = 1 << 13;
        const BIGINT_LITERAL = 1 << 14;
        const ENUM_LITERAL = 1 << 15;

        // Compound types
        const OBJECT = 1 << 16;
        const UNION = 1 << 17;
        const INTERSECTION = 1 << 18;
        const FUNCTION = 1 << 19;
        const TUPLE = 1 << 20;
        const ARRAY = 1 << 21;

        // Special types
        const TYPE_PARAMETER = 1 << 22;
        const CONDITIONAL = 1 << 23;
        const MAPPED = 1 << 24;
        const INDEX = 1 << 25;
        const INDEXED_ACCESS = 1 << 26;

        // Combined flags
        const LITERAL = Self::STRING_LITERAL.bits() | Self::NUMBER_LITERAL.bits()
            | Self::BOOLEAN_LITERAL.bits() | Self::BIGINT_LITERAL.bits()
            | Self::ENUM_LITERAL.bits();

        const PRIMITIVE = Self::STRING.bits() | Self::NUMBER.bits() | Self::BOOLEAN.bits()
            | Self::VOID.bits() | Self::UNDEFINED.bits() | Self::NULL.bits()
            | Self::SYMBOL.bits() | Self::BIGINT.bits();

        const DEFINITE_FALSY = Self::VOID.bits() | Self::UNDEFINED.bits() | Self::NULL.bits();
    }
}

/// Represents a TypeScript type
#[derive(Debug, Clone)]
pub enum Type {
    /// Any type
    Any,
    /// Unknown type
    Unknown,
    /// Never type
    Never,
    /// Void type
    Void,
    /// Undefined type
    Undefined,
    /// Null type
    Null,
    /// String type
    String,
    /// Number type
    Number,
    /// Boolean type
    Boolean,
    /// Symbol type
    Symbol,
    /// BigInt type
    BigInt,
    /// String literal type
    StringLiteral(String),
    /// Number literal type
    NumberLiteral(f64),
    /// Boolean literal type
    BooleanLiteral(bool),
    /// BigInt literal type
    BigIntLiteral(String),
    /// Object type
    Object(ObjectType),
    /// Array type
    Array(Box<Type>),
    /// Tuple type
    Tuple(Vec<Type>),
    /// Function type
    Function(FunctionType),
    /// Union type (A | B)
    Union(Vec<Type>),
    /// Intersection type (A & B)
    Intersection(Vec<Type>),
    /// Type parameter (generic)
    TypeParameter(TypeParameter),
    /// Conditional type (T extends U ? X : Y)
    Conditional(ConditionalType),
    /// Mapped type ({ [K in keyof T]: ... })
    Mapped(MappedType),
    /// Index type (keyof T)
    Index(Box<Type>),
    /// Indexed access type (T[K])
    IndexedAccess(IndexedAccessType),
    /// Type reference (refers to a named type)
    Reference(TypeReference),
    /// This type
    This,
}

impl Type {
    /// Get the flags for this type
    pub fn flags(&self) -> TypeFlags {
        match self {
            Type::Any => TypeFlags::ANY,
            Type::Unknown => TypeFlags::UNKNOWN,
            Type::Never => TypeFlags::NEVER,
            Type::Void => TypeFlags::VOID,
            Type::Undefined => TypeFlags::UNDEFINED,
            Type::Null => TypeFlags::NULL,
            Type::String => TypeFlags::STRING,
            Type::Number => TypeFlags::NUMBER,
            Type::Boolean => TypeFlags::BOOLEAN,
            Type::Symbol => TypeFlags::SYMBOL,
            Type::BigInt => TypeFlags::BIGINT,
            Type::StringLiteral(_) => TypeFlags::STRING_LITERAL,
            Type::NumberLiteral(_) => TypeFlags::NUMBER_LITERAL,
            Type::BooleanLiteral(_) => TypeFlags::BOOLEAN_LITERAL,
            Type::BigIntLiteral(_) => TypeFlags::BIGINT_LITERAL,
            Type::Object(_) => TypeFlags::OBJECT,
            Type::Array(_) => TypeFlags::ARRAY,
            Type::Tuple(_) => TypeFlags::TUPLE,
            Type::Function(_) => TypeFlags::FUNCTION,
            Type::Union(_) => TypeFlags::UNION,
            Type::Intersection(_) => TypeFlags::INTERSECTION,
            Type::TypeParameter(_) => TypeFlags::TYPE_PARAMETER,
            Type::Conditional(_) => TypeFlags::CONDITIONAL,
            Type::Mapped(_) => TypeFlags::MAPPED,
            Type::Index(_) => TypeFlags::INDEX,
            Type::IndexedAccess(_) => TypeFlags::INDEXED_ACCESS,
            Type::Reference(_) => TypeFlags::OBJECT,
            Type::This => TypeFlags::OBJECT,
        }
    }

    /// Check if this type is assignable to another type
    pub fn is_assignable_to(&self, target: &Type) -> bool {
        // Any is assignable to anything
        if matches!(self, Type::Any) {
            return true;
        }

        // Anything is assignable to any
        if matches!(target, Type::Any) {
            return true;
        }

        // Unknown accepts everything
        if matches!(target, Type::Unknown) {
            return true;
        }

        // Never is assignable to nothing (except itself)
        if matches!(self, Type::Never) {
            return true;
        }

        // Nothing is assignable to never
        if matches!(target, Type::Never) {
            return false;
        }

        // Same types are assignable
        match (self, target) {
            (Type::String, Type::String) => true,
            (Type::Number, Type::Number) => true,
            (Type::Boolean, Type::Boolean) => true,
            (Type::Void, Type::Void) => true,
            (Type::Undefined, Type::Undefined) => true,
            (Type::Undefined, Type::Void) => true,
            (Type::Null, Type::Null) => true,
            (Type::Symbol, Type::Symbol) => true,
            (Type::BigInt, Type::BigInt) => true,

            // Literals are assignable to their base types
            (Type::StringLiteral(_), Type::String) => true,
            (Type::NumberLiteral(_), Type::Number) => true,
            (Type::BooleanLiteral(_), Type::Boolean) => true,
            (Type::BigIntLiteral(_), Type::BigInt) => true,

            // Same literals
            (Type::StringLiteral(a), Type::StringLiteral(b)) => a == b,
            (Type::NumberLiteral(a), Type::NumberLiteral(b)) => (a - b).abs() < f64::EPSILON,
            (Type::BooleanLiteral(a), Type::BooleanLiteral(b)) => a == b,

            // Arrays
            (Type::Array(a), Type::Array(b)) => a.is_assignable_to(b),

            // Unions - source must be assignable to at least one member
            (_, Type::Union(members)) => members.iter().any(|m| self.is_assignable_to(m)),

            // Source union - all members must be assignable to target
            (Type::Union(members), _) => members.iter().all(|m| m.is_assignable_to(target)),

            // Intersections - source must be assignable to all members
            (_, Type::Intersection(members)) => members.iter().all(|m| self.is_assignable_to(m)),

            // TODO: More complex type relationships
            _ => false,
        }
    }
}

/// Object type (interface, class, etc.)
#[derive(Debug, Clone, Default)]
pub struct ObjectType {
    /// Properties of the object
    pub properties: HashMap<String, Property>,
    /// Index signatures
    pub index_signatures: Vec<IndexSignature>,
    /// Call signatures (for callable objects)
    pub call_signatures: Vec<FunctionType>,
    /// Constructor signatures (for newable objects)
    pub construct_signatures: Vec<FunctionType>,
}

/// Property of an object type
#[derive(Debug, Clone)]
pub struct Property {
    /// Property name
    pub name: String,
    /// Property type
    pub ty: Box<Type>,
    /// Is the property optional?
    pub optional: bool,
    /// Is the property readonly?
    pub readonly: bool,
}

/// Index signature ([key: string]: T)
#[derive(Debug, Clone)]
pub struct IndexSignature {
    /// Key type (string or number)
    pub key_type: Box<Type>,
    /// Value type
    pub value_type: Box<Type>,
    /// Is readonly?
    pub readonly: bool,
}

/// Function type
#[derive(Debug, Clone)]
#[allow(dead_code)] // Fields used for type representation
pub struct FunctionType {
    /// Type parameters
    pub type_parameters: Vec<TypeParameter>,
    /// Parameters
    pub parameters: Vec<Parameter>,
    /// Return type
    pub return_type: Box<Type>,
    /// This type (for methods)
    pub this_type: Option<Box<Type>>,
}

/// Function parameter
#[derive(Debug, Clone)]
pub struct Parameter {
    /// Parameter name
    pub name: String,
    /// Parameter type
    pub ty: Box<Type>,
    /// Is optional?
    pub optional: bool,
    /// Is rest parameter?
    pub rest: bool,
}

/// Type parameter (generic)
#[derive(Debug, Clone)]
pub struct TypeParameter {
    /// Parameter name
    pub name: String,
    /// Constraint (extends clause)
    pub constraint: Option<Box<Type>>,
    /// Default type
    pub default: Option<Box<Type>>,
}

/// Conditional type
#[derive(Debug, Clone)]
pub struct ConditionalType {
    /// Check type
    pub check_type: Box<Type>,
    /// Extends type
    pub extends_type: Box<Type>,
    /// True branch
    pub true_type: Box<Type>,
    /// False branch
    pub false_type: Box<Type>,
}

/// Mapped type
#[derive(Debug, Clone)]
pub struct MappedType {
    /// Type parameter name
    pub type_parameter: String,
    /// Constraint type (in keyof T, etc.)
    pub constraint: Box<Type>,
    /// Template type
    pub template_type: Box<Type>,
    /// Readonly modifier (+readonly, -readonly, or none)
    pub readonly_modifier: Option<bool>,
    /// Optional modifier (+?, -?, or none)
    pub optional_modifier: Option<bool>,
}

/// Indexed access type (T[K])
#[derive(Debug, Clone)]
pub struct IndexedAccessType {
    /// Object type
    pub object_type: Box<Type>,
    /// Index type
    pub index_type: Box<Type>,
}

/// Type reference (named type)
#[derive(Debug, Clone)]
pub struct TypeReference {
    /// Type name
    pub name: String,
    /// Type arguments
    pub type_arguments: Vec<Type>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_type_id() {
        let id1 = TypeId::new(1);
        let id2 = TypeId::new(1);
        let id3 = TypeId::new(2);

        assert_eq!(id1, id2);
        assert_ne!(id1, id3);
    }

    #[test]
    fn test_type_flags_primitives() {
        assert!(TypeFlags::STRING.intersects(TypeFlags::PRIMITIVE));
        assert!(TypeFlags::NUMBER.intersects(TypeFlags::PRIMITIVE));
        assert!(TypeFlags::BOOLEAN.intersects(TypeFlags::PRIMITIVE));
        assert!(TypeFlags::VOID.intersects(TypeFlags::PRIMITIVE));
        assert!(TypeFlags::UNDEFINED.intersects(TypeFlags::PRIMITIVE));
        assert!(TypeFlags::NULL.intersects(TypeFlags::PRIMITIVE));
    }

    #[test]
    fn test_type_flags_literals() {
        assert!(TypeFlags::STRING_LITERAL.intersects(TypeFlags::LITERAL));
        assert!(TypeFlags::NUMBER_LITERAL.intersects(TypeFlags::LITERAL));
        assert!(TypeFlags::BOOLEAN_LITERAL.intersects(TypeFlags::LITERAL));
    }

    #[test]
    fn test_type_flags_combine() {
        let flags = TypeFlags::STRING | TypeFlags::NUMBER;
        assert!(flags.contains(TypeFlags::STRING));
        assert!(flags.contains(TypeFlags::NUMBER));
        assert!(!flags.contains(TypeFlags::BOOLEAN));
    }

    #[test]
    fn test_type_flags() {
        assert_eq!(Type::Any.flags(), TypeFlags::ANY);
        assert_eq!(Type::Unknown.flags(), TypeFlags::UNKNOWN);
        assert_eq!(Type::Never.flags(), TypeFlags::NEVER);
        assert_eq!(Type::Void.flags(), TypeFlags::VOID);
        assert_eq!(Type::String.flags(), TypeFlags::STRING);
        assert_eq!(Type::Number.flags(), TypeFlags::NUMBER);
        assert_eq!(Type::Boolean.flags(), TypeFlags::BOOLEAN);
    }

    #[test]
    fn test_type_literal_flags() {
        assert_eq!(
            Type::StringLiteral("hello".to_string()).flags(),
            TypeFlags::STRING_LITERAL
        );
        assert_eq!(Type::NumberLiteral(42.0).flags(), TypeFlags::NUMBER_LITERAL);
        assert_eq!(
            Type::BooleanLiteral(true).flags(),
            TypeFlags::BOOLEAN_LITERAL
        );
    }

    #[test]
    fn test_any_assignable_to_anything() {
        let any = Type::Any;
        assert!(any.is_assignable_to(&Type::String));
        assert!(any.is_assignable_to(&Type::Number));
        assert!(any.is_assignable_to(&Type::Boolean));
        assert!(any.is_assignable_to(&Type::Void));
    }

    #[test]
    fn test_anything_assignable_to_any() {
        let any = Type::Any;
        assert!(Type::String.is_assignable_to(&any));
        assert!(Type::Number.is_assignable_to(&any));
        assert!(Type::Boolean.is_assignable_to(&any));
    }

    #[test]
    fn test_anything_assignable_to_unknown() {
        let unknown = Type::Unknown;
        assert!(Type::String.is_assignable_to(&unknown));
        assert!(Type::Number.is_assignable_to(&unknown));
        assert!(Type::Any.is_assignable_to(&unknown));
    }

    #[test]
    fn test_never_assignable_to_everything() {
        let never = Type::Never;
        assert!(never.is_assignable_to(&Type::String));
        assert!(never.is_assignable_to(&Type::Number));
        assert!(never.is_assignable_to(&Type::Never));
    }

    #[test]
    fn test_nothing_assignable_to_never() {
        let never = Type::Never;
        assert!(!Type::String.is_assignable_to(&never));
        assert!(!Type::Number.is_assignable_to(&never));
        // Note: Any is special and is assignable to everything
        // This is by TypeScript design
    }

    #[test]
    fn test_same_primitives_assignable() {
        assert!(Type::String.is_assignable_to(&Type::String));
        assert!(Type::Number.is_assignable_to(&Type::Number));
        assert!(Type::Boolean.is_assignable_to(&Type::Boolean));
        assert!(Type::Void.is_assignable_to(&Type::Void));
        assert!(Type::Null.is_assignable_to(&Type::Null));
        assert!(Type::Undefined.is_assignable_to(&Type::Undefined));
    }

    #[test]
    fn test_different_primitives_not_assignable() {
        assert!(!Type::String.is_assignable_to(&Type::Number));
        assert!(!Type::Number.is_assignable_to(&Type::Boolean));
        assert!(!Type::Boolean.is_assignable_to(&Type::String));
    }

    #[test]
    fn test_string_literal_assignable_to_string() {
        let literal = Type::StringLiteral("hello".to_string());
        assert!(literal.is_assignable_to(&Type::String));
    }

    #[test]
    fn test_number_literal_assignable_to_number() {
        let literal = Type::NumberLiteral(42.0);
        assert!(literal.is_assignable_to(&Type::Number));
    }

    #[test]
    fn test_boolean_literal_assignable_to_boolean() {
        let literal = Type::BooleanLiteral(true);
        assert!(literal.is_assignable_to(&Type::Boolean));
    }

    #[test]
    fn test_same_string_literals_assignable() {
        let a = Type::StringLiteral("hello".to_string());
        let b = Type::StringLiteral("hello".to_string());
        assert!(a.is_assignable_to(&b));
    }

    #[test]
    fn test_different_string_literals_not_assignable() {
        let a = Type::StringLiteral("hello".to_string());
        let b = Type::StringLiteral("world".to_string());
        assert!(!a.is_assignable_to(&b));
    }

    #[test]
    fn test_array_assignability() {
        let string_array = Type::Array(Box::new(Type::String));
        let another_string_array = Type::Array(Box::new(Type::String));
        let number_array = Type::Array(Box::new(Type::Number));

        assert!(string_array.is_assignable_to(&another_string_array));
        assert!(!string_array.is_assignable_to(&number_array));
    }

    #[test]
    fn test_union_target_assignability() {
        let string_or_number = Type::Union(vec![Type::String, Type::Number]);

        // string is assignable to string | number
        assert!(Type::String.is_assignable_to(&string_or_number));
        // number is assignable to string | number
        assert!(Type::Number.is_assignable_to(&string_or_number));
        // boolean is NOT assignable to string | number
        assert!(!Type::Boolean.is_assignable_to(&string_or_number));
    }

    #[test]
    fn test_union_source_assignability() {
        let string_or_number = Type::Union(vec![Type::String, Type::Number]);

        // string | number is NOT assignable to string alone
        assert!(!string_or_number.is_assignable_to(&Type::String));
    }

    #[test]
    fn test_undefined_assignable_to_void() {
        assert!(Type::Undefined.is_assignable_to(&Type::Void));
    }

    #[test]
    fn test_object_type_default() {
        let obj = ObjectType::default();
        assert!(obj.properties.is_empty());
        assert!(obj.index_signatures.is_empty());
        assert!(obj.call_signatures.is_empty());
        assert!(obj.construct_signatures.is_empty());
    }

    #[test]
    fn test_property_creation() {
        let prop = Property {
            name: "test".to_string(),
            ty: Box::new(Type::String),
            optional: false,
            readonly: false,
        };

        assert_eq!(prop.name, "test");
        assert!(!prop.optional);
        assert!(!prop.readonly);
    }

    #[test]
    fn test_function_type() {
        let func = FunctionType {
            type_parameters: Vec::new(),
            parameters: vec![Parameter {
                name: "x".to_string(),
                ty: Box::new(Type::Number),
                optional: false,
                rest: false,
            }],
            return_type: Box::new(Type::String),
            this_type: None,
        };

        assert_eq!(func.parameters.len(), 1);
        assert_eq!(func.parameters[0].name, "x");
    }

    #[test]
    fn test_type_parameter() {
        let param = TypeParameter {
            name: "T".to_string(),
            constraint: Some(Box::new(Type::String)),
            default: None,
        };

        assert_eq!(param.name, "T");
        assert!(param.constraint.is_some());
        assert!(param.default.is_none());
    }

    #[test]
    fn test_type_reference() {
        let reference = TypeReference {
            name: "Array".to_string(),
            type_arguments: vec![Type::String],
        };

        assert_eq!(reference.name, "Array");
        assert_eq!(reference.type_arguments.len(), 1);
    }

    #[test]
    fn test_intersection_assignability() {
        let a_and_b = Type::Intersection(vec![
            Type::Object(ObjectType::default()),
            Type::Object(ObjectType::default()),
        ]);

        // For now, basic test - full implementation would check properties
        assert!(Type::Any.is_assignable_to(&a_and_b));
    }
}