javascriptyui-compressor

yui compressor giving syntax error for valid javascript syntax


I am getting syntax error while compressing js file using a command line tool yuicompressor jar.

[ERROR] 1796:28:invalid property id

I have a variable commentGroup. I want to push it as JSON object with the value of commentGroup as key. To achieve this I referred this answer.

var commentGroup = 'owner';
var groupIndex= [];
groupIndex.push({[commentGroup]: 1}); // Error line 1796

Why is it telling invalid property id?

I am using yuicompressor-2.4.7.jar

I have tried above code on JavaScript/CSS/HTML Compressor

Output

{
  "message": "Unexpected token: name (commentGroup)",
  "filename": 0,
  "line": 3,
  "col": 18,
  "pos": 66
}

I think YUI compressor does not understand {[commentGroup]: 1}

I got the solution for above problem, I need to declare temporary variable, then I put my object as a key into that variable and then pushed into the array.

var commentGroup = 'owner';
var aObject= {};
aObject[commentGroup]= 1;  
var groupIndex= [];
groupIndex.push(aObject);

Solution

  • That's because what you have used is ES6 syntax. And YUI Compressor is not supporting ES6 yet.There's open ticket here.