1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-29 16:30:56 -05:00

feat(lint): Add support for reading input from stdin (#7263)

This commit is contained in:
Yusuke Tanaka 2020-08-31 20:53:42 +09:00 committed by GitHub
parent a451a97486
commit fa65e49bc6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 92 additions and 0 deletions

View file

@ -1015,6 +1015,10 @@ fn lint_subcommand<'a, 'b>() -> App<'a, 'b> {
Print result as JSON: Print result as JSON:
deno lint --unstable --json deno lint --unstable --json
Read from stdin:
cat file.ts | deno lint --unstable -
cat file.ts | deno lint --unstable --json -
List available rules: List available rules:
deno lint --unstable --rules deno lint --unstable --rules

View file

@ -11,6 +11,7 @@ use crate::file_fetcher::map_file_extension;
use crate::fmt::collect_files; use crate::fmt::collect_files;
use crate::fmt::run_parallelized; use crate::fmt::run_parallelized;
use crate::fmt_errors; use crate::fmt_errors;
use crate::msg;
use crate::swc_util; use crate::swc_util;
use deno_core::ErrBox; use deno_core::ErrBox;
use deno_lint::diagnostic::LintDiagnostic; use deno_lint::diagnostic::LintDiagnostic;
@ -20,6 +21,7 @@ use deno_lint::rules;
use deno_lint::rules::LintRule; use deno_lint::rules::LintRule;
use serde::Serialize; use serde::Serialize;
use std::fs; use std::fs;
use std::io::{stdin, Read};
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
@ -42,6 +44,9 @@ pub async fn lint_files(
ignore: Vec<String>, ignore: Vec<String>,
json: bool, json: bool,
) -> Result<(), ErrBox> { ) -> Result<(), ErrBox> {
if args.len() == 1 && args[0] == "-" {
return lint_stdin(json);
}
let mut target_files = collect_files(args)?; let mut target_files = collect_files(args)?;
if !ignore.is_empty() { if !ignore.is_empty() {
// collect all files to be ignored // collect all files to be ignored
@ -133,6 +138,51 @@ fn lint_file(file_path: PathBuf) -> Result<Vec<LintDiagnostic>, ErrBox> {
Ok(file_diagnostics) 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 { trait LintReporter {
fn visit(&mut self, d: &LintDiagnostic); fn visit(&mut self, d: &LintDiagnostic);
fn visit_error(&mut self, file_path: &str, err: &ErrBox); fn visit_error(&mut self, file_path: &str, err: &ErrBox);

View file

@ -2253,6 +2253,20 @@ itest!(deno_lint_glob {
exit_code: 1, exit_code: 1,
}); });
itest!(deno_lint_from_stdin {
args: "lint --unstable -",
input: Some("let a: any;"),
output: "lint/expected_from_stdin.out",
exit_code: 1,
});
itest!(deno_lint_from_stdin_json {
args: "lint --unstable --json -",
input: Some("let a: any;"),
output: "lint/expected_from_stdin_json.out",
exit_code: 1,
});
itest!(deno_doc_builtin { itest!(deno_doc_builtin {
args: "doc", args: "doc",
output: "deno_doc_builtin.out", output: "deno_doc_builtin.out",

View file

@ -0,0 +1,2 @@
[WILDCARD]
Found 1 problem

View file

@ -0,0 +1,16 @@
{
"diagnostics": [
{
"location": {
"line": 1,
"col": 7
},
"filename": "_stdin.ts",
"message": "`any` type is not allowed",
"code": "no-explicit-any",
"line_src": "let a: any;",
"snippet_length": 3
}
],
"errors": []
}

View file

@ -10,8 +10,14 @@ flag**
deno lint --unstable deno lint --unstable
# lint specific files # lint specific files
deno lint --unstable myfile1.ts myfile2.ts deno lint --unstable myfile1.ts myfile2.ts
# print result as JSON
deno lint --unstable --json
# read from stdin
cat file.ts | deno lint --unstable -
``` ```
For more detail, run `deno lint --help`.
### Available rules ### Available rules
- `adjacent-overload-signatures` - `adjacent-overload-signatures`