Skip to content

Commit 3ba356f

Browse files
committed
Merge pull request yui#187 from tml/preserveHints
Provide an option to preserve unknown hints
2 parents 861d89f + ce1be5b commit 3ba356f

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

src/com/yahoo/platform/yui/compressor/JavaScriptCompressor.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ private static void optimizeObjLitMemberDecl(ArrayList tokens) {
520520

521521
private boolean munge;
522522
private boolean verbose;
523+
private boolean preserveUnknownHints;
523524

524525
private static final int BUILDING_SYMBOL_TREE = 1;
525526
private static final int CHECKING_SYMBOL_TREE = 2;
@@ -541,14 +542,16 @@ public JavaScriptCompressor(Reader in, ErrorReporter reporter)
541542
public void compress(Writer out, int linebreak, boolean munge, boolean verbose,
542543
boolean preserveAllSemiColons, boolean disableOptimizations)
543544
throws IOException {
544-
compress(out, null, linebreak, munge, verbose, preserveAllSemiColons, disableOptimizations);
545+
compress(out, null, linebreak, munge, verbose, preserveAllSemiColons,
546+
disableOptimizations, false);
545547
}
546548
public void compress(Writer out, Writer mungemap, int linebreak, boolean munge, boolean verbose,
547-
boolean preserveAllSemiColons, boolean disableOptimizations)
549+
boolean preserveAllSemiColons, boolean disableOptimizations, boolean preserveUnknownHints)
548550
throws IOException {
549551

550552
this.munge = munge;
551553
this.verbose = verbose;
554+
this.preserveUnknownHints = preserveUnknownHints;
552555

553556
processStringLiterals(this.tokens, !disableOptimizations);
554557

@@ -730,9 +733,9 @@ private void parseFunctionDeclaration() {
730733
String hint = st1.nextToken();
731734
int idx = hint.indexOf(':');
732735
if (idx <= 0 || idx >= hint.length() - 1) {
733-
if (mode == BUILDING_SYMBOL_TREE) {
736+
if (mode == BUILDING_SYMBOL_TREE && (! preserveUnknownHints)) {
734737
// No need to report the error twice, hence the test...
735-
warn("Invalid hint syntax: " + hint, true);
738+
warn("Not a YUICompressor hint: " + hint, true);
736739
}
737740
break;
738741
}
@@ -1239,9 +1242,11 @@ private StringBuffer printSymbolTree(int linebreakpos, boolean preserveAllSemiCo
12391242
token = getToken(0);
12401243
if (token.getType() == Token.STRING &&
12411244
getToken(1).getType() == Token.SEMI) {
1242-
// This is a hint. Skip it!
1243-
consumeToken();
1244-
consumeToken();
1245+
if (! preserveUnknownHints) {
1246+
// This is an unknown hint. Skip it!
1247+
consumeToken();
1248+
consumeToken();
1249+
}
12451250
}
12461251
break;
12471252

src/com/yahoo/platform/yui/compressor/YUICompressor.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public static void main(String args[]) {
3131
CmdLineParser.Option charsetOpt = parser.addStringOption("charset");
3232
CmdLineParser.Option outputFilenameOpt = parser.addStringOption('o', "output");
3333
CmdLineParser.Option mungemapFilenameOpt = parser.addStringOption('m', "mungemap");
34+
CmdLineParser.Option preserveUnknownHintsOpt = parser.addBooleanOption('p', "preservehints");
3435

3536
Reader in = null;
3637
Writer out = null;
@@ -90,6 +91,7 @@ public static void main(String args[]) {
9091
boolean munge = parser.getOptionValue(nomungeOpt) == null;
9192
boolean preserveAllSemiColons = parser.getOptionValue(preserveSemiOpt) != null;
9293
boolean disableOptimizations = parser.getOptionValue(disableOptimizationsOpt) != null;
94+
boolean preserveUnknownHints = parser.getOptionValue(preserveUnknownHintsOpt) != null;
9395

9496
String[] fileArgs = parser.getRemainingArgs();
9597
java.util.List files = java.util.Arrays.asList(fileArgs);
@@ -206,7 +208,7 @@ public EvaluatorException runtimeError(String message, String sourceName,
206208
}
207209

208210
compressor.compress(out, mungemap, linebreakpos, munge, verbose,
209-
preserveAllSemiColons, disableOptimizations);
211+
preserveAllSemiColons, disableOptimizations, preserveUnknownHints);
210212

211213
} catch (EvaluatorException e) {
212214

@@ -288,6 +290,7 @@ private static void usage() {
288290
+ " --charset <charset> Read the input file using <charset>\n"
289291
+ " --line-break <column> Insert a line break after the specified column number\n"
290292
+ " -v, --verbose Display informational messages and warnings\n"
293+
+ " -p, --preservehints Don't elide unrecognized compiler hints (e.g. \"use strict\", \"use asm\")\n"
291294
+ " -m <file> Place a mapping of munged identifiers to originals in this file\n\n"
292295
+ " -o <file> Place the output into <file>. Defaults to stdout.\n"
293296
+ " Multiple files can be processed using the following syntax:\n"

tests/issue71.js.FAIL

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function foo() {
2+
"use strict";
3+
alert('hello');
4+
}

tests/issue71.js.min

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
function foo(){"use strict";alert('hello');}

0 commit comments

Comments
 (0)