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

rokucommunity / brighterscript / #13117

01 Oct 2024 08:24AM UTC coverage: 86.842% (-1.4%) from 88.193%
#13117

push

web-flow
Merge abd960cd5 into 3a2dc7282

11537 of 14048 branches covered (82.13%)

Branch coverage included in aggregate %.

6991 of 7582 new or added lines in 100 files covered. (92.21%)

83 existing lines in 18 files now uncovered.

12692 of 13852 relevant lines covered (91.63%)

29478.96 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,595,480✔
12
    ) {
13
        super();
3,595,480✔
14
    }
15

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

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

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

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

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

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

63
    public toString() {
64
        let paramTexts = [];
8,365✔
65
        for (let param of this.params) {
8,365✔
66
            paramTexts.push(`${param.name}${param.isOptional ? '?' : ''} as ${param.type.toString()}`);
11,157✔
67
        }
68
        let variadicText = '';
8,365✔
69
        if (this.isVariadic) {
8,365✔
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,365✔
76
    }
77

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

82
    isEqual(targetType: BscType) {
83
        if (isTypedFunctionType(targetType)) {
175!
84
            return this.checkParamsAndReturnValue(targetType, false, (t1, t2) => t1.isEqual(t2));
234✔
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);
631✔
92
        for (let i = 0; i < len; i++) {
631✔
93
            let myParam = this.params[i];
398✔
94
            let targetParam = targetType.params[i];
398✔
95
            if (allowOptionalParamDifferences && !myParam && targetParam.isOptional) {
398✔
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)) {
397✔
101
                return false;
23✔
102
            }
103
            if (!allowOptionalParamDifferences && myParam.isOptional !== targetParam.isOptional) {
374✔
104
                return false;
1✔
105
            } else if (!myParam.isOptional && targetParam.isOptional) {
373✔
106
                return false;
1✔
107
            }
108
        }
109
        //compare return type
110
        if (!this.returnType || !targetType.returnType || !predicate(this.returnType, targetType.returnType, data)) {
606✔
111
            return false;
46✔
112
        }
113
        if (this.isVariadic !== targetType.isVariadic) {
560✔
114
            return false;
1✔
115
        }
116
        //made it here, all params and return type  pass predicate
117
        return true;
559✔
118
    }
119
}
120

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