|
| 1 | +tests/cases/compiler/APISample_jsdoc.ts(79,32): error TS2495: Type '{ [x: number]: readonly JSDocParameterTag[]; hasTrailingComma?: readonly JSDocParameterTag[] | undefined; length: readonly JSDocParameterTag[]; toString: readonly JSDocParameterTag[]; toLocaleString: readonly JSDocParameterTag[]; concat: readonly JSDocParameterTag[]; join: readonly JSDocParameterTag[]; slice: readonly JSDocParameterTag[]; indexOf: readonly JSDocParameterTag[]; lastIndexOf: readonly JSDocParameterTag[]; every: readonly JSDocParameterTag[]; some: readonly JSDocParameterTag[]; forEach: readonly JSDocParameterTag[]; map: readonly JSDocParameterTag[]; filter: readonly JSDocParameterTag[]; reduce: readonly JSDocParameterTag[]; reduceRight: readonly JSDocParameterTag[]; pos: readonly JSDocParameterTag[]; end: readonly JSDocParameterTag[]; }' is not an array type or a string type. |
| 2 | + |
| 3 | + |
| 4 | +==== tests/cases/compiler/node_modules/typescript/index.d.ts (0 errors) ==== |
| 5 | + declare module "typescript" { |
| 6 | + export = ts; |
| 7 | + } |
| 8 | + |
| 9 | +==== tests/cases/compiler/APISample_jsdoc.ts (1 errors) ==== |
| 10 | + /* |
| 11 | + * Note: This test is a public API sample. The original sources can be found |
| 12 | + * at: https://github.com./YousefED/typescript-json-schema |
| 13 | + * https://github.com./vega/ts-json-schema-generator |
| 14 | + * Please log a "breaking change" issue for any API breaking change affecting this issue |
| 15 | + */ |
| 16 | + |
| 17 | + declare var console: any; |
| 18 | + |
| 19 | + import * as ts from "typescript"; |
| 20 | + |
| 21 | + // excerpted from https://github.com./YousefED/typescript-json-schema |
| 22 | + // (converted from a method and modified; for example, `this: any` to compensate, among other changes) |
| 23 | + function parseCommentsIntoDefinition(this: any, |
| 24 | + symbol: ts.Symbol, |
| 25 | + definition: {description?: string, [s: string]: string | undefined}, |
| 26 | + otherAnnotations: { [s: string]: true}): void { |
| 27 | + if (!symbol) { |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + // the comments for a symbol |
| 32 | + let comments = symbol.getDocumentationComment(undefined); |
| 33 | + |
| 34 | + if (comments.length) { |
| 35 | + definition.description = comments.map(comment => comment.kind === "lineBreak" ? comment.text : comment.text.trim().replace(/\r\n/g, "\n")).join(""); |
| 36 | + } |
| 37 | + |
| 38 | + // jsdocs are separate from comments |
| 39 | + const jsdocs = symbol.getJsDocTags(); |
| 40 | + jsdocs.forEach(doc => { |
| 41 | + // if we have @TJS-... annotations, we have to parse them |
| 42 | + const { name, text } = doc; |
| 43 | + if (this.userValidationKeywords[name]) { |
| 44 | + definition[name] = this.parseValue(text); |
| 45 | + } else { |
| 46 | + // special annotations |
| 47 | + otherAnnotations[doc.name] = true; |
| 48 | + } |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + // excerpted from https://github.com./vega/ts-json-schema-generator |
| 54 | + export interface Annotations { |
| 55 | + [name: string]: any; |
| 56 | + } |
| 57 | + function getAnnotations(this: any, node: ts.Node): Annotations | undefined { |
| 58 | + const symbol: ts.Symbol = (node as any).symbol; |
| 59 | + if (!symbol) { |
| 60 | + return undefined; |
| 61 | + } |
| 62 | + |
| 63 | + const jsDocTags: ts.JSDocTagInfo[] = symbol.getJsDocTags(); |
| 64 | + if (!jsDocTags || !jsDocTags.length) { |
| 65 | + return undefined; |
| 66 | + } |
| 67 | + |
| 68 | + const annotations: Annotations = jsDocTags.reduce((result: Annotations, jsDocTag: ts.JSDocTagInfo) => { |
| 69 | + const value = this.parseJsDocTag(jsDocTag); |
| 70 | + if (value !== undefined) { |
| 71 | + result[jsDocTag.name] = value; |
| 72 | + } |
| 73 | + |
| 74 | + return result; |
| 75 | + }, {}); |
| 76 | + return Object.keys(annotations).length ? annotations : undefined; |
| 77 | + } |
| 78 | + |
| 79 | + // these examples are artificial and mostly nonsensical |
| 80 | + function parseSpecificTags(node: ts.Node) { |
| 81 | + if (node.kind === ts.SyntaxKind.Parameter) { |
| 82 | + return ts.getJSDocParameterTags(node as ts.ParameterDeclaration); |
| 83 | + } |
| 84 | + if (node.kind === ts.SyntaxKind.FunctionDeclaration) { |
| 85 | + const func = node as ts.FunctionDeclaration; |
| 86 | + if (ts.hasJSDocParameterTags(func)) { |
| 87 | + const flat: ts.JSDocTag[] = []; |
| 88 | + for (const tags of func.parameters.map(ts.getJSDocParameterTags)) { |
| 89 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 90 | +!!! error TS2495: Type '{ [x: number]: readonly JSDocParameterTag[]; hasTrailingComma?: readonly JSDocParameterTag[] | undefined; length: readonly JSDocParameterTag[]; toString: readonly JSDocParameterTag[]; toLocaleString: readonly JSDocParameterTag[]; concat: readonly JSDocParameterTag[]; join: readonly JSDocParameterTag[]; slice: readonly JSDocParameterTag[]; indexOf: readonly JSDocParameterTag[]; lastIndexOf: readonly JSDocParameterTag[]; every: readonly JSDocParameterTag[]; some: readonly JSDocParameterTag[]; forEach: readonly JSDocParameterTag[]; map: readonly JSDocParameterTag[]; filter: readonly JSDocParameterTag[]; reduce: readonly JSDocParameterTag[]; reduceRight: readonly JSDocParameterTag[]; pos: readonly JSDocParameterTag[]; end: readonly JSDocParameterTag[]; }' is not an array type or a string type. |
| 91 | + if (tags) flat.push(...tags); |
| 92 | + } |
| 93 | + return flat; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + function getReturnTypeFromJSDoc(node: ts.Node) { |
| 99 | + if (node.kind === ts.SyntaxKind.FunctionDeclaration) { |
| 100 | + return ts.getJSDocReturnType(node); |
| 101 | + } |
| 102 | + let type = ts.getJSDocType(node); |
| 103 | + if (type && type.kind === ts.SyntaxKind.FunctionType) { |
| 104 | + return (type as ts.FunctionTypeNode).type; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + function getAllTags(node: ts.Node) { |
| 109 | + ts.getJSDocTags(node); |
| 110 | + } |
| 111 | + |
| 112 | + function getSomeOtherTags(node: ts.Node) { |
| 113 | + const tags: (ts.JSDocTag | undefined)[] = []; |
| 114 | + tags.push(ts.getJSDocAugmentsTag(node)); |
| 115 | + tags.push(ts.getJSDocClassTag(node)); |
| 116 | + tags.push(ts.getJSDocReturnTag(node)); |
| 117 | + const type = ts.getJSDocTypeTag(node); |
| 118 | + if (type) { |
| 119 | + tags.push(type); |
| 120 | + } |
| 121 | + tags.push(ts.getJSDocTemplateTag(node)); |
| 122 | + return tags; |
| 123 | + } |
| 124 | + |
0 commit comments