|
| 1 | +import { TSTypeAliasDeclaration } from '@babel/types' |
| 2 | +import { parse } from '../../src' |
| 3 | +import { ScriptCompileContext } from '../../src/script/context' |
| 4 | +import { |
| 5 | + inferRuntimeType, |
| 6 | + resolveTypeElements |
| 7 | +} from '../../src/script/resolveType' |
| 8 | + |
| 9 | +describe('resolveType', () => { |
| 10 | + test('type literal', () => { |
| 11 | + const { elements, callSignatures } = resolve(`type Target = { |
| 12 | + foo: number // property |
| 13 | + bar(): void // method |
| 14 | + 'baz': string // string literal key |
| 15 | + (e: 'foo'): void // call signature |
| 16 | + (e: 'bar'): void |
| 17 | + }`) |
| 18 | + expect(elements).toStrictEqual({ |
| 19 | + foo: ['Number'], |
| 20 | + bar: ['Function'], |
| 21 | + baz: ['String'] |
| 22 | + }) |
| 23 | + expect(callSignatures?.length).toBe(2) |
| 24 | + }) |
| 25 | + |
| 26 | + test('reference type', () => { |
| 27 | + expect( |
| 28 | + resolve(` |
| 29 | + type Aliased = { foo: number } |
| 30 | + type Target = Aliased |
| 31 | + `).elements |
| 32 | + ).toStrictEqual({ |
| 33 | + foo: ['Number'] |
| 34 | + }) |
| 35 | + }) |
| 36 | + |
| 37 | + test('reference exported type', () => { |
| 38 | + expect( |
| 39 | + resolve(` |
| 40 | + export type Aliased = { foo: number } |
| 41 | + type Target = Aliased |
| 42 | + `).elements |
| 43 | + ).toStrictEqual({ |
| 44 | + foo: ['Number'] |
| 45 | + }) |
| 46 | + }) |
| 47 | + |
| 48 | + test('reference interface', () => { |
| 49 | + expect( |
| 50 | + resolve(` |
| 51 | + interface Aliased { foo: number } |
| 52 | + type Target = Aliased |
| 53 | + `).elements |
| 54 | + ).toStrictEqual({ |
| 55 | + foo: ['Number'] |
| 56 | + }) |
| 57 | + }) |
| 58 | + |
| 59 | + test('reference exported interface', () => { |
| 60 | + expect( |
| 61 | + resolve(` |
| 62 | + export interface Aliased { foo: number } |
| 63 | + type Target = Aliased |
| 64 | + `).elements |
| 65 | + ).toStrictEqual({ |
| 66 | + foo: ['Number'] |
| 67 | + }) |
| 68 | + }) |
| 69 | + |
| 70 | + test('reference interface extends', () => { |
| 71 | + expect( |
| 72 | + resolve(` |
| 73 | + export interface A { a(): void } |
| 74 | + export interface B extends A { b: boolean } |
| 75 | + interface C { c: string } |
| 76 | + interface Aliased extends B, C { foo: number } |
| 77 | + type Target = Aliased |
| 78 | + `).elements |
| 79 | + ).toStrictEqual({ |
| 80 | + a: ['Function'], |
| 81 | + b: ['Boolean'], |
| 82 | + c: ['String'], |
| 83 | + foo: ['Number'] |
| 84 | + }) |
| 85 | + }) |
| 86 | + |
| 87 | + test('function type', () => { |
| 88 | + expect( |
| 89 | + resolve(` |
| 90 | + type Target = (e: 'foo') => void |
| 91 | + `).callSignatures?.length |
| 92 | + ).toBe(1) |
| 93 | + }) |
| 94 | + |
| 95 | + test('reference function type', () => { |
| 96 | + expect( |
| 97 | + resolve(` |
| 98 | + type Fn = (e: 'foo') => void |
| 99 | + type Target = Fn |
| 100 | + `).callSignatures?.length |
| 101 | + ).toBe(1) |
| 102 | + }) |
| 103 | + |
| 104 | + test('intersection type', () => { |
| 105 | + expect( |
| 106 | + resolve(` |
| 107 | + type Foo = { foo: number } |
| 108 | + type Bar = { bar: string } |
| 109 | + type Baz = { bar: string | boolean } |
| 110 | + type Target = { self: any } & Foo & Bar & Baz |
| 111 | + `).elements |
| 112 | + ).toStrictEqual({ |
| 113 | + self: ['Unknown'], |
| 114 | + foo: ['Number'], |
| 115 | + // both Bar & Baz has 'bar', but Baz['bar] is wider so it should be |
| 116 | + // preferred |
| 117 | + bar: ['String', 'Boolean'] |
| 118 | + }) |
| 119 | + }) |
| 120 | + |
| 121 | + // #7553 |
| 122 | + test('union type', () => { |
| 123 | + expect( |
| 124 | + resolve(` |
| 125 | + interface CommonProps { |
| 126 | + size?: 'xl' | 'l' | 'm' | 's' | 'xs' |
| 127 | + } |
| 128 | +
|
| 129 | + type ConditionalProps = |
| 130 | + | { |
| 131 | + color: 'normal' | 'primary' | 'secondary' |
| 132 | + appearance: 'normal' | 'outline' | 'text' |
| 133 | + } |
| 134 | + | { |
| 135 | + color: number |
| 136 | + appearance: 'outline' |
| 137 | + note: string |
| 138 | + } |
| 139 | +
|
| 140 | + type Target = CommonProps & ConditionalProps |
| 141 | + `).elements |
| 142 | + ).toStrictEqual({ |
| 143 | + size: ['String'], |
| 144 | + color: ['String', 'Number'], |
| 145 | + appearance: ['String'], |
| 146 | + note: ['String'] |
| 147 | + }) |
| 148 | + }) |
| 149 | + |
| 150 | + // describe('built-in utility types', () => { |
| 151 | + |
| 152 | + // }) |
| 153 | + |
| 154 | + describe('errors', () => { |
| 155 | + test('error on computed keys', () => { |
| 156 | + expect(() => resolve(`type Target = { [Foo]: string }`)).toThrow( |
| 157 | + `computed keys are not supported in types referenced by SFC macros` |
| 158 | + ) |
| 159 | + }) |
| 160 | + }) |
| 161 | +}) |
| 162 | + |
| 163 | +function resolve(code: string) { |
| 164 | + const { descriptor } = parse(`<script setup lang="ts">${code}</script>`) |
| 165 | + const ctx = new ScriptCompileContext(descriptor, { id: 'test' }) |
| 166 | + const targetDecl = ctx.scriptSetupAst!.body.find( |
| 167 | + s => s.type === 'TSTypeAliasDeclaration' && s.id.name === 'Target' |
| 168 | + ) as TSTypeAliasDeclaration |
| 169 | + const raw = resolveTypeElements(ctx, targetDecl.typeAnnotation) |
| 170 | + const elements: Record<string, string[]> = {} |
| 171 | + for (const key in raw) { |
| 172 | + elements[key] = inferRuntimeType(ctx, raw[key]) |
| 173 | + } |
| 174 | + return { |
| 175 | + elements, |
| 176 | + callSignatures: raw.__callSignatures, |
| 177 | + raw |
| 178 | + } |
| 179 | +} |
0 commit comments