From 3e43e8f6c1cc90042f1dc1e2b6064427eb19edc6 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Wed, 6 Nov 2024 16:57:01 -0500 Subject: [PATCH] fix: check for {} type in inferFieldMetadata script Closes #3 --- biome.json | 3 +++ nodeFields.json | 17 +++++++++++++---- scripts/generateTypes.ts | 30 +++++++++--------------------- scripts/inferFieldMetadata.ts | 3 +++ src/lib/ast.ts | 5 +++-- 5 files changed, 31 insertions(+), 27 deletions(-) diff --git a/biome.json b/biome.json index 44616a2..f68d6e3 100644 --- a/biome.json +++ b/biome.json @@ -3,6 +3,9 @@ "extends": ["@radashi-org/biome-config"], "linter": { "rules": { + "complexity": { + "noBannedTypes": "off" + }, "performance": { "noDelete": "off" }, diff --git a/nodeFields.json b/nodeFields.json index 10e4e64..72cc2b0 100644 --- a/nodeFields.json +++ b/nodeFields.json @@ -97,6 +97,7 @@ "distinctClause": [ true, [ + "{}", "ColumnRef", "A_Const" ], @@ -313,7 +314,9 @@ "A_Const": { "ival": [ true, - null, + [ + "{}" + ], null ], "location": [ @@ -338,7 +341,9 @@ ], "boolval": [ true, - null, + [ + "{}" + ], null ], "bsval": [ @@ -668,6 +673,7 @@ [ "A_Const", "FuncCall", + "{}", "String", "SubLink", "ColumnRef", @@ -733,6 +739,7 @@ ], [ "FuncCall", + "{}", "List", "SQLValueFunction", "TypeCast", @@ -1606,7 +1613,8 @@ true, [ "List", - "Integer" + "Integer", + "{}" ], [ "FunctionParameter" @@ -1957,7 +1965,8 @@ "objargs": [ true, [ - "TypeName" + "TypeName", + "{}" ], null ], diff --git a/scripts/generateTypes.ts b/scripts/generateTypes.ts index 06ac216..df1213f 100644 --- a/scripts/generateTypes.ts +++ b/scripts/generateTypes.ts @@ -206,6 +206,12 @@ async function main() { } } + const renderTagType = (tag: string) => + tag === '{}' ? tag : `{ ${tag}: ${tag} }` + + const renderTagTypes = (tags: string[]) => + tags.sort().map(renderTagType).join(' | ') + delete structsByModule['../backend/parser/gram'] delete structsByModule['../backend/parser/gramparse'] delete structsByModule['commands/vacuum'] @@ -337,13 +343,7 @@ async function main() { fieldName, inferredTags, ) - fieldType = - '(' + - inferredTags - .sort() - .map(tag => `{ ${tag}: ${tag} }`) - .join(' | ') + - ')' + fieldType = '(' + renderTagTypes(inferredTags) + ')' } } else if (fieldType === 'any[]') { const inferredListTags = fieldMetadata?.[fieldName]?.[2] @@ -356,13 +356,7 @@ async function main() { inferredListTags, ) - fieldType = - 'List<' + - inferredListTags - .sort() - .map(tag => `{ ${tag}: ${tag} }`) - .join(' | ') + - '>' + fieldType = 'List<' + renderTagTypes(inferredListTags) + '>' if (field.c_type === 'List*') { fieldType += '[]' @@ -379,13 +373,7 @@ async function main() { inferredTags, ) - fieldType = - '(' + - inferredTags - .sort() - .map(tag => `{ ${tag}: ${tag} }`) - .join(' | ') + - ')[]' + fieldType = '(' + renderTagTypes(inferredTags) + ')[]' } } } diff --git a/scripts/inferFieldMetadata.ts b/scripts/inferFieldMetadata.ts index 9e1a47e..9bd7733 100644 --- a/scripts/inferFieldMetadata.ts +++ b/scripts/inferFieldMetadata.ts @@ -30,6 +30,9 @@ const toNodeTag = (value: unknown) => { if (keys.length === 1 && /^[A-Z]/.test(keys[0])) { return keys[0] } + if (keys.length === 0) { + return '{}' + } } } diff --git a/src/lib/ast.ts b/src/lib/ast.ts index 5909708..772c016 100644 --- a/src/lib/ast.ts +++ b/src/lib/ast.ts @@ -1884,6 +1884,7 @@ export type RangeFunction = { | { List: List } | { SQLValueFunction: SQLValueFunction } | { TypeCast: TypeCast } + | {} >[] /** table alias & optional column aliases */ alias?: Alias @@ -2865,7 +2866,7 @@ export type SelectStmt = { */ /** NULL, list of DISTINCT ON exprs, or * lcons(NIL,NIL) for all (SELECT DISTINCT) */ - distinctClause?: ({ A_Const: A_Const } | { ColumnRef: ColumnRef })[] + distinctClause?: ({ A_Const: A_Const } | { ColumnRef: ColumnRef } | {})[] /** target for SELECT INTO */ intoClause?: IntoClause /** the target list (of ResTarget) */ @@ -3203,7 +3204,7 @@ export type ObjectWithArgs = { /** qualified name of function/operator */ objname: QualifiedName /** list of Typename nodes (input args only) */ - objargs?: { TypeName: TypeName }[] + objargs?: ({ TypeName: TypeName } | {})[] /** list of FunctionParameter nodes */ objfuncargs?: { FunctionParameter: FunctionParameter }[] /** argument list was omitted? */