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

rokucommunity / brighterscript / #13592

13 Jan 2025 02:40PM UTC coverage: 86.911%. Remained the same
#13592

push

web-flow
Merge 38702985d into 9d6ef67ba

12071 of 14663 branches covered (82.32%)

Branch coverage included in aggregate %.

90 of 94 new or added lines in 11 files covered. (95.74%)

93 existing lines in 8 files now uncovered.

13048 of 14239 relevant lines covered (91.64%)

31884.8 hits per line

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

95.8
/src/types/TypedFunctionType.ts
1
import { isDynamicType, isObjectType, isTypedFunctionType } from '../astUtils/reflection';
1✔
2
import { BaseFunctionType } from './BaseFunctionType';
1✔
3
import type { BscType } from './BscType';
4
import { BscTypeKind } from './BscTypeKind';
1✔
5
import { isUnionTypeCompatible } from './helpers';
1✔
6
import { BuiltInInterfaceAdder } from './BuiltInInterfaceAdder';
1✔
7
import type { TypeCompatibilityData } from '../interfaces';
8
import { CallExpression } from '../parser/Expression';
1✔
9

10
export class TypedFunctionType extends BaseFunctionType {
1✔
11
    constructor(
12
        public returnType: BscType
3,956,147✔
13
    ) {
14
        super();
3,956,147✔
15
    }
16

17
    public readonly kind = BscTypeKind.TypedFunctionType;
3,956,147✔
18

19
    /**
20
     * The name of the function for this type. Can be null
21
     */
22
    public name: string;
23

24
    /**
25
     * Determines if this is a sub or not
26
     */
27
    public isSub = false;
3,956,147✔
28

29
    /**
30
     * Does this function accept more args than just those in this.params
31
     */
32
    public isVariadic = false;
3,956,147✔
33

34
    public params = [] as Array<{ name: string; type: BscType; isOptional: boolean }>;
3,956,147✔
35

36
    public setName(name: string) {
37
        this.name = name;
7,665✔
38
        return this;
7,665✔
39
    }
40

41
    public addParameter(name: string, type: BscType, isOptional = false) {
11✔
42
        this.params.push({
3,085,898✔
43
            name: name,
44
            type: type,
45
            isOptional: isOptional === true ? true : false
3,085,898✔
46
        });
47
        return this;
3,085,898✔
48
    }
49

50
    public seVariadic(variadic: boolean) {
51
        this.isVariadic = variadic;
1✔
52
        return this;
1✔
53
    }
54

55
    public isTypeCompatible(targetType: BscType, data: TypeCompatibilityData = {}) {
26✔
56
        if (
459✔
57
            isDynamicType(targetType) ||
1,375✔
58
            isObjectType(targetType) ||
59
            isUnionTypeCompatible(this, targetType, data)
60
        ) {
61
            return true;
1✔
62
        }
63
        if (isTypedFunctionType(targetType)) {
458✔
64
            return this.checkParamsAndReturnValue(targetType, true, (t1, t2, d) => t1.isTypeCompatible(t2, d), data);
761✔
65
        }
66
        return false;
2✔
67
    }
68

69
    public toString() {
70
        let paramTexts = [];
8,374✔
71
        for (let param of this.params) {
8,374✔
72
            paramTexts.push(`${param.name}${param.isOptional ? '?' : ''} as ${param.type.toString()}`);
11,160✔
73
        }
74
        let variadicText = '';
8,374✔
75
        if (this.isVariadic) {
8,374✔
76
            if (paramTexts.length > 0) {
8!
UNCOV
77
                variadicText += ', ';
×
78
            }
79
            variadicText += '...';
8✔
80
        }
81
        return `${this.isSub ? 'sub' : 'function'} ${this.name ?? ''}(${paramTexts.join(', ')}${variadicText}) as ${this.returnType.toString()}`;
8,374✔
82
    }
83

84
    public toTypeString(): string {
UNCOV
85
        return 'Function';
×
86
    }
87

88
    isEqual(targetType: BscType) {
89
        if (isTypedFunctionType(targetType)) {
187!
90
            return this.checkParamsAndReturnValue(targetType, false, (t1, t2) => t1.isEqual(t2));
243✔
91
        }
UNCOV
92
        return false;
×
93
    }
94

95
    private checkParamsAndReturnValue(targetType: TypedFunctionType, allowOptionalParamDifferences: boolean, predicate: (type1: BscType, type2: BscType, data: TypeCompatibilityData) => boolean, data?: TypeCompatibilityData) {
96
        //compare all parameters
97
        let len = Math.max(this.params.length, targetType.params.length);
643✔
98
        for (let i = 0; i < len; i++) {
643✔
99
            let myParam = this.params[i];
407✔
100
            let targetParam = targetType.params[i];
407✔
101
            if (allowOptionalParamDifferences && !myParam && targetParam.isOptional) {
407✔
102
                // target func has MORE (optional) params... that's ok
103
                break;
1✔
104
            }
105

106
            if (!myParam || !targetParam || !predicate(targetParam.type, myParam.type, data)) {
406✔
107
                return false;
29✔
108
            }
109
            if (!allowOptionalParamDifferences && myParam.isOptional !== targetParam.isOptional) {
377✔
110
                return false;
1✔
111
            } else if (!myParam.isOptional && targetParam.isOptional) {
376✔
112
                return false;
1✔
113
            }
114
        }
115
        //compare return type
116
        if (!this.returnType || !targetType.returnType || !predicate(this.returnType, targetType.returnType, data)) {
612✔
117
            return false;
46✔
118
        }
119
        if (this.isVariadic !== targetType.isVariadic) {
566✔
120
            return false;
1✔
121
        }
122
        //made it here, all params and return type  pass predicate
123
        return true;
565✔
124
    }
125

126
    public getMinMaxParamCount(): { minParams: number; maxParams: number } {
127
        //get min/max parameter count for callable
128
        let minParams = 0;
681✔
129
        let maxParams = 0;
681✔
130
        for (let param of this.params) {
681✔
131
            maxParams++;
910✔
132
            //optional parameters must come last, so we can assume that minParams won't increase once we hit
133
            //the first isOptional
134
            if (param.isOptional !== true) {
910✔
135
                minParams++;
494✔
136
            }
137
        }
138
        if (this.isVariadic) {
681✔
139
            // function accepts variable number of arguments
140
            maxParams = CallExpression.MaximumArguments;
5✔
141
        }
142
        return { minParams: minParams, maxParams: maxParams };
681✔
143
    }
144
}
145

146
BuiltInInterfaceAdder.typedFunctionFactory = (returnType: BscType) => {
1✔
147
    return new TypedFunctionType(returnType);
3,951,725✔
148
};
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