Skip to content

Commit

Permalink
feat(lint): Add support for reading input from stdin (denoland#7263)
Browse files Browse the repository at this point in the history
  • Loading branch information
magurotuna committed Aug 31, 2020
1 parent a451a97 commit fa65e49
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cli/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,10 @@ fn lint_subcommand<'a, 'b>() -> App<'a, 'b> {
Print result as JSON:
deno lint --unstable --json
Read from stdin:
cat file.ts | deno lint --unstable -
cat file.ts | deno lint --unstable --json -
List available rules:
deno lint --unstable --rules
Expand Down
50 changes: 50 additions & 0 deletions cli/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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};
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions cli/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2253,6 +2253,20 @@ itest!(deno_lint_glob {
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 {
args: "doc",
output: "deno_doc_builtin.out",
Expand Down
2 changes: 2 additions & 0 deletions cli/tests/lint/expected_from_stdin.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[WILDCARD]
Found 1 problem
16 changes: 16 additions & 0 deletions cli/tests/lint/expected_from_stdin_json.out
Original file line number Diff line number Diff line change
@@ -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": []
}
6 changes: 6 additions & 0 deletions docs/tools/linter.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ flag**
deno lint --unstable
# lint specific files
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

- `adjacent-overload-signatures`
Expand Down

0 comments on commit fa65e49

Please sign in to comment.