• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rokucommunity / brighterscript / #13308

22 Nov 2024 02:25PM UTC coverage: 86.801%. Remained the same
#13308

push

web-flow
Merge 332332a1f into 2a6afd921

11833 of 14419 branches covered (82.07%)

Branch coverage included in aggregate %.

191 of 205 new or added lines in 26 files covered. (93.17%)

201 existing lines in 18 files now uncovered.

12868 of 14038 relevant lines covered (91.67%)

32022.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.16
/src/types/UnionType.ts
1
import type { GetTypeOptions, TypeCompatibilityData } from '../interfaces';
2
import { isDynamicType, isObjectType, isUnionType } from '../astUtils/reflection';
1✔
3
import { BscType } from './BscType';
1✔
4
import { ReferenceType } from './ReferenceType';
1✔
5
import { findTypeUnion, getUniqueType, isEnumTypeCompatible } from './helpers';
1✔
6
import { BscTypeKind } from './BscTypeKind';
1✔
7
import type { TypeCacheEntry } from '../SymbolTable';
8
import { SymbolTable } from '../SymbolTable';
1✔
9
import { SymbolTypeFlag } from '../SymbolTypeFlag';
1✔
10
import { BuiltInInterfaceAdder } from './BuiltInInterfaceAdder';
1✔
11

12
export function unionTypeFactory(types: BscType[]) {
1✔
13
    return new UnionType(types);
114,612✔
14
}
15

16
export class UnionType extends BscType {
1✔
17
    constructor(
18
        public types: BscType[]
170,248✔
19
    ) {
20
        super(joinTypesString(types));
170,248✔
21
    }
22

23
    public readonly kind = BscTypeKind.UnionType;
170,248✔
24

25
    public addType(type: BscType) {
26
        this.types.push(type);
×
27
    }
28

29
    isResolvable(): boolean {
30
        for (const type of this.types) {
62✔
31
            if (!type.isResolvable()) {
128✔
32
                return false;
7✔
33
            }
34
        }
35
        return true;
55✔
36
    }
37

38
    private getMemberTypeFromInnerTypes(name: string, options: GetTypeOptions) {
39
        return this.types.map((innerType) => innerType?.getMemberType(name, options));
243!
40
    }
41

42
    getMemberType(name: string, options: GetTypeOptions) {
43
        const innerTypesMemberTypes = this.getMemberTypeFromInnerTypes(name, options);
29✔
44
        if (!innerTypesMemberTypes || innerTypesMemberTypes.includes(undefined)) {
29✔
45
            // We don't have any members of any inner types that match
46
            // so instead, create reference type that will
47
            return new ReferenceType(name, name, options.flags, () => {
11✔
48
                return {
66✔
49
                    name: `UnionType MemberTable: '${this.__identifier}'`,
50
                    getSymbolType: (innerName: string, innerOptions: GetTypeOptions) => {
51
                        const referenceTypeInnerMemberTypes = this.getMemberTypeFromInnerTypes(name, options);
66✔
52
                        if (!innerTypesMemberTypes || innerTypesMemberTypes.includes(undefined)) {
66!
53
                            return undefined;
66✔
54
                        }
55
                        return getUniqueType(findTypeUnion(referenceTypeInnerMemberTypes), unionTypeFactory);
×
56
                    },
57
                    setCachedType: (innerName: string, innerCacheEntry: TypeCacheEntry, innerOptions: GetTypeOptions) => {
58
                        // TODO: is this even cachable? This is a NO-OP for now, and it shouldn't hurt anything
59
                    },
60
                    addSibling: (symbolTable: SymbolTable) => {
61
                        // TODO: I don't know what this means in this context?
62
                    }
63
                };
64
            });
65
        }
66
        return getUniqueType(findTypeUnion(innerTypesMemberTypes), unionTypeFactory);
18✔
67
    }
68

69
    isTypeCompatible(targetType: BscType, data?: TypeCompatibilityData): boolean {
70
        if (isDynamicType(targetType) || isObjectType(targetType)) {
54✔
71
            return true;
5✔
72
        }
73
        if (isEnumTypeCompatible(this, targetType, data)) {
49!
UNCOV
74
            return true;
×
75
        }
76
        if (isUnionType(targetType)) {
49✔
77
            // check if this set of inner types is a SUPERSET of targetTypes's inner types
78
            for (const targetInnerType of targetType.types) {
13✔
79
                if (!this.isTypeCompatible(targetInnerType, data)) {
30✔
80
                    return false;
2✔
81
                }
82
            }
83
            return true;
11✔
84
        }
85
        for (const innerType of this.types) {
36✔
86
            const foundCompatibleInnerType = innerType.isTypeCompatible(targetType, data);
58✔
87
            if (foundCompatibleInnerType) {
58✔
88
                return true;
32✔
89
            }
90
        }
91

92

93
        return false;
4✔
94
    }
95
    toString(): string {
96
        return joinTypesString(this.types);
170✔
97
    }
98
    toTypeString(): string {
UNCOV
99
        return 'dynamic';
×
100
    }
101

102
    checkAllMemberTypes(predicate: (BscType) => boolean) {
UNCOV
103
        return this.types.reduce((acc, type) => {
×
UNCOV
104
            return acc && predicate(type);
×
105
        }, true);
106
    }
107

108
    isEqual(targetType: BscType): boolean {
109
        if (!isUnionType(targetType)) {
2✔
110
            return false;
1✔
111
        }
112
        return this.isTypeCompatible(targetType) && targetType.isTypeCompatible(this);
1✔
113
    }
114

115
    getMemberTable(): SymbolTable {
116
        const unionTable = new SymbolTable(this.__identifier + ' UnionTable');
4✔
117
        const firstType = this.types[0];
4✔
118
        if (!firstType) {
4!
UNCOV
119
            return unionTable;
×
120
        }
121
        firstType.addBuiltInInterfaces();
4✔
122
        for (const symbol of firstType.getMemberTable().getAllSymbols(SymbolTypeFlag.runtime)) {
4✔
123
            const foundType = this.getMemberTypeFromInnerTypes(symbol.name, { flags: SymbolTypeFlag.runtime });
19✔
124
            const allResolvableTypes = foundType.reduce((acc, curType) => {
19✔
125
                return acc && curType?.isResolvable();
53!
126
            }, true);
127

128
            if (!allResolvableTypes) {
19✔
129
                continue;
16✔
130
            }
131
            const uniqueType = getUniqueType(findTypeUnion(foundType), unionTypeFactory);
3✔
132
            unionTable.addSymbol(symbol.name, {}, uniqueType, SymbolTypeFlag.runtime);
3✔
133
        }
134
        return unionTable;
4✔
135
    }
136
}
137

138

139
function joinTypesString(types: BscType[]) {
140
    return types.map(t => t.toString()).join(' or ');
340,860✔
141
}
142

143
BuiltInInterfaceAdder.unionTypeFactory = (types: BscType[]) => {
1✔
144
    return new UnionType(types);
55,548✔
145
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc