Skip to content

JS/Java backends: literal newlines emitted inside regex and string literals #322

@ldayton

Description

@ldayton

Summary

When the Python source contains strings with \n characters used in lstrip()/rstrip() calls, the JS and Java backends emit the newline as a literal character inside regex/string literals instead of as \n. This produces a syntax error in both languages since neither allows raw newlines inside regex or string literals.

Reproduction

Source Python:

parser = Parser(inner.lstrip(" \t\n|"))
# and
return value.rstrip("\n")

Transpiled JS:

parser = new Parser(inner.replace(/^[ 	
|]+/, ""));
//                                    ^ literal newline

return value.replace(/[
]+$/, "");
//                     ^ literal newline

Transpiled Java:

return value.replaceAll("[
]+$", "");
//                       ^ literal newline

Both produce syntax errors (SyntaxError: Invalid regular expression: missing / in JS, unclosed string literal in Java).

Expected

Newlines should be emitted as \n escape sequences inside regex and string literals:

parser = new Parser(inner.replace(/^[ \t\n|]+/, ""));
return value.replace(/[\n]+$/, "");

Scope

Found 2 instances in the Parable transpilation. Affects any Python code that uses \n in string arguments to lstrip(), rstrip(), strip(), or similar methods that get lowered to regex replacements.

Discovered via ldayton/Parable#413.

Metadata

Metadata

Assignees

No one assigned

    Labels

    backendbugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions