|
| 1 | +import * as ts from 'typescript'; |
| 2 | +import { couldBeType } from '../../src/etc/could-be-type'; |
| 3 | +import { createSourceFileAndTypeChecker } from './create-source-file-and-type-checker'; |
| 4 | + |
| 5 | +describe('couldBeType', () => { |
| 6 | + it('should match a specific type', () => { |
| 7 | + const { sourceFile, typeChecker } = createSourceFileAndTypeChecker( |
| 8 | + ` |
| 9 | + class A {} |
| 10 | + let a: A; |
| 11 | + `, |
| 12 | + ); |
| 13 | + const node = (sourceFile.statements[1] as ts.VariableStatement).declarationList.declarations[0]; |
| 14 | + const type = typeChecker.getTypeAtLocation(node); |
| 15 | + |
| 16 | + expect(couldBeType(type, 'A')).toBe(true); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should not match different types', () => { |
| 20 | + const { sourceFile, typeChecker } = createSourceFileAndTypeChecker( |
| 21 | + ` |
| 22 | + class A {} |
| 23 | + class B {} |
| 24 | + let b: B; |
| 25 | + `, |
| 26 | + ); |
| 27 | + const node = (sourceFile.statements[2] as ts.VariableStatement).declarationList.declarations[0]; |
| 28 | + const type = typeChecker.getTypeAtLocation(node); |
| 29 | + |
| 30 | + expect(couldBeType(type, 'A')).toBe(false); |
| 31 | + expect(couldBeType(type, 'B')).toBe(true); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should match a base type', () => { |
| 35 | + const { sourceFile, typeChecker } = createSourceFileAndTypeChecker( |
| 36 | + ` |
| 37 | + class A {} |
| 38 | + class B extends A {} |
| 39 | + let b: B; |
| 40 | + `, |
| 41 | + ); |
| 42 | + const node = (sourceFile.statements[2] as ts.VariableStatement).declarationList.declarations[0]; |
| 43 | + const type = typeChecker.getTypeAtLocation(node); |
| 44 | + |
| 45 | + expect(couldBeType(type, 'A')).toBe(true); |
| 46 | + expect(couldBeType(type, 'B')).toBe(true); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should match an implemented interface', () => { |
| 50 | + const { sourceFile, typeChecker } = createSourceFileAndTypeChecker( |
| 51 | + ` |
| 52 | + interface A { name: string; } |
| 53 | + class B implements A { name = ""; } |
| 54 | + let b: B; |
| 55 | + `, |
| 56 | + ); |
| 57 | + const node = (sourceFile.statements[2] as ts.VariableStatement).declarationList.declarations[0]; |
| 58 | + const type = typeChecker.getTypeAtLocation(node); |
| 59 | + |
| 60 | + expect(couldBeType(type, 'A')).toBe(true); |
| 61 | + expect(couldBeType(type, 'B')).toBe(true); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should match an implemented generic interface', () => { |
| 65 | + const { sourceFile, typeChecker } = createSourceFileAndTypeChecker( |
| 66 | + ` |
| 67 | + interface A<T> { value: T; } |
| 68 | + class B<T> implements A<T> { constructor(public value: T) {} } |
| 69 | + let b = new B<string>("B"); |
| 70 | + `, |
| 71 | + ); |
| 72 | + const node = (sourceFile.statements[2] as ts.VariableStatement).declarationList.declarations[0]; |
| 73 | + const type = typeChecker.getTypeAtLocation(node); |
| 74 | + |
| 75 | + expect(couldBeType(type, 'A')).toBe(true); |
| 76 | + expect(couldBeType(type, 'B')).toBe(true); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should match an intersection type', () => { |
| 80 | + const { sourceFile, typeChecker } = createSourceFileAndTypeChecker( |
| 81 | + ` |
| 82 | + class A {} |
| 83 | + class B {} |
| 84 | + let ab: A & B; |
| 85 | + `, |
| 86 | + ); |
| 87 | + const node = (sourceFile.statements[2] as ts.VariableStatement).declarationList.declarations[0]; |
| 88 | + const type = typeChecker.getTypeAtLocation(node); |
| 89 | + |
| 90 | + expect(couldBeType(type, 'A')).toBe(true); |
| 91 | + expect(couldBeType(type, 'B')).toBe(true); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should match a union type', () => { |
| 95 | + const { sourceFile, typeChecker } = createSourceFileAndTypeChecker( |
| 96 | + ` |
| 97 | + class A {} |
| 98 | + class B {} |
| 99 | + let ab: A | B; |
| 100 | + `, |
| 101 | + ); |
| 102 | + const node = (sourceFile.statements[2] as ts.VariableStatement).declarationList.declarations[0]; |
| 103 | + const type = typeChecker.getTypeAtLocation(node); |
| 104 | + |
| 105 | + expect(couldBeType(type, 'A')).toBe(true); |
| 106 | + expect(couldBeType(type, 'B')).toBe(true); |
| 107 | + }); |
| 108 | + |
| 109 | + it.todo('should support fully-qualified types', () => { |
| 110 | + // TODO: This test is disabled because we're failing to import from other files using @typescript/vfs. See env.languageService.getSemanticDiagnostics(fileName) for error message. |
| 111 | + const { sourceFile, typeChecker } = createSourceFileAndTypeChecker( |
| 112 | + ` |
| 113 | + import { A } from "./a"; |
| 114 | + class B {} |
| 115 | + let a: A; |
| 116 | + let b: B; |
| 117 | + `, |
| 118 | + ); |
| 119 | + const nodeA = (sourceFile.statements[2] as ts.VariableStatement).declarationList.declarations[0]; |
| 120 | + const nodeB = (sourceFile.statements[3] as ts.VariableStatement).declarationList.declarations[0]; |
| 121 | + const typeA = typeChecker.getTypeAtLocation(nodeA); |
| 122 | + const typeB = typeChecker.getTypeAtLocation(nodeB); |
| 123 | + |
| 124 | + expect( |
| 125 | + couldBeType(typeA, 'A', { |
| 126 | + name: /"a"/, |
| 127 | + typeChecker, |
| 128 | + }), |
| 129 | + ).toBe(true); |
| 130 | + expect( |
| 131 | + couldBeType(typeB, 'B', { |
| 132 | + name: /"b"/, |
| 133 | + typeChecker, |
| 134 | + }), |
| 135 | + ).toBe(false); |
| 136 | + }); |
| 137 | +}); |
0 commit comments