parsec_js/diagnostics.rs
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 James Conway
3
4//! Every error and warning this crate reports, by its code.
5//!
6//! A [`Diagnostic`](crate::Diagnostic)'s `code` is stable: a code isn't
7//! renamed or reused for something else, so you can match on it, count
8//! it or turn it off. The message beside it is for people and may be
9//! reworded. Where a second place matters, like the first of two
10//! declarations or the bracket left open, a label points at it.
11//!
12//! [`parse`](crate::parse) stops at the first error;
13//! [`parse_recovering`](crate::parse_recovering) goes on and reports
14//! each; [`early_errors`](crate::early_errors) gives the ones checked on
15//! the finished tree. [`Diagnostic::render`](crate::Diagnostic::render)
16//! prints any of them as a code frame:
17//!
18//! ```
19//! use parsec_js::{LineIndex, Options, RenderOptions, parse_recovering};
20//!
21//! let src = "let total = 0;\nouter: outer: total++;\n";
22//! let errors = parse_recovering(src, &Options::default()).unwrap_err();
23//! let lines = LineIndex::ecmascript(src);
24//! let frame = errors[0].render(src, &lines, &RenderOptions::default());
25//! assert_eq!(frame, "\
26//! error[duplicate-label]: the label `outer` is already in use here
27//! --> 2:8
28//! |
29//! 2 | outer: outer: total++;
30//! | ----- ^^^^^
31//! | |
32//! | the label is defined here
33//! ");
34//! ```
35//!
36//! The section in each row is ECMAScript 2025's, where the rule is.
37//!
38//! # The lexer's
39//!
40//! | Code | What's wrong | Spec |
41//! |---|---|---|
42//! | `input-too-large` | the source is longer than [`Limits::max_input`](crate::Limits::max_input), or 4 GiB | |
43//! | `unexpected-character` | a character that starts no token, like `@` or `ⸯ` | [12.6] |
44//! | `unterminated-comment` | a `/*` with no `*/` | [12.4] |
45//! | `invalid-escape` | a `\` in a name that doesn't start `\u`, or a `\x` or `\u` escape in a string without its hex digits | [12.7], [12.9.4] |
46//! | `invalid-identifier-escape` | a `\u` escape in a name for a character a name can't hold there | [12.7.1.1] |
47//! | `missing-digits` | `0x`, `0o` or `0b` with no digits after it | [12.9.3] |
48//! | `missing-exponent` | an `e` with no digits after it, like `1e` | [12.9.3] |
49//! | `identifier-after-number` | a letter or digit straight after a number, like `3in` | [12.9.3] |
50//! | `invalid-numeric-separator` | a `_` in a number that isn't between two digits | [12.9.3] |
51//! | `legacy-octal-literal` | `017` in strict code; a warning in sloppy code | [12.9.3.1] |
52//! | `leading-zero-literal` | `08` in strict code; a warning in sloppy code | [12.9.3.1] |
53//! | `unterminated-string` | a string with no closing quote on its line | [12.9.4] |
54//! | `legacy-octal-escape` | `"\01"` or `"\8"` in strict code; a warning in sloppy code | [12.9.4.1] |
55//! | `unterminated-template` | a template with no closing `` ` `` | [12.9.6] |
56//! | `unterminated-regexp` | a regular expression with no closing `/` on its line | [12.9.5] |
57//! | `invalid-regexp-flags` | a regular expression's flags written with an escape | [12.9.5] |
58//!
59//! # The parser's
60//!
61//! Syntax errors, and the early errors the parser checks as it goes.
62//!
63//! | Code | What's wrong | Spec |
64//! |---|---|---|
65//! | `unexpected-token` | a token the grammar doesn't allow where it is | |
66//! | `unexpected-end` | the source ends in the middle of something | |
67//! | `too-deep` | the tree nests deeper than [`Limits::max_depth`](crate::Limits::max_depth) | |
68//! | `step-budget` | the parse read more tokens than [`Limits::max_steps`](crate::Limits::max_steps) | |
69//! | `reserved-word` | a reserved word, or one strict code, a generator, an async function or a module reserves, as a name | [13.1.1] |
70//! | `strict-binding` | `eval` or `arguments` declared or assigned in strict code | [13.1.1] |
71//! | `legacy-octal-in-strict` | a legacy octal literal or escape in code a later `"use strict"` makes strict | [12.9.3.1], [12.9.4.1] |
72//! | `arguments-in-class-init` | `arguments` in a class field's initializer or a static block | [15.7.1] |
73//! | `await-in-class-init` | `await` in a class field's initializer or a static block | [15.7.1] |
74//! | `shorthand-init` | `{a = 1}` as an object literal, not a pattern | [13.2.5.1] |
75//! | `duplicate-proto` | `__proto__` set twice in an object literal | [13.2.5.1] |
76//! | `invalid-template-escape` | a bad escape in a template that isn't tagged | [13.2.8.1] |
77//! | `tagged-template-in-chain` | an optional chain as a template's tag, like ``a?.b`c` `` | [13.3.1.1] |
78//! | `optional-chain-in-new` | `new a?.b()` | [13.3.9] |
79//! | `super-outside-method` | `super.x` outside a method, or `super()` outside a derived class's constructor | [15.2.1], [15.7.1] |
80//! | `super-private` | `super.#x` | [13.3.7] |
81//! | `new-target-outside-function` | `new.target` outside a function | [16.1.1], [16.2.1.1] |
82//! | `import-meta-outside-module` | `import.meta` in a script | [13.3.1.1] |
83//! | `new-import` | `new import(...)` | [13.3.10] |
84//! | `invalid-target` | an assignment, update or binding of something that can't be assigned, like `1 = a` or `a() ++` | [13.4.1], [13.15.1] |
85//! | `delete-identifier` | `delete x` of a plain name in strict code | [13.5.1.1] |
86//! | `delete-private` | `delete this.#x` | [13.5.1.1] |
87//! | `unary-before-exponent` | `-a ** b` without parentheses | [13.6] |
88//! | `mixed-coalesce` | `??` beside `\|\|` or `&&` without parentheses | [13.13] |
89//! | `lone-private-name` | `#x` alone, anywhere but before `in` | [13.10] |
90//! | `arrow-operand` | an arrow function as an operand without parentheses | [15.3] |
91//! | `arrow-params-only` | `()`, `(...a)` or a trailing comma kept by an expression that isn't an arrow's parameters | [13.2.9.1] |
92//! | `rest-not-last` | a rest element or parameter that isn't last | [14.3.3], [15.1.1] |
93//! | `rest-default` | a rest element with a default, like `[...a = 1]` | [14.3.3] |
94//! | `comma-after-rest` | a comma after a rest parameter | [15.1.1] |
95//! | `yield-in-params` | `yield` in a generator's parameters | [15.5.1] |
96//! | `await-in-params` | `await` in an async function's parameters | [15.8.1], [15.9.1] |
97//! | `duplicate-binding` | a parameter given twice where that's not allowed, or a name a `let` or `const` declares twice in one declaration | [14.3.1.1], [15.1.1], [15.2.1] |
98//! | `use-strict-with-complex-params` | `"use strict"` in a function with defaults, patterns or a rest parameter | [15.2.1] |
99//! | `accessor-params` | a getter with parameters, or a setter without exactly one | [15.4.1] |
100//! | `special-constructor` | a constructor that's a getter, setter, generator or async | [15.7.1] |
101//! | `duplicate-constructor` | a class with two constructors | [15.7.1] |
102//! | `static-prototype` | a static method or accessor named `prototype` | [15.7.1] |
103//! | `bad-field-name` | a field named `constructor`, or a static one `prototype` | [15.7.1] |
104//! | `private-constructor` | `#constructor` | [15.7.1] |
105//! | `private-outside-class` | a private name declared outside a class | [15.7.1] |
106//! | `duplicate-private-name` | a private name declared twice in a class, other than a getter and a setter | [15.7.1] |
107//! | `undeclared-private-name` | a private name no class around it declares | [15.7.1], [16.1.1] |
108//! | `declaration-not-allowed` | a declaration as the body of an `if`, loop, label or `with` | [14.6.1], [B.3.1] |
109//! | `const-without-init` | a `const` without an initializer | [14.3.1.1] |
110//! | `pattern-without-init` | a destructuring declaration without an initializer | [14.3.1], [14.3.2] |
111//! | `let-binding` | `let` as a name a `let` or `const` declares | [14.3.1.1] |
112//! | `for-await-outside-async` | `for await` outside an async function or a module | [14.7.5] |
113//! | `for-in-of-declarations` | a for-in or for-of head that declares more than one binding | [14.7.5] |
114//! | `for-in-of-initializer` | an initializer in a for-in or for-of head, except Annex B's `for (var a = 1 in b)` | [14.7.5], [B.3.5] |
115//! | `for-of-ambiguous` | a for-of head that starts with `let`, or is `async of` | [14.7.5] |
116//! | `jump-outside-loop` | `break` outside a loop or a switch, or `continue` outside a loop | [14.8.1], [14.9.1] |
117//! | `continue-not-loop` | `continue a` where `a` isn't on a loop | [15.2.1], [16.1.1] |
118//! | `undefined-label` | a `break` or `continue` to a label that isn't around it | [15.2.1], [16.1.1] |
119//! | `duplicate-label` | a label inside another of the same name | [15.2.1], [16.1.1] |
120//! | `return-outside-function` | `return` outside a function | [14.10] |
121//! | `with-in-strict` | `with` in strict code | [14.11.1] |
122//! | `newline-after-throw` | a line break between `throw` and its expression | [14.14] |
123//! | `duplicate-default` | a switch with two `default`s | [14.12] |
124//! | `module-item-not-allowed` | `import` or `export` anywhere but a module's top level | [16.2] |
125//! | `duplicate-export` | a name a module exports twice | [16.2.1.1] |
126//! | `duplicate-attribute` | an import attribute given twice | [16.2.2.1] |
127//! | `import-needs-as` | a string imported without `as` a name | [16.2.2] |
128//! | `export-string-local` | a string as the local name in an export that isn't a re-export | [16.2.3.1] |
129//! | `lone-surrogate-name` | an import or export name, as a string, with a lone surrogate | [16.2.1.1] |
130//!
131//! # From [`early_errors`](crate::early_errors)
132//!
133//! | Code | What's wrong | Spec |
134//! |---|---|---|
135//! | `duplicate-declaration` | a lexical name declared twice in a scope, or as a parameter too | [14.2.1], [15.2.1], [16.1.1] |
136//! | `lexical-var-conflict` | a `var` and a lexical declaration of one name in a scope | [14.2.1], [16.1.1] |
137//! | `export-undeclared` | a module exports a name it doesn't declare | [16.2.1.1] |
138//! | `regexp-…` | a regular expression's pattern or flags that don't compile; the [`regexp`](crate::regexp) module has each | [13.2.7.1], [22.2.1.1] |
139//!
140//! # From [`Ast::reparse`](crate::Ast::reparse)
141//!
142//! Besides the new source's syntax errors:
143//!
144//! | Code | What's wrong | Spec |
145//! |---|---|---|
146//! | `edit-out-of-range` | the edit's range is past the end of the source, backwards, or splits a character | |
147//!
148//! [12.4]: https://tc39.es/ecma262/2025/#sec-comments
149//! [12.6]: https://tc39.es/ecma262/2025/#sec-tokens
150//! [12.7]: https://tc39.es/ecma262/2025/#sec-names-and-keywords
151//! [12.7.1.1]: https://tc39.es/ecma262/2025/#sec-identifier-names-static-semantics-early-errors
152//! [12.9.3]: https://tc39.es/ecma262/2025/#sec-literals-numeric-literals
153//! [12.9.3.1]: https://tc39.es/ecma262/2025/#sec-numeric-literals-early-errors
154//! [12.9.4]: https://tc39.es/ecma262/2025/#sec-literals-string-literals
155//! [12.9.4.1]: https://tc39.es/ecma262/2025/#sec-string-literals-early-errors
156//! [12.9.5]: https://tc39.es/ecma262/2025/#sec-literals-regular-expression-literals
157//! [12.9.6]: https://tc39.es/ecma262/2025/#sec-template-literal-lexical-components
158//! [12.10]: https://tc39.es/ecma262/2025/#sec-automatic-semicolon-insertion
159//! [13.1.1]: https://tc39.es/ecma262/2025/#sec-identifiers-static-semantics-early-errors
160//! [13.2.5.1]: https://tc39.es/ecma262/2025/#sec-object-initializer-static-semantics-early-errors
161//! [13.2.7.1]: https://tc39.es/ecma262/2025/#sec-primary-expression-regular-expression-literals-static-semantics-early-errors
162//! [13.2.8.1]: https://tc39.es/ecma262/2025/#sec-primary-expression-template-literals-static-semantics-early-errors
163//! [13.2.9.1]: https://tc39.es/ecma262/2025/#sec-grouping-operator-static-semantics-early-errors
164//! [13.3.1.1]: https://tc39.es/ecma262/2025/#sec-left-hand-side-expressions-static-semantics-early-errors
165//! [13.3.9]: https://tc39.es/ecma262/2025/#sec-optional-chains
166//! [13.3.7]: https://tc39.es/ecma262/2025/#sec-super-keyword
167//! [13.3.10]: https://tc39.es/ecma262/2025/#sec-import-calls
168//! [13.4.1]: https://tc39.es/ecma262/2025/#sec-update-expressions-static-semantics-early-errors
169//! [13.5.1.1]: https://tc39.es/ecma262/2025/#sec-delete-operator-static-semantics-early-errors
170//! [13.6]: https://tc39.es/ecma262/2025/#sec-exp-operator
171//! [13.10]: https://tc39.es/ecma262/2025/#sec-relational-operators
172//! [13.13]: https://tc39.es/ecma262/2025/#sec-binary-logical-operators
173//! [13.15.1]: https://tc39.es/ecma262/2025/#sec-assignment-operators-static-semantics-early-errors
174//! [14.2.1]: https://tc39.es/ecma262/2025/#sec-block-static-semantics-early-errors
175//! [14.3.1.1]: https://tc39.es/ecma262/2025/#sec-let-and-const-declarations-static-semantics-early-errors
176//! [14.3.1]: https://tc39.es/ecma262/2025/#sec-let-and-const-declarations
177//! [14.3.2]: https://tc39.es/ecma262/2025/#sec-variable-statement
178//! [14.3.3]: https://tc39.es/ecma262/2025/#sec-destructuring-binding-patterns
179//! [14.6.1]: https://tc39.es/ecma262/2025/#sec-if-statement-static-semantics-early-errors
180//! [14.7.5]: https://tc39.es/ecma262/2025/#sec-for-in-and-for-of-statements
181//! [14.7.5.1]: https://tc39.es/ecma262/2025/#sec-for-in-and-for-of-statements-static-semantics-early-errors
182//! [14.8.1]: https://tc39.es/ecma262/2025/#sec-continue-statement-static-semantics-early-errors
183//! [14.9.1]: https://tc39.es/ecma262/2025/#sec-break-statement-static-semantics-early-errors
184//! [14.10]: https://tc39.es/ecma262/2025/#sec-return-statement
185//! [14.11.1]: https://tc39.es/ecma262/2025/#sec-with-statement-static-semantics-early-errors
186//! [14.12]: https://tc39.es/ecma262/2025/#sec-switch-statement
187//! [14.12.1]: https://tc39.es/ecma262/2025/#sec-switch-statement-static-semantics-early-errors
188//! [14.13.1]: https://tc39.es/ecma262/2025/#sec-labelled-statements-static-semantics-early-errors
189//! [14.14]: https://tc39.es/ecma262/2025/#sec-throw-statement
190//! [15.1.1]: https://tc39.es/ecma262/2025/#sec-parameter-lists-static-semantics-early-errors
191//! [15.2.1]: https://tc39.es/ecma262/2025/#sec-function-definitions-static-semantics-early-errors
192//! [15.3]: https://tc39.es/ecma262/2025/#sec-arrow-function-definitions
193//! [15.4.1]: https://tc39.es/ecma262/2025/#sec-method-definitions-static-semantics-early-errors
194//! [15.5.1]: https://tc39.es/ecma262/2025/#sec-generator-function-definitions-static-semantics-early-errors
195//! [15.7.1]: https://tc39.es/ecma262/2025/#sec-class-definitions-static-semantics-early-errors
196//! [15.8.1]: https://tc39.es/ecma262/2025/#sec-async-function-definitions-static-semantics-early-errors
197//! [15.9.1]: https://tc39.es/ecma262/2025/#sec-async-arrow-function-definitions-static-semantics-early-errors
198//! [16.1.1]: https://tc39.es/ecma262/2025/#sec-scripts-static-semantics-early-errors
199//! [16.2]: https://tc39.es/ecma262/2025/#sec-modules
200//! [16.2.1.1]: https://tc39.es/ecma262/2025/#sec-module-semantics-static-semantics-early-errors
201//! [16.2.2]: https://tc39.es/ecma262/2025/#sec-imports
202//! [16.2.2.1]: https://tc39.es/ecma262/2025/#sec-imports-static-semantics-early-errors
203//! [16.2.3.1]: https://tc39.es/ecma262/2025/#sec-exports-static-semantics-early-errors
204//! [22.2.1.1]: https://tc39.es/ecma262/2025/#sec-patterns-static-semantics-early-errors
205//! [B.3.1]: https://tc39.es/ecma262/2025/#sec-labelled-function-declarations
206//! [B.3.5]: https://tc39.es/ecma262/2025/#sec-initializers-in-forin-statement-heads
207
208#[cfg(test)]
209mod tests {
210 /// Every code this crate's sources give a diagnostic is in the tables
211 /// above, and every code in the tables is one they give.
212 #[test]
213 fn every_code_is_documented() {
214 let sources = [
215 include_str!("lexer.rs"),
216 include_str!("parser/mod.rs"),
217 include_str!("parser/expr.rs"),
218 include_str!("parser/function.rs"),
219 include_str!("parser/module.rs"),
220 include_str!("parser/pattern.rs"),
221 include_str!("parser/stmt.rs"),
222 include_str!("early.rs"),
223 include_str!("scope.rs"),
224 include_str!("reparse.rs"),
225 ];
226 let docs = include_str!("diagnostics.rs");
227 let table: Vec<&str> = docs
228 .lines()
229 .filter_map(|l| l.strip_prefix("//! | `"))
230 .filter_map(|l| l.split('`').next())
231 .filter(|c| *c != "regexp-…")
232 .collect();
233 let mut used = std::collections::BTreeSet::new();
234 for src in sources {
235 for (i, _) in src.match_indices('"') {
236 let rest = &src[i + 1..];
237 let Some(end) = rest.find('"') else { continue };
238 let word = &rest[..end];
239 let is_code = word.contains('-')
240 && word.split('-').all(|p| {
241 !p.is_empty()
242 && p.bytes()
243 .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit())
244 });
245 if is_code && !word.starts_with("regexp-") {
246 used.insert(word);
247 }
248 }
249 }
250 // Codes from parsec-base the parser passes on.
251 used.insert("input-too-large");
252 for code in &used {
253 assert!(table.contains(code), "`{code}` isn't in the table");
254 }
255 for code in &table {
256 assert!(
257 used.contains(code),
258 "`{code}` is in the table, but nothing gives it"
259 );
260 }
261 }
262}