JavaScript regex literals

Trying to type a JavaScript regex literal containing an “@” character in a <script> block in a CSHTML file causes ReSharper to report false errors:

<script>const pattern = /@@test\.com$/i;</script>

 

  • “@” must be followed by a valid code block;
  • Cannot resolve symbol “test”;

Despite these errors, the code compiles and runs as expected.

Replacing the regex literal with a new RegExp expression resolves the R# errors:

<script>const pattern = new RegExp("@@test\.com$", "i");</script>

R# 2024.2.3 Build 242.0.20240829.213636 built on 2024-08-29
VS2022 v17.11.2

0
3 comments

Hello Richard Deeming, thank you for your message. I have checked the behavior you described, but could not reproduce it.

It would be great if you could share a small sample solution so that we can reproduce the problem on our side. To contact us please use the "Contact support" button at the top of the page.

Also please let me know what features are disabled in ReSharper | Options | Environment | Products & Features. Thank you!

0

OK, turns out it's slightly more complicated. I'm using a library which uses custom HTML helpers to register script blocks and render them at the end of the page. The error appears when the script block is defined within the helper method:

@Html.RequireScript(@<script>const pattern = /@@test\.com$/i;</script>)

You can reproduce it with a simplified helper method:

public static IHtmlString RequireScript(this HtmlHelper html, Func<object, HelperResult> text)
{
    return new HelperResult(writer => text(null).WriteTo(writer));
}

I have uploaded the sample project: Upload id: 2024_09_04_xxCZUWb8swoEwR1FChP7jv (file: TestReSharperRegexLiteral.zip)

Disabled features:

  • Async typing (waiting on the fix for RSRP-498495)
  • CSS language (no longer supported)
  • JavaScript and TypeScript (no longer supported)
  • Protobuf (not used)
0

Richard Deeming, thank you very much for such a wonderful explanation and for the sample project! I've submitted a new issue to our bug tracker: JS regex literals with “@” in in cshtml trigger false ReSharper errors. Please comment or vote foe it.

Regarding RSRP-498495 - this should already be fixed in 2024.2.3. Try turning it back on :)

0

Please sign in to leave a comment.