Skip to content

Commit

Permalink
JavaScript regular expression literals
Browse files Browse the repository at this point in the history
There is still one broken case that is _very_ hard to fix:

    var re = x / y // comment

Here the string "/ y /" is recognized as a literal while it is not. This
means the comment is not tokenized as a comment, and if it contains any
opening tokens the next line will be incorrectly indented. A working example
is provided in the unittests.
  • Loading branch information
JaapJoris committed Feb 21, 2023
1 parent a7f9e07 commit 01d6fdc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion djhtml/modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,13 @@ class DjJS(DjTXT):
"""

RAW_TOKENS = DjTXT.RAW_TOKENS + [
r"\\.",
r"//.*",
r"/\*",
r"[$\w-]+:",
r'"(?:\\.|[^\\"])*"', # "string"
r"'(?:\\.|[^\\'])*'", # 'string'
r"`(?:\\.|[^\\`])*`", # `string`
r"/(?:\\.|[^\\/\n])*/", # /string/
r"[{[()\]}]",
r"var ",
r"let ",
Expand Down
17 changes: 17 additions & 0 deletions tests/suite/js.html
Original file line number Diff line number Diff line change
Expand Up @@ -436,3 +436,20 @@
baz
}
</script>


<!-- Expression literals -->
<script>
re = /ab+c/
space_re = / foo /
single_re = /foo\/ + bar/
double_re = /foo/ + /bar/
look_like_comment = /\(//

// The following is known to be broken
const x = 42
const y = 7
const six = x / (function() { // comment
return y
})();
</script>

0 comments on commit 01d6fdc

Please sign in to comment.