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
chore(shadertools): Fully eliminate implicit typings #1619
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
export type PropDef = { | ||
export type PropType = { | ||
type?: string; | ||
max?: number; | ||
min?: number; | ||
value?: any; | ||
} | number; | ||
|
||
export type PropType = { | ||
export type PropDef = { | ||
type: string; | ||
value: any; | ||
max?: number; | ||
min?: number; | ||
private?: boolean; | ||
validate?(value: any, propDef: PropType): boolean; | ||
validate?(value: any, propDef: PropDef): boolean; | ||
}; | ||
|
||
const TYPE_DEFINITIONS = { | ||
const TYPE_DEFINITIONS: Record<string, {validate: (value: unknown, propType: PropType) => boolean}> = { | ||
number: { | ||
validate(value: unknown, propType: PropDef) { | ||
validate(value: unknown, propType: PropType) { | ||
return ( | ||
Number.isFinite(value) && | ||
typeof propType === 'object' && | ||
|
@@ -26,14 +26,16 @@ const TYPE_DEFINITIONS = { | |
} | ||
}, | ||
array: { | ||
validate(value: unknown, propType: PropDef) { | ||
validate(value: unknown, propType: PropType) { | ||
return Array.isArray(value) || ArrayBuffer.isView(value); | ||
} | ||
} | ||
}; | ||
|
||
export function parsePropTypes(propDefs: Record<string, PropDef>): Record<string, PropType> { | ||
const propTypes: Record<string, PropType> = {}; | ||
|
||
|
||
export function parsePropTypes(propDefs: Record<string, PropType>): Record<string, PropDef> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find the types and parameter naming confusing. It's not clear why you chose to swap the names of the types but kept the parameter names. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. Overhauled in same PR #1679 |
||
const propTypes: Record<string, PropDef> = {}; | ||
for (const [name, propDef] of Object.entries(propDefs)) { | ||
propTypes[name] = parsePropType(propDef); | ||
} | ||
|
@@ -45,7 +47,7 @@ export function parsePropTypes(propDefs: Record<string, PropDef>): Record<string | |
* - a valid prop type object ({type, ...}) | ||
* - or just a default value, in which case type and name inference is used | ||
*/ | ||
function parsePropType(propDef: PropDef): PropType { | ||
function parsePropType(propDef: PropType): PropDef { | ||
let type = getTypeOf(propDef); | ||
if (type !== 'object') { | ||
return {type, value: propDef, ...TYPE_DEFINITIONS[type]}; | ||
|
@@ -55,13 +57,15 @@ function parsePropType(propDef: PropDef): PropType { | |
return {type: 'object', value: null}; | ||
} | ||
if ('type' in propDef) { | ||
return {...propDef, ...TYPE_DEFINITIONS[propDef.type]}; | ||
// @ts-expect-error | ||
return {...propDef, ...TYPE_DEFINITIONS[propDef.type]}; | ||
} | ||
if (!('value' in propDef)) { | ||
// If no type and value this object is likely the value | ||
return {type: 'object', value: propDef}; | ||
} | ||
type = getTypeOf(propDef.value); | ||
// @ts-expect-error | ||
return {type, ...propDef, ...TYPE_DEFINITIONS[type]}; | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would be better called
VALIDATOR
or similarThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is a good idea, addressed in #1679