aspnet / Tooling

Issue tracker and info on Visual Studio tooling for ASP.NET
Other
256 stars 124 forks source link

Javascript formating not work with c# code without quotas #913

Open Eilon opened 7 years ago

Eilon commented 7 years ago

From @mojtabakaviani on November 27, 2016 8:28

For example some time need insert value to javascript code in cshtml files:

<script>
var value = @ViewBag.Id;
</script>

but formatting not working until add quotas:

<script>
var value = '@ViewBag.Id';
</script>

Copied from original issue: aspnet/Razor#871

Eilon commented 7 years ago

From @dougbu on November 27, 2016 20:27

@mojtabakaviani what are your expectations exactly?

@ViewBag.Id inserts the current value into the generated HTML / JavaScript. So, unless that value happened to be a valid literal, I'd expect JavaScript errors in the first example.

Eilon commented 7 years ago

From @mojtabakaviani on November 28, 2016 8:31

No any error, just in the first example formatting not working. can not inserts numerical or code without literals.

Eilon commented 7 years ago

From @dougbu on November 28, 2016 18:3

Sounds like the system is working as-expected. Or, what special-casing were you requesting here?

Suggest looking at the generated HTML and confirming.

Eilon commented 7 years ago

From @mojtabakaviani on November 28, 2016 18:26

I think formatting not support this scenario and must add this feature.

Eilon commented 7 years ago

@doug the quotes are needed only for certain data types, e.g. strings. If the id is a numeric type the quotes would be incorrect.

I am able to reproduce the problem in VS2015 U3:

  1. Create new ASP.NET Core app
  2. In a Razor file, add this snippet:
    <script>
    var value   =   @ViewBag.Id;
    </script>

    (Note all the extraneous spaces around the = sign.)

  3. Press Ctrl+K, Ctrl+D to Format Document

Expected: The JavaScript code should be better-formatted by trimming the extraneous spaces around the = sign.

Result: No formatting takes place.

However, if you wrap the @ViewBag.Id in quotes, i.e. '@ViewBag.Id', then repeat, it all works great.

mlorbetske commented 7 years ago

TFS Bug#362028