Skip to content

syntax: javascript: Highlight regex literals - #4277

Open
ShryukGrandhi wants to merge 1 commit into
micro-editor:masterfrom
ShryukGrandhi:fix/issue-1110-js-syntax-highlighting-thinks-quotes-ins
Open

ShryukGrandhi wants to merge 1 commit into
micro-editor:masterfrom
ShryukGrandhi:fix/issue-1110-js-syntax-highlighting-thinks-quotes-ins

Conversation

@ShryukGrandhi

Copy link
Copy Markdown

Summary

The JavaScript syntax file had no notion of regex literals, so a quote inside one (e.g. .match(/['"]/)) opened a string region that ran on through the rest of the file.

Changes

  • Add a constant.regex region to runtime/syntax/javascript.yaml. Since Go regexps have no lookbehind, a regex literal is detected as a / that follows an operator-like character (( , = : [ ! & | ? { ;), return, or the start of the line, and whose next character isn't / or * (so // and /* comments are unaffected). Escaped slashes don't end the region, and it always ends at end of line so a false positive can't spill into the following lines.
  • Ordinary division like a / b / c doesn't match and is left alone.
  • Known limitation: the character just before the opening / (e.g. the ( in .match(/x/)) gets the regex color too, because it is part of the region's start match.

Testing

  • Added runtime/syntax_javascript_test.go, which runs the embedded JavaScript syntax through the highlighter. It checks the example from the issue (the next line isn't highlighted as a string), division, an escaped \/ in a regex, and // comments after (, , and code. It fails before the change and passes after it.
  • go test ./..., go vet ./runtime/ and gofmt -l runtime/ are clean.

Fixes #1110

Quotes inside a regex literal such as /['"]/ were treated as the start
of a string, breaking the highlighting of the rest of the file. Add a
region for regex literals, recognized by a slash that follows an
operator, opening bracket, comma, semicolon or return.

Fixes micro-editor#1110

Signed-off-by: ShryukGrandhi <85529990+ShryukGrandhi@users.noreply.github.com>

@Andriamanitra Andriamanitra left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've thought about this problem before and I don't think there is an acceptable solution that doesn't involve changing or extending the syntax highlighting system to support either a context stack or regex look-behind.

# regex literal: a slash that follows an operator, opening bracket,
# comma, semicolon or `return` and is not the start of a comment
- constant.regex:
start: "(^|[(,=:\\[!&|?{};]|\\breturn)\\s*/([^/*\\\\]|\\\\.)"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also colors the preceding operator/keyword as constant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JS syntax highlighting thinks quotes inside of a regular expression should be treated as string quotes

2 participants