This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/texera.git
commit 4e144048dab8de241eaf6ce63f1a2d61b77c558b Author: carloea2 <[email protected]> AuthorDate: Tue Nov 10 22:43:41 2026 +0000 fix(frontend): avoid R UI parameter binding collisions (#9314) ### What changes were proposed in this PR? Reserve script-level R assignments and `for` iterators when generating a UI parameter variable. Braces do not create an R scope, and loops inside calls also bind their iterator in the script environment. These bindings now reserve their names regardless of nesting, so insertion chooses `count_2` or the next available suffix. Skip function bodies and keep named call arguments separate. Only `=` retains the argument-depth check; `<-`, `<<-`, `->`, `->>` and loop iterators are recognized inside blocks and calls. Original R SDK reproduction: | Value | Before | After | | --- | --- | --- | | Supplied UI parameter | 42 | 42 | | Generated parameter variable after the existing loop | 3 | 42 | | Existing loop iterator | 3 | 3 | ### Any related issues, documentation, discussions? Closes #9313. Follow-up to #9279. The companion runtime PR Texera/texera-rudf#2 has merged. ### How was this PR tested? Retested on freshly fetched upstream main `1ec692c5da`. The ten added binding regressions fail before the review correction and pass afterward. All 67 R parser tests pass, including controls for function-local bindings and named call arguments. From `frontend`: ```text node --max-old-space-size=4096 node_modules/@angular/cli/bin/ng.js test --watch=false --progress=false --include=src/app/workspace/service/code-editor/r-ui-udf-parameters-parser.spec.ts 67 passed node --max-old-space-size=4096 node_modules/@angular/cli/bin/ng.js test --watch=false --progress=false --coverage --coverage-reporters=lcovonly 7200 passed, 1 skipped across 253 files corepack yarn format:fix corepack yarn format:ci ``` Repository formatting, ESLint and `git diff --check` passed. The earlier native R 4.5.3 SDK reproduction of the original loop collision remains unchanged; this follow-up verified the new binding cases through the production frontend parser. ### Was this PR authored or co-authored using generative AI tooling? Generated-by: Codex (GPT-6) --- .../code-editor/r-ui-udf-parameters-parser.spec.ts | 57 ++++++++++++-- .../code-editor/r-ui-udf-parameters-parser.ts | 87 +++++++++++++++++++--- 2 files changed, 127 insertions(+), 17 deletions(-) diff --git a/frontend/src/app/workspace/service/code-editor/r-ui-udf-parameters-parser.spec.ts b/frontend/src/app/workspace/service/code-editor/r-ui-udf-parameters-parser.spec.ts index 451b0f647b..a6d4fcaa8f 100644 --- a/frontend/src/app/workspace/service/code-editor/r-ui-udf-parameters-parser.spec.ts +++ b/frontend/src/app/workspace/service/code-editor/r-ui-udf-parameters-parser.spec.ts @@ -144,13 +144,56 @@ UiParameter("active", AttributeType$INT)`) ]); }); - it.each(["function() count", "helper(count = 1); function() count", "function() { count <- count + 1; count }"])( - "keeps a referenced name available without a top-level binding: %s", - code => { - const edit = parser.computeParameterInsertion(code, "count", "integer", "r"); - expect(edit.text).toContain('count <- UiParameter(name="count",'); - } - ); + it.each([ + ["for (count in 1:3) NULL\nfunction() count", "count_2"], + ["for (`count` in seq_len(3)) { print(count) }\nfunction() count", "count_2"], + ["count_2 <- 0; for (count in 1:3) NULL\nfunction() count", "count_3"], + ["for (count in integer()) NULL\nfunction() count", "count_2"], + ["helper <- function() 1\nfor (count in 1:3) NULL\nfunction() count", "count_2"], + ["if (TRUE) { for (count in 1:3) NULL }\nfunction() count", "count_2"], + ])("does not reuse a top-level loop binding in %s", (code, identifier) => { + const edit = parser.computeParameterInsertion(code, "count", "integer", "r"); + expect(edit.text).toContain(`${identifier} <- UiParameter(name="count",`); + }); + + it.each([ + "function() count", + "helper(count = 1); function() count", + "function() { count <- count + 1; count }", + "helper <- function() { for (count in 1:3) NULL }; function() count", + "helper <- function() for (count in seq_len(count)) NULL; function() count", + "helper <- function() if (TRUE) NULL else for (count in seq_len(count)) NULL; function() count", + "helper <- \\() for (count in seq_len(count)) NULL; function() count", + "helper <- function() (0) + for (i in 1:3)\nfor (count in seq_len(count)) NULL; function() count", + ])("keeps a referenced name available without a top-level binding: %s", code => { + const edit = parser.computeParameterInsertion(code, "count", "integer", "r"); + expect(edit.text).toContain('count <- UiParameter(name="count",'); + }); + + it.each([ + "if (TRUE) { count <- 3 }", + "for (i in 1:3) { count <- i }", + "if (TRUE) { count <<- 3 }", + "if (TRUE) { count = 3 }", + "if (TRUE) { 3 -> count }", + "if (TRUE) { 3 ->> count }", + "suppressWarnings(count <- 3)", + "suppressWarnings(3 -> count)", + "tryCatch({ for (count in 1:3) NULL }, error = function(e) NULL)", + "suppressWarnings(for (count in 1:3) NULL)", + ])("reserves a script binding inside blocks or calls: %s", code => { + const edit = parser.computeParameterInsertion(`${code}\nfunction() count`, "count", "integer", "r"); + expect(edit.text).toContain('count_2 <- UiParameter(name="count",'); + }); + + it.each([ + "if (TRUE) { helper(count = 3) }", + "tryCatch(stop('failure'), count = 3, error = function(count) count)", + "helper <- function() { count <- 3; for (count in 1:3) NULL }", + ])("keeps call arguments and function bindings out of the script scope: %s", code => { + const edit = parser.computeParameterInsertion(`${code}\nfunction() count`, "count", "integer", "r"); + expect(edit.text).toContain('count <- UiParameter(name="count",'); + }); it("rejects duplicate, empty, and unsupported additions before editing", () => { expect(() => parser.computeParameterInsertion("", " ", "string", "r")).toThrow("name is required"); diff --git a/frontend/src/app/workspace/service/code-editor/r-ui-udf-parameters-parser.ts b/frontend/src/app/workspace/service/code-editor/r-ui-udf-parameters-parser.ts index ecdb9c4587..49f1ca2440 100644 --- a/frontend/src/app/workspace/service/code-editor/r-ui-udf-parameters-parser.ts +++ b/frontend/src/app/workspace/service/code-editor/r-ui-udf-parameters-parser.ts @@ -24,7 +24,7 @@ import { createUiParameter, } from "./ui-udf-parameter"; -type Token = Readonly<{ text: string; kind: "name" | "string" | "symbol" }>; +type Token = Readonly<{ text: string; kind: "name" | "keyword" | "string" | "symbol"; lineBreakBefore: boolean }>; const R_KEYWORDS = new Set( "if else repeat while function for in next break TRUE FALSE NULL Inf NaN NA NA_integer_ NA_real_ NA_complex_ NA_character_".split( " " @@ -65,6 +65,11 @@ function decodeRString(text: string): string { /** Tokenize only the R syntax needed to recognize declarations, never evaluating user code. */ function tokenize(code: string): Token[] { const tokens: Token[] = []; + let previousEnd = 0; + const push = (text: string, kind: Token["kind"], start: number, end: number) => { + tokens.push({ text, kind, lineBreakBefore: code.slice(previousEnd, start).includes("\n") }); + previousEnd = end; + }; for (let index = 0; index < code.length; ) { const rest = code.slice(index); if (/\s/.test(code[index])) { @@ -82,7 +87,7 @@ function tokenize(code: string): Token[] { const close = ({ "(": ")", "[": "]", "{": "}" } as Record<string, string>)[raw[3]] + raw[2] + raw[1]; const end = code.indexOf(close, index + raw[0].length); if (end < 0) break; - tokens.push({ text: code.slice(index + raw[0].length, end), kind: "string" }); + push(code.slice(index + raw[0].length, end), "string", index, end + close.length); index = end + close.length; continue; } @@ -90,23 +95,69 @@ function tokenize(code: string): Token[] { if (quote === '"' || quote === "'" || quote === "`") { const literal = /^(["'`])((?:\\[\s\S]|(?!\1)[^\\])*)\1/.exec(rest); if (!literal) break; - tokens.push({ text: decodeRString(literal[2]), kind: quote === "`" ? "name" : "string" }); + push(decodeRString(literal[2]), quote === "`" ? "name" : "string", index, index + literal[0].length); index += literal[0].length; continue; } const name = /^[\p{L}_.][\p{L}\p{N}_.]*/u.exec(rest); if (name) { - tokens.push({ text: name[0], kind: "name" }); + push(name[0], R_KEYWORDS.has(name[0]) ? "keyword" : "name", index, index + name[0].length); index += name[0].length; continue; } const symbol = /^(:::|::|<<-|<-|->>|->|.)/s.exec(rest)![0]; - tokens.push({ text: symbol, kind: "symbol" }); + push(symbol, "symbol", index, index + symbol.length); index += symbol.length; } return tokens; } +/** Find the end of a function body without treating its local bindings as script bindings. */ +function endOfExpression(tokens: Token[], start: number, groupOnly = false): number { + const first = tokens[start]; + if (!first) return start - 1; + if (groupOnly && first.kind === "symbol" && ["(", "[", "{"].includes(first.text)) { + let depth = 0; + for (let end = start; end < tokens.length; end++) { + if (tokens[end].kind !== "symbol") continue; + if (["(", "[", "{"].includes(tokens[end].text)) depth++; + else if ([")", "]", "}"].includes(tokens[end].text) && --depth === 0) return end; + } + return tokens.length - 1; + } + if ( + (first.kind === "keyword" && ["function", "if", "for", "while"].includes(first.text)) || + (first.kind === "symbol" && first.text === "\\") + ) { + const headerEnd = endOfExpression(tokens, start + 1, true); + const bodyEnd = endOfExpression(tokens, headerEnd + 1); + return first.text === "if" && tokens[bodyEnd + 1]?.kind === "keyword" && tokens[bodyEnd + 1].text === "else" + ? endOfExpression(tokens, bodyEnd + 2) + : bodyEnd; + } + if (first.kind === "keyword" && first.text === "repeat") return endOfExpression(tokens, start + 1); + for (let end = start; end < tokens.length; end++) { + if (tokens[end].kind === "symbol" && ["(", "[", "{"].includes(tokens[end].text)) + end = endOfExpression(tokens, end, true); + else if ( + (tokens[end].kind === "keyword" && ["function", "if", "for", "while", "repeat"].includes(tokens[end].text)) || + (tokens[end].kind === "symbol" && tokens[end].text === "\\") + ) + end = endOfExpression(tokens, end); + const current = tokens[end]; + const next = tokens[end + 1]; + if ( + !next || + (next.kind === "symbol" && [";", ")", "]", "}"].includes(next.text)) || + (next.kind === "keyword" && next.text === "else") + ) + return end; + const canEnd = current.kind !== "symbol" || [")", "]", "}"].includes(current.text) || /^\d$/.test(current.text); + if (next.lineBreakBefore && canEnd) return end; + } + return tokens.length - 1; +} + function member(tokens: Token[], receiver: string): string | undefined { return tokens.length === 3 && tokens[0].kind === "name" && @@ -197,17 +248,33 @@ export function computeRParameterInsertion( identifier = `parameter_${identifier}`; const tokens = tokenize(code); const names = new Set<string>(); - let depth = 0; + let argumentDepth = 0; for (let index = 0; index < tokens.length; index++) { const token = tokens[index]; + if ( + ((token.kind === "keyword" && token.text === "function") || (token.kind === "symbol" && token.text === "\\")) && + tokens[index + 1]?.text === "(" + ) { + index = endOfExpression(tokens, index); + continue; + } if (token.kind === "symbol") { - if (["(", "[", "{"].includes(token.text)) depth++; - else if ([")", "]", "}"].includes(token.text)) depth--; + if (["(", "["].includes(token.text)) argumentDepth++; + else if ([")", "]"].includes(token.text)) argumentDepth--; } if ( - depth === 0 && + token.kind === "keyword" && + token.text === "for" && + tokens[index + 1]?.text === "(" && + tokens[index + 2]?.kind === "name" && + tokens[index + 3]?.text === "in" + ) + names.add(tokens[index + 2].text); + if ( token.kind === "name" && - (["<-", "<<-", "="].includes(tokens[index + 1]?.text) || ["->", "->>"].includes(tokens[index - 1]?.text)) + (["<-", "<<-"].includes(tokens[index + 1]?.text) || + ["->", "->>"].includes(tokens[index - 1]?.text) || + (argumentDepth === 0 && tokens[index + 1]?.text === "=")) ) names.add(token.text); }
