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

rokucommunity / brighterscript / #13683

03 Feb 2025 04:19PM UTC coverage: 86.753% (-1.4%) from 88.185%
#13683

push

web-flow
Merge 34e72243e into 4afb6f658

12476 of 15203 branches covered (82.06%)

Branch coverage included in aggregate %.

7751 of 8408 new or added lines in 101 files covered. (92.19%)

85 existing lines in 17 files now uncovered.

13398 of 14622 relevant lines covered (91.63%)

34302.41 hits per line

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

96.33
/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

9
export class TypedFunctionType extends BaseFunctionType {
1✔
10
    constructor(
11
        public returnType: BscType
4,098,710✔
12
    ) {
13
        super();
4,098,710✔
14
    }
15

16
    public readonly kind = BscTypeKind.TypedFunctionType;
4,098,710✔
17

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

23
    /**
24
     * Determines if this is a sub or not
25
     */
26
    public isSub = false;
4,098,710✔
27

28
    /**
29
     * Does this function accept more args than just those in this.params
30
     */
31
    public isVariadic = false;
4,098,710✔
32

33
    public params = [] as Array<{ name: string; type: BscType; isOptional: boolean }>;
4,098,710✔
34

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

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

49
    public isTypeCompatible(targetType: BscType, data: TypeCompatibilityData = {}) {
26✔
50
        if (
19,931✔
51
            isDynamicType(targetType) ||
59,791✔
52
            isObjectType(targetType) ||
53
            isUnionTypeCompatible(this, targetType, data)
54
        ) {
55
            return true;
1✔
56
        }
57
        if (isTypedFunctionType(targetType)) {
19,930✔
58
            return this.checkParamsAndReturnValue(targetType, true, (t1, t2, d) => t1.isTypeCompatible(t2, d), data);
39,177✔
59
        }
60
        return false;
2✔
61
    }
62

63
    public toString() {
64
        let paramTexts = [];
8,774✔
65
        for (let param of this.params) {
8,774✔
66
            paramTexts.push(`${param.name}${param.isOptional ? '?' : ''} as ${param.type.toString()}`);
11,409✔
67
        }
68
        let variadicText = '';
8,774✔
69
        if (this.isVariadic) {
8,774✔
70
            if (paramTexts.length > 0) {
11✔
71
                variadicText += ', ';
1✔
72
            }
73
            variadicText += '...';
11✔
74
        }
75
        return `${this.isSub ? 'sub' : 'function'} ${this.name ?? ''}(${paramTexts.join(', ')}${variadicText}) as ${this.returnType.toString()}`;
8,774✔
76
    }
77

78
    public toTypeString(): string {
NEW
79
        return 'Function';
×
80
    }
81

82
    isEqual(targetType: BscType, data: TypeCompatibilityData = {}) {
96✔
83
        if (isTypedFunctionType(targetType)) {
198!
84
            if (this.toString().toLowerCase() === targetType.toString().toLowerCase()) {
198✔
85
                // this function has the same param names and types and return type as the target
86
                return true;
143✔
87
            }
88
            return this.checkParamsAndReturnValue(targetType, false, (t1, t2, predData = {}) => {
55!
89
                return t1.isEqual(t2, { ...predData, allowNameEquality: true });
60✔
90
            }, data);
91
        }
NEW
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);
19,983✔
98
        for (let i = 0; i < len; i++) {
19,983✔
99
            let myParam = this.params[i];
19,301✔
100
            let targetParam = targetType.params[i];
19,301✔
101
            if (allowOptionalParamDifferences && !myParam && targetParam.isOptional) {
19,301✔
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)) {
19,300✔
107
                return false;
29✔
108
            }
109
            if (!allowOptionalParamDifferences && myParam.isOptional !== targetParam.isOptional) {
19,271✔
110
                return false;
1✔
111
            } else if (!myParam.isOptional && targetParam.isOptional) {
19,270✔
112
                return false;
1✔
113
            }
114
        }
115
        //compare return type
116
        if (!this.returnType || !targetType.returnType || !predicate(this.returnType, targetType.returnType, data)) {
19,952✔
117
            return false;
45✔
118
        }
119
        if (this.isVariadic !== targetType.isVariadic) {
19,907✔
120
            return false;
1✔
121
        }
122
        //made it here, all params and return type  pass predicate
123
        return true;
19,906✔
124
    }
125
}
126

127
BuiltInInterfaceAdder.typedFunctionFactory = (returnType: BscType) => {
1✔
128
    return new TypedFunctionType(returnType);
4,094,137✔
129
};
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