Skip to content

Commit

Permalink
Don't record duplicate licenses within a file
Browse files Browse the repository at this point in the history
fixes #1848

Closes #2245

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=144867412
  • Loading branch information
alexeagle authored and blickly committed Jan 19, 2017
1 parent d05cda8 commit 48a459f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/Compiler.java
Expand Up @@ -384,7 +384,7 @@ protected void reconcileOptionsWithGuards() {
// But we do not want to see the warnings from OTI.
if (options.getNewTypeInference()) {
options.checkTypes = true;
// Supress warnings from the const checks of CheckAccessControls so as to avoid
// Suppress warnings from the const checks of CheckAccessControls so as to avoid
// duplication.
options.setWarningLevel(DiagnosticGroups.ACCESS_CONTROLS_CONST, CheckLevel.OFF);
if (!options.reportOTIErrorsUnderNTI) {
Expand Down
10 changes: 10 additions & 0 deletions src/com/google/javascript/rhino/JSDocInfoBuilder.java
Expand Up @@ -41,6 +41,7 @@

import com.google.common.base.Preconditions;
import com.google.javascript.rhino.JSDocInfo.Visibility;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;
Expand All @@ -66,6 +67,9 @@ public final class JSDocInfoBuilder {
// the current marker, if any.
private JSDocInfo.Marker currentMarker;

// the set of unique license texts
private final Set<String> licenseTexts;

public JSDocInfoBuilder(boolean parseDocumentation) {
this(new JSDocInfo(parseDocumentation), parseDocumentation, false);
}
Expand All @@ -75,6 +79,7 @@ private JSDocInfoBuilder(
this.currentInfo = info;
this.parseDocumentation = parseDocumentation;
this.populated = populated;
this.licenseTexts = new HashSet<>();
}

public static JSDocInfoBuilder copyFrom(JSDocInfo info) {
Expand Down Expand Up @@ -814,10 +819,15 @@ public boolean recordLicense(String license) {
}

public boolean addLicense(String license) {
if (!licenseTexts.add(license)) {
return false;
}

String txt = currentInfo.getLicense();
if (txt == null) {
txt = "";
}

currentInfo.setLicense(txt + license);
populated = true;
return true;
Expand Down
12 changes: 8 additions & 4 deletions test/com/google/javascript/jscomp/CompilerTest.java
Expand Up @@ -492,7 +492,7 @@ public void testMultipleUniqueImportantComments() throws Exception {
assertEquals(expected, compiler.toSource());
}

public void testMultipleIndenticalImportantComments() throws Exception {
public void testMultipleIdenticalImportantComments() throws Exception {
String js1 = "/*! Identical license here */\n" + "var x;";
String js2 = "/*! Identical license here */\n" + "var y;";
String expected = "/*\n Identical license here */\n";
Expand Down Expand Up @@ -618,25 +618,29 @@ public void testMultipleUniqueLicenses() throws Exception {
assertEquals(expected, compiler.toSource());
}

public void testMultipleIndenticalLicenses() throws Exception {
public void testMultipleIdenticalLicenses() throws Exception {
String js1 = "/** @license Identical license here */\n"
+ "var x;";
String js2 = "/** @license Identical license here */\n"
+ "var y;";
String js3 = "/** @license Identical license here */\n"
+ "var z;\n"
+ "/** @license Identical license here */";
String expected = "/*\n Identical license here */\n";

Compiler compiler = new Compiler();
CompilerOptions options = createNewFlagBasedOptions();
List<SourceFile> inputs = ImmutableList.of(
SourceFile.fromCode("testcode1", js1),
SourceFile.fromCode("testcode2", js2));
SourceFile.fromCode("testcode2", js2),
SourceFile.fromCode("bundled", js3));
Result result = compiler.compile(EMPTY_EXTERNS, inputs, options);

assertTrue(Joiner.on(",").join(result.errors), result.success);
assertEquals(expected, compiler.toSource());
}

public void testIndenticalLicenseAndImportantComent() throws Exception {
public void testIdenticalLicenseAndImportantComment() throws Exception {
String js1 = "/** @license Identical license here */\n" + "var x;";
String js2 = "/*! Identical license here */\n" + "var y;";
String expected = "/*\n Identical license here */\n";
Expand Down

0 comments on commit 48a459f

Please sign in to comment.