summaryrefslogtreecommitdiff
path: root/cli/lint.rs
diff options
context:
space:
mode:
authorYusuke Tanaka <yusuktan@maguro.dev>2020-08-31 20:53:42 +0900
committerGitHub <noreply@github.com>2020-08-31 13:53:42 +0200
commitfa65e49bc689b2b84a3fa2dd123fa616f430f7fe (patch)
tree7b744a69c76d444cf59f7a0d078b48127c11bee1 /cli/lint.rs
parenta451a97486e4cd9c311cef2c016137dcff672522 (diff)
feat(lint): Add support for reading input from stdin (#7263)
Diffstat (limited to 'cli/lint.rs')
-rw-r--r--cli/lint.rs50
1 files changed, 50 insertions, 0 deletions
diff --git a/cli/lint.rs b/cli/lint.rs
index 637e40b29..2edadecbf 100644
--- a/cli/lint.rs
+++ b/cli/lint.rs
@@ -11,6 +11,7 @@ use crate::file_fetcher::map_file_extension;
use crate::fmt::collect_files;
use crate::fmt::run_parallelized;
use crate::fmt_errors;
+use crate::msg;
use crate::swc_util;
use deno_core::ErrBox;
use deno_lint::diagnostic::LintDiagnostic;
@@ -20,6 +21,7 @@ use deno_lint::rules;
use deno_lint::rules::LintRule;
use serde::Serialize;
use std::fs;
+use std::io::{stdin, Read};
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
@@ -42,6 +44,9 @@ pub async fn lint_files(
ignore: Vec<String>,
json: bool,
) -> Result<(), ErrBox> {
+ if args.len() == 1 && args[0] == "-" {
+ return lint_stdin(json);
+ }
let mut target_files = collect_files(args)?;
if !ignore.is_empty() {
// collect all files to be ignored
@@ -133,6 +138,51 @@ fn lint_file(file_path: PathBuf) -> Result<Vec<LintDiagnostic>, ErrBox> {
Ok(file_diagnostics)
}
+/// Lint stdin and write result to stdout.
+/// Treats input as TypeScript.
+/// Compatible with `--json` flag.
+fn lint_stdin(json: bool) -> Result<(), ErrBox> {
+ let mut source = String::new();
+ if stdin().read_to_string(&mut source).is_err() {
+ return Err(ErrBox::error("Failed to read from stdin"));
+ }
+
+ let reporter_kind = if json {
+ LintReporterKind::Json
+ } else {
+ LintReporterKind::Pretty
+ };
+ let mut reporter = create_reporter(reporter_kind);
+ let lint_rules = rules::get_recommended_rules();
+ let syntax = swc_util::get_syntax_for_media_type(msg::MediaType::TypeScript);
+ let mut linter = create_linter(syntax, lint_rules);
+ let mut has_error = false;
+ let pseudo_file_name = "_stdin.ts";
+ match linter
+ .lint(pseudo_file_name.to_string(), source)
+ .map_err(|e| e.into())
+ {
+ Ok(diagnostics) => {
+ for d in diagnostics {
+ has_error = true;
+ reporter.visit(&d);
+ }
+ }
+ Err(err) => {
+ has_error = true;
+ reporter.visit_error(pseudo_file_name, &err);
+ }
+ }
+
+ reporter.close();
+
+ if has_error {
+ std::process::exit(1);
+ }
+
+ Ok(())
+}
+
trait LintReporter {
fn visit(&mut self, d: &LintDiagnostic);
fn visit_error(&mut self, file_path: &str, err: &ErrBox);