diff --git a/.gitmodules b/.gitmodules index fdf474a693d10..0e4e0b3ed773c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -34,3 +34,7 @@ path = tests/cases/user/axios-src/axios-src url = https://github.com/axios/axios.git ignore = all +[submodule "tests/cases/user/prettier/prettier"] + path = tests/cases/user/prettier/prettier + url = https://github.com/prettier/prettier.git + ignore = all diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index c5cb19e876a6f..fd4af88235d5f 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -1889,7 +1889,13 @@ namespace ts { export function getJSDocHost(node: JSDocTag): HasJSDoc { while (node.parent.kind === SyntaxKind.JSDocTypeLiteral) { - node = node.parent.parent.parent as JSDocParameterTag; + if (node.parent.parent.kind === SyntaxKind.JSDocTypedefTag) { + node = node.parent.parent as JSDocTypedefTag; + } + else { + // node.parent.parent is a type expression, child of a parameter type + node = node.parent.parent.parent as JSDocParameterTag; + } } Debug.assert(node.parent!.kind === SyntaxKind.JSDocComment); return node.parent!.parent!; diff --git a/tests/baselines/reference/jsdocTemplateConstructorFunction2.errors.txt b/tests/baselines/reference/jsdocTemplateConstructorFunction2.errors.txt index db54928b28808..6a8e45566ecab 100644 --- a/tests/baselines/reference/jsdocTemplateConstructorFunction2.errors.txt +++ b/tests/baselines/reference/jsdocTemplateConstructorFunction2.errors.txt @@ -1,7 +1,8 @@ tests/cases/conformance/jsdoc/templateTagWithNestedTypeLiteral.js(21,1): error TS2322: Type 'false' is not assignable to type 'number'. +tests/cases/conformance/jsdoc/templateTagWithNestedTypeLiteral.js(26,15): error TS2304: Cannot find name 'T'. -==== tests/cases/conformance/jsdoc/templateTagWithNestedTypeLiteral.js (1 errors) ==== +==== tests/cases/conformance/jsdoc/templateTagWithNestedTypeLiteral.js (2 errors) ==== /** * @template {T} * @param {T} t @@ -25,4 +26,14 @@ tests/cases/conformance/jsdoc/templateTagWithNestedTypeLiteral.js(21,1): error T z.u = false ~~~ !!! error TS2322: Type 'false' is not assignable to type 'number'. + + // lookup in typedef should not crash the compiler, even when the type is unknown + /** + * @typedef {Object} A + * @property {T} value + ~ +!!! error TS2304: Cannot find name 'T'. + */ + /** @type {A} */ + const options = { value: null }; \ No newline at end of file diff --git a/tests/baselines/reference/jsdocTemplateConstructorFunction2.symbols b/tests/baselines/reference/jsdocTemplateConstructorFunction2.symbols index 1913ee53cdfb2..c65570f84c4c5 100644 --- a/tests/baselines/reference/jsdocTemplateConstructorFunction2.symbols +++ b/tests/baselines/reference/jsdocTemplateConstructorFunction2.symbols @@ -54,3 +54,13 @@ z.u = false >z : Symbol(z, Decl(templateTagWithNestedTypeLiteral.js, 18, 3)) >u : Symbol(Zet.u, Decl(templateTagWithNestedTypeLiteral.js, 4, 17), Decl(templateTagWithNestedTypeLiteral.js, 14, 36)) +// lookup in typedef should not crash the compiler, even when the type is unknown +/** + * @typedef {Object} A + * @property {T} value + */ +/** @type {A} */ +const options = { value: null }; +>options : Symbol(options, Decl(templateTagWithNestedTypeLiteral.js, 28, 5)) +>value : Symbol(value, Decl(templateTagWithNestedTypeLiteral.js, 28, 17)) + diff --git a/tests/baselines/reference/jsdocTemplateConstructorFunction2.types b/tests/baselines/reference/jsdocTemplateConstructorFunction2.types index 9f0a55289f356..5737f8caaf9f1 100644 --- a/tests/baselines/reference/jsdocTemplateConstructorFunction2.types +++ b/tests/baselines/reference/jsdocTemplateConstructorFunction2.types @@ -72,3 +72,15 @@ z.u = false >u : number >false : false +// lookup in typedef should not crash the compiler, even when the type is unknown +/** + * @typedef {Object} A + * @property {T} value + */ +/** @type {A} */ +const options = { value: null }; +>options : { value: any; } +>{ value: null } : { value: null; } +>value : null +>null : null + diff --git a/tests/baselines/reference/user/prettier.log b/tests/baselines/reference/user/prettier.log new file mode 100644 index 0000000000000..d2c6574313184 --- /dev/null +++ b/tests/baselines/reference/user/prettier.log @@ -0,0 +1,92 @@ +Exit Code: 1 +Standard output: +index.js(3,25): error TS2307: Cannot find module './package.json'. +index.js(138,21): error TS2532: Object is possibly 'undefined'. +src/cli/util.js(262,64): error TS2339: Property 'length' does not exist on type 'Ignore'. +src/cli/util.js(335,52): error TS2339: Property 'length' does not exist on type 'Ignore'. +src/cli/util.js(396,46): error TS2345: Argument of type 'null' is not assignable to parameter of type 'number | undefined'. +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")'. +src/common/parser-create-error.js(8,9): error TS2339: Property 'loc' does not exist on type 'SyntaxError'. +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; }'. + Property 'cache' is missing in type '{ sync: false; }'. +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; }'. + Property 'cache' is missing in type '{ sync: true; }'. +src/doc/doc-printer.js(213,17): error TS2532: Object is possibly 'undefined'. +src/doc/doc-printer.js(214,18): error TS2532: Object is possibly 'undefined'. +src/doc/doc-printer.js(215,17): error TS2532: Object is possibly 'undefined'. +src/language-css/clean.js(3,30): error TS2307: Cannot find module 'html-tag-names'. +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'. +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'. +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'. +src/language-css/parser-postcss.js(100,30): error TS2345: Argument of type 'any' is not assignable to parameter of type 'never'. +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'. +src/language-css/parser-postcss.js(407,32): error TS2531: Object is possibly 'null'. +src/language-css/printer-postcss.js(3,30): error TS2307: Cannot find module 'html-tag-names'. +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'. + Type '{ plugins: { ast: (() => { [x: string]: any; visitor: { [x: string]: any; Program(node: any): voi...' is not assignable to type 'PreprocessOptions'. + Types of property 'plugins' are incompatible. + Type '{ ast: (() => { [x: string]: any; visitor: { [x: string]: any; Program(node: any): void; ElementN...' is not assignable to type '{ ast?: ASTPluginBuilder[] | undefined; } | undefined'. + Type '{ ast: (() => { [x: string]: any; visitor: { [x: string]: any; Program(node: any): void; ElementN...' is not assignable to type '{ ast?: ASTPluginBuilder[] | undefined; }'. + Types of property 'ast' are incompatible. + Type '(() => { [x: string]: any; visitor: { [x: string]: any; Program(node: any): void; ElementNode(nod...' is not assignable to type 'ASTPluginBuilder[] | undefined'. + Type '(() => { [x: string]: any; visitor: { [x: string]: any; Program(node: any): void; ElementNode(nod...' is not assignable to type 'ASTPluginBuilder[]'. + Type '() => { [x: string]: any; visitor: { [x: string]: any; Program(node: any): void; ElementNode(node...' is not assignable to type 'ASTPluginBuilder'. + Type '{ [x: string]: any; visitor: { [x: string]: any; Program(node: any): void; ElementNode(node: any)...' is not assignable to type 'ASTPlugin'. + Property 'name' is missing in type '{ [x: string]: any; visitor: { [x: string]: any; Program(node: any): void; ElementNode(node: any)...'. +src/language-handlebars/printer-glimmer.js(270,7): error TS2554: Expected 0-1 arguments, but got 2. +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; }'. + Property 'parts' is missing in type '{ [x: string]: any; type: string; }'. +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'. + Type '{ [x: string]: any; type: string; parts: any; }' is not assignable to type 'ConcatArray'. + Property 'length' is missing in type '{ [x: string]: any; type: string; parts: any; }'. +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'. + Type '{ [x: string]: any; type: string; parts: any; }' is not assignable to type 'ConcatArray'. + Property 'length' is missing in type '{ [x: string]: any; type: string; parts: any; }'. +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; }'. +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; }'. + Property 'contents' is missing in type '{ [x: string]: any; type: string; parts: any; }'. +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; }'. +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; }'. +src/language-js/printer-estree.js(3293,23): error TS2532: Object is possibly 'undefined'. +src/language-js/printer-estree.js(3294,24): error TS2532: Object is possibly 'undefined'. +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'. + Type '{ [x: string]: any; type: string; parts: any; }' is not assignable to type 'string'. +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'. +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'. +src/language-js/printer-estree.js(3995,14): error TS2554: Expected 0-2 arguments, but got 3. +src/language-js/printer-estree.js(5034,9): error TS2554: Expected 0-1 arguments, but got 2. +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>'. + Type '(string | number)[]' is not assignable to type 'ConcatArray<(childPath: any) => any>'. + Types of property 'slice' are incompatible. + Type '(start?: number | undefined, end?: number | undefined) => (string | number)[]' is not assignable to type '(start?: number | undefined, end?: number | undefined) => ((childPath: any) => any)[]'. + Type '(string | number)[]' is not assignable to type '((childPath: any) => any)[]'. + Type 'string | number' is not assignable to type '(childPath: any) => any'. + Type 'string' is not assignable to type '(childPath: any) => any'. +src/language-markdown/printer-markdown.js(258,18): error TS2532: Object is possibly 'undefined'. +src/language-markdown/printer-markdown.js(259,17): error TS2532: Object is possibly 'undefined'. +src/language-markdown/printer-markdown.js(283,14): error TS2532: Object is possibly 'undefined'. +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'. + Type 'void' is not assignable to type 'string'. +src/language-vue/parser-vue.js(180,34): error TS2339: Property 'toLowerCase' does not exist on type 'never'. +src/language-vue/parser-vue.js(244,26): error TS2345: Argument of type 'any' is not assignable to parameter of type 'never'. +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'. +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...'. + Property 'contentStart' is missing in type '{ [x: string]: any; tag: any; attrs: any; unary: any; start: any; children: never[]; }'. +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...'. +src/main/core-options.js(51,43): error TS1005: '}' expected. +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";...'. + Property 'cursorOffset' is incompatible with index signature. + 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...'. + 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...'. +src/main/parser.js(61,9): error TS2345: Argument of type 'PropertyDescriptor | undefined' is not assignable to parameter of type 'PropertyDescriptor & ThisType'. + Type 'undefined' is not assignable to type 'PropertyDescriptor & ThisType'. + Type 'undefined' is not assignable to type 'PropertyDescriptor'. +src/main/support.js(5,32): error TS2307: Cannot find module '../../package.json'. +src/main/support.js(36,24): error TS2339: Property 'name' does not exist on type 'never'. +src/main/support.js(36,35): error TS2339: Property 'name' does not exist on type 'never'. +src/main/support.js(36,48): error TS2339: Property 'name' does not exist on type 'never'. +src/main/support.js(36,57): error TS2339: Property 'name' does not exist on type 'never'. + + + +Standard error: diff --git a/tests/cases/conformance/jsdoc/jsdocTemplateConstructorFunction2.ts b/tests/cases/conformance/jsdoc/jsdocTemplateConstructorFunction2.ts index 750126a05b902..a4f107190ff1c 100644 --- a/tests/cases/conformance/jsdoc/jsdocTemplateConstructorFunction2.ts +++ b/tests/cases/conformance/jsdoc/jsdocTemplateConstructorFunction2.ts @@ -24,3 +24,11 @@ Zet.prototype.add = function(v, o) { var z = new Zet(1) z.t = 2 z.u = false + +// lookup in typedef should not crash the compiler, even when the type is unknown +/** + * @typedef {Object} A + * @property {T} value + */ +/** @type {A} */ +const options = { value: null }; diff --git a/tests/cases/user/prettier/prettier b/tests/cases/user/prettier/prettier new file mode 160000 index 0000000000000..67f1c4877ee10 --- /dev/null +++ b/tests/cases/user/prettier/prettier @@ -0,0 +1 @@ +Subproject commit 67f1c4877ee1090b66d468a847caccca411a6f82 diff --git a/tests/cases/user/prettier/test.json b/tests/cases/user/prettier/test.json new file mode 100644 index 0000000000000..b6495a1b80ba2 --- /dev/null +++ b/tests/cases/user/prettier/test.json @@ -0,0 +1,3 @@ +{ + "types": ["node"] +} diff --git a/tests/cases/user/prettier/tsconfig.json b/tests/cases/user/prettier/tsconfig.json new file mode 100644 index 0000000000000..d3b1f270dbcb5 --- /dev/null +++ b/tests/cases/user/prettier/tsconfig.json @@ -0,0 +1,17 @@ +{ + "compilerOptions": { + "noImplicitAny": false, + "noImplicitThis": false, + "maxNodeModuleJsDepth": 0, + "strict": true, + "noEmit": true, + "allowJs": true, + "checkJs": true, + "types": ["node"], + "lib": ["esnext", "dom"], + "target": "esnext", + "module": "commonjs", + "pretty": false, + }, + "include": ["prettier/src"] +}