Skip to content

Commit

Permalink
fix: handle property after function call
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT committed Dec 19, 2024
1 parent c4bb3dd commit 22039a9
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 3 deletions.
8 changes: 7 additions & 1 deletion docs/.vitepress/components/api-docs/method.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,17 @@ function initRefresh(): Element[] {
// Skip to end of the invocation (if multiline)
while (
domLines[lineIndex] != null &&
!/^([^ ].*)?\);? ?(\/\/|$)/.test(domLines[lineIndex]?.textContent ?? '')
!/^([^ ].*)?\)(\.\w+)?;? ?(\/\/|$)/.test(
domLines[lineIndex]?.textContent ?? ''
)
) {
lineIndex++;
}
if (lineIndex >= domLines.length) {
break;
}
const domLine = domLines[lineIndex];
result.push(domLine);
lineIndex++;
Expand Down
6 changes: 4 additions & 2 deletions scripts/apidocs/output/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ export function extractSummaryDefault(description: string): string | undefined {
return defaultCommentRegex.exec(description)?.[1];
}

async function toRefreshFunction(method: RawApiDocsMethod): Promise<string> {
export async function toRefreshFunction(
method: RawApiDocsMethod
): Promise<string> {
const { name, signatures } = method;
const signatureData = required(signatures.at(-1), 'method signature');
const { examples } = signatureData;
Expand All @@ -216,7 +218,7 @@ async function toRefreshFunction(method: RawApiDocsMethod): Promise<string> {
.replaceAll(/^import .*$/gm, '') // Remove imports
.replaceAll(
// record results of faker calls
/^(\w*faker\w*\..+(?:(?:.|\n..)*\n[^ ])?\));?$/gim,
/^(\w*faker\w*\..+(?:(?:.|\n..)*\n[^ ])?\)(?:\.\w+)?);?$/gim,
`try { result.push($1); } catch (error: unknown) { result.push(error instanceof Error ? error.name : 'Error'); }\n`
);

Expand Down
97 changes: 97 additions & 0 deletions test/scripts/apidocs/__snapshots__/page.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`toRefreshFunction > should handle multiline calls 1`] = `
"async (): Promise<unknown[]> => {
await enableFaker();
faker.seed();
faker.setDefaultRefDate();
const result: unknown[] = [];
try {
result.push(
faker.number.int({
min: 1,
max: 10,
})
);
} catch (error: unknown) {
result.push(error instanceof Error ? error.name : 'Error');
}
return result;
}"
`;

exports[`toRefreshFunction > should handle multiple calls 1`] = `
"async (): Promise<unknown[]> => {
await enableFaker();
faker.seed();
faker.setDefaultRefDate();
const result: unknown[] = [];
try {
result.push(faker.number.int());
} catch (error: unknown) {
result.push(error instanceof Error ? error.name : 'Error');
}
try {
result.push(faker.number.int());
} catch (error: unknown) {
result.push(error instanceof Error ? error.name : 'Error');
}
return result;
}"
`;

exports[`toRefreshFunction > should handle properties after calls 1`] = `
"async (): Promise<unknown[]> => {
await enableFaker();
faker.seed();
faker.setDefaultRefDate();
const result: unknown[] = [];
try {
result.push(faker.airline.airport().name);
} catch (error: unknown) {
result.push(error instanceof Error ? error.name : 'Error');
}
return result;
}"
`;

exports[`toRefreshFunction > should handle single line calls with semicolon 1`] = `
"async (): Promise<unknown[]> => {
await enableFaker();
faker.seed();
faker.setDefaultRefDate();
const result: unknown[] = [];
try {
result.push(faker.number.int());
} catch (error: unknown) {
result.push(error instanceof Error ? error.name : 'Error');
}
return result;
}"
`;

exports[`toRefreshFunction > should handle single line calls without semicolon 1`] = `
"async (): Promise<unknown[]> => {
await enableFaker();
faker.seed();
faker.setDefaultRefDate();
const result: unknown[] = [];
try {
result.push(faker.number.int());
} catch (error: unknown) {
result.push(error instanceof Error ? error.name : 'Error');
}
return result;
}"
`;
114 changes: 114 additions & 0 deletions test/scripts/apidocs/page.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { describe, expect, it } from 'vitest';
import { toRefreshFunction } from '../../../scripts/apidocs/output/page';
import type { RawApiDocsMethod } from '../../../scripts/apidocs/processing/method';
import type { RawApiDocsSignature } from '../../../scripts/apidocs/processing/signature';

function newTestMethod(
signature: Partial<RawApiDocsSignature>
): RawApiDocsMethod {
return {
name: 'test',
signatures: [
{
deprecated: 'deprecated',
description: 'description',
since: 'since',
parameters: [],
returns: {
type: 'simple',
text: 'returns',
},
throws: [],
signature: 'signature',
examples: [],
seeAlsos: [],
...signature,
},
],
source: {
filePath: 'test/page.spec.ts',
line: 1,
column: 1,
},
};
}

describe('toRefreshFunction', () => {
it("should return 'undefined' when there are no faker calls", async () => {
// given
const method = newTestMethod({
examples: ['const a = 1;'],
});

// when
const result = await toRefreshFunction(method);

// then
expect(result).toBe('undefined');
});

it('should handle single line calls with semicolon', async () => {
// given
const method = newTestMethod({
examples: ['faker.number.int(); // 834135'],
});

// when
const result = await toRefreshFunction(method);

// then
expect(result).toMatchSnapshot();
});

it('should handle single line calls without semicolon', async () => {
// given
const method = newTestMethod({
examples: ['faker.number.int() // 834135'],
});

// when
const result = await toRefreshFunction(method);

// then
expect(result).toMatchSnapshot();
});

it('should handle multiple calls', async () => {
// given
const method = newTestMethod({
examples: ['faker.number.int()', 'faker.number.int()'],
});

// when
const result = await toRefreshFunction(method);

// then
expect(result).toMatchSnapshot();
});

it('should handle multiline calls', async () => {
// given
const method = newTestMethod({
examples: 'faker.number.int({\n min: 1,\n max: 10\n})'.split('\n'),
});

// when
const result = await toRefreshFunction(method);

// then
expect(result).toMatchSnapshot();
});

it('should handle properties after calls', async () => {
// given
const method = newTestMethod({
examples: ['faker.airline.airport().name'],
});

// when
const result = await toRefreshFunction(method);

// then
expect(result).toMatchSnapshot();
});
});

0 comments on commit 22039a9

Please sign in to comment.