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

rokucommunity / brighterscript / #13388

29 Nov 2024 07:55PM UTC coverage: 86.824%. Remained the same
#13388

push

web-flow
Merge b1d7d7253 into 57fa2ad4d

12087 of 14723 branches covered (82.1%)

Branch coverage included in aggregate %.

379 of 407 new or added lines in 36 files covered. (93.12%)

244 existing lines in 22 files now uncovered.

13071 of 14253 relevant lines covered (91.71%)

33084.46 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
3,911,241✔
12
    ) {
13
        super();
3,911,241✔
14
    }
15

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

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

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

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

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

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

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

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

82
    isEqual(targetType: BscType, data: TypeCompatibilityData = {}) {
88✔
83
        if (isTypedFunctionType(targetType)) {
190!
84
            if (this.toString().toLowerCase() === targetType.toString().toLowerCase()) {
190✔
85
                // this function has the same param names and types and return type as the target
86
                return true;
135✔
87
            }
88
            return this.checkParamsAndReturnValue(targetType, false, (t1, t2, predData = {}) => {
55!
89
                return t1.isEqual(t2, { ...predData, allowNameEquality: true });
60✔
90
            }, data);
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);
21,763✔
98
        for (let i = 0; i < len; i++) {
21,763✔
99
            let myParam = this.params[i];
18,950✔
100
            let targetParam = targetType.params[i];
18,950✔
101
            if (allowOptionalParamDifferences && !myParam && targetParam.isOptional) {
18,950✔
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)) {
18,949✔
107
                return false;
297✔
108
            }
109
            if (!allowOptionalParamDifferences && myParam.isOptional !== targetParam.isOptional) {
18,652✔
110
                return false;
1✔
111
            } else if (!myParam.isOptional && targetParam.isOptional) {
18,651✔
112
                return false;
1✔
113
            }
114
        }
115
        //compare return type
116
        if (!this.returnType || !targetType.returnType || !predicate(this.returnType, targetType.returnType, data)) {
21,464✔
117
            return false;
45✔
118
        }
119
        if (this.isVariadic !== targetType.isVariadic) {
21,419✔
120
            return false;
1✔
121
        }
122
        //made it here, all params and return type  pass predicate
123
        return true;
21,418✔
124
    }
125
}
126

127
BuiltInInterfaceAdder.typedFunctionFactory = (returnType: BscType) => {
1✔
128
    return new TypedFunctionType(returnType);
3,906,894✔
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