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 function

View on GitHub (pinned to f7dbce2997)

Solutions

  1. Read the diagnostics in the error message - they include line/column and a code snippet.
  2. Fix the reported syntax error in the script editor.
  3. Remove TypeScript-only syntax (type annotations, interfaces); only plain JS/ESM is supported.
  4. 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

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.

Related errors


AI-assisted analysis of libnyanpasu/clash-nyanpasu@f7dbce2997 (2026-09-08). Data as JSON: /api/errors/4c4cb87d7a7d0a48. Report an issue: GitHub.