fgysin
fgysin

Reputation: 11943

Eclipse Formatting breaks EL expressions inside JavaScript script

Wow, the title is quite a mouth full. Let me explain. I have a .jsp page, on this page is a JavaScript <script ..> element and inside the JavaScript there is an EL expression (${..}). The code looks like this:

<script type="text/javascript">
  jQuery(document).ready(function() {
    var HOST_ID = ${host.id};
    ...
  });
</script>

The Problem:
The Eclipse JavaScript formatter will per default expand ${host.id} over multiple lines. I am guessing that the EL expression is somehow interpreted as a JavaScript object. Anyway, the tag will make end up looking like this:

var HOST_ID = $
{
  host.id
}
;

=> This actually breaks the code!

If split over multiple lines the tag is no longer recognized as valid EL expression and execution of the JavaScript code will stop throwing around exceptions.

I know about a way to turn off formatting adhoc in the Java formatter using tags (// @formatter:off or on) but there is no such option for the JavaScript formatter in Eclipse.


The only way to fix (read workaround) this is to force the JavaScript formatter to never expand objects. But this is problematic in two ways:

This means if any member of my team accidentially auto-formats a .jsp file containing the described construct the code will silently break. It still looks like valid JS, and passes build etc, but at execution time it won't work.


Does anyone have any solution for this? The best solution would be to somehow disable formatting with a tag like the one used in the Java formatter (this solution works without enforcing company/teamwide formatter standards or hoping no one will accidentally hit Shift+Ctrl+F).

Upvotes: 4

Views: 841

Answers (2)

krick
krick

Reputation: 183

The accepted solution at the time I am writing this suggests wrapping the EL expression in single quotes as a solution to the problem. If you decide to do this and the value in the EL expression is a boolean, be very careful as the value is now a string in Javascript and conditional comparisons might not work as expected...

Assume that you have a boolean variable host.isActive and the value is true.

var HOST_IS_ACTIVE1 = ${host.isActive}; // var type is boolean in Javascript

if (HOST_IS_ACTIVE1 == true) {
  // this code will be executed
}

if (HOST_IS_ACTIVE1 == 'true') {
  // this code will not be executed
}

var HOST_IS_ACTIVE2 = '${host.isActive}'; // var type is string in Javascript

if (HOST_IS_ACTIVE2 == true) {
  // this code will NOT be executed
}

if (HOST_IS_ACTIVE2 == 'true') {
  // this code will be executed
}

Upvotes: 2

fgysin
fgysin

Reputation: 11943

Apparently it is possible to wrap EL expressions in single quotes. This will make them look like a string in JavaScript, but they will still be evaluated as EL expressions.

So changing

var HOST_ID = ${host.id};

to

var HOST_ID = '${host.id}';

actually does the trick. The Eclipse JavaScript formatter now thinks of the expression as a string and does not expand it. Problem solved.

Upvotes: 0

Related Questions