Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check formatting #5760

Merged
merged 2 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
> - :house: [Internal]
> - :nail_care: [Polish]

# 10.1.0-rc.4

#### :rocket: New Feature

- Support format check with `rescript format -check`. https://github.com/rescript-lang/rescript-compiler/pull/5760

#### :bug: Bug Fix

- Fix issue where the last line of `rescript format --help` usage was being swallowed https://github.com/rescript-lang/rescript-compiler/pull/5760

# 10.1.0-rc.3

#### :rocket: New Feature
Expand Down
6 changes: 2 additions & 4 deletions scripts/rescript_arg.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function bad_arg(s) {
/**
* @typedef {{val : string}} stringref
* @typedef {{val : boolean}} boolref
* @typedef {{kind:"Unit_call",data : ()=>void } | {kind : "Unit_set", data : boolref}}unit_action
* @typedef {{kind:"Unit_call",data : ()=>void } | {kind : "Unit_set", data : boolref}} unit_action
* @typedef {{kind:"String_call",data:(s : string)=>void} | {kind : "String_set",data: stringref}} string_action
* @typedef {{kind:"Unit",data : unit_action } | {kind:"String", data: string_action}} action
* @typedef {Array<[string,action,string]>} specs
Expand Down Expand Up @@ -67,9 +67,7 @@ function usage_b(b, usage, specs) {
cur = i + 1;
}
}
if (i < specs.length - 1) {
b.add("\n");
}
b.add("\n");
}
}
}
Expand Down
102 changes: 63 additions & 39 deletions scripts/rescript_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ var format_usage = `Usage: rescript format <options> [files]
\`rescript format\` formats the current directory
`;
var child_process = require("child_process");
var util = require("node:util");
var asyncExecFile = util.promisify(child_process.execFile);
var path = require("path");
var fs = require("fs");
var asyncFs = fs.promises;
/**
* @type {arg.stringref}
*/
Expand All @@ -17,6 +20,11 @@ var stdin = { val: undefined };
*/
var format = { val: undefined };

/**
* @type {arg.boolref}
*/
var check = { val: undefined };

/**
* @type{arg.specs}
*/
Expand All @@ -27,12 +35,16 @@ var specs = [
`[.res|.resi|.ml|.mli] Read the code from stdin and print
the formatted code to stdout in ReScript syntax`,
],
// ml|mli
[
"-all",
{ kind: "Unit", data: { kind: "Unit_set", data: format } },
"Format the whole project ",
],
[
"-check",
{ kind: "Unit", data: { kind: "Unit_set", data: check } },
"Check formatting only",
],
];
var formattedStdExtensions = [".res", ".resi", ".ml", ".mli"];
var formattedFileExtensions = [".res", ".resi"];
Expand All @@ -55,12 +67,55 @@ async function readStdin() {
return Buffer.concat(chunks).toString("utf8");
}

/**
* @param {string[]} files
* @param {string} bsc_exe
* @param {(x: string) => boolean} isSupportedFile
* @param {boolean} checkFormatting
*/
async function formatFiles(files, bsc_exe, isSupportedFile, checkFormatting) {
var incorrectlyFormattedFiles = 0;
try {
const _promises = await Promise.all(
files.map(async file => {
if (isSupportedFile(file)) {
const flags = checkFormatting
? ["-format", file]
: ["-o", file, "-format", file];
const { stdout } = await asyncExecFile(bsc_exe, flags);
if (check.val) {
const original = await asyncFs.readFile(file, "utf-8");
if (original != stdout) {
console.error("[format check]", file);
incorrectlyFormattedFiles++;
}
}
}
return null;
})
);
} catch (err) {
console.error(err);
process.exit(2);
}
if (incorrectlyFormattedFiles > 0) {
if (incorrectlyFormattedFiles == 1) {
console.error("The file listed above needs formatting");
} else {
console.error(
`The ${incorrectlyFormattedFiles} files listed above need formatting`
);
}
process.exit(3);
}
}

/**
* @param {string[]} argv
* @param {string} rescript_exe
* @param {string} bsc_exe
*/
function main(argv, rescript_exe, bsc_exe) {
async function main(argv, rescript_exe, bsc_exe) {
var isSupportedFile = hasExtension(formattedFileExtensions);
var isSupportedStd = hasExtension(formattedStdExtensions);

Expand Down Expand Up @@ -95,26 +150,12 @@ function main(argv, rescript_exe, bsc_exe) {
process.exit(2);
}
files = output.stdout.split("\n").map(x => x.trim());
var hasError = false;
for (let arg of files) {
if (isSupportedFile(arg)) {
// console.log(`processing ${arg}`);
child_process.execFile(
bsc_exe,
["-o", arg, "-format", arg],
(error, _stdout, stderr) => {
if (error !== null) {
console.error(stderr);
hasError = true;
}
}
);
}
}
if (hasError) {
await formatFiles(files, bsc_exe, isSupportedFile, check.val);
} else if (use_stdin) {
if (check.val) {
console.error("format -stdin cannot be used with -check flag");
process.exit(2);
}
} else if (use_stdin) {
if (isSupportedStd(use_stdin)) {
var crypto = require("crypto");
var os = require("os");
Expand Down Expand Up @@ -144,7 +185,7 @@ function main(argv, rescript_exe, bsc_exe) {
);
})();
} else {
console.error(`Unsupported exetnsion ${use_stdin}`);
console.error(`Unsupported extension ${use_stdin}`);
console.error(`Supported extensions: ${formattedStdExtensions} `);
process.exit(2);
}
Expand All @@ -163,24 +204,7 @@ function main(argv, rescript_exe, bsc_exe) {
process.exit(2);
}
}
var hasError = false;
files.forEach(file => {
var write = isSupportedFile(file);
var flags = write ? ["-o", file, "-format", file] : ["-format", file];
child_process.execFile(bsc_exe, flags, (error, stdout, stderr) => {
if (error === null) {
if (!write) {
process.stdout.write(stdout);
}
} else {
console.error(stderr);
hasError = true;
}
});
});
if (hasError) {
process.exit(2);
}
await formatFiles(files, bsc_exe, isSupportedFile, check.val);
}
} catch (e) {
if (e instanceof arg.ArgError) {
Expand Down