libnyanpasu/clash-nyanpasu · error
parse error: {}
Error message
parse error: {} What it means
wrap_script_if_not_esm parses user JavaScript with the SWC parser; when parsing produces diagnostics (syntax errors), it collects them with source context into a single string and throws anyhow "parse error: {errors}". This means the script text itself is not valid JavaScript for the parser.
Source
Thrown at backend/tauri/src/enhance/script/js.rs:373
}
}
/// This is a tool function to wrap the script if it is not a ESM script.
pub fn wrap_script_if_not_esm(script: &str) -> Result<Cow<'_, str>, anyhow::Error> {
let allocator = Allocator::default();
let source_type = SourceType::default().with_module(true);
let source_text = script.trim_matches(['\t', '\n', '\r', ' ']);
let result = Parser::new(&allocator, source_text, source_type).parse();
if !result.diagnostics.is_empty() {
let mut errors = String::new();
for error in result.diagnostics {
errors.push_str(&format!(
"{:?}\n",
error.with_source_code(source_text.to_string())
));
}
return Err(anyhow::anyhow!("parse error: {}", errors));
}
#[cfg(test)]
eprintln!("result: {:#?}", result.program);
let mut visitor = FunctionVisitor::default();
visitor.visit_program(&result.program);
#[cfg(test)]
eprintln!("visitor: {:#?}", visitor);
if visitor.default_export.is_some() {
return Ok(Cow::Borrowed(script));
}
// check whether `function main` exists
match visitor
.declared_functions
.iter()
.find(|(name, _)| name.contains("main"))
{
Some((_, span)) => {
// just insert `export default` before the functionView on GitHub (pinned to f7dbce2997)
Solutions
- Read the diagnostics in the error message - they include line/column and a code snippet.
- Fix the reported syntax error in the script editor.
- Remove TypeScript-only syntax (type annotations, interfaces); only plain JS/ESM is supported.
- Validate the script in Node or an online JS parser before saving.
Example fix
// before (invalid)
function main(config: Object) { return config; }
// after (valid)
function main(config) { return config; } Defensive patterns
Strategy: validation
Validate before calling
// caller-side syntax check before submitting the script
try { new Function(scriptSource); } catch (e) { alert(`syntax error: ${e.message}`); } Try / catch
try {
await saveScript(src);
} catch (e) {
if (String(e).startsWith('parse error:')) showEditorDiagnostics(e.message);
else throw e;
} Prevention
- Validate script syntax in an editor with JS linting before saving
- Never paste TypeScript into the plain-JS editor
- Check for smart quotes after copy-pasting code
- Keep scripts ESM-compatible with a default export
When it happens
Trigger: Calling wrap_script_if_not_esm with script text containing syntax errors: unbalanced braces, reserved words as identifiers, or TypeScript/JSX syntax fed to a plain-JS parser.
Common situations: Users paste TypeScript or JSX into the JS enhancement editor, truncate the script, or copy code containing smart quotes or invisible characters.
Understand the failure class
Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- PAC script must contain FindProxyForURL function
- {:?}
- no default export or main function
- invalid url
- should never drop oneshot tx
AI-assisted analysis of libnyanpasu/clash-nyanpasu@f7dbce2997 (2026-09-08).
Data as JSON: /api/errors/4c4cb87d7a7d0a48.
Report an issue: GitHub.