Skip to content

Commit

Permalink
Added option to treat errors as warnings and added option to return e…
Browse files Browse the repository at this point in the history
…rror code when any error is encountered. Should solve #3.
  • Loading branch information
SkaceKamen committed Nov 10, 2016
1 parent d234427 commit 7077016
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/cz/zipek/sqflint/SQFLint.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cz.zipek.sqflint;

import cz.zipek.sqflint.linter.Linter;
import cz.zipek.sqflint.preprocessor.SQFPreprocessor;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
Expand Down Expand Up @@ -29,6 +28,8 @@ public static void main(String[] args) {
options.addOption("v", "variables", false, "output variables info (only in json mode)");
options.addOption("e", "error", false, "stop on error");
options.addOption("nw", "no-warning", false, "skip warnings");
options.addOption("we", "warning-as-error", false, "output warnings as errors");
options.addOption("oc", "output-code", false, "output ERR return code when any error is encountered");
options.addOption("h", "help", false, "");

try {
Expand Down Expand Up @@ -63,9 +64,11 @@ public static void main(String[] args) {
linter.setSkipWarnings(cmd.hasOption("nw"));
linter.setJsonOutput(cmd.hasOption("j"));
linter.setOutputVariables(cmd.hasOption("v"));
linter.setExitCodeEnabled(cmd.hasOption("oc"));
linter.setWarningAsError(cmd.hasOption("we"));

try {
linter.start();
System.exit(linter.start());
} catch (IOException ex) {
Logger.getLogger(SQFLint.class.getName()).log(Level.SEVERE, null, ex);
}
Expand Down
34 changes: 32 additions & 2 deletions src/cz/zipek/sqflint/linter/Linter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@
* @author Jan Zípek <jan at zipek.cz>
*/
public class Linter extends SQFParser {
public static final int CODE_OK = 0;
public static final int CODE_ERR = 1;

private boolean stopOnError = false;
private boolean skipWarnings = false;
private boolean jsonOutput = false;
private boolean outputVariables = false;
private boolean exitCodeEnabled = false;
private boolean warningAsError = false;

private final Map<String, SQFCommand> commands = new HashMap<>();
private final Set<String> ignoredVariables = new HashSet<>();
Expand All @@ -46,7 +51,7 @@ public Linter(InputStream stream) {
ignoredVariables.addAll(Arrays.asList(new String[] { "_this", "_x", "_foreachindex" }));
}

public void start() throws IOException {
public int start() throws IOException {
if (jsonOutput)
setTabSize(1);

Expand All @@ -72,6 +77,13 @@ public void start() throws IOException {

out.print(this);
}

// Always return OK if exit code is disabled
if (!exitCodeEnabled)
return CODE_OK;

// Return ERR code when any error was encountered
return (getErrors().size() > 0) ? CODE_ERR : CODE_OK;
}

protected void postParse() {
Expand All @@ -82,7 +94,11 @@ protected void postParse() {
SQFVariable var = entry.getValue();
if (var.isLocal() && var.definitions.isEmpty()) {
var.usage.stream().forEach((u) -> {
getWarnings().add(new Warning(u, "Possibly undefined variable " + u));
if (warningAsError) {
getErrors().add(new SQFParseException(u, "Possibly undefined variable " + u));
} else {
getWarnings().add(new Warning(u, "Possibly undefined variable " + u));
}
});
}
});
Expand Down Expand Up @@ -294,4 +310,18 @@ public List<SQFInclude> getIncludes() {
public List<SQFMacro> getMacros() {
return macros;
}

/**
* @param exitCodeEnabled the exitCodeEnabled to set
*/
public void setExitCodeEnabled(boolean exitCodeEnabled) {
this.exitCodeEnabled = exitCodeEnabled;
}

/**
* @param warningAsError the warningAsError to set
*/
public void setWarningAsError(boolean warningAsError) {
this.warningAsError = warningAsError;
}
}

0 comments on commit 7077016

Please sign in to comment.