Skip to content

Commit

Permalink
Heregex Comment
Browse files Browse the repository at this point in the history
Fixes jashkenas#5428

This fix is simple enough but it causes a substantial change in behavior for Heregexes.

`#` inside of a character class shouldn't be considered a comment. By treating `#`
without whitespace in front as non-comments we could have slight compatability with
Python.

There are other places in the CoffeeScript source where people avoided escaping the `#`
by keeping it next to non-whitespace characters even outside of a character class. This
is different than how Python does it and is probably a bug but maybe it is too late with
the de facto CoffeeScript2 behavior.

I'm not sure if this should be merged in since it changes the behavior quite a lot. Maybe
another one for the CoffeeScript3/Civet bucket.

Refs
---
Python Docs: https://docs.python.org/3/library/re.html#re.X
  • Loading branch information
STRd6 committed Nov 26, 2022
1 parent 1dfa23b commit a4b6e8a
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docs/v2/browser-compiler-legacy/coffeescript.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/v2/browser-compiler-modern/coffeescript.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/coffeescript-browser-compiler-legacy/coffeescript.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/coffeescript-browser-compiler-modern/coffeescript.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions lib/coffeescript/lexer.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/coffeescript/nodes.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions src/lexer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1341,10 +1341,10 @@ HERE_JSTOKEN = ///^ ``` ((?: [^`\\] | \\[\s\S] | `(?!``) )*) ``` ///
# String-matching-regexes.
STRING_START = /^(?:'''|"""|'|")/

STRING_SINGLE = /// ^(?: [^\\'] | \\[\s\S] )* ///
STRING_DOUBLE = /// ^(?: [^\\"#] | \\[\s\S] | \#(?!\{) )* ///
HEREDOC_SINGLE = /// ^(?: [^\\'] | \\[\s\S] | '(?!'') )* ///
HEREDOC_DOUBLE = /// ^(?: [^\\"#] | \\[\s\S] | "(?!"") | \#(?!\{) )* ///
STRING_SINGLE = /// ^(?: [^\\'] | \\[\s\S] )* ///
STRING_DOUBLE = /// ^(?: [^\\"\#] | \\[\s\S] | \#(?!\{) )* ///
HEREDOC_SINGLE = /// ^(?: [^\\'] | \\[\s\S] | '(?!'') )* ///
HEREDOC_DOUBLE = /// ^(?: [^\\"\#] | \\[\s\S] | "(?!"") | \#(?!\{) )* ///

INSIDE_JSX = /// ^(?:
[^
Expand Down Expand Up @@ -1376,19 +1376,19 @@ VALID_FLAGS = /^(?!.*(.).*\1)[gimsuy]*$/
HEREGEX = /// ^
(?:
# Match any character, except those that need special handling below.
[^\\/#\s]
[^\\/\#\s]
# Match `\` followed by any character.
| \\[\s\S]
# Match any `/` except `///`.
| /(?!//)
# Match `#` which is not part of interpolation, e.g. `#{}`.
| \#(?!\{)
# Comments consume everything until the end of the line, including `///`.
| \s+(?:#(?!\{).*)?
| \s*(?:\#(?!\{).*)?
)*
///

HEREGEX_COMMENT = /(\s+)(#(?!{).*)/gm
HEREGEX_COMMENT = /(\s*)(#(?!{).*)/gm

REGEX_ILLEGAL = /// ^ ( / | /{3}\s*) (\*) ///

Expand Down
2 changes: 1 addition & 1 deletion src/nodes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5783,7 +5783,7 @@ STRING_OMIT = ///
HEREGEX_OMIT = ///
((?:\\\\)+) # Consume (and preserve) an even number of backslashes.
| \\(\s) # Preserve escaped whitespace.
| \s+(?:#.*)? # Remove whitespace and comments.
| \s+(?:\#.*)? # Remove whitespace and comments.
///g

# Helper Functions
Expand Down

0 comments on commit a4b6e8a

Please sign in to comment.