Skip to content

Commit 82d3596

Browse files
authoredOct 25, 2022
fix(docs): avoid duplicating manual documentation (#3766)
this commit fixes a regression that was introduced in 2db4f4d (#3635). the original commit did not take into account the fact that a user could have manually written text in their component's markdown file. that content would be assigned to the value used to generated the markdown file's "Overview" section, causing the content to be displayed twice. the contents of the `docs` field (i.e. the way that value is generated). cannot be changed, as `JsonDocs` publicly exposes that field (via a `JsonDocsComponent` value), and may be used by custom documentation generators. instead, this commit adds an optional 'overview' field to the `JsonDocsComponent` type. the intent here is to avoid _most_ assignability errors by making this field optional. the value is assigned when stencil generates its documentation data with the text from the class component's jsdoc. this commit adds additional testing coverage to the 'docs' field of `JsonDocsComponent`
1 parent 02a578b commit 82d3596

File tree

5 files changed

+207
-2
lines changed

5 files changed

+207
-2
lines changed
 

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

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const getDocsComponents = async (
6060
usagesDir,
6161
tag: cmp.tagName,
6262
readme,
63+
overview: cmp.docs.text,
6364
usage,
6465
docs: generateDocs(readme, cmp.docs),
6566
docsTags: cmp.docs.tags,

‎src/compiler/docs/readme/markdown-overview.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @param overview a component-level comment string to place in a markdown file
44
* @returns The generated Overview section. If the provided overview is empty, return an empty list
55
*/
6-
export const overviewToMarkdown = (overview: string): ReadonlyArray<string> => {
6+
export const overviewToMarkdown = (overview: string | undefined): ReadonlyArray<string> => {
77
if (!overview) {
88
return [];
99
}

‎src/compiler/docs/readme/output-docs.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export const generateMarkdown = (
5555
'',
5656
'',
5757
...getDocsDeprecation(cmp),
58-
...overviewToMarkdown(cmp.docs),
58+
...overviewToMarkdown(cmp.overview),
5959
...usageToMarkdown(cmp.usage),
6060
...propsToMarkdown(cmp.props),
6161
...eventsToMarkdown(cmp.events),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
import { mockBuildCtx, mockCompilerCtx, mockModule, mockValidatedConfig } from '@stencil/core/testing';
2+
3+
import type * as d from '../../../declarations';
4+
import { getComponentsFromModules } from '../../output-targets/output-utils';
5+
import { stubComponentCompilerMeta } from '../../types/tests/ComponentCompilerMeta.stub';
6+
import { AUTO_GENERATE_COMMENT } from '../constants';
7+
import { generateDocData } from '../generate-doc-data';
8+
9+
describe('generate-doc-data', () => {
10+
describe('getDocsComponents', () => {
11+
let moduleCmpWithJsdoc: d.Module;
12+
let moduleCmpNoJsdoc: d.Module;
13+
14+
beforeEach(() => {
15+
moduleCmpWithJsdoc = mockModule({
16+
cmps: [
17+
stubComponentCompilerMeta({
18+
docs: {
19+
tags: [],
20+
text: 'This is the overview of `my-component`',
21+
},
22+
}),
23+
],
24+
});
25+
moduleCmpNoJsdoc = mockModule({
26+
cmps: [
27+
stubComponentCompilerMeta({
28+
docs: {
29+
tags: [],
30+
text: '',
31+
},
32+
}),
33+
],
34+
});
35+
});
36+
37+
/**
38+
* Setup function for the {@link generateDocData} function exported by the module under test
39+
* @param moduleMap a map of {@link d.ModuleMap} entities to add to the returned compiler and build contexts
40+
* @returns the arguments required to invoke the method under test
41+
*/
42+
const setup = (
43+
moduleMap: d.ModuleMap
44+
): { validatedConfig: d.ValidatedConfig; compilerCtx: d.CompilerCtx; buildCtx: d.BuildCtx } => {
45+
const validatedConfig: d.ValidatedConfig = mockValidatedConfig();
46+
47+
const compilerCtx: d.CompilerCtx = mockCompilerCtx(validatedConfig);
48+
compilerCtx.moduleMap = moduleMap;
49+
50+
const modules = Array.from(compilerCtx.moduleMap.values());
51+
const buildCtx: d.BuildCtx = mockBuildCtx(validatedConfig, compilerCtx);
52+
buildCtx.moduleFiles = modules;
53+
buildCtx.components = getComponentsFromModules(modules);
54+
55+
return { validatedConfig, compilerCtx, buildCtx };
56+
};
57+
58+
describe('component JSDoc overview', () => {
59+
it("takes the value from the component's JSDoc", async () => {
60+
const moduleMap: d.ModuleMap = new Map();
61+
moduleMap.set('path/to/component.tsx', moduleCmpWithJsdoc);
62+
const { validatedConfig, compilerCtx, buildCtx } = setup(moduleMap);
63+
64+
const generatedDocData = await generateDocData(validatedConfig, compilerCtx, buildCtx);
65+
66+
expect(generatedDocData.components).toHaveLength(1);
67+
const componentDocData = generatedDocData.components[0];
68+
expect(componentDocData.overview).toBe('This is the overview of `my-component`');
69+
});
70+
71+
it('sets the value to the empty string when there is no JSDoc', async () => {
72+
const moduleMap: d.ModuleMap = new Map();
73+
moduleMap.set('path/to/component.tsx', moduleCmpNoJsdoc);
74+
const { validatedConfig, compilerCtx, buildCtx } = setup(moduleMap);
75+
76+
const generatedDocData = await generateDocData(validatedConfig, compilerCtx, buildCtx);
77+
78+
expect(generatedDocData.components).toHaveLength(1);
79+
const componentDocData = generatedDocData.components[0];
80+
expect(componentDocData.overview).toBe('');
81+
});
82+
});
83+
84+
describe('docs content', () => {
85+
it("sets the field's contents to the jsdoc text if present", async () => {
86+
const moduleMap: d.ModuleMap = new Map();
87+
moduleMap.set('path/to/component.tsx', moduleCmpWithJsdoc);
88+
const { validatedConfig, compilerCtx, buildCtx } = setup(moduleMap);
89+
90+
const generatedDocData = await generateDocData(validatedConfig, compilerCtx, buildCtx);
91+
92+
expect(generatedDocData.components).toHaveLength(1);
93+
const componentDocData = generatedDocData.components[0];
94+
expect(componentDocData.docs).toBe('This is the overview of `my-component`');
95+
});
96+
97+
it("sets the field's contents to an empty string if neither the readme, nor jsdoc are set", async () => {
98+
const moduleMap: d.ModuleMap = new Map();
99+
moduleMap.set('path/to/component.tsx', moduleCmpNoJsdoc);
100+
const { validatedConfig, compilerCtx, buildCtx } = setup(moduleMap);
101+
102+
const generatedDocData = await generateDocData(validatedConfig, compilerCtx, buildCtx);
103+
104+
expect(generatedDocData.components).toHaveLength(1);
105+
const componentDocData = generatedDocData.components[0];
106+
expect(componentDocData.docs).toBe('');
107+
});
108+
109+
it("sets the field's contents to an empty string if the readme doesn't contain the autogenerated comment", async () => {
110+
const moduleMap: d.ModuleMap = new Map();
111+
moduleMap.set('path/to/component.tsx', moduleCmpNoJsdoc);
112+
const { validatedConfig, compilerCtx, buildCtx } = setup(moduleMap);
113+
114+
await compilerCtx.fs.writeFile('readme.md', 'this is manually generated user content');
115+
116+
const generatedDocData = await generateDocData(validatedConfig, compilerCtx, buildCtx);
117+
118+
expect(generatedDocData.components).toHaveLength(1);
119+
const componentDocData = generatedDocData.components[0];
120+
expect(componentDocData.docs).toBe('');
121+
});
122+
123+
it("sets the field's contents to manually generated content when the autogenerated comment is present", async () => {
124+
const moduleMap: d.ModuleMap = new Map();
125+
moduleMap.set('path/to/component.tsx', moduleCmpNoJsdoc);
126+
const { validatedConfig, compilerCtx, buildCtx } = setup(moduleMap);
127+
128+
await compilerCtx.fs.writeFile(
129+
'readme.md',
130+
`this is manually generated user content\n${AUTO_GENERATE_COMMENT}\nauto-generated content`
131+
);
132+
133+
const generatedDocData = await generateDocData(validatedConfig, compilerCtx, buildCtx);
134+
135+
expect(generatedDocData.components).toHaveLength(1);
136+
const componentDocData = generatedDocData.components[0];
137+
expect(componentDocData.docs).toBe('this is manually generated user content');
138+
});
139+
140+
it("sets the field's contents to a subset of the manually generated content", async () => {
141+
const moduleMap: d.ModuleMap = new Map();
142+
moduleMap.set('path/to/component.tsx', moduleCmpNoJsdoc);
143+
const { validatedConfig, compilerCtx, buildCtx } = setup(moduleMap);
144+
145+
const readmeContent = `
146+
this is manually generated user content
147+
148+
# user header
149+
user content
150+
151+
# another user header
152+
more user content
153+
154+
${AUTO_GENERATE_COMMENT}
155+
156+
#some-header
157+
158+
auto-generated content
159+
`;
160+
await compilerCtx.fs.writeFile('readme.md', readmeContent);
161+
162+
const generatedDocData = await generateDocData(validatedConfig, compilerCtx, buildCtx);
163+
164+
expect(generatedDocData.components).toHaveLength(1);
165+
const componentDocData = generatedDocData.components[0];
166+
expect(componentDocData.docs).toBe('this is manually generated user content');
167+
});
168+
169+
it("sets the field's contents to a an empty string when the manually generated content starts with a '#'", async () => {
170+
const moduleMap: d.ModuleMap = new Map();
171+
moduleMap.set('path/to/component.tsx', moduleCmpNoJsdoc);
172+
const { validatedConfig, compilerCtx, buildCtx } = setup(moduleMap);
173+
174+
const readmeContent = `
175+
# header that leads to skipping
176+
this is manually generated user content
177+
178+
# user header
179+
user content
180+
181+
# another user header
182+
more user content
183+
184+
${AUTO_GENERATE_COMMENT}
185+
186+
#some-header
187+
188+
auto-generated content
189+
`;
190+
await compilerCtx.fs.writeFile('readme.md', readmeContent);
191+
192+
const generatedDocData = await generateDocData(validatedConfig, compilerCtx, buildCtx);
193+
194+
expect(generatedDocData.components).toHaveLength(1);
195+
const componentDocData = generatedDocData.components[0];
196+
expect(componentDocData.docs).toBe('');
197+
});
198+
});
199+
});
200+
});

‎src/declarations/stencil-public-docs.ts

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export interface JsonDocsComponent {
1919
readme: string;
2020
docs: string;
2121
docsTags: JsonDocsTag[];
22+
/**
23+
* The comment found at in a class-level JSDoc for a Stencil component.
24+
*/
25+
overview?: string;
2226
usage: JsonDocsUsage;
2327
props: JsonDocsProp[];
2428
methods: JsonDocsMethod[];

0 commit comments

Comments
 (0)
Please sign in to comment.