Skip to content

Commit

Permalink
Support reading files from standard input
Browse files Browse the repository at this point in the history
Fixes quick-lint#263
Updates quick-lint#275

Signed-off-by: wagner riffel <[email protected]>
  • Loading branch information
wgrr committed May 12, 2021
1 parent dac5b6a commit 7f767f9
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ read_file_result read_file(const char *path) {
windows_handle_file file(handle);
return read_file(path, file.ref());
}

read_file_result read_stdin() {
windows_handle_file file(::GetStdHandle(STD_INPUT_HANDLE));
return read_file("<stdin>", file.ref());
}
#endif

#if defined(QLJS_FILE_POSIX)
Expand All @@ -224,6 +229,11 @@ read_file_result read_file(const char *path) {
posix_fd_file file(fd);
return read_file(path, file.ref());
}

read_file_result read_stdin() {
posix_fd_file file(STDIN_FILENO);
return read_file("<stdin>", file.ref());
}
#endif

void write_file(const std::string &path, string8_view content) {
Expand Down
10 changes: 9 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,20 @@ void handle_options(quick_lint_js::options o) {
quick_lint_js::run_lsp_server();
std::exit(EXIT_SUCCESS);
}
if (o.files_to_lint.empty()) {
if (o.files_to_lint.empty() && !o.stdin) {
std::cerr << "error: expected file name\n";
std::exit(EXIT_FAILURE);
}

quick_lint_js::any_error_reporter reporter =
quick_lint_js::any_error_reporter::make(o.output_format, &o.exit_fail_on);
if (o.stdin) {
quick_lint_js::read_file_result source = quick_lint_js::read_stdin();
file_to_lint fstdin{.path = "<stdin>", .vim_bufnr = std::nullopt};
reporter.set_source(&source.content, fstdin);
quick_lint_js::process_file(&source.content, reporter.get(),
o.print_parser_visits);
}
for (const quick_lint_js::file_to_lint &file : o.files_to_lint) {
quick_lint_js::read_file_result source =
quick_lint_js::read_file(file.path);
Expand Down Expand Up @@ -394,6 +401,7 @@ void print_help_message() {
print_option("--exit-fail-on=[CODES]",
"Fail with a non-zero exit code if any of these");
print_option("", "errors are found (default: \"all\")");
print_option("--s, --stdin", "Read standard input as a javascript file");
print_option("--lsp, --lsp-server",
"Run in Language Server mode (for LSP-aware editors)");
print_option("--output-format=[FORMAT]",
Expand Down
10 changes: 10 additions & 0 deletions src/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ class arg_parser {
this->is_ignoring_options_ = true;
this->option_ = std::nullopt;
} else if (this->current_arg()[0] == '-') {
if (std::strcmp(this->current_arg(), "-") == 0) {
this->option_ = std::nullopt;
return;
}
const char* equal = std::strchr(this->current_arg(), '=');
option o;
o.arg_has_equal = equal != nullptr;
Expand Down Expand Up @@ -152,6 +156,10 @@ options parse_options(int argc, char** argv) {
arg_parser parser(argc, argv);
while (!parser.done()) {
if (const char* argument = parser.match_argument()) {
if (std::strcmp(argument, "-") == 0) {
o.stdin = true;
continue;
}
file_to_lint file{.path = argument, .vim_bufnr = next_vim_file_bufnr};
o.files_to_lint.emplace_back(file);
next_vim_file_bufnr = std::nullopt;
Expand Down Expand Up @@ -188,6 +196,8 @@ options parse_options(int argc, char** argv) {
o.version = true;
} else if (parser.match_flag_option("--lsp-server"sv, "--lsp"sv)) {
o.lsp_server = true;
} else if (parser.match_flag_option("--stdin"sv, "--s"sv)) {
o.stdin = true;
} else {
const char* unrecognized = parser.match_anything();
o.error_unrecognized_options.emplace_back(unrecognized);
Expand Down
2 changes: 2 additions & 0 deletions src/quick-lint-js/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ read_file_result read_file(const char *path);

read_file_result read_file(const char *path, platform_file_ref);

read_file_result read_stdin(void);

void write_file(const std::string &path, string8_view content);
void write_file(const char *path, string8_view content);
}
Expand Down
1 change: 1 addition & 0 deletions src/quick-lint-js/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct options {
bool version = false;
bool print_parser_visits = false;
bool lsp_server = false;
bool stdin = false;
quick_lint_js::output_format output_format =
quick_lint_js::output_format::default_format;
std::vector<file_to_lint> files_to_lint;
Expand Down
20 changes: 20 additions & 0 deletions test/test-options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ TEST(test_options, default_options_with_no_files) {
EXPECT_FALSE(o.help);
EXPECT_FALSE(o.version);
EXPECT_FALSE(o.lsp_server);
EXPECT_FALSE(o.stdin);
EXPECT_EQ(o.output_format, output_format::default_format);
EXPECT_THAT(o.files_to_lint, IsEmpty());
}
Expand Down Expand Up @@ -176,6 +177,25 @@ TEST(test_options, lsp_server) {
}
}

TEST(test_options, stdin) {
{
options o = parse_options({"--stdin"});
EXPECT_TRUE(o.stdin);
}
{
options o = parse_options({"--s"});
EXPECT_TRUE(o.stdin);
}
}

TEST(test_options, single_hypen_is_argument) {
{
options o = parse_options({"one.js", "-", "two.js"});
ASSERT_EQ(o.files_to_lint.size(), 2);
EXPECT_TRUE(o.stdin);
}
}

TEST(test_options, print_help) {
{
options o = parse_options({"--help"});
Expand Down

0 comments on commit 7f767f9

Please sign in to comment.