Skip to content

Support "with" expression - #1912

Open
iObject wants to merge 16 commits into
v1from
19-support-with-statement
Open

iObject wants to merge 16 commits into
v1from
19-support-with-statement

Conversation

@iObject

@iObject iObject commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #19

Adds a with <target> ... end with expression (BrighterScript only). Inside the body, .member refers to the target, and the body can contain any statement. It's modeled on VB's With...End With. Docs are in docs/with-expression.md.

with m.top.findNode("title")
    .text = "Hello"
    print .text
end with
__bsc_tmp_with0 = m.top.findNode("title")
__bsc_tmp_with0.text = "Hello"
print __bsc_tmp_with0.text
  • New AST nodes: WithExpression (target plus a Block body) and WithContextExpression (the implicit object of a leading .member). A with can stand alone (wrapped in an ExpressionStatement), or be the entire value of a local assignment (x = with ...) or a return.
  • Transpile flattens the block. The target is evaluated once into __bsc_tmp_with<n>, unless it's a plain variable that the body never reassigns. Then the variable is used directly.
  • .member works in any expression position, with type checking, hover, go-to-definition, semantic tokens and completions.
  • New diagnostics: with-member-outside-with-block, with-member-in-nested-function, goto-into-with-block, expected-expression-after-with.
  • In .brs files with/endwith stay ordinary identifiers. Only with <identifier> (never valid BrightScript) is parsed as a block, so it can report bs-feature-not-supported. In .bs files, with.x, with(1), with = 1 and similar forms still read with as a variable. Only the whitespace separates with (a) from with(a).

Risks:

  • WithExpression is an expression that holds statements. Statement-only walks still reach the body, because AssignmentStatement, ExpressionStatement and ReturnStatement walk a with value in statement mode. Plugins that assume ExpressionStatement.expression is a call will now also see a WithExpression.
  • Follow-ups: brighterscript-formatter indentation, the TextMate grammar in vscode-brightscript-language, and a check of bslint's code-flow rules against the new node.

Verified with npm run test:nocover and npm run lint.

🤖 Generated with Claude Code

access that starts with a `.` (`.name = "x"`, `.save()`, `print .name`)
refers to the target, so a series of operations on one object doesn't
have to repeat its name on every line.

The block itself produces no BrightScript. When the target is a plain
variable it is used directly (`with order` / `.id = 1` becomes
`order.id = 1`). Any other target (a property chain, a call, a literal,
a nested `.member`) is evaluated once into a local, `__bsc_tmp_with<n>`,
so the expression doesn't run again for every member. Nested blocks that
each need a local get distinct names; sibling blocks reuse the same one.

Parsing:
- `with` is not a reserved word in BrightScript, so existing code may use
  it as a variable or function. `with = 1`, `print with`, `with.foo`,
  `with(1)` and `with[0]` still parse as before. When `with` is followed
  by `.`, `(` or `[`, only the whitespace tells a block (`with .foo`,
  `with (a)`) from a variable (`with.foo`, `with(a)`), the same way `? [`
  and `?[` are already told apart.
- A leading `.` becomes a `WithContextExpression` wrapped in the usual
  `DottedGetExpression`, so type checking, hover, completions and the
  transpiler work on `.member` with no special casing. The node resolves
  its block by walking up to the innermost enclosing `WithStatement`,
  skipping the block's own target (`with .inner` refers to the outer
  block) and stopping at a function boundary.
- The parser tracks `with` depth per function. Outside a block a leading
  `.` is still a syntax error that discards the line, so error recovery
  for stray dots is unchanged; in `.bs` files it gets a specific message.

Validation:
- `.member` outside a `with` block, or inside a function defined within
  the block (BrightScript functions can't see the containing function's
  locals), is an error.
- `goto` into a `with` block from outside it is an error, since that
  would skip the step that evaluates the target.
- `with` in a `.brs` file is flagged like other BrighterScript-only
  features.

