Skip to content

Commit 2d16db6

Browse files
fix(compiler): fix transforming method parameters into docs (#5166)
fixes #4394 STENCIL-846
1 parent 97dcb45 commit 2d16db6

File tree

14 files changed

+139
-72
lines changed

14 files changed

+139
-72
lines changed

src/compiler/docs/generate-doc-data.ts

+19-16
Original file line numberDiff line numberDiff line change
@@ -268,22 +268,25 @@ const parseTypeIntoValues = (type: string): d.JsonDocsValue[] => {
268268
const getDocsMethods = (methods: d.ComponentCompilerMethod[]): d.JsonDocsMethod[] => {
269269
return sortBy(methods, (member) => member.name)
270270
.filter((member) => !member.internal)
271-
.map((member) => ({
272-
name: member.name,
273-
returns: {
274-
type: member.complexType.return,
275-
docs: member.docs.tags
276-
.filter((t) => t.name === 'return' || t.name === 'returns')
277-
.map((t) => t.text)
278-
.join('\n'),
279-
},
280-
complexType: member.complexType,
281-
signature: `${member.name}${member.complexType.signature}`,
282-
parameters: [], // TODO
283-
docs: member.docs.text,
284-
docsTags: member.docs.tags,
285-
deprecation: getDocsDeprecationText(member.docs.tags),
286-
}));
271+
.map(
272+
(member) =>
273+
<d.JsonDocsMethod>{
274+
name: member.name,
275+
returns: {
276+
type: member.complexType.return,
277+
docs: member.docs.tags
278+
.filter((t) => t.name === 'return' || t.name === 'returns')
279+
.map((t) => t.text)
280+
.join('\n'),
281+
},
282+
complexType: member.complexType,
283+
signature: `${member.name}${member.complexType.signature}`,
284+
parameters: member.complexType.parameters,
285+
docs: member.docs.text,
286+
docsTags: member.docs.tags,
287+
deprecation: getDocsDeprecationText(member.docs.tags),
288+
},
289+
);
287290
};
288291

289292
const getDocsEvents = (events: d.ComponentCompilerEvent[]): d.JsonDocsEvent[] => {

src/compiler/transformers/decorators-to-static/method-decorator.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
mapJSDocTagInfo,
1212
retrieveTsDecorators,
1313
retrieveTsModifiers,
14-
serializeSymbol,
1514
typeToString,
1615
validateReferences,
1716
} from '../transform-utils';
@@ -96,7 +95,11 @@ const parseMethodDecorator = (
9695
const methodMeta: d.ComponentCompilerStaticMethod = {
9796
complexType: {
9897
signature: signatureString,
99-
parameters: signature.parameters.map((symbol) => serializeSymbol(typeChecker, symbol)),
98+
parameters: signature.parameters.map((symbol) => ({
99+
name: symbol.getName(),
100+
type: typeToString(typeChecker, typeChecker.getTypeOfSymbolAtLocation(symbol, method)),
101+
docs: ts.displayPartsToString(symbol.getDocumentationComment(typeChecker)),
102+
})),
100103
references: {
101104
...getAttributeTypeInfo(returnTypeNode, tsSourceFile, typeChecker, program),
102105
...getAttributeTypeInfo(method, tsSourceFile, typeChecker, program),

src/compiler/transformers/test/parse-comments.spec.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ describe('parse comments', () => {
6161
complexType: {
6262
parameters: [
6363
{
64-
tags: [],
65-
text: '',
64+
name: 'prop',
65+
type: 'string',
66+
docs: '',
6667
},
6768
],
6869
return: 'unknown',

src/compiler/transformers/test/parse-methods.spec.ts

+33-20
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,52 @@ describe('parse methods', () => {
55
const t = transpileModule(`
66
@Component({tag: 'cmp-a'})
77
export class CmpA {
8+
/**
9+
* @param foo bar
10+
* @param bar foo
11+
*/
812
@Method()
9-
someMethod() {
13+
someMethod(foo: string, bar: number) {
1014
1115
}
1216
}
1317
`);
1418

15-
expect(getStaticGetter(t.outputText, 'methods')).toEqual({
16-
someMethod: {
17-
complexType: {
18-
parameters: [],
19-
return: 'void',
20-
references: {},
21-
signature: '() => void',
22-
},
23-
docs: {
24-
text: '',
25-
tags: [],
26-
},
27-
},
28-
});
29-
30-
expect(t.method).toEqual({
19+
const someMethod = {
3120
complexType: {
32-
parameters: [],
21+
parameters: [
22+
{
23+
name: 'foo',
24+
type: 'string',
25+
docs: 'bar',
26+
},
27+
{
28+
name: 'bar',
29+
type: 'number',
30+
docs: 'foo',
31+
},
32+
],
3333
return: 'void',
3434
references: {},
35-
signature: '() => void',
35+
signature: '(foo: string, bar: number) => void',
3636
},
3737
docs: {
38-
tags: [],
3938
text: '',
39+
tags: [
40+
{
41+
name: 'param',
42+
text: 'foo bar',
43+
},
44+
{
45+
name: 'param',
46+
text: 'bar foo',
47+
},
48+
],
4049
},
50+
};
51+
expect(getStaticGetter(t.outputText, 'methods')).toEqual({ someMethod });
52+
expect(t.method).toEqual({
53+
...someMethod,
4154
internal: false,
4255
name: 'someMethod',
4356
});

src/compiler/types/tests/ComponentCompilerMethod.stub.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const stubComponentCompilerMethod = (
1414
name: 'myMethod',
1515
internal: false,
1616
complexType: {
17-
parameters: [{ tags: [], text: '' }],
17+
parameters: [{ name: 'name', type: 'Foo', docs: '' }],
1818
references: { Foo: { location: 'import', path: './resources', id: 'placeholder' } },
1919
return: 'Promise<void>',
2020
signature: '(name: Foo) => Promise<void>',

src/compiler/types/tests/generate-method-types.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ describe('generate-method-types', () => {
9797
name: 'myOtherMethod',
9898
internal: true,
9999
complexType: {
100-
parameters: [{ tags: [], text: '' }],
100+
parameters: [{ name: 'age', type: 'Bar', docs: '' }],
101101
references: { Bar: { location: 'local', id: 'placeholder_id', path: './other-resources' } },
102102
return: 'Promise<boolean>',
103103
signature: '(age: Bar) => Promise<boolean>',

src/declarations/stencil-private.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import type {
2828
TaskCommand,
2929
ValidatedConfig,
3030
} from './stencil-public-compiler';
31+
import type { JsonDocMethodParameter } from './stencil-public-docs';
3132
import type { ComponentInterface, ListenTargetOptions, VNode } from './stencil-public-runtime';
3233

3334
export interface SourceMap {
@@ -799,7 +800,7 @@ export interface ComponentCompilerStaticMethod {
799800

800801
export interface ComponentCompilerMethodComplexType {
801802
signature: string;
802-
parameters: CompilerJsDoc[];
803+
parameters: JsonDocMethodParameter[];
803804
references: ComponentCompilerTypeReferences;
804805
return: string;
805806
}

test/docs-json/docs.d.ts

+1-21
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,10 @@ interface ComponentCompilerEventComplexType {
6060
}
6161
interface ComponentCompilerMethodComplexType {
6262
signature: string;
63-
parameters: CompilerJsDoc[];
63+
parameters: JsonDocMethodParameter[];
6464
references: ComponentCompilerTypeReferences;
6565
return: string;
6666
}
67-
interface CompilerJsDoc {
68-
/**
69-
* The text associated with the JSDoc
70-
*/
71-
text: string;
72-
/**
73-
* Tags included in the JSDoc
74-
*/
75-
tags: CompilerJsDocTagInfo[];
76-
}
77-
interface CompilerJsDocTagInfo {
78-
/**
79-
* The name of the tag - e.g. `@deprecated`
80-
*/
81-
name: string;
82-
/**
83-
* Additional text that is associated with the tag - e.g. `@deprecated use v2 of this API`
84-
*/
85-
text?: string;
86-
}
8767
/**
8868
* The Type Library holds information about the types which are used in a
8969
* Stencil project. During compilation, Stencil gathers information about the

test/docs-json/docs.json

+10-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
"signature": "<T>(arg: T) => Promise<ImportedInterface<T>>",
2020
"parameters": [
2121
{
22-
"tags": [],
23-
"text": ""
22+
"name": "arg",
23+
"type": "T",
24+
"docs": ""
2425
}
2526
],
2627
"references": {
@@ -41,7 +42,13 @@
4142
"return": "Promise<ImportedInterface<T>>"
4243
},
4344
"signature": "onDidDismiss<T>(arg: T) => Promise<ImportedInterface<T>>",
44-
"parameters": [],
45+
"parameters": [
46+
{
47+
"name": "arg",
48+
"type": "T",
49+
"docs": ""
50+
}
51+
],
4552
"docs": "A comment, which should be included, I should think!",
4653
"docsTags": []
4754
}

test/end-to-end/src/components.d.ts

+23
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,21 @@ export namespace Components {
3333
interface EnvData {
3434
}
3535
interface EventCmp {
36+
/**
37+
* this is some method that fires an event with options
38+
* @returns
39+
*/
3640
"methodThatFiresEventWithOptions": () => Promise<void>;
41+
/**
42+
* this is some method that fires a document event
43+
* @returns
44+
*/
3745
"methodThatFiresMyDocumentEvent": () => Promise<void>;
46+
/**
47+
* this is some method that fires a window event
48+
* @param value some value
49+
* @returns
50+
*/
3851
"methodThatFiresMyWindowEvent": (value: number) => Promise<void>;
3952
}
4053
interface ImportAssets {
@@ -43,7 +56,17 @@ export namespace Components {
4356
"opened": boolean;
4457
}
4558
interface MethodCmp {
59+
/**
60+
* this is some method
61+
* @returns some number
62+
*/
4663
"someMethod": () => Promise<number>;
64+
/**
65+
* this is some method with args
66+
* @param unit some unit
67+
* @param value some value
68+
* @returns some string
69+
*/
4770
"someMethodWithArgs": (unit: string, value: number) => Promise<string>;
4871
"someProp": number;
4972
}

test/end-to-end/src/event-cmp/event-cmp.tsx

+13
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,29 @@ export class EventCmp {
1515

1616
@Event() myWindowEvent: EventEmitter<number>;
1717

18+
/**
19+
* this is some method that fires a window event
20+
* @param value some value
21+
* @returns {void}
22+
*/
1823
@Method()
1924
async methodThatFiresMyWindowEvent(value: number) {
2025
this.myWindowEvent.emit(value);
2126
}
2227

28+
/**
29+
* this is some method that fires a document event
30+
* @returns {void}
31+
*/
2332
@Method()
2433
async methodThatFiresMyDocumentEvent() {
2534
this.myDocumentEvent.emit();
2635
}
2736

37+
/**
38+
* this is some method that fires an event with options
39+
* @returns {void}
40+
*/
2841
@Method()
2942
async methodThatFiresEventWithOptions() {
3043
this.myEventWithOptions.emit({ mph: 88 });

test/end-to-end/src/event-cmp/readme.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
### `methodThatFiresEventWithOptions() => Promise<void>`
2020

21-
21+
this is some method that fires an event with options
2222

2323
#### Returns
2424

@@ -28,7 +28,7 @@ Type: `Promise<void>`
2828

2929
### `methodThatFiresMyDocumentEvent() => Promise<void>`
3030

31-
31+
this is some method that fires a document event
3232

3333
#### Returns
3434

@@ -38,7 +38,13 @@ Type: `Promise<void>`
3838

3939
### `methodThatFiresMyWindowEvent(value: number) => Promise<void>`
4040

41+
this is some method that fires a window event
42+
43+
#### Parameters
4144

45+
| Name | Type | Description |
46+
| ------- | -------- | ----------- |
47+
| `value` | `number` | some value |
4248

4349
#### Returns
4450

test/end-to-end/src/method-cmp/method-cmp.tsx

+10
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,21 @@ import { Component, Method, Prop } from '@stencil/core';
66
export class MethodCmp {
77
@Prop() someProp = 0;
88

9+
/**
10+
* this is some method
11+
* @returns {number} some number
12+
*/
913
@Method()
1014
async someMethod() {
1115
return this.someProp;
1216
}
1317

18+
/**
19+
* this is some method with args
20+
* @param unit some unit
21+
* @param value some value
22+
* @returns {string} some string
23+
*/
1424
@Method()
1525
async someMethodWithArgs(unit: string, value: number) {
1626
return `${value} ${unit}`;

test/end-to-end/src/method-cmp/readme.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,30 @@
1616

1717
### `someMethod() => Promise<number>`
1818

19-
19+
this is some method
2020

2121
#### Returns
2222

2323
Type: `Promise<number>`
2424

25-
25+
some number
2626

2727
### `someMethodWithArgs(unit: string, value: number) => Promise<string>`
2828

29+
this is some method with args
2930

31+
#### Parameters
32+
33+
| Name | Type | Description |
34+
| ------- | -------- | ----------- |
35+
| `unit` | `string` | some unit |
36+
| `value` | `number` | some value |
3037

3138
#### Returns
3239

3340
Type: `Promise<string>`
3441

35-
42+
some string
3643

3744

3845
----------------------------------------------

0 commit comments

Comments
 (0)