Conversation
harehare
commented
Mar 6, 2026
- Add mq-check dependency to mq-wasm
- Extend diagnostics API to support type checking
- Implement inlayHints API for type annotation hints
- Update TypeScript interface for inlayHints
…ith type checking support - Add mq-check dependency to mq-wasm - Extend diagnostics API to support type checking - Implement inlayHints API for type annotation hints - Update TypeScript interface for inlayHints Closes #issue (if relevant)
There was a problem hiding this comment.
Pull request overview
Adds type-checking capabilities to the mq-wasm WebAssembly bindings by wiring in mq-check, extending the diagnostics API, and exposing a new inlayHints API for inferred type annotations.
Changes:
- Add
mq-checkas a dependency ofmq-wasm. - Extend
diagnostics(code)to optionally include type-checking errors viaenableTypeCheck. - Add
inlayHints(code)API (plus TS types) to return inferred type annotation hints.
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| crates/mq-wasm/src/script.rs | Adds TS typings, optional type-checking diagnostics, and a new inlayHints wasm export backed by mq-check. |
| crates/mq-wasm/Cargo.toml | Adds mq-check workspace dependency for wasm bindings. |
| Cargo.lock | Locks the new mq-check dependency for mq-wasm. |
crates/mq-wasm/src/script.rs
Outdated
| serde_wasm_bindgen::to_value(&hints).unwrap_or(JsValue::NULL) | ||
| } |
There was a problem hiding this comment.
inlay_hints returns JsValue::NULL if serialization fails, but the TypeScript signature promises an array. Returning null will force consumers to add null checks and may cause runtime errors. Prefer returning an empty array on failure or propagating a JS error (consistent with other exports that unwrap() or return Result).
Co-authored-by: Copilot <[email protected]>
| pub async fn inlay_hints(code: &str) -> JsValue { | ||
| let mut hir = mq_hir::Hir::default(); | ||
| hir.add_code(None, code); | ||
|
|
||
| let mut checker = mq_check::TypeChecker::default(); | ||
| let _ = checker.check(&hir); | ||
|
|
There was a problem hiding this comment.
inlay_hints always runs HIR construction + type checking even when the source has parse errors. Since Hir::add_code uses recovery and drops parse errors, this can produce inlay hints for syntactically-invalid code (and do extra work) while diagnostics explicitly skips type checking unless there are no parse errors. Consider mirroring diagnostics behavior by checking mq_lang::parse_recovery first and returning an empty hint list when parse errors exist.
| serde_wasm_bindgen::to_value(&errors).unwrap() | ||
| } |
There was a problem hiding this comment.
diagnostics() ends with serde_wasm_bindgen::to_value(&errors).unwrap(), which will panic the WASM module on serialization failure. Since this function now aggregates parse + type-check errors (and is called on every edit in the playground), prefer returning a safe fallback (e.g., empty array) or converting the error into a JsValue rather than panicking.
| serde_wasm_bindgen::to_value(&errors).unwrap() | |
| } | |
| serde_wasm_bindgen::to_value(&errors) | |
| .unwrap_or_else(|_| JsValue::from(js_sys::Array::new())) |
| if enable_type_check.unwrap_or(false) && errors.is_empty() { | ||
| let mut hir = mq_hir::Hir::default(); | ||
| hir.add_code(None, code); | ||
|
|
||
| let mut checker = mq_check::TypeChecker::default(); | ||
| let type_errors = checker.check(&hir); |
There was a problem hiding this comment.
diagnostics() now has an enable_type_check path that runs mq_check::TypeChecker and converts TypeError into Diagnostic, but there are no wasm_bindgen tests covering this behavior. Add a test that calls diagnostics(code, Some(true)) on a known type mismatch and asserts the returned message + 1-based line/column mapping.
| #[wasm_bindgen(js_name=inlayHints, skip_typescript)] | ||
| pub async fn inlay_hints(code: &str) -> JsValue { | ||
| let mut hir = mq_hir::Hir::default(); | ||
| hir.add_code(None, code); | ||
|
|
||
| let mut checker = mq_check::TypeChecker::default(); | ||
| let _ = checker.check(&hir); | ||
|
|
||
| let symbol_types = checker.symbol_types(); |
There was a problem hiding this comment.
The new exported inlay_hints() API currently has no wasm_bindgen tests. Add a test that type-checks a small snippet (e.g., a let binding and/or def) and asserts that inlay_hints() returns a hint at the end of the identifier with the expected label, and that built-in symbols are excluded.