Skip to content

Commit

Permalink
Fix the bug that jscompiler does not respect relative order of --js t…
Browse files Browse the repository at this point in the history
…o --zip inputs.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=112285511
  • Loading branch information
nanj01 authored and blickly committed Jan 16, 2016
1 parent 0f54fd1 commit 62ca536
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 122 deletions.
161 changes: 84 additions & 77 deletions src/com/google/javascript/jscomp/AbstractCommandLineRunner.java
Expand Up @@ -323,18 +323,17 @@ protected void setRunOptions(CompilerOptions options)
DiagnosticGroups diagnosticGroups = getDiagnosticGroups(); DiagnosticGroups diagnosticGroups = getDiagnosticGroups();


if (config.warningGuards != null) { if (config.warningGuards != null) {
for (WarningGuardSpec.Entry entry : config.warningGuards.entries) { for (FlagEntry<CheckLevel> entry : config.warningGuards) {
if ("*".equals(entry.groupName)) { if ("*".equals(entry.value)) {
Set<String> groupNames = Set<String> groupNames =
diagnosticGroups.getRegisteredGroups().keySet(); diagnosticGroups.getRegisteredGroups().keySet();
for (String groupName : groupNames) { for (String groupName : groupNames) {
if (!DiagnosticGroups.wildcardExcludedGroups.contains(groupName)) { if (!DiagnosticGroups.wildcardExcludedGroups.contains(groupName)) {
diagnosticGroups.setWarningLevel(options, groupName, entry.level); diagnosticGroups.setWarningLevel(options, groupName, entry.flag);
} }
} }
} else { } else {
diagnosticGroups.setWarningLevel(options, entry.groupName, diagnosticGroups.setWarningLevel(options, entry.value, entry.flag);
entry.level);
} }
} }
} }
Expand Down Expand Up @@ -608,14 +607,14 @@ public List<JsonFileSpec> parseJsonFilesFromInputStream() throws IOException {
* Can be overridden by subclasses who want to pull files from different * Can be overridden by subclasses who want to pull files from different
* places. * places.
* *
* @param files A list of filenames * @param files A list of flag entries indicates js and zip file names.
* @param allowStdIn Whether '-' is allowed appear as a filename to represent * @param allowStdIn Whether '-' is allowed appear as a filename to represent
* stdin. If true, '-' is only allowed to appear once. * stdin. If true, '-' is only allowed to appear once.
* @return An array of inputs * @return An array of inputs
*/ */
protected List<SourceFile> createInputs(List<String> files, protected List<SourceFile> createInputs(List<FlagEntry<JsSourceType>> files,
boolean allowStdIn) throws FlagUsageException, IOException { boolean allowStdIn) throws FlagUsageException, IOException {
return createInputs(files, new ArrayList<String>() /* zips */, allowStdIn); return createInputs(files, null /* jsonFiles */, allowStdIn);
} }


/** /**
Expand All @@ -624,30 +623,13 @@ protected List<SourceFile> createInputs(List<String> files,
* Can be overridden by subclasses who want to pull files from different * Can be overridden by subclasses who want to pull files from different
* places. * places.
* *
* @param files A list of filenames. * @param files A list of flag entries indicates js and zip file names.
* @param jsonFiles A list of json encoded files. * @param jsonFiles A list of json encoded files.
* @return An array of inputs * @return An array of inputs
*/ */
protected List<SourceFile> createInputs(List<String> files, protected List<SourceFile> createInputs(List<FlagEntry<JsSourceType>> files,
List<JsonFileSpec> jsonFiles) throws FlagUsageException, IOException { List<JsonFileSpec> jsonFiles) throws FlagUsageException, IOException {
return createInputs(files, new ArrayList<String>() /* zips */, jsonFiles, false); return createInputs(files, jsonFiles, false);
}

/**
* Creates inputs from a list of source files and zips.
*
* Can be overridden by subclasses who want to pull files from different
* places.
*
* @param files A list of filenames.
* @param zips A list of zip filenames.
* @param allowStdIn Whether '-' is allowed appear as a filename to represent
* stdin. If true, '-' is only allowed to appear once.
* @return An array of inputs
*/
protected List<SourceFile> createInputs(List<String> files,
List<String> zips, boolean allowStdIn) throws FlagUsageException, IOException {
return createInputs(files, zips, null, allowStdIn);
} }


/** /**
Expand All @@ -656,20 +638,25 @@ protected List<SourceFile> createInputs(List<String> files,
* Can be overridden by subclasses who want to pull files from different * Can be overridden by subclasses who want to pull files from different
* places. * places.
* *
* @param files A list of filenames. * @param files A list of flag entries indicates js and zip file names
* @param zips A list of zip filenames.
* @param jsonFiles A list of json encoded files. * @param jsonFiles A list of json encoded files.
* @param allowStdIn Whether '-' is allowed appear as a filename to represent * @param allowStdIn Whether '-' is allowed appear as a filename to represent
* stdin. If true, '-' is only allowed to appear once. * stdin. If true, '-' is only allowed to appear once.
* @return An array of inputs * @return An array of inputs
*/ */
protected List<SourceFile> createInputs(List<String> files, protected List<SourceFile> createInputs(List<FlagEntry<JsSourceType>> files,
List<String> zips, List<JsonFileSpec> jsonFiles, boolean allowStdIn) List<JsonFileSpec> jsonFiles, boolean allowStdIn)
throws FlagUsageException, IOException { throws FlagUsageException, IOException {
List<SourceFile> inputs = new ArrayList<>(files.size()); List<SourceFile> inputs = new ArrayList<>(files.size());
boolean usingStdin = false; boolean usingStdin = false;
for (String filename : files) { for (FlagEntry<JsSourceType> file : files) {
if (!"-".equals(filename)) { String filename = file.value;
if (file.flag == JsSourceType.JS_ZIP) {
if (!"-".equals(filename)) {
List<SourceFile> newFiles = SourceFile.fromZipFile(filename, inputCharset);
inputs.addAll(newFiles);
}
} else if (!"-".equals(filename)) {
SourceFile newFile = SourceFile.fromFile(filename, inputCharset); SourceFile newFile = SourceFile.fromFile(filename, inputCharset);
inputs.add(newFile); inputs.add(newFile);
} else { } else {
Expand All @@ -694,12 +681,6 @@ protected List<SourceFile> createInputs(List<String> files,
usingStdin = true; usingStdin = true;
} }
} }
for (String zipName : zips) {
if (!"-".equals(zipName)) {
List<SourceFile> newFiles = SourceFile.fromZipFile(zipName, inputCharset);
inputs.addAll(newFiles);
}
}
if (jsonFiles != null) { if (jsonFiles != null) {
for (JsonFileSpec jsonFile : jsonFiles) { for (JsonFileSpec jsonFile : jsonFiles) {
inputs.add(SourceFile.fromCode(jsonFile.getPath(), jsonFile.getSrc())); inputs.add(SourceFile.fromCode(jsonFile.getPath(), jsonFile.getSrc()));
Expand All @@ -711,22 +692,24 @@ protected List<SourceFile> createInputs(List<String> files,
/** /**
* Creates JS source code inputs from a list of files. * Creates JS source code inputs from a list of files.
*/ */
private List<SourceFile> createSourceInputs(List<String> files, List<String> zips, private List<SourceFile> createSourceInputs(
List<FlagEntry<JsSourceType>> files,
List<JsonFileSpec> jsonFiles) List<JsonFileSpec> jsonFiles)
throws FlagUsageException, IOException { throws FlagUsageException, IOException {
if (isInTestMode()) { if (isInTestMode()) {
return inputsSupplierForTesting != null ? inputsSupplierForTesting.get() return inputsSupplierForTesting != null ? inputsSupplierForTesting.get()
: null; : null;
} }
if (files.isEmpty() && zips.isEmpty() && jsonFiles == null) { if (files.isEmpty() && jsonFiles == null) {
// Request to read from stdin. // Request to read from stdin.
files = Collections.singletonList("-"); files = Collections.singletonList(
new FlagEntry<JsSourceType>(JsSourceType.JS, "-"));
} }
try { try {
if (jsonFiles != null) { if (jsonFiles != null) {
return createInputs(files, jsonFiles); return createInputs(files, jsonFiles);
} else { } else {
return createInputs(files, zips, true); return createInputs(files, true);
} }
} catch (FlagUsageException e) { } catch (FlagUsageException e) {
throw new FlagUsageException("Bad --js flag. " + e.getMessage()); throw new FlagUsageException("Bad --js flag. " + e.getMessage());
Expand All @@ -741,8 +724,12 @@ private List<SourceFile> createExternInputs(List<String> files)
if (files.isEmpty()) { if (files.isEmpty()) {
return ImmutableList.of(SourceFile.fromCode("/dev/null", "")); return ImmutableList.of(SourceFile.fromCode("/dev/null", ""));
} }
List<FlagEntry<JsSourceType>> externFiles = new ArrayList<>();
for (String file : files) {
externFiles.add(new FlagEntry<JsSourceType>(JsSourceType.EXTERN, file));
}
try { try {
return createInputs(files, false); return createInputs(externFiles, false);
} catch (FlagUsageException e) { } catch (FlagUsageException e) {
throw new FlagUsageException("Bad --externs flag. " + e.getMessage()); throw new FlagUsageException("Bad --externs flag. " + e.getMessage());
} }
Expand Down Expand Up @@ -1060,7 +1047,6 @@ protected int doRun() throws FlagUsageException, IOException {
outputFileNames.add(config.jsOutputFile); outputFileNames.add(config.jsOutputFile);
} }


List<String> jsFiles = config.js;
List<String> moduleSpecs = config.module; List<String> moduleSpecs = config.module;
List<JsonFileSpec> jsonFiles = null; List<JsonFileSpec> jsonFiles = null;


Expand Down Expand Up @@ -1094,7 +1080,9 @@ protected int doRun() throws FlagUsageException, IOException {
createCommonJsModules = true; createCommonJsModules = true;
moduleSpecs.remove(0); moduleSpecs.remove(0);
} }
List<SourceFile> inputs = createSourceInputs(jsFiles, config.jsZip, jsonFiles);
List<SourceFile> inputs =
createSourceInputs(config.mixedJsSources, jsonFiles);
if (!moduleSpecs.isEmpty()) { if (!moduleSpecs.isEmpty()) {
modules = createJsModules(moduleSpecs, inputs); modules = createJsModules(moduleSpecs, inputs);
for (JSModule m : modules) { for (JSModule m : modules) {
Expand Down Expand Up @@ -2028,6 +2016,20 @@ CommandLineConfig setJsZip(List<String> zip) {
return this; return this;
} }


private final List<FlagEntry<JsSourceType>> mixedJsSources =
new ArrayList<>();

/**
* The JavaScript source file names, including .js and .zip files. You may
* specify multiple.
*/
CommandLineConfig setMixedJsSources(
List<FlagEntry<JsSourceType>> mixedJsSources) {
this.mixedJsSources.clear();
this.mixedJsSources.addAll(mixedJsSources);
return this;
}

private String jsOutputFile = ""; private String jsOutputFile = "";


/** /**
Expand Down Expand Up @@ -2228,13 +2230,14 @@ CommandLineConfig setSourceMapLocationMappings(
return this; return this;
} }


private WarningGuardSpec warningGuards = null; private ArrayList<FlagEntry<CheckLevel>> warningGuards = new ArrayList<>();


/** /**
* Add warning guards. * Add warning guards.
*/ */
CommandLineConfig setWarningGuardSpec(WarningGuardSpec spec) { CommandLineConfig setWarningGuards(List<FlagEntry<CheckLevel>> warningGuards) {
this.warningGuards = spec; this.warningGuards.clear();
this.warningGuards.addAll(warningGuards);
return this; return this;
} }


Expand Down Expand Up @@ -2488,33 +2491,6 @@ CommandLineConfig setJsonStreamMode(JsonStreamMode mode) {


} }


/**
* A little helper class to make it easier to collect warning types
* from --jscomp_error, --jscomp_warning, and --jscomp_off.
*/
protected static class WarningGuardSpec {
private static class Entry {
private final CheckLevel level;
private final String groupName;

private Entry(CheckLevel level, String groupName) {
this.level = level;
this.groupName = groupName;
}
}

// The entries, in the order that they were added.
private final List<Entry> entries = new ArrayList<>();

protected void add(CheckLevel level, String groupName) {
entries.add(new Entry(level, groupName));
}

protected void clear() {
entries.clear();
}
}

/** /**
* Representation of a source file from an encoded json stream input * Representation of a source file from an encoded json stream input
*/ */
Expand Down Expand Up @@ -2547,4 +2523,35 @@ public void setSourceMap(String map) {
this.source_map = map; this.source_map = map;
} }
} }

/**
* Flag types for js source files.
*/
protected enum JsSourceType {
EXTERN("extern"),
JS("js"),
JS_ZIP("jszip");

@VisibleForTesting
final String flagName;

private JsSourceType(String flagName) {
this.flagName = flagName;
}
}

/**
* A pair from flag to its value.
*/
protected static class FlagEntry<T> {
@VisibleForTesting
final T flag;
@VisibleForTesting
final String value;

protected FlagEntry(T flag, String value) {
this.flag = flag;
this.value = value;
}
}
} }

0 comments on commit 62ca536

Please sign in to comment.