fix: compile typed catches and reject event loop syntax
This commit is contained in:
@@ -246,6 +246,18 @@ function astDiagnostics(ast: PageAst, options: DiagnoseOptions): WrnDiagnostic[]
|
||||
const tag = node.tag.toLowerCase();
|
||||
|
||||
for (const attribute of node.attrs) {
|
||||
if (attribute.event && (attribute.name === "for" || attribute.name === "key")) {
|
||||
diagnostics.push({
|
||||
code: "WRN-TEMPLATE-LOOP-DIRECTIVE",
|
||||
severity: "error",
|
||||
message: `@${attribute.name} is an event binding, not a loop directive.`,
|
||||
hint:
|
||||
attribute.name === "for"
|
||||
? 'Use data-for="item in items".'
|
||||
: 'Use data-key="item.id" alongside data-for.',
|
||||
file: options.file,
|
||||
});
|
||||
}
|
||||
if (attribute.event || attribute.boolean || !urlAttributes.has(attribute.name.toLowerCase()))
|
||||
continue;
|
||||
if (attribute.value.includes("{")) continue;
|
||||
|
||||
@@ -63,16 +63,18 @@ export function validateTypedInitializer(
|
||||
* Server output retains the original typed source.
|
||||
*/
|
||||
export function eraseFunctionTypes(source: string): string {
|
||||
return source.replace(
|
||||
/(\b(?:async\s+)?function\s+[A-Za-z_$][\w$]*\s*\()([^)]*)(\)\s*)(?::\s*([^{}=>]+)\s*)?(\{)/g,
|
||||
(_whole, open: string, params: string, close: string, _returnType: string, brace: string) => {
|
||||
const plainParams = params
|
||||
.split(",")
|
||||
.map((param) =>
|
||||
param.replace(/([A-Za-z_$][\w$]*)(\?)?\s*:\s*([^=]+?)(?=\s*=|$)/, "$1").trim(),
|
||||
)
|
||||
.join(", ");
|
||||
return `${open}${plainParams}${close}${brace}`;
|
||||
},
|
||||
);
|
||||
return source
|
||||
.replace(/\bcatch\s*\(\s*([A-Za-z_$][\w$]*)\s*:\s*(?:any|unknown)\s*\)/g, "catch ($1)")
|
||||
.replace(
|
||||
/(\b(?:async\s+)?function\s+[A-Za-z_$][\w$]*\s*\()([^)]*)(\)\s*)(?::\s*([^{}=>]+)\s*)?(\{)/g,
|
||||
(_whole, open: string, params: string, close: string, _returnType: string, brace: string) => {
|
||||
const plainParams = params
|
||||
.split(",")
|
||||
.map((param) =>
|
||||
param.replace(/([A-Za-z_$][\w$]*)(\?)?\s*:\s*([^=]+?)(?=\s*=|$)/, "$1").trim(),
|
||||
)
|
||||
.join(", ");
|
||||
return `${open}${plainParams}${close}${brace}`;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user