Skip to content

Commit 2c5192e

Browse files
committed
Add unit tests
1 parent 81bee7f commit 2c5192e

30 files changed

+1578
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// === findAllReferences ===
2+
// === /a.js ===
3+
// --- (line: 3) skipped ---
4+
// class Collection {}
5+
//
6+
// /**
7+
// * <|@typedef {object} [|{| isWriteAccess: true, isDefinition: true |}U/*FIND ALL REFS*/serData|]
8+
// * @property {string} id
9+
// * @property {string} name
10+
// |>*/
11+
//
12+
// /** @specialize {[|UserData|]} */
13+
// const users = new Collection('users');
14+
15+
// === Definitions ===
16+
// === /a.js ===
17+
// --- (line: 3) skipped ---
18+
// class Collection {}
19+
//
20+
// /**
21+
// * <|@typedef {object} [|U/*FIND ALL REFS*/serData|]
22+
// * @property {string} id
23+
// * @property {string} name
24+
// |>*/
25+
//
26+
// /** @specialize {UserData} */
27+
// const users = new Collection('users');
28+
29+
// === Details ===
30+
[
31+
{
32+
"containerKind": "",
33+
"containerName": "",
34+
"kind": "type",
35+
"name": "type UserData = {\n id: string;\n name: string;\n}",
36+
"displayParts": [
37+
{
38+
"text": "type",
39+
"kind": "keyword"
40+
},
41+
{
42+
"text": " ",
43+
"kind": "space"
44+
},
45+
{
46+
"text": "UserData",
47+
"kind": "aliasName"
48+
},
49+
{
50+
"text": " ",
51+
"kind": "space"
52+
},
53+
{
54+
"text": "=",
55+
"kind": "operator"
56+
},
57+
{
58+
"text": " ",
59+
"kind": "space"
60+
},
61+
{
62+
"text": "{",
63+
"kind": "punctuation"
64+
},
65+
{
66+
"text": "\n",
67+
"kind": "lineBreak"
68+
},
69+
{
70+
"text": " ",
71+
"kind": "space"
72+
},
73+
{
74+
"text": "id",
75+
"kind": "propertyName"
76+
},
77+
{
78+
"text": ":",
79+
"kind": "punctuation"
80+
},
81+
{
82+
"text": " ",
83+
"kind": "space"
84+
},
85+
{
86+
"text": "string",
87+
"kind": "keyword"
88+
},
89+
{
90+
"text": ";",
91+
"kind": "punctuation"
92+
},
93+
{
94+
"text": "\n",
95+
"kind": "lineBreak"
96+
},
97+
{
98+
"text": " ",
99+
"kind": "space"
100+
},
101+
{
102+
"text": "name",
103+
"kind": "propertyName"
104+
},
105+
{
106+
"text": ":",
107+
"kind": "punctuation"
108+
},
109+
{
110+
"text": " ",
111+
"kind": "space"
112+
},
113+
{
114+
"text": "string",
115+
"kind": "keyword"
116+
},
117+
{
118+
"text": ";",
119+
"kind": "punctuation"
120+
},
121+
{
122+
"text": "\n",
123+
"kind": "lineBreak"
124+
},
125+
{
126+
"text": "}",
127+
"kind": "punctuation"
128+
}
129+
]
130+
}
131+
]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//// [tests/cases/conformance/jsdoc/specializeTag1.ts] ////
2+
3+
//// [specializeTag1.js]
4+
/**
5+
* @template T
6+
* @param {object} _jsonSchema
7+
* @returns {(x: unknown) => x is T}
8+
*/
9+
function createValidator(_jsonSchema) {
10+
/**
11+
* @param {unknown} _x
12+
* @returns {_x is T}
13+
*/
14+
return (_x) => true;
15+
}
16+
17+
/** @specialize {number} */
18+
const isNumber = createValidator({ type: 'number' });
19+
20+
const isString = /** @specialize {string} */(createValidator({ type: 'string' }));
21+
22+
23+
//// [specializeTag1.js]
24+
"use strict";
25+
/**
26+
* @template T
27+
* @param {object} _jsonSchema
28+
* @returns {(x: unknown) => x is T}
29+
*/
30+
function createValidator(_jsonSchema) {
31+
/**
32+
* @param {unknown} _x
33+
* @returns {_x is T}
34+
*/
35+
return (_x) => true;
36+
}
37+
/** @specialize {number} */
38+
const isNumber = createValidator({ type: 'number' });
39+
const isString = /** @specialize {string} */ (createValidator({ type: 'string' }));
40+
41+
42+
//// [specializeTag1.d.ts]
43+
/**
44+
* @template T
45+
* @param {object} _jsonSchema
46+
* @returns {(x: unknown) => x is T}
47+
*/
48+
declare function createValidator<T>(_jsonSchema: object): (x: unknown) => x is T;
49+
/** @specialize {number} */
50+
declare const isNumber: (x: unknown) => x is number;
51+
declare const isString: (x: unknown) => x is string;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//// [tests/cases/conformance/jsdoc/specializeTag1.ts] ////
2+
3+
=== specializeTag1.js ===
4+
/**
5+
* @template T
6+
* @param {object} _jsonSchema
7+
* @returns {(x: unknown) => x is T}
8+
*/
9+
function createValidator(_jsonSchema) {
10+
>createValidator : Symbol(createValidator, Decl(specializeTag1.js, 0, 0))
11+
>_jsonSchema : Symbol(_jsonSchema, Decl(specializeTag1.js, 5, 25))
12+
13+
/**
14+
* @param {unknown} _x
15+
* @returns {_x is T}
16+
*/
17+
return (_x) => true;
18+
>_x : Symbol(_x, Decl(specializeTag1.js, 10, 12))
19+
}
20+
21+
/** @specialize {number} */
22+
const isNumber = createValidator({ type: 'number' });
23+
>isNumber : Symbol(isNumber, Decl(specializeTag1.js, 14, 5))
24+
>createValidator : Symbol(createValidator, Decl(specializeTag1.js, 0, 0))
25+
>type : Symbol(type, Decl(specializeTag1.js, 14, 34))
26+
27+
const isString = /** @specialize {string} */(createValidator({ type: 'string' }));
28+
>isString : Symbol(isString, Decl(specializeTag1.js, 16, 5))
29+
>createValidator : Symbol(createValidator, Decl(specializeTag1.js, 0, 0))
30+
>type : Symbol(type, Decl(specializeTag1.js, 16, 62))
31+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//// [tests/cases/conformance/jsdoc/specializeTag1.ts] ////
2+
3+
=== specializeTag1.js ===
4+
/**
5+
* @template T
6+
* @param {object} _jsonSchema
7+
* @returns {(x: unknown) => x is T}
8+
*/
9+
function createValidator(_jsonSchema) {
10+
>createValidator : <T>(_jsonSchema: object) => (x: unknown) => x is T
11+
> : ^ ^^ ^^ ^^^^^
12+
>_jsonSchema : object
13+
> : ^^^^^^
14+
15+
/**
16+
* @param {unknown} _x
17+
* @returns {_x is T}
18+
*/
19+
return (_x) => true;
20+
>(_x) => true : (_x: unknown) => _x is T
21+
> : ^ ^^ ^^^^^
22+
>_x : unknown
23+
> : ^^^^^^^
24+
>true : true
25+
> : ^^^^
26+
}
27+
28+
/** @specialize {number} */
29+
const isNumber = createValidator({ type: 'number' });
30+
>isNumber : (x: unknown) => x is number
31+
> : ^ ^^ ^^^^^ ^^^^^^
32+
>createValidator({ type: 'number' }) : (x: unknown) => x is number
33+
> : ^ ^^ ^^^^^ ^^^^^^
34+
>createValidator : <T>(_jsonSchema: object) => (x: unknown) => x is T
35+
> : ^ ^^ ^^ ^^^^^
36+
>{ type: 'number' } : { type: string; }
37+
> : ^^^^^^^^^^^^^^^^^
38+
>type : string
39+
> : ^^^^^^
40+
>'number' : "number"
41+
> : ^^^^^^^^
42+
43+
const isString = /** @specialize {string} */(createValidator({ type: 'string' }));
44+
>isString : (x: unknown) => x is string
45+
> : ^ ^^ ^^^^^ ^^^^^^
46+
>(createValidator({ type: 'string' })) : (x: unknown) => x is string
47+
> : ^ ^^ ^^^^^ ^^^^^^
48+
>createValidator({ type: 'string' }) : (x: unknown) => x is string
49+
> : ^ ^^ ^^^^^ ^^^^^^
50+
>createValidator : <T>(_jsonSchema: object) => (x: unknown) => x is T
51+
> : ^ ^^ ^^ ^^^^^
52+
>{ type: 'string' } : { type: string; }
53+
> : ^^^^^^^^^^^^^^^^^
54+
>type : string
55+
> : ^^^^^^
56+
>'string' : "string"
57+
> : ^^^^^^^^
58+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
specializeTag2.js(19,15): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
2+
specializeTag2.js(23,26): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
3+
4+
5+
==== specializeTag2.js (2 errors) ====
6+
/**
7+
* @template T
8+
* @param {TemplateStringsArray} strings
9+
* @param {...T} values
10+
* @returns {Record<string, T>}
11+
*/
12+
function parse(strings, ...values) {
13+
/** @type {Record<string, T>} */
14+
const result = {};
15+
strings.forEach((key, i) => {
16+
if (i < values.length) {
17+
result[key] = values[i];
18+
}
19+
})
20+
return result;
21+
}
22+
23+
const query1 = /** @specialize {string} */(
24+
parse`a=${1}b=${2}`
25+
~
26+
!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
27+
)
28+
29+
/** @specialize {string} */
30+
const query2 = parse`a=${1}b=${2}`; // Type error
31+
~
32+
!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
33+
34+
/** @specialize {`${number}`} */
35+
const query3 = parse`a=${"1"}b=${"2"}`;
36+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//// [tests/cases/conformance/jsdoc/specializeTag2.ts] ////
2+
3+
//// [specializeTag2.js]
4+
/**
5+
* @template T
6+
* @param {TemplateStringsArray} strings
7+
* @param {...T} values
8+
* @returns {Record<string, T>}
9+
*/
10+
function parse(strings, ...values) {
11+
/** @type {Record<string, T>} */
12+
const result = {};
13+
strings.forEach((key, i) => {
14+
if (i < values.length) {
15+
result[key] = values[i];
16+
}
17+
})
18+
return result;
19+
}
20+
21+
const query1 = /** @specialize {string} */(
22+
parse`a=${1}b=${2}`
23+
)
24+
25+
/** @specialize {string} */
26+
const query2 = parse`a=${1}b=${2}`; // Type error
27+
28+
/** @specialize {`${number}`} */
29+
const query3 = parse`a=${"1"}b=${"2"}`;
30+
31+
32+
//// [specializeTag2.js]
33+
"use strict";
34+
/**
35+
* @template T
36+
* @param {TemplateStringsArray} strings
37+
* @param {...T} values
38+
* @returns {Record<string, T>}
39+
*/
40+
function parse(strings, ...values) {
41+
/** @type {Record<string, T>} */
42+
const result = {};
43+
strings.forEach((key, i) => {
44+
if (i < values.length) {
45+
result[key] = values[i];
46+
}
47+
});
48+
return result;
49+
}
50+
const query1 = /** @specialize {string} */ (parse `a=${1}b=${2}`);
51+
/** @specialize {string} */
52+
const query2 = parse `a=${1}b=${2}`; // Type error
53+
/** @specialize {`${number}`} */
54+
const query3 = parse `a=${"1"}b=${"2"}`;
55+
56+
57+
//// [specializeTag2.d.ts]
58+
/**
59+
* @template T
60+
* @param {TemplateStringsArray} strings
61+
* @param {...T} values
62+
* @returns {Record<string, T>}
63+
*/
64+
declare function parse<T>(strings: TemplateStringsArray, ...values: T[]): Record<string, T>;
65+
declare const query1: Record<string, string>;
66+
/** @specialize {string} */
67+
declare const query2: Record<string, string>;
68+
/** @specialize {`${number}`} */
69+
declare const query3: Record<string, `${number}`>;

0 commit comments

Comments
 (0)