Check for local name clash with existing variables in Insert flow (#6375)

Check for local name alias clashes with extisting var in Insert flow
Fixes #6230
This commit is contained in:
Pierre Jacquier
2025-04-21 06:35:43 -04:00
committed by GitHub
parent 790613e708
commit 0dee219e46
5 changed files with 62 additions and 13 deletions

View File

@ -220,6 +220,13 @@ export type CommandArgumentConfig<
machineContext?: C
) => OutputType)
defaultValueFromContext?: (context: C) => OutputType
validation?: ({
data,
context,
}: {
data: any
context: CommandBarContext
}) => Promise<boolean | string>
}
| {
inputType: 'text'
@ -343,6 +350,13 @@ export type CommandArgument<
commandBarContext: ContextFrom<typeof commandBarMachine>,
machineContext?: ContextFrom<T>
) => OutputType)
validation?: ({
data,
context,
}: {
data: any
context: CommandBarContext
}) => Promise<boolean | string>
}
| {
inputType: 'path'

View File

@ -211,6 +211,15 @@ export function buildCommandArgument<
defaultValue: arg.defaultValue,
...baseCommandArgument,
} satisfies CommandArgument<O, T> & { inputType: 'kcl' }
} else if (arg.inputType === 'string') {
return {
inputType: arg.inputType,
defaultValue: arg.defaultValueFromContext
? arg.defaultValueFromContext(context)
: arg.defaultValue,
validation: arg.validation,
...baseCommandArgument,
} satisfies CommandArgument<O, T> & { inputType: 'string' }
} else {
return {
inputType: arg.inputType,

View File

@ -133,6 +133,14 @@ export function kclCommands(commandProps: KclCommandConfig): Command[] {
const path = context.argumentsToSubmit['path'] as string
return getPathFilenameInVariableCase(path)
},
validation: async ({ data, context }) => {
const variableExists = kclManager.variables[data.localName]
if (variableExists) {
return 'This variable name is already in use.'
}
return true
},
},
},
onSubmit: (data) => {