Skip to content

Commit 1595f7f

Browse files
authored
Add prettier user test and fix associated crash (microsoft#23715)
* Add prettier and fix crash bug Name resolution would crash when resolving a type name inside a typedef's property tag. * Update tsconfig and thefore prettier baseline
1 parent 1541599 commit 1595f7f

10 files changed

+166
-2
lines changed

.gitmodules

+4
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,7 @@
3434
path = tests/cases/user/axios-src/axios-src
3535
url = https://github.com/axios/axios.git
3636
ignore = all
37+
[submodule "tests/cases/user/prettier/prettier"]
38+
path = tests/cases/user/prettier/prettier
39+
url = https://github.com/prettier/prettier.git
40+
ignore = all

src/compiler/utilities.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1889,7 +1889,13 @@ namespace ts {
18891889

18901890
export function getJSDocHost(node: JSDocTag): HasJSDoc {
18911891
while (node.parent.kind === SyntaxKind.JSDocTypeLiteral) {
1892-
node = node.parent.parent.parent as JSDocParameterTag;
1892+
if (node.parent.parent.kind === SyntaxKind.JSDocTypedefTag) {
1893+
node = node.parent.parent as JSDocTypedefTag;
1894+
}
1895+
else {
1896+
// node.parent.parent is a type expression, child of a parameter type
1897+
node = node.parent.parent.parent as JSDocParameterTag;
1898+
}
18931899
}
18941900
Debug.assert(node.parent!.kind === SyntaxKind.JSDocComment);
18951901
return node.parent!.parent!;

tests/baselines/reference/jsdocTemplateConstructorFunction2.errors.txt

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
tests/cases/conformance/jsdoc/templateTagWithNestedTypeLiteral.js(21,1): error TS2322: Type 'false' is not assignable to type 'number'.
2+
tests/cases/conformance/jsdoc/templateTagWithNestedTypeLiteral.js(26,15): error TS2304: Cannot find name 'T'.
23

34

4-
==== tests/cases/conformance/jsdoc/templateTagWithNestedTypeLiteral.js (1 errors) ====
5+
==== tests/cases/conformance/jsdoc/templateTagWithNestedTypeLiteral.js (2 errors) ====
56
/**
67
* @template {T}
78
* @param {T} t
@@ -25,4 +26,14 @@ tests/cases/conformance/jsdoc/templateTagWithNestedTypeLiteral.js(21,1): error T
2526
z.u = false
2627
~~~
2728
!!! error TS2322: Type 'false' is not assignable to type 'number'.
29+
30+
// lookup in typedef should not crash the compiler, even when the type is unknown
31+
/**
32+
* @typedef {Object} A
33+
* @property {T} value
34+
~
35+
!!! error TS2304: Cannot find name 'T'.
36+
*/
37+
/** @type {A} */
38+
const options = { value: null };
2839

tests/baselines/reference/jsdocTemplateConstructorFunction2.symbols

+10
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,13 @@ z.u = false
5454
>z : Symbol(z, Decl(templateTagWithNestedTypeLiteral.js, 18, 3))
5555
>u : Symbol(Zet.u, Decl(templateTagWithNestedTypeLiteral.js, 4, 17), Decl(templateTagWithNestedTypeLiteral.js, 14, 36))
5656

57+
// lookup in typedef should not crash the compiler, even when the type is unknown
58+
/**
59+
* @typedef {Object} A
60+
* @property {T} value
61+
*/
62+
/** @type {A} */
63+
const options = { value: null };
64+
>options : Symbol(options, Decl(templateTagWithNestedTypeLiteral.js, 28, 5))
65+
>value : Symbol(value, Decl(templateTagWithNestedTypeLiteral.js, 28, 17))
66+

tests/baselines/reference/jsdocTemplateConstructorFunction2.types

+12
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,15 @@ z.u = false
7272
>u : number
7373
>false : false
7474

75+
// lookup in typedef should not crash the compiler, even when the type is unknown
76+
/**
77+
* @typedef {Object} A
78+
* @property {T} value
79+
*/
80+
/** @type {A} */
81+
const options = { value: null };
82+
>options : { value: any; }
83+
>{ value: null } : { value: null; }
84+
>value : null
85+
>null : null
86+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
Exit Code: 1
2+
Standard output:
3+
index.js(3,25): error TS2307: Cannot find module './package.json'.
4+
index.js(138,21): error TS2532: Object is possibly 'undefined'.
5+
src/cli/util.js(262,64): error TS2339: Property 'length' does not exist on type 'Ignore'.
6+
src/cli/util.js(335,52): error TS2339: Property 'length' does not exist on type 'Ignore'.
7+
src/cli/util.js(396,46): error TS2345: Argument of type 'null' is not assignable to parameter of type 'number | undefined'.
8+
src/cli/util.js(403,39): error TS2339: Property 'grey' does not exist on type 'typeof import("/home/nathansa/ts/node_modules/chalk/types/index")'.
9+
src/common/parser-create-error.js(8,9): error TS2339: Property 'loc' does not exist on type 'SyntaxError'.
10+
src/config/resolve-config.js(75,32): error TS2345: Argument of type '{ sync: false; }' is not assignable to parameter of type '{ cache: boolean; sync: boolean; }'.
11+
Property 'cache' is missing in type '{ sync: false; }'.
12+
src/config/resolve-config.js(82,32): error TS2345: Argument of type '{ sync: true; }' is not assignable to parameter of type '{ cache: boolean; sync: boolean; }'.
13+
Property 'cache' is missing in type '{ sync: true; }'.
14+
src/doc/doc-printer.js(213,17): error TS2532: Object is possibly 'undefined'.
15+
src/doc/doc-printer.js(214,18): error TS2532: Object is possibly 'undefined'.
16+
src/doc/doc-printer.js(215,17): error TS2532: Object is possibly 'undefined'.
17+
src/language-css/clean.js(3,30): error TS2307: Cannot find module 'html-tag-names'.
18+
src/language-css/parser-postcss.js(78,32): error TS2345: Argument of type '{ [x: string]: any; groups: never[]; type: string; }' is not assignable to parameter of type 'never'.
19+
src/language-css/parser-postcss.js(88,30): error TS2345: Argument of type '{ [x: string]: any; open: null; close: null; groups: never[]; type: string; }' is not assignable to parameter of type 'never'.
20+
src/language-css/parser-postcss.js(93,30): error TS2345: Argument of type '{ [x: string]: any; groups: never[]; type: string; }' is not assignable to parameter of type 'never'.
21+
src/language-css/parser-postcss.js(100,30): error TS2345: Argument of type 'any' is not assignable to parameter of type 'never'.
22+
src/language-css/parser-postcss.js(104,28): error TS2345: Argument of type '{ [x: string]: any; groups: never[]; type: string; }' is not assignable to parameter of type 'never'.
23+
src/language-css/parser-postcss.js(407,32): error TS2531: Object is possibly 'null'.
24+
src/language-css/printer-postcss.js(3,30): error TS2307: Cannot find module 'html-tag-names'.
25+
src/language-handlebars/parser-glimmer.js(27,26): error TS2345: Argument of type '{ plugins: { ast: (() => { [x: string]: any; visitor: { [x: string]: any; Program(node: any): voi...' is not assignable to parameter of type 'PreprocessOptions | undefined'.
26+
Type '{ plugins: { ast: (() => { [x: string]: any; visitor: { [x: string]: any; Program(node: any): voi...' is not assignable to type 'PreprocessOptions'.
27+
Types of property 'plugins' are incompatible.
28+
Type '{ ast: (() => { [x: string]: any; visitor: { [x: string]: any; Program(node: any): void; ElementN...' is not assignable to type '{ ast?: ASTPluginBuilder[] | undefined; } | undefined'.
29+
Type '{ ast: (() => { [x: string]: any; visitor: { [x: string]: any; Program(node: any): void; ElementN...' is not assignable to type '{ ast?: ASTPluginBuilder[] | undefined; }'.
30+
Types of property 'ast' are incompatible.
31+
Type '(() => { [x: string]: any; visitor: { [x: string]: any; Program(node: any): void; ElementNode(nod...' is not assignable to type 'ASTPluginBuilder[] | undefined'.
32+
Type '(() => { [x: string]: any; visitor: { [x: string]: any; Program(node: any): void; ElementNode(nod...' is not assignable to type 'ASTPluginBuilder[]'.
33+
Type '() => { [x: string]: any; visitor: { [x: string]: any; Program(node: any): void; ElementNode(node...' is not assignable to type 'ASTPluginBuilder'.
34+
Type '{ [x: string]: any; visitor: { [x: string]: any; Program(node: any): void; ElementNode(node: any)...' is not assignable to type 'ASTPlugin'.
35+
Property 'name' is missing in type '{ [x: string]: any; visitor: { [x: string]: any; Program(node: any): void; ElementNode(node: any)...'.
36+
src/language-handlebars/printer-glimmer.js(270,7): error TS2554: Expected 0-1 arguments, but got 2.
37+
src/language-js/printer-estree.js(99,9): error TS2322: Type '{ [x: string]: any; type: string; }' is not assignable to type '{ [x: string]: any; type: string; parts: any; }'.
38+
Property 'parts' is missing in type '{ [x: string]: any; type: string; }'.
39+
src/language-js/printer-estree.js(302,9): error TS2345: Argument of type '{ [x: string]: any; type: string; parts: any; } | { [x: string]: any; type: string; contents: any...' is not assignable to parameter of type 'ConcatArray<never>'.
40+
Type '{ [x: string]: any; type: string; parts: any; }' is not assignable to type 'ConcatArray<never>'.
41+
Property 'length' is missing in type '{ [x: string]: any; type: string; parts: any; }'.
42+
src/language-js/printer-estree.js(1224,28): error TS2345: Argument of type '{ [x: string]: any; type: string; parts: any; }' is not assignable to parameter of type 'string | ConcatArray<string>'.
43+
Type '{ [x: string]: any; type: string; parts: any; }' is not assignable to type 'ConcatArray<string>'.
44+
Property 'length' is missing in type '{ [x: string]: any; type: string; parts: any; }'.
45+
src/language-js/printer-estree.js(1601,20): error TS2345: Argument of type '" "' is not assignable to parameter of type '{ [x: string]: any; type: string; contents: any; break: boolean; expandedStates: any; }'.
46+
src/language-js/printer-estree.js(1603,20): error TS2345: Argument of type '{ [x: string]: any; type: string; parts: any; }' is not assignable to parameter of type '{ [x: string]: any; type: string; contents: any; break: boolean; expandedStates: any; }'.
47+
Property 'contents' is missing in type '{ [x: string]: any; type: string; parts: any; }'.
48+
src/language-js/printer-estree.js(1605,18): error TS2345: Argument of type '"while ("' is not assignable to parameter of type '{ [x: string]: any; type: string; contents: any; break: boolean; expandedStates: any; }'.
49+
src/language-js/printer-estree.js(1614,9): error TS2345: Argument of type '")"' is not assignable to parameter of type '{ [x: string]: any; type: string; contents: any; break: boolean; expandedStates: any; }'.
50+
src/language-js/printer-estree.js(3293,23): error TS2532: Object is possibly 'undefined'.
51+
src/language-js/printer-estree.js(3294,24): error TS2532: Object is possibly 'undefined'.
52+
src/language-js/printer-estree.js(3647,5): error TS2345: Argument of type '"" | { [x: string]: any; type: string; parts: any; } | { [x: string]: any; type: string; contents...' is not assignable to parameter of type 'string'.
53+
Type '{ [x: string]: any; type: string; parts: any; }' is not assignable to type 'string'.
54+
src/language-js/printer-estree.js(3651,16): error TS2345: Argument of type '{ [x: string]: any; type: string; parts: any; }' is not assignable to parameter of type 'string'.
55+
src/language-js/printer-estree.js(3693,9): error TS2345: Argument of type '{ [x: string]: any; type: string; parts: any; }' is not assignable to parameter of type 'string'.
56+
src/language-js/printer-estree.js(3995,14): error TS2554: Expected 0-2 arguments, but got 3.
57+
src/language-js/printer-estree.js(5034,9): error TS2554: Expected 0-1 arguments, but got 2.
58+
src/language-js/printer-estree.js(5070,7): error TS2345: Argument of type '(string | number)[]' is not assignable to parameter of type '((childPath: any) => any) | ConcatArray<(childPath: any) => any>'.
59+
Type '(string | number)[]' is not assignable to type 'ConcatArray<(childPath: any) => any>'.
60+
Types of property 'slice' are incompatible.
61+
Type '(start?: number | undefined, end?: number | undefined) => (string | number)[]' is not assignable to type '(start?: number | undefined, end?: number | undefined) => ((childPath: any) => any)[]'.
62+
Type '(string | number)[]' is not assignable to type '((childPath: any) => any)[]'.
63+
Type 'string | number' is not assignable to type '(childPath: any) => any'.
64+
Type 'string' is not assignable to type '(childPath: any) => any'.
65+
src/language-markdown/printer-markdown.js(258,18): error TS2532: Object is possibly 'undefined'.
66+
src/language-markdown/printer-markdown.js(259,17): error TS2532: Object is possibly 'undefined'.
67+
src/language-markdown/printer-markdown.js(283,14): error TS2532: Object is possibly 'undefined'.
68+
src/language-vue/parser-vue.js(54,23): error TS2345: Argument of type '(m: string, g: any) => void' is not assignable to parameter of type '(substring: string, ...args: any[]) => string'.
69+
Type 'void' is not assignable to type 'string'.
70+
src/language-vue/parser-vue.js(180,34): error TS2339: Property 'toLowerCase' does not exist on type 'never'.
71+
src/language-vue/parser-vue.js(244,26): error TS2345: Argument of type 'any' is not assignable to parameter of type 'never'.
72+
src/language-vue/parser-vue.js(393,25): error TS2345: Argument of type '{ [x: string]: any; tag: any; attrs: any; unary: any; start: any; children: never[]; }' is not assignable to parameter of type 'never'.
73+
src/language-vue/parser-vue.js(398,23): error TS2345: Argument of type '{ [x: string]: any; tag: any; attrs: any; unary: any; start: any; children: never[]; }' is not assignable to parameter of type '{ [x: string]: any; tag: string; attrs: never[]; unary: boolean; start: number; contentStart: num...'.
74+
Property 'contentStart' is missing in type '{ [x: string]: any; tag: any; attrs: any; unary: any; start: any; children: never[]; }'.
75+
src/language-vue/parser-vue.js(399,9): error TS2322: Type '{ [x: string]: any; tag: any; attrs: any; unary: any; start: any; children: never[]; }' is not assignable to type '{ [x: string]: any; tag: string; attrs: never[]; unary: boolean; start: number; contentStart: num...'.
76+
src/main/core-options.js(51,43): error TS1005: '}' expected.
77+
src/main/core-options.js(63,5): error TS2322: Type '{ cursorOffset: { since: string; category: string; type: "int"; default: number; range: { start: ...' is not assignable to type '{ [name: string]: { since: string; category: string; type: "boolean" | "path" | "int" | "choice";...'.
78+
Property 'cursorOffset' is incompatible with index signature.
79+
Type '{ since: string; category: string; type: "int"; default: number; range: { start: number; end: num...' is not assignable to type '{ since: string; category: string; type: "boolean" | "path" | "int" | "choice"; array: boolean; d...'.
80+
Object literal may only specify known properties, and 'cliCategory' does not exist in type '{ since: string; category: string; type: "boolean" | "path" | "int" | "choice"; array: boolean; d...'.
81+
src/main/parser.js(61,9): error TS2345: Argument of type 'PropertyDescriptor | undefined' is not assignable to parameter of type 'PropertyDescriptor & ThisType<any>'.
82+
Type 'undefined' is not assignable to type 'PropertyDescriptor & ThisType<any>'.
83+
Type 'undefined' is not assignable to type 'PropertyDescriptor'.
84+
src/main/support.js(5,32): error TS2307: Cannot find module '../../package.json'.
85+
src/main/support.js(36,24): error TS2339: Property 'name' does not exist on type 'never'.
86+
src/main/support.js(36,35): error TS2339: Property 'name' does not exist on type 'never'.
87+
src/main/support.js(36,48): error TS2339: Property 'name' does not exist on type 'never'.
88+
src/main/support.js(36,57): error TS2339: Property 'name' does not exist on type 'never'.
89+
90+
91+
92+
Standard error:

tests/cases/conformance/jsdoc/jsdocTemplateConstructorFunction2.ts

+8
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,11 @@ Zet.prototype.add = function(v, o) {
2424
var z = new Zet(1)
2525
z.t = 2
2626
z.u = false
27+
28+
// lookup in typedef should not crash the compiler, even when the type is unknown
29+
/**
30+
* @typedef {Object} A
31+
* @property {T} value
32+
*/
33+
/** @type {A} */
34+
const options = { value: null };

tests/cases/user/prettier/prettier

Submodule prettier added at 67f1c48

tests/cases/user/prettier/test.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"types": ["node"]
3+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"compilerOptions": {
3+
"noImplicitAny": false,
4+
"noImplicitThis": false,
5+
"maxNodeModuleJsDepth": 0,
6+
"strict": true,
7+
"noEmit": true,
8+
"allowJs": true,
9+
"checkJs": true,
10+
"types": ["node"],
11+
"lib": ["esnext", "dom"],
12+
"target": "esnext",
13+
"module": "commonjs",
14+
"pretty": false,
15+
},
16+
"include": ["prettier/src"]
17+
}

0 commit comments

Comments
 (0)