More quoting nonsense

This commit is contained in:
Dustin Swan 2026-02-23 22:22:13 -07:00
parent d79166a5bc
commit 515ad7fc9c
No known key found for this signature in database
GPG key ID: 30D46587E2100467
5 changed files with 593 additions and 9 deletions

View file

@ -220,7 +220,7 @@ export function prettyPrint(ast: AST, indent = 0): string {
const parts = ast.entries.map(entry =>
entry.kind === 'spread'
? `...${prettyPrint(entry.expr, indent + 1)}`
: `${needsQuotes(entry.key) ? `"${entry.key}"` : entry.key} = ${prettyPrint(entry.value, indent + 1)}`
: `${needsQuotes(entry.key) ? JSON.stringify(entry.key) : entry.key} = ${prettyPrint(entry.value, indent + 1)}`
);
if (parts.length <= 1) return `{ ${parts.join(', ')} }`;
const inner = parts.map(p => `${' '.repeat(indent + 1)}${p}`).join(',\n');
@ -238,12 +238,12 @@ export function prettyPrint(ast: AST, indent = 0): string {
}
case 'record-access':
const field = needsQuotes(ast.field) ? `"${ast.field}"` : ast.field;
const field = needsQuotes(ast.field) ? `${JSON.stringify(ast.field)}` : ast.field;
return `${prettyPrint(ast.record, indent)}.${field}`;
case 'record-update': {
const updates = Object.entries(ast.updates)
.map(([k, v]) => `${needsQuotes(k) ? `"${k}"` : k} = ${prettyPrint(v, indent)}`)
.map(([k, v]) => `${needsQuotes(k) ? `${JSON.stringify(k)}` : k} = ${prettyPrint(v, indent)}`)
.join(', ');
return `${prettyPrint(ast.record, indent)}.{ ${updates} }`
}
@ -299,7 +299,7 @@ function prettyPrintPattern(pattern: Pattern): string {
case 'record':
const fields = Object.entries(pattern.fields)
.map(([k, p]) => `${needsQuotes(k) ? `"${k}"` : k} = ${prettyPrintPattern(p)}`)
.map(([k, p]) => `${needsQuotes(k) ? `${JSON.stringify(k)}` : k} = ${prettyPrintPattern(p)}`)
.join(', ');
return `{${fields}}`;
@ -309,5 +309,5 @@ function prettyPrintPattern(pattern: Pattern): string {
}
function needsQuotes(key: string): boolean {
return !/^[a-z_][a-zA-Z0-9_]*$/.test(key);
return !/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(key);
}