Also:
- `end with` / `endwith` tokens, `BlockTerminators`, visitor and
  reflection entries for the two new AST nodes.
- Completions after a leading `.` list the target's members.
- Comments on and around the block are preserved in the output.
- Docs: docs/with-statement.md, linked from the docs index and README.
@iObject iObject changed the title Adds a with <target> ... end with block. Inside the body, any member Adds with statement Oct 14, 2026
@iObject iObject changed the title Adds with statement Support "with" statement Oct 14, 2026
@iObject iObject linked an issue Oct 14, 2026 that may be closed by this pull request
@iObject
iObject requested a review from chrisdp October 15, 2026 11:32
@iObject

iObject commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator Author

@copilot resolve the merge conflicts in this pull request

# Conflicts:
#	src/bscPlugin/validation/BrsFileValidator.ts

Co-authored-by: iObject <2446955+iObject@users.noreply.github.com>
@iObject
iObject marked this pull request as ready for review October 16, 2026 12:34

Copilot AI commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

@copilot resolve the merge conflicts in this pull request

Resolved in b1ed137 by merging the latest v1 and retaining both validator imports. Build, lint, and full tests pass (5,302 passing, 9 pending). CodeQL found no alerts; a separate read-only review found no merge-related issues.

@iObject

iObject commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator Author

@copilot resolve the merge conflicts in this pull request

# Conflicts:
#	src/bscPlugin/validation/BrsFileValidator.ts

Co-authored-by: iObject <2446955+iObject@users.noreply.github.com>

Copilot AI commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

@copilot resolve the merge conflicts in this pull request

Resolved in 999289b by merging latest v1 and preserving both validator imports. Build, lint, and the full suite pass (5,307 passing, 9 pending). CodeQL found no alerts; a separate read-only review found no merge-related issues.

@iObject

iObject commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator Author

@copilot resolve the merge conflicts in this pull request

# Conflicts:
#	src/bscPlugin/validation/BrsFileValidator.ts

Co-authored-by: iObject <2446955+iObject@users.noreply.github.com>

Copilot AI commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

@copilot resolve the merge conflicts in this pull request

Resolved in fc727eb by merging latest v1 and retaining both branches’ validator imports. Build, lint, and full tests pass (5,401 passing, 9 pending). CodeQL found no alerts; a separate read-only review found no merge-related issues.

…with` body

Any other statement (print, plain function calls, local assignments, if, loops,
goto, labels) is flagged as an unexpected token on its first token.
A `with` block can now be the entire value of a local assignment
(`x = with target ... end with`) or a return (`return with target ... end with`),
as well as a standalone statement. Assignments run the body against the assigned
variable; returns run the body and then return the target.

Also use a temp local for a variable target that doesn't transpile to its own
name (such as an inlined const), which previously produced a reference to a
variable that doesn't exist at runtime.
A `with` block can now be the entire value of a local assignment
(`x = with target ... end with`) or a return (`return with target ... end with`),
as well as a standalone statement. Assignments run the body against the assigned
variable; returns run the body and then return the target.

Also use a temp local for a variable target that doesn't transpile to its own
name (such as an inlined const), which previously produced a reference to a
variable that doesn't exist at runtime.
…brighterscript into 19-support-with-statement
@iObject iObject changed the title Support "with" statement Support "with" expression Oct 16, 2026
Rename the doc and its README links, call the feature a with expression in the
docs and the .brs-file warning, and use a utils.getScene() namespace example
instead of promises.
Comment thread src/parser/Expression.ts Resolved
Remove isReturnValue (unused) and make isAssignmentValue, usesTempVariable and
getTempVariableName private. getContextName and getWithExpression stay public
since ReturnStatement, WithContextExpression and BrsFileValidator use them.
@TwitchBronBron
TwitchBronBron force-pushed the 19-support-with-statement branch from c793186 to abfa406 Compare November 13, 2026 18:49

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

support "with" expression

3 participants