From 8366f36873bb7311f533e5e49d9ad13581b0b5c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 9 Jun 2020 18:40:08 +0200 Subject: [PATCH] upgrade: deno_lint v0.1.8 (#6208) --- Cargo.lock | 4 ++-- cli/Cargo.toml | 2 +- cli/flags.rs | 13 +++++++++--- cli/main.rs | 4 +++- cli/swc_util.rs | 34 ++++++++++++++++++-------------- cli/tests/integration_tests.rs | 6 ++++++ cli/tests/lint/expected.out | 36 ++++++++++++++++++++++++++++++++++ cli/tests/lint/file1.js | 3 +++ cli/tests/lint/file2.ts | 9 +++++++++ cli/tests/lint/ignored_file.ts | 3 +++ tools/lint.py | 11 ++++++++--- 11 files changed, 100 insertions(+), 25 deletions(-) create mode 100644 cli/tests/lint/expected.out create mode 100644 cli/tests/lint/file1.js create mode 100644 cli/tests/lint/file2.ts create mode 100644 cli/tests/lint/ignored_file.ts diff --git a/Cargo.lock b/Cargo.lock index 9eb1e61228..eb3349c1f6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -506,9 +506,9 @@ dependencies = [ [[package]] name = "deno_lint" -version = "0.1.7" +version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77a3ec96c92609aa121d085f3a1351d3836e55b78a4b8ce79ea771c2ad9bd80b" +checksum = "0ca00aa150fff66457af99578360267a988b44b3dd0d7d503571b2e98b15094d" dependencies = [ "lazy_static", "regex", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 01a240d3a6..667a6e2c3f 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -20,7 +20,7 @@ deno_typescript = { path = "../deno_typescript", version = "0.47.1" } [dependencies] deno_core = { path = "../core", version = "0.47.1" } -deno_lint = { version = "0.1.7" } +deno_lint = { version = "0.1.8" } deno_typescript = { path = "../deno_typescript", version = "0.47.1" } atty = "0.2.14" diff --git a/cli/flags.rs b/cli/flags.rs index fc16d378d9..f630f4bbf8 100644 --- a/cli/flags.rs +++ b/cli/flags.rs @@ -911,10 +911,17 @@ fn lint_subcommand<'a, 'b>() -> App<'a, 'b> { .about("Lint source files") .long_about( "Lint JavaScript/TypeScript source code. - deno lint myfile1.ts myfile2.js + deno lint --unstable myfile1.ts myfile2.js -Ignore diagnostics on next line preceding it with an ignore comment and code: - // deno-lint-ignore no-explicit-any", +Ignore diagnostics on the next line by preceding it with an ignore comment and +rule name: + // deno-lint-ignore no-explicit-any + + // deno-lint-ignore require-await no-empty + +Ignore linting a file by adding an ignore comment at the top of the file: + // deno-lint-ignore-file +", ) .arg(unstable_arg()) .arg( diff --git a/cli/main.rs b/cli/main.rs index 909295439f..d3be3bdbe5 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -339,11 +339,13 @@ async fn lint_command(flags: Flags, files: Vec) -> Result<(), ErrBox> { .fetch_source_file(&specifier, None, Permissions::allow_all()) .await?; let source_code = String::from_utf8(source_file.source_code)?; + let syntax = swc_util::get_syntax_for_media_type(source_file.media_type); let mut linter = deno_lint::linter::Linter::default(); let lint_rules = deno_lint::rules::get_all_rules(); - let file_diagnostics = linter.lint(file, source_code, lint_rules)?; + let file_diagnostics = + linter.lint(file, source_code, syntax, lint_rules)?; error_counts += file_diagnostics.len(); for d in file_diagnostics.iter() { diff --git a/cli/swc_util.rs b/cli/swc_util.rs index 0d75877bf3..eb41fbc348 100644 --- a/cli/swc_util.rs +++ b/cli/swc_util.rs @@ -52,6 +52,24 @@ fn get_default_ts_config() -> TsConfig { ts_config } +pub fn get_syntax_for_media_type(media_type: MediaType) -> Syntax { + match media_type { + MediaType::JavaScript => Syntax::Es(get_default_es_config()), + MediaType::JSX => { + let mut config = get_default_es_config(); + config.jsx = true; + Syntax::Es(config) + } + MediaType::TypeScript => Syntax::Typescript(get_default_ts_config()), + MediaType::TSX => { + let mut config = get_default_ts_config(); + config.tsx = true; + Syntax::Typescript(config) + } + _ => Syntax::Es(get_default_es_config()), + } +} + #[derive(Clone, Debug)] pub struct SwcDiagnosticBuffer { pub diagnostics: Vec, @@ -169,21 +187,7 @@ impl AstParser { handler: &self.handler, }; - let syntax = match media_type { - MediaType::JavaScript => Syntax::Es(get_default_es_config()), - MediaType::JSX => { - let mut config = get_default_es_config(); - config.jsx = true; - Syntax::Es(config) - } - MediaType::TypeScript => Syntax::Typescript(get_default_ts_config()), - MediaType::TSX => { - let mut config = get_default_ts_config(); - config.tsx = true; - Syntax::Typescript(config) - } - _ => Syntax::Es(get_default_es_config()), - }; + let syntax = get_syntax_for_media_type(media_type); let lexer = Lexer::new( session, diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index 2da5eebcb5..d70527881c 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -1939,6 +1939,12 @@ itest!(proto_exploit { output: "proto_exploit.js.out", }); +itest!(deno_lint { + args: "lint --unstable lint/file1.js lint/file2.ts lint/ignored_file.ts", + output: "lint/expected.out", + exit_code: 1, +}); + #[test] fn cafile_fetch() { use url::Url; diff --git a/cli/tests/lint/expected.out b/cli/tests/lint/expected.out new file mode 100644 index 0000000000..0b0654298e --- /dev/null +++ b/cli/tests/lint/expected.out @@ -0,0 +1,36 @@ +(no-var) `var` keyword is not allowed +var a = 1, +~~~~~~~~~~ + at [WILDCARD]file1.js:1:0 + +(single-var-declarator) Multiple variable declarators are not allowed +var a = 1, +~~~~~~~~~~ + at [WILDCARD]file1.js:1:0 + +(no-empty) Empty block statement +} catch (e) {} + ~~ + at [WILDCARD]file2.ts:3:12 + +(ban-unused-ignore) Ignore for code "require-await" was not used. +// deno-lint-ignore no-explicit-any require-await +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + at [WILDCARD]file2.ts:5:0 + +(no-empty-function) Empty functions are not allowed +function foo(): any {} +~~~~~~~~~~~~~~~~~~~~~~ + at [WILDCARD]file2.ts:6:0 + +(ban-untagged-ignore) Ignore directive requires lint rule code +// deno-lint-ignore +~~~~~~~~~~~~~~~~~~~ + at [WILDCARD]file2.ts:8:0 + +(no-empty) Empty block statement +while (false) {} + ~~ + at [WILDCARD]file2.ts:9:14 + +Found 7 problems diff --git a/cli/tests/lint/file1.js b/cli/tests/lint/file1.js new file mode 100644 index 0000000000..d74b6f47a8 --- /dev/null +++ b/cli/tests/lint/file1.js @@ -0,0 +1,3 @@ +var a = 1, + b = 2, + c = 3; diff --git a/cli/tests/lint/file2.ts b/cli/tests/lint/file2.ts new file mode 100644 index 0000000000..f0f3a3ba3a --- /dev/null +++ b/cli/tests/lint/file2.ts @@ -0,0 +1,9 @@ +try { + await Deno.open("./some/file.txt"); +} catch (e) {} + +// deno-lint-ignore no-explicit-any require-await +function foo(): any {} + +// deno-lint-ignore +while (false) {} diff --git a/cli/tests/lint/ignored_file.ts b/cli/tests/lint/ignored_file.ts new file mode 100644 index 0000000000..97befafa38 --- /dev/null +++ b/cli/tests/lint/ignored_file.ts @@ -0,0 +1,3 @@ +// deno-lint-ignore-file + +function foo(): any {} diff --git a/tools/lint.py b/tools/lint.py index 0e71fa9465..0ff0b16d7b 100755 --- a/tools/lint.py +++ b/tools/lint.py @@ -61,9 +61,14 @@ def eslint(): "eslint") # Find all *directories* in the main repo that contain .ts/.js files. source_files = get_sources(root_path, [ - "*.js", "*.ts", ":!:cli/tests/swc_syntax_error.ts", - ":!:std/**/testdata/*", ":!:std/**/node_modules/*", - ":!:cli/compilers/wasm_wrap.js", ":!:cli/tests/error_syntax.js" + "*.js", + "*.ts", + ":!:cli/tests/swc_syntax_error.ts", + ":!:std/**/testdata/*", + ":!:std/**/node_modules/*", + ":!:cli/compilers/wasm_wrap.js", + ":!:cli/tests/error_syntax.js", + ":!:cli/tests/lint/**", ]) if source_files: print_command("eslint", source_files)