Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trailing comma problem is actual for IE. -t option is added for this purpose #128

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/jargs-1.0.jar"/>
<classpathentry kind="lib" path="lib/rhino-1.7R2.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ CVS/
*#*#
build/classes
build/jar
build
bin
18 changes: 18 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>yuicompressor</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>

</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
2 changes: 1 addition & 1 deletion ant.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ lib.dir = lib
doc.dir = doc
build.dir = build
product.name = yuicompressor
version.number = 2.4.8pre
version.number = 2.4.9-trailing-comma
jar.name = ${product.name}-${version.number}.jar
dist.package.name = ${product.name}-${version.number}
Binary file removed build/yuicompressor-2.4.8pre.jar
Binary file not shown.
39 changes: 34 additions & 5 deletions src/com/yahoo/platform/yui/compressor/YUICompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,42 @@
package com.yahoo.platform.yui.compressor;

import jargs.gnu.CmdLineParser;
import org.mozilla.javascript.ErrorReporter;
import org.mozilla.javascript.EvaluatorException;

import java.io.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.Charset;

import org.mozilla.javascript.ErrorReporter;
import org.mozilla.javascript.EvaluatorException;

public class YUICompressor {
private static class TrailingCommaException extends RuntimeException
{
private static final long serialVersionUID = -7662229702322699326L;
}

public static void main(String args[]) {

CmdLineParser parser = new CmdLineParser();
CmdLineParser.Option typeOpt = parser.addStringOption("type");
CmdLineParser.Option versionOpt = parser.addBooleanOption('V', "version");
CmdLineParser.Option verboseOpt = parser.addBooleanOption('v', "verbose");
CmdLineParser.Option trailingOpt = parser.addBooleanOption('t', "enabletrailing");
CmdLineParser.Option nomungeOpt = parser.addBooleanOption("nomunge");
CmdLineParser.Option linebreakOpt = parser.addStringOption("line-break");
CmdLineParser.Option preserveSemiOpt = parser.addBooleanOption("preserve-semi");
CmdLineParser.Option disableOptimizationsOpt = parser.addBooleanOption("disable-optimizations");
CmdLineParser.Option helpOpt = parser.addBooleanOption('h', "help");
CmdLineParser.Option charsetOpt = parser.addStringOption("charset");
CmdLineParser.Option outputFilenameOpt = parser.addStringOption('o', "output");




Reader in = null;
Writer out = null;

Expand All @@ -51,6 +65,7 @@ public static void main(String args[]) {
}

boolean verbose = parser.getOptionValue(verboseOpt) != null;
final boolean enableTrailing = parser.getOptionValue(trailingOpt) != null;

String charset = (String) parser.getOptionValue(charsetOpt);
if (charset == null || !Charset.isSupported(charset)) {
Expand Down Expand Up @@ -122,6 +137,16 @@ public static void main(String args[]) {
System.exit(1);
}

String prefix = "*********";
StringBuilder sb = new StringBuilder(prefix);
for (int i = 0; i < inputFilename.length() + 2; ++i) {
sb.append('*');
}
sb.append(prefix);
System.err.println(sb.toString());
System.err.println(prefix + " " + inputFilename + " " + prefix);
System.err.println(sb.toString());

in = new InputStreamReader(new FileInputStream(inputFilename), charset);
}

Expand All @@ -132,13 +157,17 @@ public static void main(String args[]) {
}

if (type.equalsIgnoreCase("js")) {

try {

JavaScriptCompressor compressor = new JavaScriptCompressor(in, new ErrorReporter() {

public void warning(String message, String sourceName,
int line, String lineSource, int lineOffset) {
if (message != null && message.startsWith("Trailing comma is not legal") && !enableTrailing) {
//report error instead of warn
error(message, sourceName, line, lineSource, lineOffset);
throw new TrailingCommaException();
}
if (line < 0) {
System.err.println("\n[WARNING] " + message);
} else {
Expand Down
10 changes: 9 additions & 1 deletion src/org/mozilla/javascript/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2222,12 +2222,14 @@ private Node primaryExpr()
int destructuringLen = 0;
decompiler.addToken(Token.LB);
boolean after_lb_or_comma = true;
boolean after_comma = false;
for (;;) {
tt = peekToken();

if (tt == Token.COMMA) {
consumeToken();
decompiler.addToken(Token.COMMA);
after_comma = true;
if (!after_lb_or_comma) {
after_lb_or_comma = true;
} else {
Expand All @@ -2244,6 +2246,9 @@ private Node primaryExpr()
// length value just for destructuring assignment.
destructuringLen = elems.size() +
(after_lb_or_comma ? 1 : 0);
if (after_comma) {
addWarning("msg.extra.trailing.comma", "");
}
break;
} else if (skipCount == 0 && elems.size() == 1 &&
tt == Token.FOR)
Expand Down Expand Up @@ -2276,6 +2281,7 @@ private Node primaryExpr()
}
elems.add(assignExpr(false));
after_lb_or_comma = false;
after_comma = false;
}
}
return nf.createArrayLiteral(elems, skipCount, destructuringLen);
Expand Down Expand Up @@ -2345,7 +2351,9 @@ private Node primaryExpr()
break;

case Token.RC:
// trailing comma is OK.
// trailing comma is a warning as minimum.
// use msgId that is not translated in order to catch inside YUICompressor
addWarning("msg.extra.trailing.comma", "");
break commaloop;
default:
reportError("msg.bad.prop");
Expand Down
107 changes: 107 additions & 0 deletions tests_custom/dynamic-welcome.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/

/*
* Alfresco.dashlet.UserWelcome
* Registers a event handler on the 'Remove Me' button to have the component remove itself.
*
* @namespace Alfresco.dashlet
* @class Alfresco.dashlet.UserWelcome
*/
(function()
{
/**
* YUI Library aliases
*/
var Dom = YAHOO.util.Dom,
Event = YAHOO.util.Event;

/**
* DynamicWelcome constructor.
*
* @param {String} htmlId The HTML id of the parent element
* @return {DynamicWelcome} The new component instance
* @constructor
*/
Alfresco.dashlet.DynamicWelcome = function DynamicWelcome_constructor(htmlId, dashboardUrl, dashboardType, site)
{
Alfresco.dashlet.DynamicWelcome.superclass.constructor.call(this, "Alfresco.dashlet.DynamicWelcome", htmlId);

this.name = "Alfresco.dashlet.DynamicWelcome";
this.dashboardUrl = dashboardUrl;
this.createSite = null;
this.dashboardType = dashboardType;
this.site = site;

this.services.preferences = new Alfresco.service.Preferences();
return this;
}

YAHOO.extend(Alfresco.dashlet.DynamicWelcome, Alfresco.component.Base,
{
site: "",
dashboardType: "",
dashboardUrl: "",
closeDialog: null,

/**
* CreateSite module instance.
*
* @property createSite
* @type Alfresco.module.CreateSite
*/
createSite: null,

/**
* Fired by YUI when parent element is available for scripting.
* Initialises components, including YUI widgets.
*
* @method onReady
*/
onReady: function DynamicWelcome_onReady()
{
// Listen on clicks for the create site link
Event.addListener(this.id + "-close-button", "click", this.onCloseClick, this, true);
Event.addListener(this.id + "-createSite-button", "click", this.onCreateSiteLinkClick, this, true);
Event.addListener(this.id + "-requestJoin-button", "click", this.onRequestJoinLinkClick, this, true);
},

/**
* Fired by YUI Link when the "Create site" label is clicked
* @method onCreateSiteLinkClick
* @param p_event {domEvent} DOM event
*/
onCreateSiteLinkClick: function DynamicWelcome_onCreateSiteLinkClick(p_event)
{
// Create the CreateSite module if it doesn't exist
if (this.createSite === null)
{
this.createSite = Alfresco.module.getCreateSiteInstance();
}
// and show it
this.createSite.show();
Event.stopEvent(p_event);
var obj = {
x: "y"
}
var arr = ["a"]
var empty = [];
}
});
})();