Skip to content

feat: enforce lowercase primitive type names #144

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/ParsedDocumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export declare type PossibleStringValue = {
description: string;
};
export declare type DetailedStringType = {
type: 'String';
type: 'string';
possibleValues: PossibleStringValue[] | null;
};
export declare type DetailedObjectType = {
Expand Down
12 changes: 8 additions & 4 deletions src/markdown-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,14 @@ export const rawTypeToTypeInformation = (
}))
: [],
};
} else if (typeString === 'String' || typeString === 'string') {
} else if (typeString === 'Boolean' || typeString === 'Number' || typeString === 'String') {
throw new Error(
`Use lowercase "${typeString.toLowerCase()}" instead of "${typeString}" for a primitive type`,
);
} else if (typeString === 'string') {
return {
collection,
type: 'String',
type: 'string',
possibleValues:
subTypedKeys && !subTypedKeys.consumed
? consumeTypedKeysList(subTypedKeys).map<PossibleStringValue>((typedKey) => ({
Expand Down Expand Up @@ -895,7 +899,7 @@ const convertNestedListToTypedKeys = (list: List): TypedKey[] => {
for (const item of list.items) {
// If the current list would fail checks, but it has a nested list, assume that the nested list is the content we want
// E.g.
// * `foo` - String
// * `foo` - string
// * On windows these keys
// * `option1`
// * `option2`
Expand Down Expand Up @@ -930,7 +934,7 @@ const convertNestedListToTypedKeys = (list: List): TypedKey[] => {
const typeAndDescriptionTokens = targetToken.children!.slice(1);
const joinedContent = safelyJoinTokens(typeAndDescriptionTokens);

let rawType = 'String';
let rawType = 'string';
if (typeAndDescriptionTokens.length !== 0) {
rawType = joinedContent.split('-')[0];
}
Expand Down
2 changes: 1 addition & 1 deletion tests/__snapshots__/markdown-helpers.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ exports[`markdown-helpers > rawTypeToTypeInformation() > should map a nested com
exports[`markdown-helpers > rawTypeToTypeInformation() > should map a primitive types correctly 1`] = `
{
"collection": false,
"type": "Boolean",
"type": "boolean",
}
`;

Expand Down
39 changes: 36 additions & 3 deletions tests/markdown-helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,41 @@ def fn():
});

describe('rawTypeToTypeInformation()', () => {
it('should throw if a primitive type is not lowercase', () => {
expect(() =>
expect(rawTypeToTypeInformation('Boolean', '', null)),
).toThrowErrorMatchingInlineSnapshot(
`[Error: Use lowercase "boolean" instead of "Boolean" for a primitive type]`,
);
expect(() =>
expect(rawTypeToTypeInformation('Promise<Boolean>', '', null)),
).toThrowErrorMatchingInlineSnapshot(
`[Error: Use lowercase "boolean" instead of "Boolean" for a primitive type]`,
);
expect(() =>
expect(rawTypeToTypeInformation('String', '', null)),
).toThrowErrorMatchingInlineSnapshot(
`[Error: Use lowercase "string" instead of "String" for a primitive type]`,
);
expect(() =>
expect(rawTypeToTypeInformation('Promise<String>', '', null)),
).toThrowErrorMatchingInlineSnapshot(
`[Error: Use lowercase "string" instead of "String" for a primitive type]`,
);
expect(() =>
expect(rawTypeToTypeInformation('Number', '', null)),
).toThrowErrorMatchingInlineSnapshot(
`[Error: Use lowercase "number" instead of "Number" for a primitive type]`,
);
expect(() =>
expect(rawTypeToTypeInformation('Promise<Number>', '', null)),
).toThrowErrorMatchingInlineSnapshot(
`[Error: Use lowercase "number" instead of "Number" for a primitive type]`,
);
});

it('should map a primitive types correctly', () => {
expect(rawTypeToTypeInformation('Boolean', '', null)).toMatchSnapshot();
expect(rawTypeToTypeInformation('boolean', '', null)).toMatchSnapshot();
});

it('should map an unknown types correctly', () => {
Expand Down Expand Up @@ -550,12 +583,12 @@ foo`),
type: 'Integer',
});

const stringTokens = getTokens(`Returns \`String\` - Returns the WebRTC IP Handling Policy.`);
const stringTokens = getTokens(`Returns \`string\` - Returns the WebRTC IP Handling Policy.`);
const stringRet = extractReturnType(stringTokens);
expect(stringRet.parsedReturnType).toEqual({
collection: false,
possibleValues: null,
type: 'String',
type: 'string',
});
});

Expand Down