fix(compiler): reject malformed structured props
This commit is contained in:
@@ -2414,7 +2414,7 @@ function generateComponent(ast: PageAst): string {
|
||||
);
|
||||
}
|
||||
decls.push(
|
||||
` const ${nameRefs.get(prop.name)}: ${prop.valueType ?? "any"} = __coerce(__p[${JSON.stringify(prop.name)}], (${resolveExpr(prop.default)}), ${JSON.stringify(runtimeTypeOf(prop.valueType))});`,
|
||||
` const ${nameRefs.get(prop.name)}: ${prop.valueType ?? "any"} = __coerce(__p[${JSON.stringify(prop.name)}], (${resolveExpr(prop.default)}), ${JSON.stringify(runtimeTypeOf(prop.valueType))}, ${JSON.stringify(prop.name)});`,
|
||||
);
|
||||
}
|
||||
if (!effectiveProps.some((prop) => prop.name === "attrs")) {
|
||||
@@ -2504,7 +2504,7 @@ function generateComponent(ast: PageAst): string {
|
||||
);
|
||||
}
|
||||
|
||||
out.push(`function __coerce(v: any, def: any, declared: string = "unknown"): any {
|
||||
out.push(`function __coerce(v: any, def: any, declared: string = "unknown", propName: string = "prop"): any {
|
||||
if (v === undefined || v === null) {
|
||||
return def;
|
||||
}
|
||||
@@ -2529,14 +2529,14 @@ function generateComponent(ast: PageAst): string {
|
||||
if (typeof v === "string") {
|
||||
try {
|
||||
const parsed = JSON.parse(v);
|
||||
return Array.isArray(parsed) ? parsed : def;
|
||||
if (!Array.isArray(parsed)) throw new TypeError("Expected an array prop '" + propName + "'");
|
||||
return parsed;
|
||||
} catch {
|
||||
if (declared === "array") throw new TypeError("Expected an array prop");
|
||||
return def;
|
||||
throw new TypeError("Expected an array prop '" + propName + "'");
|
||||
}
|
||||
}
|
||||
|
||||
return def;
|
||||
throw new TypeError("Expected an array prop '" + propName + "'");
|
||||
}
|
||||
|
||||
if (declared === "object" || (def !== null && typeof def === "object")) {
|
||||
@@ -2552,20 +2552,16 @@ function generateComponent(ast: PageAst): string {
|
||||
try {
|
||||
const parsed = JSON.parse(v);
|
||||
|
||||
return (
|
||||
parsed !== null &&
|
||||
typeof parsed === "object" &&
|
||||
!Array.isArray(parsed)
|
||||
)
|
||||
? parsed
|
||||
: def;
|
||||
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
||||
throw new TypeError("Expected an object prop '" + propName + "'");
|
||||
}
|
||||
return parsed;
|
||||
} catch {
|
||||
if (declared === "object") throw new TypeError("Expected an object prop");
|
||||
return def;
|
||||
throw new TypeError("Expected an object prop '" + propName + "'");
|
||||
}
|
||||
}
|
||||
|
||||
return def;
|
||||
throw new TypeError("Expected an object prop '" + propName + "'");
|
||||
}
|
||||
|
||||
if (declared === "bigint") return BigInt(v);
|
||||
|
||||
Reference in New Issue
Block a user