Skip to content

Commit 6fbd6d0

Browse files
committed
fix: jsdocs tests
1 parent 8b53b0e commit 6fbd6d0

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

test/scripts/apidocs/__snapshots__/verify-jsdoc-tags.spec.ts.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ exports[`check docs completeness > all modules and methods are present 1`] = `
3131
[
3232
"generateMersenne32Randomizer",
3333
"generateMersenne53Randomizer",
34+
"getDefaultRefDate",
3435
"mergeLocales",
36+
"setDefaultRefDate",
3537
],
3638
],
3739
[

test/scripts/apidocs/verify-jsdoc-tags.spec.ts

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ afterAll(() => {
2424
});
2525

2626
const modules = processComponents(getProject());
27+
const moduleByMethod = new Map(
28+
modules.flatMap((m) => m.methods.map((method) => [method.name, m.camelTitle]))
29+
);
2730

2831
function resolveDirToModule(moduleName: string): string {
2932
return resolve(tempDir, moduleName);
@@ -38,6 +41,13 @@ function resolvePathToMethodFile(
3841
return resolve(dir, `${methodName}_${signature}.ts`);
3942
}
4043

44+
function toKebabCase(str: string): string {
45+
return str
46+
.replaceAll(/([a-z])([A-Z])/g, '$1-$2')
47+
.replaceAll(/[\s_]+/g, '-')
48+
.toLowerCase();
49+
}
50+
4151
const allowedReferences = new Set(
4252
modules.flatMap(({ camelTitle, methods, category }) => {
4353
return methods.map(({ name }) =>
@@ -94,6 +104,10 @@ describe('verify JSDoc tags', () => {
94104
});
95105
});
96106

107+
const thisModuleByMethod = new Map(
108+
module.methods.map((method) => [method.name, moduleName])
109+
);
110+
97111
describe.each(module.methods.map((m) => [m.name, m]))(
98112
'%s',
99113
(methodName, method) => {
@@ -145,10 +159,41 @@ ${examples}`;
145159
];
146160

147161
if (imports.length > 0) {
148-
examples = `import { ${imports.join(
149-
', '
150-
)} } from '${relativeImportPath}';\n\n${examples}`;
162+
examples = `// imports
163+
import { ${imports.join(', ')} } from '${relativeImportPath}';
164+
165+
${examples}`;
166+
}
167+
168+
const functionImports = [
169+
...new Set(examples.match(/\b(\w+)\(fakerCore/g)),
170+
]
171+
.map((s) => s.replace('(fakerCore', ''))
172+
.map((functionName) => [
173+
functionName,
174+
(
175+
thisModuleByMethod.get(functionName) ??
176+
moduleByMethod.get(functionName) ??
177+
'..'
178+
)?.replace(
179+
'utils',
180+
moduleName === 'utils' ? '../utils' : 'utils'
181+
),
182+
]);
183+
184+
if (examples.includes('fakerCore')) {
185+
examples = `// fakerCore
186+
import { base, en } from '${relativeImportPath}';
187+
import { createFakerCore } from '${relativeImportPath}/faker-core';
188+
const fakerCore = createFakerCore({ definitions: [en, base] });
189+
190+
${examples.replace("import { fakerCore } from '../../../../../src';", '')}`;
151191
}
192+
193+
examples = `// functionImports
194+
${functionImports.map(([functionName, moduleName]) => `import { ${functionName} } from '${relativeImportPath}/modules/${moduleName}/${toKebabCase(functionName)}';`).join('\n')}
195+
196+
${examples}`;
152197
}
153198

154199
writeFileSync(path, examples);

0 commit comments

Comments
 (0)