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

rokucommunity / brighterscript / #12717

14 Jun 2024 08:20PM UTC coverage: 85.629% (-2.3%) from 87.936%
#12717

push

web-flow
Merge 94311dc0a into 42db50190

10808 of 13500 branches covered (80.06%)

Branch coverage included in aggregate %.

6557 of 7163 new or added lines in 96 files covered. (91.54%)

83 existing lines in 17 files now uncovered.

12270 of 13451 relevant lines covered (91.22%)

26531.66 hits per line

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

95.1
/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
3,151,886✔
12
    ) {
13
        super();
3,151,886✔
14
    }
15

16
    public readonly kind = BscTypeKind.TypedFunctionType;
3,151,886✔
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;
3,151,886✔
27

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

33
    public params = [] as Array<{ name: string; type: BscType; isOptional: boolean }>;
3,151,886✔
34

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

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

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

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

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

82
    isEqual(targetType: BscType) {
83
        if (isTypedFunctionType(targetType)) {
171!
84
            return this.checkParamsAndReturnValue(targetType, false, (t1, t2) => t1.isEqual(t2));
230✔
85
        }
NEW
86
        return false;
×
87
    }
88

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

100
            if (!myParam || !targetParam || !predicate(targetParam.type, myParam.type, data)) {
374✔
101
                return false;
22✔
102
            }
103
            if (!allowOptionalParamDifferences && myParam.isOptional !== targetParam.isOptional) {
352✔
104
                return false;
1✔
105
            } else if (!myParam.isOptional && targetParam.isOptional) {
351✔
106
                return false;
1✔
107
            }
108
        }
109
        //compare return type
110
        if (!this.returnType || !targetType.returnType || !predicate(this.returnType, targetType.returnType, data)) {
583✔
111
            return false;
45✔
112
        }
113
        if (this.isVariadic !== targetType.isVariadic) {
538✔
114
            return false;
1✔
115
        }
116
        //made it here, all params and return type  pass predicate
117
        return true;
537✔
118
    }
119
}
120

121
BuiltInInterfaceAdder.typedFunctionFactory = (returnType: BscType) => {
1✔
122
    return new TypedFunctionType(returnType);
3,148,186✔
123
};
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