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

rokucommunity / brighterscript / #12931

13 Aug 2024 05:02PM UTC coverage: 86.193% (-1.7%) from 87.933%
#12931

push

web-flow
Merge 58ad447a2 into 0e968f1c3

10630 of 13125 branches covered (80.99%)

Branch coverage included in aggregate %.

6675 of 7284 new or added lines in 99 files covered. (91.64%)

84 existing lines in 18 files now uncovered.

12312 of 13492 relevant lines covered (91.25%)

26867.76 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,206,560✔
12
    ) {
13
        super();
3,206,560✔
14
    }
15

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

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

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

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

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

49
    public isTypeCompatible(targetType: BscType, data: TypeCompatibilityData = {}) {
26✔
50
        if (
444✔
51
            isDynamicType(targetType) ||
1,330✔
52
            isObjectType(targetType) ||
53
            isUnionTypeCompatible(this, targetType, data)
54
        ) {
55
            return true;
1✔
56
        }
57
        if (isTypedFunctionType(targetType)) {
443✔
58
            return this.checkParamsAndReturnValue(targetType, true, (t1, t2, d) => t1.isTypeCompatible(t2, d), data);
735✔
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)) {
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);
612✔
92
        for (let i = 0; i < len; i++) {
612✔
93
            let myParam = this.params[i];
385✔
94
            let targetParam = targetType.params[i];
385✔
95
            if (allowOptionalParamDifferences && !myParam && targetParam.isOptional) {
385✔
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)) {
384✔
101
                return false;
22✔
102
            }
103
            if (!allowOptionalParamDifferences && myParam.isOptional !== targetParam.isOptional) {
362✔
104
                return false;
1✔
105
            } else if (!myParam.isOptional && targetParam.isOptional) {
361✔
106
                return false;
1✔
107
            }
108
        }
109
        //compare return type
110
        if (!this.returnType || !targetType.returnType || !predicate(this.returnType, targetType.returnType, data)) {
588✔
111
            return false;
45✔
112
        }
113
        if (this.isVariadic !== targetType.isVariadic) {
543✔
114
            return false;
1✔
115
        }
116
        //made it here, all params and return type  pass predicate
117
        return true;
542✔
118
    }
119
}
120

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