Skip to content

Commit 21bcf2a

Browse files
author
John Jenkins
committed
auto add '@readonly' to jsdoc
1 parent 4b78586 commit 21bcf2a

File tree

2 files changed

+70
-14
lines changed

2 files changed

+70
-14
lines changed

src/compiler/types/generate-prop-types.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,22 @@ import { updateTypeIdentifierNames } from './stencil-types';
1111
*/
1212
export const generatePropTypes = (cmpMeta: d.ComponentCompilerMeta, typeImportData: d.TypesImportData): d.TypeInfo => {
1313
return [
14-
...cmpMeta.properties.map((cmpProp) => ({
15-
name: cmpProp.name,
16-
type: getType(cmpProp, typeImportData, cmpMeta.sourceFilePath),
17-
optional: cmpProp.optional,
18-
required: cmpProp.required,
19-
internal: cmpProp.internal,
20-
jsdoc: getTextDocs(cmpProp.docs),
21-
})),
14+
...cmpMeta.properties.map((cmpProp) => {
15+
let doc = getTextDocs(cmpProp.docs);
16+
if (cmpProp.getter && !cmpProp.setter && !doc?.match('@readonly')) {
17+
cmpProp.docs = cmpProp.docs || { tags: [], text: '' };
18+
cmpProp.docs.tags = [...(cmpProp.docs.tags || []), { name: 'readonly', text: '' }];
19+
doc = getTextDocs(cmpProp.docs);
20+
}
21+
return {
22+
name: cmpProp.name,
23+
type: getType(cmpProp, typeImportData, cmpMeta.sourceFilePath),
24+
optional: cmpProp.optional,
25+
required: cmpProp.required,
26+
internal: cmpProp.internal,
27+
jsdoc: doc,
28+
};
29+
}),
2230
...cmpMeta.virtualProperties.map((cmpProp) => ({
2331
name: cmpProp.name,
2432
type: cmpProp.type,

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

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type * as d from '../../../declarations';
2-
import * as Util from '../../../utils/util';
32
import { generatePropTypes } from '../generate-prop-types';
43
import * as StencilTypes from '../stencil-types';
54
import { stubComponentCompilerMeta } from './ComponentCompilerMeta.stub';
@@ -13,7 +12,6 @@ describe('generate-prop-types', () => {
1312
ReturnType<typeof StencilTypes.updateTypeIdentifierNames>,
1413
Parameters<typeof StencilTypes.updateTypeIdentifierNames>
1514
>;
16-
let getTextDocsSpy: jest.SpyInstance<ReturnType<typeof Util.getTextDocs>, Parameters<typeof Util.getTextDocs>>;
1715

1816
beforeEach(() => {
1917
updateTypeIdentifierNamesSpy = jest.spyOn(StencilTypes, 'updateTypeIdentifierNames');
@@ -25,14 +23,10 @@ describe('generate-prop-types', () => {
2523
initialType: string,
2624
) => initialType,
2725
);
28-
29-
getTextDocsSpy = jest.spyOn(Util, 'getTextDocs');
30-
getTextDocsSpy.mockReturnValue('');
3126
});
3227

3328
afterEach(() => {
3429
updateTypeIdentifierNamesSpy.mockRestore();
35-
getTextDocsSpy.mockRestore();
3630
});
3731

3832
it('returns an empty array when no props are provided', () => {
@@ -141,5 +135,59 @@ describe('generate-prop-types', () => {
141135

142136
expect(actualTypeInfo).toEqual(expectedTypeInfo);
143137
});
138+
139+
it('appends `@readonly` to jsdoc when the property has a getter and no setter', () => {
140+
const stubImportTypes = stubTypesImportData();
141+
const componentMeta = stubComponentCompilerMeta({
142+
properties: [
143+
stubComponentCompilerProperty({
144+
getter: true,
145+
setter: false,
146+
}),
147+
],
148+
});
149+
150+
const expectedTypeInfo: d.TypeInfo = [
151+
{
152+
jsdoc: '@readonly',
153+
internal: false,
154+
name: 'propName',
155+
optional: false,
156+
required: false,
157+
type: 'UserCustomPropType',
158+
},
159+
];
160+
161+
const actualTypeInfo = generatePropTypes(componentMeta, stubImportTypes);
162+
163+
expect(actualTypeInfo).toEqual(expectedTypeInfo);
164+
});
165+
166+
it('does not include `@readonly` to jsdoc when the property has a getter and a setter', () => {
167+
const stubImportTypes = stubTypesImportData();
168+
const componentMeta = stubComponentCompilerMeta({
169+
properties: [
170+
stubComponentCompilerProperty({
171+
getter: true,
172+
setter: true,
173+
}),
174+
],
175+
});
176+
177+
const expectedTypeInfo: d.TypeInfo = [
178+
{
179+
jsdoc: '',
180+
internal: false,
181+
name: 'propName',
182+
optional: false,
183+
required: false,
184+
type: 'UserCustomPropType',
185+
},
186+
];
187+
188+
const actualTypeInfo = generatePropTypes(componentMeta, stubImportTypes);
189+
190+
expect(actualTypeInfo).toEqual(expectedTypeInfo);
191+
});
144192
});
145193
});

0 commit comments

Comments
 (0)