• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2* Copyright (c) Microsoft Corporation. All rights reserved.
3* Copyright (c) 2023 Huawei Device Co., Ltd.
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8*     http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*
16* This file has been modified by Huawei to verify type inference by adding verification statements.
17*/
18
19// === tests/cases/conformance/jsdoc/declarations/source.js ===
20declare function AssertType(value:any, type:string):void;
21/**
22 * @param {number} len
23 */
24export function Vec(len) {
25    /**
26     * @type {number[]}
27     */
28    this.storage = new Array(len);
29AssertType(this.storage = new Array(len), "any[]");
30AssertType(this.storage, "number[]");
31AssertType(this, "this");
32AssertType(new Array(len), "any[]");
33AssertType(Array, "ArrayConstructor");
34AssertType(len, "number");
35}
36
37Vec.prototype = {
38AssertType(Vec.prototype, "{ dot(Vec): number; magnitude(): number; }");
39AssertType({    /**     * @param {Vec} other     */    dot(other) {        if (other.storage.length !== this.storage.length) {            throw new Error(`Dot product only applicable for vectors of equal length`);        }        let sum = 0;        for (let i = 0; i < this.storage.length; i++) {            sum += (this.storage[i] * other.storage[i]);        }        return sum;    },    magnitude() {        let sum = 0;        for (let i = 0; i < this.storage.length; i++) {            sum += (this.storage[i] ** 2);        }        return Math.sqrt(sum);    }}, "{ dot(Vec): number; magnitude(): number; }");
40AssertType(Vec.prototype = {    /**     * @param {Vec} other     */    dot(other) {        if (other.storage.length !== this.storage.length) {            throw new Error(`Dot product only applicable for vectors of equal length`);        }        let sum = 0;        for (let i = 0; i < this.storage.length; i++) {            sum += (this.storage[i] * other.storage[i]);        }        return sum;    },    magnitude() {        let sum = 0;        for (let i = 0; i < this.storage.length; i++) {            sum += (this.storage[i] ** 2);        }        return Math.sqrt(sum);    }}, "{ dot(Vec): number; magnitude(): number; }");
41
42    /**
43     * @param {Vec} other
44     */
45    dot(other) {
46AssertType(dot, "(Vec) => number");
47AssertType(other, "Vec");
48
49        if (other.storage.length !== this.storage.length) {
50AssertType(other.storage.length !== this.storage.length, "boolean");
51AssertType(other.storage.length, "number");
52AssertType(other.storage, "number[]");
53AssertType(this.storage.length, "number");
54AssertType(this.storage, "number[]");
55AssertType(this, "this");
56
57            throw new Error(`Dot product only applicable for vectors of equal length`);
58AssertType(new Error(`Dot product only applicable for vectors of equal length`), "Error");
59AssertType(Error, "ErrorConstructor");
60AssertType(`Dot product only applicable for vectors of equal length`, "string");
61        }
62        let sum = 0;
63AssertType(sum, "number");
64AssertType(0, "int");
65
66        for (let i = 0; i < this.storage.length; i++) {
67AssertType(i, "number");
68AssertType(0, "int");
69AssertType(i < this.storage.length, "boolean");
70AssertType(i, "number");
71AssertType(this.storage.length, "number");
72AssertType(this.storage, "number[]");
73AssertType(this, "this");
74AssertType(i++, "number");
75AssertType(i, "number");
76
77            sum += (this.storage[i] * other.storage[i]);
78AssertType(sum += (this.storage[i] * other.storage[i]), "number");
79AssertType(sum, "number");
80AssertType((this.storage[i] * other.storage[i]), "number");
81AssertType(this.storage[i] * other.storage[i], "number");
82AssertType(this.storage[i], "number");
83AssertType(this.storage, "number[]");
84AssertType(this, "this");
85AssertType(i, "number");
86AssertType(other.storage[i], "number");
87AssertType(other.storage, "number[]");
88AssertType(i, "number");
89        }
90AssertType(sum, "number");
91        return sum;
92
93    },
94    magnitude() {
95AssertType(magnitude, "() => number");
96
97        let sum = 0;
98AssertType(sum, "number");
99AssertType(0, "int");
100
101        for (let i = 0; i < this.storage.length; i++) {
102AssertType(i, "number");
103AssertType(0, "int");
104AssertType(i < this.storage.length, "boolean");
105AssertType(i, "number");
106AssertType(this.storage.length, "number");
107AssertType(this.storage, "number[]");
108AssertType(this, "this");
109AssertType(i++, "number");
110AssertType(i, "number");
111
112            sum += (this.storage[i] ** 2);
113AssertType(sum += (this.storage[i] ** 2), "number");
114AssertType(sum, "number");
115AssertType((this.storage[i] ** 2), "number");
116AssertType(this.storage[i] ** 2, "number");
117AssertType(this.storage[i], "number");
118AssertType(this.storage, "number[]");
119AssertType(this, "this");
120AssertType(i, "number");
121AssertType(2, "int");
122        }
123AssertType(Math.sqrt(sum), "number");
124AssertType(Math.sqrt, "(number) => number");
125AssertType(sum, "number");
126        return Math.sqrt(sum);
127    }
128}
129
130/**
131 * @param {number} x
132 * @param {number} y
133 */
134export function Point2D(x, y) {
135    if (!(this instanceof Point2D)) {
136AssertType(!(this instanceof Point2D), "boolean");
137AssertType((this instanceof Point2D), "boolean");
138AssertType(this instanceof Point2D, "boolean");
139AssertType(this, "this");
140AssertType(Point2D, "typeof Point2D");
141
142AssertType(new Point2D(x, y), "Point2D");
143AssertType(Point2D, "typeof Point2D");
144AssertType(x, "number");
145AssertType(y, "number");
146        return new Point2D(x, y);
147    }
148    Vec.call(this, 2);
149AssertType(Vec.call(this, 2), "any");
150AssertType(Vec.call, "(Function, any, ...any[]) => any");
151AssertType(this, "this");
152AssertType(2, "int");
153
154    this.x = x;
155AssertType(this.x = x, "number");
156AssertType(this.x, "number");
157AssertType(this, "this");
158AssertType(x, "number");
159
160    this.y = y;
161AssertType(this.y = y, "number");
162AssertType(this.y, "number");
163AssertType(this, "this");
164AssertType(y, "number");
165}
166
167Point2D.prototype = {
168AssertType(Point2D.prototype, "{ __proto__: typeof Vec; x: number; y: number; }");
169AssertType({    __proto__: Vec,    get x() {        return this.storage[0];    },    /**     * @param {number} x     */    set x(x) {        this.storage[0] = x;    },    get y() {        return this.storage[1];    },    /**     * @param {number} y     */    set y(y) {        this.storage[1] = y;    }}, "{ __proto__: typeof Vec; x: number; y: number; }");
170AssertType(Point2D.prototype = {    __proto__: Vec,    get x() {        return this.storage[0];    },    /**     * @param {number} x     */    set x(x) {        this.storage[0] = x;    },    get y() {        return this.storage[1];    },    /**     * @param {number} y     */    set y(y) {        this.storage[1] = y;    }}, "{ __proto__: typeof Vec; x: number; y: number; }");
171
172    __proto__: Vec,
173AssertType(__proto__, "typeof Vec");
174AssertType(Vec, "typeof Vec");
175
176    get x() {
177AssertType(x, "number");
178
179AssertType(this.storage[0], "any");
180AssertType(this.storage, "any");
181AssertType(this, "{ __proto__: typeof Vec; x: number; y: number; }");
182AssertType(0, "int");
183        return this.storage[0];
184
185    },
186    /**
187     * @param {number} x
188     */
189    set x(x) {
190AssertType(x, "number");
191AssertType(x, "number");
192
193        this.storage[0] = x;
194AssertType(this.storage[0] = x, "number");
195AssertType(this.storage[0], "any");
196AssertType(this.storage, "any");
197AssertType(this, "{ __proto__: typeof Vec; x: number; y: number; }");
198AssertType(0, "int");
199AssertType(x, "number");
200
201    },
202    get y() {
203AssertType(y, "number");
204
205AssertType(this.storage[1], "any");
206AssertType(this.storage, "any");
207AssertType(this, "{ __proto__: typeof Vec; x: number; y: number; }");
208AssertType(1, "int");
209        return this.storage[1];
210
211    },
212    /**
213     * @param {number} y
214     */
215    set y(y) {
216AssertType(y, "number");
217AssertType(y, "number");
218
219        this.storage[1] = y;
220AssertType(this.storage[1] = y, "number");
221AssertType(this.storage[1], "any");
222AssertType(this.storage, "any");
223AssertType(this, "{ __proto__: typeof Vec; x: number; y: number; }");
224AssertType(1, "int");
225AssertType(y, "number");
226    }
227};
228
229
230