Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ compiler/compiler.wasi.d.cts
compiler/compiler.wasi-browser.js
compiler/wasi-worker.mjs
compiler/wasi-worker-browser.mjs
compiler/guide/
2 changes: 2 additions & 0 deletions compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"index.d.ts",
"vite.mjs",
"vite.d.mts",
"guide/",
"README.md"
],
"scripts": {
Expand All @@ -33,6 +34,7 @@
"artifacts": "napi artifacts",
"create-npm-dirs": "napi create-npm-dirs",
"version": "napi version && node scripts/sync-optional-deps.mjs",
"prepack": "node scripts/guide.mjs",
"test": "node test/node.mjs"
},
"napi": {
Expand Down
11 changes: 11 additions & 0 deletions compiler/scripts/guide.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// prepack: the guide ships in the package (guide/diagnostics.md is the page each diagnostic code points to, and
// language.md, async.md and reference.md are what it points on to), copied from the repository's guide/.
import { copyFileSync, mkdirSync, readdirSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";

const here = path.dirname(fileURLToPath(import.meta.url));
const from = path.join(here, "../../guide");
const to = path.join(here, "../guide");
mkdirSync(to, { recursive: true });
for (const f of readdirSync(from).filter(f => f.endsWith(".md"))) copyFileSync(path.join(from, f), path.join(to, f));
8 changes: 7 additions & 1 deletion compiler/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ pub fn app(entry: &str, target: Target, module_name: &str, spa: bool) -> Value {
if read_disk(entry).is_none() {
return Err(json!({ "error": format!("{entry}: no such file") }));
}
let set = load_modules(entry, &read_disk).map_err(|e| json!({ "error": format!("{entry}: {}", e.0) }))?;
let set = load_modules(entry, &read_disk).map_err(|e| {
// a parse error is a diagnostic like the checker's, with its code (PARSE, or the habit it names)
let file = e.2.clone().unwrap_or_else(|| entry.to_string());
let mut d = crate::serve::parse_error_in(&file, &e, read_disk(&file).as_deref());
d["msg"] = json!(crate::serve::parse_message(&e));
json!({ "diagnostics": [d] })
})?;
let checked = check_modules(&set, spa);
if !checked.diags.is_empty() {
return Err(json!({ "diagnostics": checked.diags.iter().map(|d| { let mut j = d.to_json(); if j["file"].is_null() { j["file"] = json!(set.entry); } j }).collect::<Vec<_>>() }));
Expand Down
15 changes: 13 additions & 2 deletions compiler/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,17 @@ fn lower_one(r: &Value) -> Value {
v
}

/// A parse error as `rxc check` prints a diagnostic: `file:line:column: error CODE [rule] message`, the message without the
/// `file: line n:` it carries.
fn parse_error_text(f: &str, e: &reactivescript::parse::ParseError) -> String {
let v = parse_error_json(f, e);
let msg = serve::parse_message(e);
let at = match (v["range"]["start"]["line"].as_i64(), v["range"]["start"]["column"].as_i64()) {
(Some(l), Some(c)) => format!("{}:{}", l, c + 1),
_ => "1".into(),
};
format!("{}:{}: error {} [{}] {}", v["file"].as_str().unwrap_or(f), at, e.3, v["rule"].as_str().unwrap_or("07"), msg)
}
/// A parse error as `diagnostic_json` writes a diagnostic: from where the parser stopped to the end of that line.
fn parse_error_json(f: &str, e: &reactivescript::parse::ParseError) -> Value {
let file = e.2.clone().unwrap_or_else(|| abs(f));
Expand Down Expand Up @@ -264,7 +275,7 @@ fn main() {
let set = match load_modules(&abs(file), &read_disk) {
Ok(s) => s,
Err(e) => {
eprintln!("{}: parse error: {}", file, e.0);
eprintln!("{}", parse_error_text(file, &e));
std::process::exit(1);
}
};
Expand Down Expand Up @@ -318,7 +329,7 @@ fn main() {
if as_json {
found.push(parse_error_json(f, &e));
} else {
eprintln!("{}: parse error: {}", f, e.0);
eprintln!("{}", parse_error_text(f, &e));
}
continue;
}
Expand Down
8 changes: 4 additions & 4 deletions compiler/src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ impl Loader<'_> {
let Some(src) = (self.read)(path) else { return Ok(()) };
let mut p = if self.recover {
let (p, errs) = crate::parse::parse_program_recovering(&src);
self.parse_errors.extend(errs.into_iter().map(|e| ParseError(e.0, e.1, Some(path.to_string()))));
self.parse_errors.extend(errs.into_iter().map(|e| ParseError(e.0, e.1, Some(path.to_string()), e.3)));
p
} else {
parse_program(&src).map_err(|e| ParseError(format!("{}: {}", path, e.0), e.1, Some(path.to_string())))?
parse_program(&src).map_err(|e| ParseError(format!("{}: {}", path, e.0), e.1, Some(path.to_string()), e.3))?
};
self.state.insert(path.to_string(), State::Busy);
for d in p.decls.iter_mut() {
Expand All @@ -98,7 +98,7 @@ impl Loader<'_> {
let dts = join_path(path, &(replace_js_ext(from) + ".d.rx"));
if !self.declarations.contains_key(&dts) {
if let Some(src) = (self.read)(&dts) {
let ds = parse_declarations(&src).map_err(|e| ParseError(format!("{}: {}", dts, e.0), e.1, Some(dts.clone())))?;
let ds = parse_declarations(&src).map_err(|e| ParseError(format!("{}: {}", dts, e.0), e.1, Some(dts.clone()), e.3))?;
self.declarations.insert(dts.clone(), ds);
}
}
Expand Down Expand Up @@ -134,7 +134,7 @@ impl Loader<'_> {
// MO-9: no body — its signature, read in place
let dts = to.strip_suffix(".rx").map(|s| format!("{}.d.rx", s)).unwrap_or_else(|| to.clone());
if let Some(sig) = (self.read)(&dts) {
let s = parse_signature(&sig, &to).map_err(|e| ParseError(e.0, None, Some(dts.clone())))?;
let s = parse_signature(&sig, &to).map_err(|e| ParseError(e.0, None, Some(dts.clone()), "PARSE"))?;
self.signatures.insert(to.clone(), s);
continue;
}
Expand Down
62 changes: 54 additions & 8 deletions compiler/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use std::collections::HashSet;
use std::sync::LazyLock;

#[derive(Debug, Clone, PartialEq)]
/// What failed to parse; where (a byte offset), and in which file, when known.
pub struct ParseError(pub String, pub Option<usize>, pub Option<String>);
/// What failed to parse; where (a byte offset), and in which file, when known; and its code: the habit `hint` recognised
/// (guide/diagnostics.md), else `PARSE`.
pub struct ParseError(pub String, pub Option<usize>, pub Option<String>, pub &'static str);
impl std::fmt::Display for ParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
Expand Down Expand Up @@ -151,7 +152,11 @@ impl<'a> Cursor<'a> {
}
pub fn fail<T>(&self, msg: &str) -> R<T> {
let near: String = self.rest().chars().take(20).collect();
Err(ParseError(format!("line {}: {} near '{}'", self.line(), msg, near), Some(self.i), None))
let near = near.split('\n').next().unwrap_or("");
let near = if near.trim().is_empty() { "at the end of the line".to_string() } else { format!("near '{}'", near) };
let h = hint(self.src, self.i, msg);
let tail = h.as_ref().map(|(_, t)| format!(" — {}", t)).unwrap_or_default();
Err(ParseError(format!("line {}: {} {}{}", self.line(), msg, near, tail), Some(self.i), None, h.map(|(c, _)| c).unwrap_or("PARSE")))
}
pub fn ident(&mut self) -> Option<String> {
let m = IDENT.find(self.rest())?;
Expand Down Expand Up @@ -253,6 +258,47 @@ fn parse_classes(c: &mut Cursor) -> R<Option<Vec<String>>> {
Ok(Some(out))
}

re!(JS_APIS, r"\b(useState|useReducer|useMemo|useCallback|useEffect|useRef|createSignal|createMemo|createEffect|createStore|createResource|createAsync)\s*\(");
re!(JS_FUNCTION, r"^(export\s+)?(default\s+)?(async\s+)?function\b");
re!(JS_LOCAL, r"^(const|let|var)\b");
re!(JS_RETURN, r"^return\b");
re!(NAME_BEFORE, r"([A-Za-z_$][A-Za-z0-9_$.]*)\s*$");
re!(ARROW_BEFORE, r"=>\s*[A-Za-z_$][A-Za-z0-9_$.]*\s*$");
re!(OP_ASSIGN, r"^(\+\+|--|(\*\*|\|\||&&|\?\?|[-+*/%])=)");

/// What a parse failure at `i` most likely meant, when the line reads like JavaScript, React or Solid: its code and the
/// text appended to the message. `ref/src/parse.ts` `hint` is the same function.
pub fn hint(src: &str, i: usize, msg: &str) -> Option<(&'static str, String)> {
let i = i.min(src.len());
let rest = &src[i..];
let at = if !rest.is_empty() && !rest.split('\n').next().unwrap_or("").trim().is_empty() { i as i64 } else { src[..i].trim_end().len() as i64 - 1 };
let p = at.max(0) as usize;
let start = if p == 0 { 0 } else { src[..p].rfind('\n').map(|j| j + 1).unwrap_or(0) };
let end = src[p..].find('\n').map(|j| p + j).unwrap_or(src.len());
let line = src[start..end].trim();
let before = &src[src[..i].rfind('\n').map(|j| j + 1).unwrap_or(0)..i];
let name = NAME_BEFORE.captures(before).map(|c| c[1].to_string()).unwrap_or_else(|| "x".into());
if msg.starts_with("W-2") && rest.starts_with(":=") && ARROW_BEFORE.is_match(before) {
return Some(("REACTIVE_WRITE_IN_OWNED_SCOPE", format!("an arrow that writes takes a braced body, `() => {{ {} := … }}`: an expression-bodied arrow is a pure function (SC-3)", name)));
}
if OP_ASSIGN.is_match(rest) || (rest.starts_with('=') && !rest[1..].starts_with('=') && !rest[1..].starts_with('>')) {
return Some(("ASSIGNMENT", format!("a source is written with `:=`, as a statement in a handler or action: `{n} := {n} + 1`, or `{n} := v => v + 1` (W-1, W-6); .rx has no `=`, `+=` or `++` on state", n = name)));
}
if JS_FUNCTION.is_match(line) {
return Some(("JS_FUNCTION", "`function` is not .rx: a pure function is `fn f(x) => …`, one that writes or awaits is `action f(x) { … }`, one that renders is `component F(props) { … }`".into()));
}
if let Some(c) = JS_LOCAL.captures(line) {
return Some(("JS_LOCAL", format!("`{}` declares a local only in a handler or action body: in a component or at a module's top level, state is `source x := …` and a value computed from others is an equation, `x = …`", &c[1])));
}
if JS_RETURN.is_match(line) {
return Some(("JS_RETURN", "a component has no `return`: its body is declarations, then its markup".into()));
}
if let Some(c) = JS_APIS.captures(line) {
return Some(("JS_API", format!("`{}` is not .rx: state is `source x := …`, a value computed from others is `x = …`, a side effect is `effect (x) {{ … }}`", &c[1])));
}
None
}

pub fn parse_program(src: &str) -> R<Program> {
let mut c = Cursor::new(src);
let mut decls = vec![];
Expand All @@ -266,7 +312,7 @@ pub fn parse_program(src: &str) -> R<Program> {
let view = parse_markup(&mut c, Close::Top)?;
c.ws(true);
if !c.eof() {
return c.fail("declaration or markup");
return c.fail("expected a declaration or markup");
}
let dflt: Vec<&Decl> = decls.iter().filter(|d| matches!(d, Decl::Component { is_default: true, .. })).collect();
if dflt.len() > 1 {
Expand Down Expand Up @@ -298,7 +344,7 @@ pub fn parse_program_recovering(src: &str) -> (Program, Vec<ParseError>) {
match parse_markup(&mut c, Close::Top) {
Ok(view) if !view.is_empty() => {
let mut errors = c.errors;
errors.push(ParseError("line 1: markup at a module's top level (MO-1c)".into(), Some(at), None));
errors.push(ParseError("line 1: markup at a module's top level (MO-1c)".into(), Some(at), None, "PARSE"));
return (Program { decls, view, legacy_view: Some(true) }, errors);
}
Ok(_) => break,
Expand Down Expand Up @@ -1564,14 +1610,14 @@ fn parse_markup(c: &mut Cursor, close: Close) -> R<Vec<Markup>> {
}
if c.i == start {
// the reference loops here forever (a stray `}`); this ends it
return c.fail("declaration or markup");
return c.fail("expected a declaration or markup");
}
continue;
}
if s.contains(":=") {
return c.fail("W-2: write in reactive scope (no statement position)");
}
return c.fail("declaration or markup");
return c.fail("expected a declaration or markup");
}
if broke && matches!(out.last(), Some(Markup { kind: MarkupKind::Text(_), .. })) {
out.push(Markup { kind: MarkupKind::Text(" ".into()), line: None, span: Span::NONE });
Expand All @@ -1582,7 +1628,7 @@ fn parse_markup(c: &mut Cursor, close: Close) -> R<Vec<Markup>> {
}
if c.i == start {
// the reference loops here forever (a stray `}` in an element); this ends it
return c.fail("declaration or markup");
return c.fail("expected a declaration or markup");
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion compiler/src/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@ pub fn diagnostic_json(set: &crate::modules::ModuleSet, d: &crate::check::Diagno
v
}

/// A parse error's message without the `file: line n:` it carries (the diagnostic's own fields say where).
pub fn parse_message(e: &crate::parse::ParseError) -> &str {
e.0.split_once("line ").filter(|(pre, _)| pre.is_empty() || pre.ends_with(": ")).and_then(|(_, r)| r.split_once(": ")).map(|(_, m)| m).unwrap_or(&e.0)
}

pub fn parse_error_in(file: &str, e: &crate::parse::ParseError, src: Option<&str>) -> Value {
let mut v = json!({ "file": file, "code": "PARSE", "rule": "07", "message": e.0 });
static RULE: std::sync::LazyLock<regex::Regex> = std::sync::LazyLock::new(|| regex::Regex::new(r"\b([A-Z]+-\d+[a-z]?)\b").unwrap());
let rule = RULE.captures(&e.0).map(|c| c[1].to_string()).unwrap_or_else(|| "07".into());
let mut v = json!({ "file": file, "code": e.3, "rule": rule, "message": parse_message(e) });
if let (Some(at), Some(src)) = (e.1, src) {
let at = at.min(src.len());
let end = src[at..].find('\n').map(|j| at + j).unwrap_or(src.len());
Expand Down
77 changes: 77 additions & 0 deletions compiler/tests/diagnostics_page.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//! guide/diagnostics.md, the page each diagnostic code points to: every code the crate (and its type prelude) can
//! report has a section, every `rx before` fence gets its section's code, and every `rx after` fence checks clean.

use reactivescript::check::check_modules;
use reactivescript::modules::load_modules;
use regex::Regex;
use std::collections::BTreeSet;
use std::path::Path;

fn root() -> &'static Path {
Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap()
}

/// The codes a program `src` gets: its parse error's, else its checker diagnostics'.
fn codes(src: &str) -> Vec<String> {
let entry = "/page/main.rx";
let read = |p: &str| if p == entry { Some(src.to_string()) } else { None };
match load_modules(entry, &read) {
Err(e) => vec![e.3.to_string()],
Ok(set) => check_modules(&set, false).diags.into_iter().map(|d| format!("{} ({}: {})", d.code, d.line, d.msg)).collect(),
}
}

#[test]
fn every_code_has_a_section_and_its_examples_hold() {
let page = std::fs::read_to_string(root().join("guide/diagnostics.md")).unwrap();
let heading = Regex::new(r"^#{2,3} ([A-Z][A-Z_]+)$").unwrap();
let mut sections = BTreeSet::new();
let mut failures = vec![];
let (mut code, mut fence): (Option<String>, Option<(String, String)>) = (None, None);
for line in page.lines() {
if let Some((mark, body)) = &mut fence {
if line == "```" {
let got = codes(body);
let c = code.clone().unwrap_or_default();
let ok = if mark == "before" { got.iter().any(|g| g == &c || g.starts_with(&format!("{} (", c))) } else { got.is_empty() };
if !ok {
failures.push(format!("{} `rx {}`: got {:?}\n{}", c, mark, got, body));
}
fence = None;
} else {
body.push_str(line);
body.push('\n');
}
continue;
}
if let Some(m) = heading.captures(line) {
code = Some(m[1].to_string());
sections.insert(m[1].to_string());
} else if let Some(mark) = line.strip_prefix("```rx ") {
fence = Some((mark.to_string(), String::new()));
}
}
let mut emitted = BTreeSet::new();
let patterns = [
r#"err\(\s*"([A-Z][A-Z_]+)""#,
r#"code: "([A-Z][A-Z_]+)""#,
r#"\{ "([A-Z][A-Z_]+)" \} else \{ "([A-Z][A-Z_]+)" \}"#,
r#"Some\(\("([A-Z][A-Z_]+)""#,
r#"unwrap_or\("([A-Z][A-Z_]+)"\)"#,
r#""([A-Z][A-Z_]+): "#,
];
for f in std::fs::read_dir(root().join("compiler/src")).unwrap() {
let p = f.unwrap().path();
let src = std::fs::read_to_string(&p).unwrap();
for pat in &patterns {
for m in Regex::new(pat).unwrap().captures_iter(&src) {
for g in m.iter().skip(1).flatten() {
emitted.insert(g.as_str().to_string());
}
}
}
}
let missing: Vec<_> = emitted.difference(&sections).collect();
assert!(missing.is_empty(), "codes with no section in guide/diagnostics.md: {:?}", missing);
assert!(failures.is_empty(), "examples that do not hold:\n\n{}", failures.join("\n\n"));
}
15 changes: 13 additions & 2 deletions compiler/vite.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,23 @@
// changes. The crate's output imports its dependencies as `./x.rx?rx-dep`, and a dependency is served from the build
// of a program that contains it. A module several programs share is one module: its instance is keyed by its factory
// (11 CP-4b `module`), not by its place in a program.
import { statSync } from "node:fs";
import { existsSync, statSync } from "node:fs";
import { createRequire } from "node:module";
import path from "node:path";
import { fileURLToPath } from "node:url";

const { app } = createRequire(import.meta.url)("./index.js");
const DEP = "rx-dep";
// the page each code points to: the package's copy (scripts/guide.mjs), else the repository's
const GUIDE = [path.join(path.dirname(fileURLToPath(import.meta.url)), "guide/diagnostics.md"), path.join(path.dirname(fileURLToPath(import.meta.url)), "../guide/diagnostics.md")].find(existsSync)
?? "https://github.com/solidjs/reactivescript/blob/main/guide/diagnostics.md";
const at = d => d.range ? `${d.range.start.line}:${d.range.start.column + 1}` : `${d.line}`;
/** The diagnostics as `rxc check` prints them, then, once per code, where its fix is explained. */
const report = ds => [
...ds.map(d => `${d.file}:${at(d)}: error ${d.code} [${d.rule}] ${d.msg}`),
"",
...[...new Set(ds.map(d => d.code))].map(c => `${c}: why, and the fix — ${GUIDE}#${c.toLowerCase()}`),
].join("\n");
const mtime = file => { try { return statSync(file).mtimeMs; } catch { return -1; } };
const isDep = moduleId => new URLSearchParams(moduleId.split("?")[1] ?? "").has(DEP);
// `import __m0 from "./catalog.rx";` → `"./catalog.rx?rx-dep"` (one line each: the module's map is by line, CP-1c)
Expand Down Expand Up @@ -55,7 +66,7 @@ export default function rx() {
const spa = target === "dom" && solid.hydratable === false;
const dep = isDep(moduleId);
const { entry, out } = dep ? containing(file, target, moduleName, spa) : build(file, target, moduleName, spa);
if (out.diagnostics) throw new Error(out.diagnostics.map(d => `${d.file}:${d.line}: error ${d.code} [${d.rule}] ${d.msg}`).join("\n"));
if (out.diagnostics) throw new Error(report(out.diagnostics));
if (out.error) throw new Error(out.error);
const m = out.modules[file];
for (const p of Object.keys(out.modules)) if (p !== file) addWatchFile(p);
Expand Down
Loading
Loading