fix(web): parse destructured and multi-line main signatures in Code node (#44046)

Co-authored-by: Stream <Stream_2@qq.com>
This commit is contained in:
YueliupandStream authored and GitHub committed 2026-10-19 05:33:55 +00:00
1 parent 3adc339814
commit 9aebf35e94
2 files changed
+132 -5

No files matched your search

@@ -5,16 +5,35 @@ import { CodeLanguage } from './types'
export const extractFunctionParams = (code: string, language: CodeLanguage) => {
if (language === CodeLanguage.json) return []
const patterns: Record<Exclude<CodeLanguage, typeof CodeLanguage.json>, RegExp> = {
[CodeLanguage.python3]: /def\s+main\s*\((.*?)\)/,
[CodeLanguage.javascript]: /function\s+main\s*\((.*?)\)/,
// Match strings first so comment markers inside them cannot consume real code.
// This is lightweight filtering, not a full lexer: quotes in JavaScript regex
// literals or nested template expressions can still confuse string boundaries.
const commentPatterns = {
[CodeLanguage.python3]:
/"""(?:\\[\s\S]|[^\\])*?"""|'''(?:\\[\s\S]|[^\\])*?'''|"(?:\\[\s\S]|[^"\\])*"|'(?:\\[\s\S]|[^'\\])*'|(#[^\r\n]*)/g,
[CodeLanguage.javascript]:
/"(?:\\[\s\S]|[^"\\])*"|'(?:\\[\s\S]|[^'\\])*'|`(?:\\[\s\S]|[^`\\])*`|(\/\/[^\r\n]*|\/\*[\s\S]*?\*\/)/g,
}
const match = patterns[language].exec(code)
const codeWithoutComments = code.replace(
commentPatterns[language],
(match: string, comment: string | undefined) =>
comment ? comment.replace(/[^\r\n]/g, ' ') : match,
)
const patterns: Record<Exclude<CodeLanguage, typeof CodeLanguage.json>, RegExp> = {
[CodeLanguage.python3]: /def\s+main\s*\(([\s\S]*?)\)/,
[CodeLanguage.javascript]: /function\s+main\s*\(([\s\S]*?)\)/,
}
const match = patterns[language].exec(codeWithoutComments)
const params: string[] = []
if (match?.[1]) {
// The JavaScript `main` is invoked as `main(inputs_obj)`, so its parameters are
// usually written as a single destructuring pattern: `function main({ arg1, arg2 })`.
const rawParams = match[1].trim().replace(/^\{([\s\S]*)\}$/, '$1')
params.push(
...match[1]
...rawParams
.split(',')
.map((p) => p.trim())
.filter(Boolean)