Add named argument support and duplicate parameter name warning#1669
Add named argument support and duplicate parameter name warning#1669
Conversation
- Named arguments: allows calling functions with named params (e.g. `greet(name: "Bob")`) with validation for unknown names, duplicates, and positional args following named args - Adds diagnostic codes 1146-1149 for named argument errors - Adds warning (1150) when a function definition has duplicate parameter names (case-insensitive) - Fixes Expression import in Scope.ts to pull Expression from parser/AstNode where it is declared Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
TwitchBronBron
left a comment
There was a problem hiding this comment.
We need to change a few things about this:
- named arguments should be evaluted in the order they are written in the text. So that will require some hoisting to local
__bs*vars. only need to hoist up until the last call expression. (and can likely skip hoisting VariableExpression and literals (maybe even AA and Array literals as long as they're only composed of literals or VariableExpressions themselves) - sequence the local hoisted vars on a per-function basis. (maybe use a WeakMap to use the functionExpression as the key and the sequence as the value)?
- move the code into ScopeValidator.ts instead of living in Scope.ts
- the transpiler should be the one responsible for reordering the args, and only on the way out....we don't want to manipulate the AST for this if we can help it, because that makes things complicated for other plugins earlier and later in the flow.
- instead of modifying the transpiler directly, we should do similar to the Ternary transpilation. We modify the CallExpression arguments array during the
beforeFileTranspile(see BrsFilePreTranspileProcessor)
| * When named arguments are used and successfully resolved during validation, this holds the | ||
| * positional-ordered argument list used for transpilation. If undefined, `args` is used directly. | ||
| */ | ||
| public resolvedArgs?: Expression[]; |
There was a problem hiding this comment.
Remove this, we don't want to be caching this type of information.
There was a problem hiding this comment.
Addressed in de7a040. Removed the prior named-arg transpile complexity and any call-expression-level cached mapping behavior; named-arg handling now validates order in ScopeValidator and rewrites directly in pre-transpile.
| * Validate calls that use named arguments, and resolve them to positional order for transpilation. | ||
| * Only handles simple function calls (VariableExpression callee); method calls are not supported. | ||
| */ | ||
| private validateNamedArgCalls(file: BscFile, callableContainerMap: CallableContainerMap) { |
There was a problem hiding this comment.
move this into ScopeValidator.ts
There was a problem hiding this comment.
Done in de7a040. Named-argument validation is handled in ScopeValidator.ts, and the Scope-level function-call count path now defers named-arg cases to that validator.
…hase - Move named arg validation from Scope.validateNamedArgCalls() into ScopeValidator.walkFiles() — pure diagnostics, no AST mutation - Remove CallExpression.resolvedArgs; transpile now reads this.args directly - Add processNamedArgumentCalls() in BrsFilePreTranspileProcessor: - Inline reorder for calls with only literal/variable args - Hoist complex args to __bsArgsN temp vars before the containing statement - IIFE fallback when hoisting is unsafe (else-if conditions, ternary branches) - Preserve written evaluation order across nesting levels via reversed callExpressions iteration and reversible parent-pointer updates on hoisted nodes - Add tests covering if/for/else-if, nested calls, ternary combinations, and outer-variable-as-IIFE-arg (no-closure correctness for BrightScript) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ss-scope conflict detection - Named args now work on namespace function calls (e.g. MyNs.myFunc(a: 1)) - Named args now work on constructor calls (e.g. new MyClass(a: 1)), including inherited constructors resolved by walking the ancestor chain - New diagnostic bs1151 fires when a shared file's named arg call targets a function whose parameter signature differs across component scopes, since the transpiler can only produce one output and would silently choose wrong ordering - Extracted rewriteNamedArgCall helper to share hoisting logic between regular, namespace, and constructor call sites - Added validation and transpilation tests for all three new cases Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This comment was marked as resolved.
This comment was marked as resolved.
Populates relatedInformation with links to both conflicting function definitions so users can navigate directly to each scope's version. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
@luis-j-soares doesn't need to be. Just means the amount of places that it can be used in isn't quite as many as it would be in v1. But since this is just a call site feature it can be expanded on in v1 to support more locations. |
|
@copilot i want to update this pr to remove the param reordering for now. Enforce the order of params If someone were to do something like this example we would transpile and populated the missing optional details for them. So greet would become: |
… rewrite Agent-Logs-Url: https://github.com/rokucommunity/brighterscript/sessions/2c9181b2-543a-493f-897d-41cba617f2ce Co-authored-by: chrisdp <9591618+chrisdp@users.noreply.github.com>
Implemented in de7a040. Named arguments now enforce declaration order, and transpilation no longer does hoisting/IIFE reordering. Calls like
Updated in de7a040 based on the latest direction to remove param reordering for now: named args are enforced in declaration order, validation lives in |
|
@copilot lets also have code actions available to the user to to fix the order. |
Agent-Logs-Url: https://github.com/rokucommunity/brighterscript/sessions/6cb3b397-2449-4e05-9ff7-260c037d93cc Co-authored-by: chrisdp <9591618+chrisdp@users.noreply.github.com>
Agent-Logs-Url: https://github.com/rokucommunity/brighterscript/sessions/6cb3b397-2449-4e05-9ff7-260c037d93cc Co-authored-by: chrisdp <9591618+chrisdp@users.noreply.github.com>
Agent-Logs-Url: https://github.com/rokucommunity/brighterscript/sessions/6cb3b397-2449-4e05-9ff7-260c037d93cc Co-authored-by: chrisdp <9591618+chrisdp@users.noreply.github.com>
Agent-Logs-Url: https://github.com/rokucommunity/brighterscript/sessions/6cb3b397-2449-4e05-9ff7-260c037d93cc Co-authored-by: chrisdp <9591618+chrisdp@users.noreply.github.com>
Implemented in f04129a (with core functionality in 1604dce): added a quick-fix code action, |
Summary
greet(name: "Bob"))function/subdefinition has two or more parameters with the same name (case-insensitive)Expressionimport inScope.tsto pull fromparser/AstNodewhere it is declaredNew diagnostic codes
'{name}'for function'{func}''{name}'was already provided'{func}'because the function definition cannot be found'{name}'Test plan
src/parser/tests/expression/NamedArgument.spec.tssrc/files/BrsFile.spec.ts🤖 Generated with Claude Code