Skip to content

Commit

Permalink
project/r styler: process warnings & errors
Browse files Browse the repository at this point in the history
  • Loading branch information
haraldschilly committed Jan 24, 2019
1 parent 1c85a76 commit 32b8755
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 50 deletions.
54 changes: 29 additions & 25 deletions src/smc-project/formatters/python-format.ts
@@ -1,4 +1,4 @@
const { writeFile, readFile } = require("fs");
const { writeFile, readFile, unlink } = require("fs");
const tmp = require("tmp");
const { callback } = require("awaiting");
const { spawn } = require("child_process");
Expand Down Expand Up @@ -39,32 +39,36 @@ export async function python_format(
): Promise<string> {
// create input temp file
const input_path: string = await callback(tmp.file);
await callback(writeFile, input_path, input);
try {
await callback(writeFile, input_path, input);

// spawn the python formatter
const util = options.util || "yapf";
const py_formatter = yapf(input_path);
// spawn the python formatter
const util = options.util || "yapf";
const py_formatter = yapf(input_path);

// stdout/err capture
let stdout: string = "";
let stderr: string = "";
// read data as it is produced.
py_formatter.stdout.on("data", data => (stdout += data.toString()));
py_formatter.stderr.on("data", data => (stderr += data.toString()));
// wait for subprocess to close.
let code = await callback(close, py_formatter);
// only last line
// stdout = last_line(stdout);
stderr = last_line(stderr);
if (code) {
const err_msg = `Python formatter "${util}" exited with code ${code}:\n${stdout}\n${stderr}`;
logger.debug(`format python error: ${err_msg}`);
throw new Error(err_msg);
}
// stdout/err capture
let stdout: string = "";
let stderr: string = "";
// read data as it is produced.
py_formatter.stdout.on("data", data => (stdout += data.toString()));
py_formatter.stderr.on("data", data => (stderr += data.toString()));
// wait for subprocess to close.
let code = await callback(close, py_formatter);
// only last line
// stdout = last_line(stdout);
stderr = last_line(stderr);
if (code) {
const err_msg = `Python formatter "${util}" exited with code ${code}:\n${stdout}\n${stderr}`;
logger.debug(`format python error: ${err_msg}`);
throw new Error(err_msg);
}

// all fine, we read from the temp file
let output: Buffer = await callback(readFile, input_path);
let s: string = output.toString("utf-8");
// all fine, we read from the temp file
let output: Buffer = await callback(readFile, input_path);
let s: string = output.toString("utf-8");

return s;
return s;
} finally {
unlink(input_path);
}
}
79 changes: 54 additions & 25 deletions src/smc-project/formatters/r-format.ts
@@ -1,12 +1,14 @@
const { writeFile, readFile } = require("fs");
const { writeFile, readFile, unlink } = require("fs");
const tmp = require("tmp");
const { callback } = require("awaiting");
const { spawn } = require("child_process");
// const { replace_all } = require("smc-util/misc");

type Variant = "styler" | "formatR";

interface ParserOptions {
parser?: string;
variant?: "styler" | "formatR";
variant?: Variant;
tabWidth?: number;
lineWidth?: number;
}
Expand All @@ -15,10 +17,9 @@ function close(proc, cb): void {
proc.on("close", code => cb(undefined, code));
}

function formatR(input_path: string, variant) {
function formatR(input_path: string, variant: Variant) {
// in-place is fine, according to my tests
let expr: string | undefined;
variant = variant ? variant : "styler";
if (variant == "formatR") {
expr = `suppressMessages(require(formatR)); tidy_source(source="${input_path}", file="${input_path}", indent=2, width.cutoff=80)`;
} else if (variant == "styler") {
Expand All @@ -31,6 +32,26 @@ function formatR(input_path: string, variant) {
}
}

/** styler error example
*
* > styler::style_file("some-code.r")
* Styling 1 files:
* some-code.r ⚠
* ────────────────────────────────────────
* Status Count Legend
* ✔ 0 File unchanged.
* ℹ 0 File changed.
* ✖ 1 Styling threw an error.
* ────────────────────────────────────────
* Warning message:
* When processing some-code.r: <text>:7:17: unexpected '=='
* 6: for (i in xx) {
* 7: if (z %% 2) ==
* ^
* >
* >
*/

export async function r_format(
input: string,
options: ParserOptions,
Expand All @@ -39,28 +60,36 @@ export async function r_format(
): Promise<string> {
// create input temp file
const input_path: string = await callback(tmp.file, { postfix: `.${ext}` });
await callback(writeFile, input_path, input);
try {
await callback(writeFile, input_path, input);
const variant: Variant = options.variant ? options.variant : "styler";

// spawn the R formatter
const r_formatter = formatR(input_path, options.variant);
// spawn the R formatter
const r_formatter = formatR(input_path, variant);
// stdout/err capture
const stdout: string[] = [];
const stderr: string[] = [];
// read data as it is produced.
r_formatter.stdout.on("data", data => stdout.push(data.toString()));
r_formatter.stderr.on("data", data => stderr.push(data.toString()));
// wait for subprocess to close.
let code = await callback(close, r_formatter);
// special case: return code 0 but warnings are errors, which we want to report back
if (variant == "styler" && stderr.length > 0) {
throw new Error(stderr.slice(1).join("\n"));
}
if (code) {
const err_msg = stderr.join("\n");
logger.debug(`R_FORMAT ${err_msg}`);
throw new Error(err_msg);
}

// stdout/err capture
let stdout: string = "";
let stderr: string = "";
// read data as it is produced.
r_formatter.stdout.on("data", data => (stdout += data.toString()));
r_formatter.stderr.on("data", data => (stderr += data.toString()));
// wait for subprocess to close.
let code = await callback(close, r_formatter);
if (code) {
const err_msg = `${stderr}`;
logger.debug(`R_FORMAT ${err_msg}`);
throw new Error(err_msg);
}
// all fine, we read from the temp file
let output: Buffer = await callback(readFile, input_path);
let s: string = output.toString("utf-8");

// all fine, we read from the temp file
let output: Buffer = await callback(readFile, input_path);
let s: string = output.toString("utf-8");

return s;
return s;
} finally {
unlink(input_path);
}
}

0 comments on commit 32b8755

Please sign in to comment.