Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support double quote string enum #122

Merged
merged 5 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/__tests__/markdown-helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,65 @@ def fn():
});
});

describe('with double quotes', () => {
it('should extract an enum of the format "can be x"', () => {
const values = extractStringEnum(`Can be "x"`)!;
expect(values).not.toBe(null);
expect(values).toHaveLength(1);
expect(values[0].value).toBe('x');
});

it('should extract an enum of the format "can be x or y"', () => {
const values = extractStringEnum(`Can be "x" or "y"`)!;
expect(values).not.toBe(null);
expect(values).toHaveLength(2);
expect(values[0].value).toBe('x');
expect(values[1].value).toBe('y');
});

it('should extract an enum of the format "can be x, y or z"', () => {
const values = extractStringEnum(`Can be "x", "y" or "z"`)!;
expect(values).not.toBe(null);
expect(values).toHaveLength(3);
expect(values[0].value).toBe('x');
expect(values[1].value).toBe('y');
expect(values[2].value).toBe('z');
});

it('should extract an enum of the format "can be x, y, or z"', () => {
const values = extractStringEnum(`Can be "x", "y", or "z"`)!;
expect(values).not.toBe(null);
expect(values).toHaveLength(3);
expect(values[0].value).toBe('x');
expect(values[1].value).toBe('y');
expect(values[2].value).toBe('z');
});

it('should extract an enum of the format "values include a', () => {
const values = extractStringEnum(`Values include "a"`)!;
expect(values).not.toBe(null);
expect(values).toHaveLength(1);
expect(values[0].value).toBe('a');
});

it('should extract an enum of the format "values include a and b', () => {
const values = extractStringEnum(`Values include "a" and "b"`)!;
expect(values).not.toBe(null);
expect(values).toHaveLength(2);
expect(values[0].value).toBe('a');
expect(values[1].value).toBe('b');
});

it('should extract an enum of the format "values include a, b and c', () => {
const values = extractStringEnum(`Values include "a", "b" and "c"`)!;
expect(values).not.toBe(null);
expect(values).toHaveLength(3);
expect(values[0].value).toBe('a');
expect(values[1].value).toBe('b');
expect(values[2].value).toBe('c');
});
});

describe('rawTypeToTypeInformation()', () => {
it('should map a primitive types correctly', () => {
expect(rawTypeToTypeInformation('Boolean', '', null)).toMatchSnapshot();
Expand Down
4 changes: 2 additions & 2 deletions src/markdown-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,11 +456,11 @@ export enum StripReturnTypeBehavior {
export const extractStringEnum = (description: string): PossibleStringValue[] | null => {
const possibleValues: PossibleStringValue[] = [];

const inlineValuesPattern = /(?:can be|values? includes?) ((?:(?:[`|'][a-zA-Z0-9-_\.:]+[`|'])(?:(, | )?))*(?:(?:or|and) [`|'][a-zA-Z0-9-_\.:]+[`|'])?)/i;
const inlineValuesPattern = /(?:can be|values? includes?) ((?:(?:[`"'][a-zA-Z0-9-_\.:]+[`"'])(?:(, | )?))*(?:(?:or|and) [`"'][a-zA-Z0-9-_\.:]+[`"'])?)/i;
const inlineMatch = inlineValuesPattern.exec(description);
if (inlineMatch) {
const valueString = inlineMatch[1];
const valuePattern = /[`|']([a-zA-Z0-9-_\.:]+)[`|']/g;
const valuePattern = /[`"']([a-zA-Z0-9-_\.:]+)[`"']/g;
let value = valuePattern.exec(valueString);

while (value) {
Expand Down