From a94097940a2af4f9902f849e0b080ff20981c8e9 Mon Sep 17 00:00:00 2001 From: Jordan Suchow Date: Thu, 31 Mar 2016 00:04:14 -0700 Subject: [PATCH 1/2] Fix off-by-one error in JSON output --- proselint/command_line.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/proselint/command_line.py b/proselint/command_line.py index d417a2ed8..ab7e8d001 100644 --- a/proselint/command_line.py +++ b/proselint/command_line.py @@ -121,10 +121,10 @@ def show_errors(filename, errors, output_json=False, compact=False): out.append({ "check": e[0], "message": e[1], - "line": e[2], - "column": e[3], - "start": e[4], - "end": e[5], + "line": 1 + e[2], + "column": 1 + e[3], + "start": 1 + e[4], + "end": 1 + e[5], "extent": e[6], "severity": e[7], "replacements": e[8], From 4d3fe2cf333e7ca5afc87fffd3dbb50f15f942b0 Mon Sep 17 00:00:00 2001 From: Jordan Suchow Date: Thu, 31 Mar 2016 00:16:31 -0700 Subject: [PATCH 2/2] Refactor into JSON formatter --- proselint/command_line.py | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/proselint/command_line.py b/proselint/command_line.py index ab7e8d001..41b5bc615 100644 --- a/proselint/command_line.py +++ b/proselint/command_line.py @@ -113,27 +113,29 @@ def clear_cache(): shell=True) +def errors_to_json(errors): + """Convert the errors to JSON.""" + out = [] + for e in errors: + out.append({ + "check": e[0], + "message": e[1], + "line": 1 + e[2], + "column": 1 + e[3], + "start": 1 + e[4], + "end": 1 + e[5], + "extent": e[6], + "severity": e[7], + "replacements": e[8], + }) + + return json.dumps(dict(status="success", data={"errors": out})) + + def show_errors(filename, errors, output_json=False, compact=False): """Print the errors, resulting from lint, for filename.""" if output_json: - out = [] - for e in errors: - out.append({ - "check": e[0], - "message": e[1], - "line": 1 + e[2], - "column": 1 + e[3], - "start": 1 + e[4], - "end": 1 + e[5], - "extent": e[6], - "severity": e[7], - "replacements": e[8], - }) - result = dict( - status="success", - data={"errors": out}) - - click.echo(json.dumps(result)) + click.echo(errors_to_json(errors)) else: for error in errors: