Skip to content
This repository was archived by the owner on Feb 23, 2022. It is now read-only.

Commit 0db031b

Browse files
committed
Support --allow-duplicate-declarations
When using CSS transform tools such as autoprefixer, the generated code can include duplicate declarations but not include the alternate annotation. This allows developers to opt out of the check during the dead code elimination pass.
1 parent 0b5667c commit 0db031b

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

src/com/google/common/css/JobDescription.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class JobDescription {
4747
public final boolean simplifyCss;
4848
public final boolean eliminateDeadStyles;
4949
public final boolean allowDefPropagation;
50+
public final boolean allowDuplicateDeclarations;
5051
public final boolean allowUnrecognizedFunctions;
5152
public final Set<String> allowedNonStandardFunctions;
5253
public final boolean allowUnrecognizedProperties;
@@ -124,6 +125,7 @@ public enum SourceMapDetailLevel { ALL, DEFAULT }
124125
boolean useInternalBidiFlipper, boolean swapLtrRtlInUrl,
125126
boolean swapLeftRightInUrl, boolean simplifyCss,
126127
boolean eliminateDeadStyles, boolean allowDefPropagation,
128+
boolean allowDuplicateDeclarations,
127129
boolean allowUnrecognizedFunctions,
128130
Set<String> allowedNonStandardFunctions,
129131
boolean allowUnrecognizedProperties,
@@ -162,6 +164,7 @@ public enum SourceMapDetailLevel { ALL, DEFAULT }
162164
this.simplifyCss = simplifyCss;
163165
this.eliminateDeadStyles = eliminateDeadStyles;
164166
this.allowDefPropagation = allowDefPropagation;
167+
this.allowDuplicateDeclarations = allowDuplicateDeclarations;
165168
this.allowUnrecognizedFunctions = allowUnrecognizedFunctions;
166169
this.allowedNonStandardFunctions = ImmutableSet.copyOf(
167170
allowedNonStandardFunctions);

src/com/google/common/css/JobDescriptionBuilder.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public class JobDescriptionBuilder {
5151
boolean simplifyCss;
5252
boolean eliminateDeadStyles;
5353
boolean allowDefPropagation;
54+
boolean allowDuplicateDeclarations;
5455
boolean allowUnrecognizedFunctions;
5556
Set<String> allowedNonStandardFunctions;
5657
boolean allowUnrecognizedProperties;
@@ -88,6 +89,7 @@ public JobDescriptionBuilder() {
8889
this.simplifyCss = false;
8990
this.eliminateDeadStyles = false;
9091
this.allowDefPropagation = false;
92+
this.allowDuplicateDeclarations = false;
9193
this.allowUnrecognizedFunctions = false;
9294
this.allowedNonStandardFunctions = Sets.newHashSet();
9395
this.allowUnrecognizedProperties = false;
@@ -411,6 +413,16 @@ public JobDescriptionBuilder allowDefPropagation() {
411413
return setAllowDefPropagation(true);
412414
}
413415

416+
public JobDescriptionBuilder setAllowDuplicateDeclarations(boolean allow) {
417+
checkJobIsNotAlreadyCreated();
418+
this.allowDuplicateDeclarations = allow;
419+
return this;
420+
}
421+
422+
public JobDescriptionBuilder allowDuplicateDeclarations() {
423+
return setAllowDuplicateDeclarations(true);
424+
}
425+
414426
public JobDescriptionBuilder setAllowUndefinedConstants(boolean allow) {
415427
checkJobIsNotAlreadyCreated();
416428
this.allowUndefinedConstants = allow;
@@ -464,7 +476,7 @@ public JobDescription getJobDescription() {
464476
copyrightNotice, outputFormat, inputOrientation, outputOrientation,
465477
optimize, trueConditionNames, useInternalBidiFlipper, swapLtrRtlInUrl,
466478
swapLeftRightInUrl, simplifyCss, eliminateDeadStyles,
467-
allowDefPropagation, allowUnrecognizedFunctions, allowedNonStandardFunctions,
479+
allowDefPropagation, allowDuplicateDeclarations, allowUnrecognizedFunctions, allowedNonStandardFunctions,
468480
allowUnrecognizedProperties, allowedUnrecognizedProperties, allowUndefinedConstants,
469481
allowMozDocument, vendor,
470482
allowKeyframes, allowWebkitKeyframes, processDependencies,

src/com/google/common/css/compiler/commandline/ClosureCommandLineCompiler.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ static class Flags {
149149
+ " from one file to propagate to other files.")
150150
private boolean allowDefPropagation = true;
151151

152+
@Option(name = "--allow-duplicate-declarations", usage = "Allow duplicate"
153+
+ " declarations without needing @alternate")
154+
private boolean allowDuplicateDeclarations = false;
155+
152156
@Option(name = "--allow-unrecognized-functions", usage =
153157
"Allow unrecognized functions.")
154158
private boolean allowUnrecognizedFunctions = false;
@@ -222,6 +226,7 @@ JobDescription createJobDescription() {
222226
builder.setCopyrightNotice(copyrightNotice);
223227
builder.setTrueConditionNames(trueConditions);
224228
builder.setAllowDefPropagation(allowDefPropagation);
229+
builder.setAllowDuplicateDeclarations(allowDuplicateDeclarations);
225230
builder.setAllowUnrecognizedFunctions(allowUnrecognizedFunctions);
226231
builder.setAllowedNonStandardFunctions(allowedNonStandardFunctions);
227232
builder.setAllowedUnrecognizedProperties(allowedUnrecognizedProperties);

src/com/google/common/css/compiler/passes/PassRunner.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,11 @@ public void runPasses(CssTree cssTree) {
144144
cssTree.getMutatingVisitController()).runPass();
145145
}
146146
if (job.eliminateDeadStyles) {
147-
// Report errors for duplicate declarations
148-
new DisallowDuplicateDeclarations(
149-
cssTree.getVisitController(), errorManager).runPass();
147+
if (!job.allowDuplicateDeclarations) {
148+
// Report errors for duplicate declarations
149+
new DisallowDuplicateDeclarations(
150+
cssTree.getVisitController(), errorManager).runPass();
151+
}
150152
// Split rules by selector and declaration.
151153
new SplitRulesetNodes(cssTree.getMutatingVisitController()).runPass();
152154
// Dead code elimination.

tests/com/google/common/css/compiler/commandline/ClosureCommandLineCompilerTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ public void testAllowDefPropagationDefaultsToTrue() throws Exception {
6868
assertTrue(jobDescription.allowDefPropagation);
6969
}
7070

71+
public void testAllowDuplicateDeclarationsDefaultsToFalse() throws Exception {
72+
ClosureCommandLineCompiler.Flags flags =
73+
ClosureCommandLineCompiler.parseArgs(new String[] {"/dev/null"}, EXIT_CODE_HANDLER);
74+
JobDescription jobDescription = flags.createJobDescription();
75+
assertFalse(jobDescription.allowDuplicateDeclarations);
76+
}
77+
7178

7279
public void testEmptyImportBlocks() throws Exception {
7380
// See b/29995881

0 commit comments

Comments
 (0)