Check JavaScript before you run it: parsing finds syntax and declaration errors without evaluating the script.
dotnet add package RecursiveAI.OneEcmaRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneEcma; var errors = ScriptEngine.Check("let = ;"); foreach (var error in errors) Console.WriteLine(error.Headline);
Unexpected token ';'Unzip the example, then dotnet run.
The full program
The download checks a broken statement, a duplicate declaration and valid code that would change a global and throw if executed. All JavaScript inputs are inline in Program.cs.
using RecursiveAI.OneEcma; string[] inputs = [ "let = ;", "let count = 1; let count = 2;", "globalThis.changed = true; throw new Error('not run');" ]; foreach (string source in inputs) { Console.WriteLine(source); var errors = ScriptEngine.Check(source); Console.WriteLine($"Diagnostics: {errors.Count}"); foreach (var error in errors) Console.WriteLine($" {error.Position.Line}:{error.Position.Column}: {error.Headline}"); }
let = ; Diagnostics: 1 1:7: Unexpected token ';' let count = 1; let count = 2; Diagnostics: 1 1:20: Identifier 'count' has already been declared globalThis.changed = true; throw new Error('not run'); Diagnostics: 0
Malformed JavaScript returns diagnostics rather than throwing; cancellation can throw OperationCanceledException.
Variations
Check a module instead of a classic script. This standalone snippet accepts an export declaration:
using RecursiveAI.OneEcma; using RecursiveAI.OneEcma.Syntax; var options = new ParseOptions { Goal = ParseGoal.Module }; var errors = ScriptEngine.Check("export const answer = 42;", options); Console.WriteLine($"Module diagnostics: {errors.Count}");
Module diagnostics: 0Related recipes
- Extract JavaScript module imports in C#
- Inspect JavaScript lexical scopes in C#
- Limit JavaScript regex execution in C#
The short answer
Call ScriptEngine.Check(source) to validate JavaScript syntax in C# without execution. It returns diagnostics; an empty collection means no syntax or early errors, such as declaring the same name twice, were found. It does not create a realm or evaluate expressions. Use it before saving an editable script or accepting a module for later processing.
The full example deliberately includes a global assignment followed by a throw. Both are valid syntax, so that input has no diagnostics. The assignment never takes effect and the exception never runs.
How do I check JavaScript syntax in C#?
Pass the source string to Check, then display each diagnostic’s Headline. For an editor, also keep PrimarySpan and Position. The span identifies the source range to underline; the position gives its line and column. Keep the original source alongside the diagnostic so offsets still refer to the text the user sees.
A check catches more than misplaced punctuation. The duplicate let count declaration in the full example is an early error: the parser records bindings and detects the redeclaration without needing either initializer’s value. Conversely, a call to an unknown function can be syntactically valid. Syntax checking cannot tell you that a host function will exist when the code eventually runs.
For a file-based editor, read the file into a string in your application and pass that string unchanged. ParseOptions.FileName controls the name in reports. It is not an instruction to read a file. The syntax tree documentation describes the available parse options and diagnostic behavior.
Can a JavaScript code checker fix syntax errors?
Check reports errors; it does not edit source. OneEcma also exposes an explicit CheckAndRepair operation for the documented syntax-only repair table. That is a separate choice, not an automatic side effect of validation. If users own the source, show proposed changes before replacing their text.
Diagnostics can contain help and recovery information. Parser recovery may insert a missing token or skip a stuck token internally to continue finding errors. That recovered tree is not a corrected program. Do not treat it as executable source, and do not infer that a list of diagnostics contains every independent mistake: nearby fallout can be merged.
Can I use a JavaScript syntax checker for modules?
Yes. Select ParseGoal.Module, as in the variation above. The default goal is a classic script; module declarations such as export need the module goal. Module parsing is strict and permits top-level await. Choose the goal according to how your application will use the source, not according to which mode happens to accept it.
If you need declarations as well as errors, call JsParser.Parse and inspect HasErrors before consuming the tree. Parse results with errors are best-effort diagnostic trees. Bound deeply nested input with MaxDepth, cap diagnostic collection with MaxErrors, and pass a cancellation token when an editor request can become obsolete. These are parser controls, not runtime execution limits.