Skip to content

stdlib: restore removed conversion fns as @deprecated - #137

Merged
vito merged 6 commits into
mainfrom
restore-deprecated-tojson
Jun 17, 2026
Merged

vito merged 6 commits into
mainfrom
restore-deprecated-tojson

Conversation

@vito

@vito vito commented Jun 17, 2026 •

Copy link
Copy Markdown
Owner

What

Brings back the top-level toJSON, fromJSON, and fromYAML functions that were removed when JSON/YAML/TOML encode/decode moved into format namespaces (#105), this time as @deprecated aliases. Calling one still works but prints a warning pointing at the call site and naming the namespaced replacement — and the LSP now flags and fixes these usages in-editor.

How

@deprecated handling for builtins

A .Deprecated(reason) builder on the function/method/static-method DSLs records the reason on BuiltinDef and the resulting BuiltinFunction. When a FunCall resolves to a deprecated builtin, eval prints a warning to the context's stderr using the existing source-highlighting renderer (factored out as formatSourceAnnotation):

Warning: toJSON is deprecated: use JSON.encode instead
  --> main.dang:1:9
  1 | let a = toJSON([1, 2, 3])
              ^^^^^^^^^^^^^^^^^

Warnings are deduped per call site within a run (a set on EvalContext), so a deprecated call inside a loop warns once. Eval is single-threaded, so no lock is needed.

Restored functions

toJSON/fromJSON/fromYAML are thin aliases that delegate to the same implementation as JSON.encode/JSON.decode/YAML.decode, so they honor codec field directives identically. There was never a top-level toYAML/toTOML/fromTOML, so none is restored.

LSP integration

  • A .Replacement(name) builder adds a structured successor (e.g. JSON.encode) alongside the prose reason, so tools can rewrite mechanically instead of parsing the message.
  • The LSP walks the inferred AST for calls whose callee names a deprecated builtin and publishes a warning diagnostic over the callee token, tagged Deprecated so editors render it struck-through.
  • A new textDocument/codeAction handler (capability now advertised) offers a quick fix that replaces just the callee — toJSON(x) → JSON.encode(x) — leaving the argument list untouched.

Matching is by callee name (the eval-time warning is the source of truth); a local binding shadowing one of these builtins is vanishingly rare.

Testing

  • Go tests: warning content + call-site location, per-call-site dedup, LSP diagnostic (severity/tag/range) and code-action (title/kind/edit/range-overlap) behavior.
  • builtins_test.go asserts the three are present and deprecated.
  • tests/test_to_json_deprecated.dang round-trips all three against their successors.
  • Full dagger check green (lint, go tests, docs-go tests).

vito added 2 commits June 17, 2026 13:54
Builtins can now be marked deprecated via a .Deprecated(reason) builder on
the function, method, and static-method DSLs, which records the reason on
BuiltinDef and the resulting BuiltinFunction value. When a FunCall resolves
to a deprecated builtin, eval prints a warning to the context's stderr that
names the reason and points at the call site, reusing the source-highlighting
renderer (factored out as formatSourceAnnotation) for a "--> file:line:col"
annotation with an underline. Warnings are deduped per call site within a run
via a set on EvalContext, so a deprecated call inside a loop warns once; eval
is single-threaded, so no lock is needed.

This is the Go-side equivalent of a @deprecated directive; a follow-up uses it
to restore the removed top-level conversion functions.

Signed-off-by: Alex Suraci <[email protected]>
These top-level conversion functions were dropped when JSON/YAML/TOML encode
and decode moved into format namespaces (issue #105). Bring them back as thin
aliases marked @deprecated so existing scripts keep working while nudging
callers toward JSON.encode, JSON.decode, and YAML.decode. Each delegates to the
same implementation as its namespaced successor, so they honor codec field
directives identically. There was never a top-level toYAML/toTOML/fromTOML, so
none is restored.

Signed-off-by: Alex Suraci <[email protected]>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 17, 2026 •

Copy link
Copy Markdown

Deploying dang with  Cloudflare Pages  Cloudflare Pages

Latest commit: 23f5422
Status:⚡️  Build in progress...

View logs

@vito vito mentioned this pull request Jun 17, 2026
vito added 2 commits June 17, 2026 14:05
The Deprecated reason is prose ("use JSON.encode instead"), which tools can't
rewrite against reliably. Add a Replacement field plus a .Replacement(name)
builder naming the superseding callee, and set it on toJSON/fromJSON/fromYAML.
This lets the LSP offer a mechanical quick fix without parsing the message.

Signed-off-by: Alex Suraci <[email protected]>
Walk the inferred AST for calls whose callee names a deprecated builtin and
publish a warning diagnostic over the callee token, tagged Deprecated so editors
strike it through. Advertise the codeAction capability and add a handler that
returns a quickfix replacing the callee with its structured Replacement (e.g.
toJSON -> JSON.encode), leaving the argument list untouched. Matching is by name,
which the eval-time warning backstops; a local binding shadowing one of these
builtins is vanishingly rare. Adds DiagnosticTag/severity wire types the local
protocol structs were missing.

Signed-off-by: Alex Suraci <[email protected]>
vito added 2 commits June 17, 2026 14:15
A deprecated builtin's card now carries a right-aligned "@deprecated" badge in
its header, linking to the superseding entry's card (e.g. toJSON -> JSON.encode)
via its anchor tag, which resolves across the page's module sections. The badge
and link are driven by the builtin's Deprecated and Replacement metadata, so the
reference stays generated rather than hand-maintained.

Signed-off-by: Alex Suraci <[email protected]>
Use a plain "=>" instead of the unicode arrow, drop the badge's vertical and
right padding so it hugs the header bar, and give the linked replacement code a
little left padding so it isn't flush against the arrow.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito merged commit 8edd3e5 into main Jun 17, 2026
1 check was pending
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.

2 participants