Skip to content

Commit

Permalink
Make Boolean types more permissive and remove default value for Object
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmengo committed Feb 25, 2025
1 parent a2686d6 commit f63933b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@ describe('Module: ValidRenderSnippetParamTypes', () => {
},
{
type: 'boolean',
validValues: ['true', 'false', 'nil', 'empty', 'product'],
invalidValues: [
{ value: "'hello'", actualType: SupportedParamTypes.String },
{ value: '123', actualType: SupportedParamTypes.Number },
],
validValues: ['true', 'false', 'nil', 'empty', 'product', '123', "'hello'"],
invalidValues: [],
},
{
type: 'object',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
inferArgumentType,
getDefaultValueForType,
SupportedParamTypes,
isTypeCompatible,
} from '../../liquid-doc/utils';

export const ValidRenderSnippetParamTypes: LiquidCheckDefinition = {
Expand Down Expand Up @@ -46,7 +47,7 @@ export const ValidRenderSnippetParamTypes: LiquidCheckDefinition = {
continue;
}

if (inferArgumentType(arg) !== paramType) {
if (!isTypeCompatible(paramType, inferArgumentType(arg))) {
typeMismatchParams.push(arg);
}
}
Expand Down
18 changes: 16 additions & 2 deletions packages/theme-check-common/src/liquid-doc/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ export function getDefaultValueForType(type: string | null) {
return '0';
case SupportedParamTypes.Boolean:
return 'false';
case SupportedParamTypes.Object:
return 'empty';
case SupportedParamTypes.Object: // Objects don't have a sensible default value (maybe `theme`?)
default:
return '';
}
Expand All @@ -51,3 +50,18 @@ export function inferArgumentType(arg: LiquidNamedArgument): SupportedParamTypes
return assertNever(arg.value);
}
}

/**
* Checks if the provided argument type is compatible with the expected type.
* Makes certain types more permissive:
* - Boolean accepts any value, since everything is truthy / falsy in Liquid
*/
export function isTypeCompatible(expectedType: string, actualType: SupportedParamTypes): boolean {
const normalizedExpectedType = expectedType.toLowerCase();

if (normalizedExpectedType === SupportedParamTypes.Boolean) {
return true;
}

return normalizedExpectedType === actualType;
}

0 comments on commit f63933b

Please sign in to comment.