summaryrefslogtreecommitdiff
path: root/cli/tools/lint/mod.rs
blob: fcefb458746f038b7994ecfc4b353337c10fcee0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

//! This module provides file linting utilities using
//! [`deno_lint`](https://github.com/denoland/deno_lint).

use deno_ast::ModuleSpecifier;
use deno_ast::ParsedSource;
use deno_config::deno_json::LintRulesConfig;
use deno_config::glob::FileCollector;
use deno_config::glob::FilePatterns;
use deno_config::workspace::WorkspaceDirectory;
use deno_core::anyhow::anyhow;
use deno_core::error::generic_error;
use deno_core::error::AnyError;
use deno_core::futures::future::LocalBoxFuture;
use deno_core::futures::FutureExt;
use deno_core::parking_lot::Mutex;
use deno_core::serde_json;
use deno_core::unsync::future::LocalFutureExt;
use deno_core::unsync::future::SharedLocal;
use deno_graph::ModuleGraph;
use deno_lint::diagnostic::LintDiagnostic;
use deno_lint::linter::LintConfig;
use log::debug;
use reporters::create_reporter;
use reporters::LintReporter;
use serde::Serialize;
use std::collections::HashSet;
use std::fs;
use std::io::stdin;
use std::io::Read;
use std::path::Path;
use std::path::PathBuf;
use std::rc::Rc;
use std::sync::Arc;

use crate::args::CliOptions;
use crate::args::Flags;
use crate::args::LintFlags;
use crate::args::LintOptions;
use crate::args::WorkspaceLintOptions;
use crate::cache::Caches;
use crate::cache::IncrementalCache;
use crate::colors;
use crate::factory::CliFactory;
use crate::graph_util::ModuleGraphCreator;
use crate::tools::fmt::run_parallelized;
use crate::util::display;
use crate::util::file_watcher;
use crate::util::fs::canonicalize_path;
use crate::util::path::is_script_ext;
use crate::util::sync::AtomicFlag;

mod linter;
mod reporters;
mod rules;

pub use linter::CliLinter;
pub use linter::CliLinterOptions;
pub use rules::collect_no_slow_type_diagnostics;
pub use rules::ConfiguredRules;
pub use rules::LintRuleProvider;

const JSON_SCHEMA_VERSION: u8 = 1;

static STDIN_FILE_NAME: &str = "$deno$stdin.mts";

pub async fn lint(
  flags: Arc<Flags>,
  lint_flags: LintFlags,
) -> Result<(), AnyError> {
  if let Some(watch_flags) = &lint_flags.watch {
    if lint_flags.is_stdin() {
      return Err(generic_error(
        "Lint watch on standard input is not supported.",
      ));
    }
    file_watcher::watch_func(
      flags,
      file_watcher::PrintConfig::new("Lint", !watch_flags.no_clear_screen),
      move |flags, watcher_communicator, changed_paths| {
        let lint_flags = lint_flags.clone();
        watcher_communicator.show_path_changed(changed_paths.clone());
        Ok(async move {
          let factory = CliFactory::from_flags(flags);
          let cli_options = factory.cli_options()?;
          let lint_config = cli_options.resolve_deno_lint_config()?;
          let mut paths_with_options_batches =
            resolve_paths_with_options_batches(cli_options, &lint_flags)?;
          for paths_with_options in &mut paths_with_options_batches {
            _ = watcher_communicator
              .watch_paths(paths_with_options.paths.clone());

            let files = std::mem::take(&mut paths_with_options.paths);
            paths_with_options.paths = if let Some(paths) = &changed_paths {
              // lint all files on any changed (https://github.com/denoland/deno/issues/12446)
              files
                .iter()
                .any(|path| {
                  canonicalize_path(path)
                    .map(|p| paths.contains(&p))
                    .unwrap_or(false)
                })
                .then_some(files)
                .unwrap_or_else(|| [].to_vec())
            } else {
              files
            };
          }

          let mut linter = WorkspaceLinter::new(
            factory.caches()?.clone(),
            factory.lint_rule_provider().await?,
            factory.module_graph_creator().await?.clone(),
            cli_options.start_dir.clone(),
            &cli_options.resolve_workspace_lint_options(&lint_flags)?,
          );
          for paths_with_options in paths_with_options_batches {
            linter
              .lint_files(
                cli_options,
                paths_with_options.options,
                lint_config.clone(),
                paths_with_options.dir,
                paths_with_options.paths,
              )
              .await?;
          }

          linter.finish();

          Ok(())
        })
      },
    )
    .await?;
  } else {
    let factory = CliFactory::from_flags(flags);
    let cli_options = factory.cli_options()?;
    let is_stdin = lint_flags.is_stdin();
    let deno_lint_config = cli_options.resolve_deno_lint_config()?;
    let workspace_lint_options =
      cli_options.resolve_workspace_lint_options(&lint_flags)?;
    let success = if is_stdin {
      let start_dir = &cli_options.start_dir;
      let reporter_lock = Arc::new(Mutex::new(create_reporter(
        workspace_lint_options.reporter_kind,
      )));
      let lint_config = start_dir
        .to_lint_config(FilePatterns::new_with_base(start_dir.dir_path()))?;
      let lint_options = LintOptions::resolve(lint_config, &lint_flags);
      let lint_rules = factory
        .lint_rule_provider()
        .await?
        .resolve_lint_rules_err_empty(
          lint_options.rules,
          start_dir.maybe_deno_json().map(|c| c.as_ref()),
        )?;
      let mut file_path = cli_options.initial_cwd().join(STDIN_FILE_NAME);
      if let Some(ext) = cli_options.ext_flag() {
        file_path.set_extension(ext);
      }
      let r = lint_stdin(&file_path, lint_rules, deno_lint_config);
      let success = handle_lint_result(
        &file_path.to_string_lossy(),
        r,
        reporter_lock.clone(),
      );
      reporter_lock.lock().close(1);
      success
    } else {
      let mut linter = WorkspaceLinter::new(
        factory.caches()?.clone(),
        factory.lint_rule_provider().await?,
        factory.module_graph_creator().await?.clone(),
        cli_options.start_dir.clone(),
        &workspace_lint_options,
      );
      let paths_with_options_batches =
        resolve_paths_with_options_batches(cli_options, &lint_flags)?;
      for paths_with_options in paths_with_options_batches {
        linter
          .lint_files(
            cli_options,
            paths_with_options.options,
            deno_lint_config.clone(),
            paths_with_options.dir,
            paths_with_options.paths,
          )
          .await?;
      }
      linter.finish()
    };
    if !success {
      deno_runtime::exit(1);
    }
  }

  Ok(())
}

struct PathsWithOptions {
  dir: WorkspaceDirectory,
  paths: Vec<PathBuf>,
  options: LintOptions,
}

fn resolve_paths_with_options_batches(
  cli_options: &CliOptions,
  lint_flags: &LintFlags,
) -> Result<Vec<PathsWithOptions>, AnyError> {
  let members_lint_options =
    cli_options.resolve_lint_options_for_members(lint_flags)?;
  let mut paths_with_options_batches =
    Vec::with_capacity(members_lint_options.len());
  for (dir, lint_options) in members_lint_options {
    let files = collect_lint_files(cli_options, lint_options.files.clone())?;
    if !files.is_empty() {
      paths_with_options_batches.push(PathsWithOptions {
        dir,
        paths: files,
        options: lint_options,
      });
    }
  }
  if paths_with_options_batches.is_empty() {
    return Err(generic_error("No target files found."));
  }
  Ok(paths_with_options_batches)
}

type WorkspaceModuleGraphFuture =
  SharedLocal<LocalBoxFuture<'static, Result<Rc<ModuleGraph>, Rc<AnyError>>>>;

struct WorkspaceLinter {
  caches: Arc<Caches>,
  lint_rule_provider: LintRuleProvider,
  module_graph_creator: Arc<ModuleGraphCreator>,
  workspace_dir: Arc<WorkspaceDirectory>,
  reporter_lock: Arc<Mutex<Box<dyn LintReporter + Send>>>,
  workspace_module_graph: Option<WorkspaceModuleGraphFuture>,
  has_error: Arc<AtomicFlag>,
  file_count: usize,
}

impl WorkspaceLinter {
  pub fn new(
    caches: Arc<Caches>,
    lint_rule_provider: LintRuleProvider,
    module_graph_creator: Arc<ModuleGraphCreator>,
    workspace_dir: Arc<WorkspaceDirectory>,
    workspace_options: &WorkspaceLintOptions,
  ) -> Self {
    let reporter_lock =
      Arc::new(Mutex::new(create_reporter(workspace_options.reporter_kind)));
    Self {
      caches,
      lint_rule_provider,
      module_graph_creator,
      workspace_dir,
      reporter_lock,
      workspace_module_graph: None,
      has_error: Default::default(),
      file_count: 0,
    }
  }

  pub async fn lint_files(
    &mut self,
    cli_options: &Arc<CliOptions>,
    lint_options: LintOptions,
    lint_config: LintConfig,
    member_dir: WorkspaceDirectory,
    paths: Vec<PathBuf>,
  ) -> Result<(), AnyError> {
    self.file_count += paths.len();

    let lint_rules = self.lint_rule_provider.resolve_lint_rules_err_empty(
      lint_options.rules,
      member_dir.maybe_deno_json().map(|c| c.as_ref()),
    )?;
    let maybe_incremental_cache =
      lint_rules.incremental_cache_state().map(|state| {
        Arc::new(IncrementalCache::new(
          self.caches.lint_incremental_cache_db(),
          &state,
          &paths,
        ))
      });

    let linter = Arc::new(CliLinter::new(CliLinterOptions {
      configured_rules: lint_rules,
      fix: lint_options.fix,
      deno_lint_config: lint_config,
    }));

    let mut futures = Vec::with_capacity(2);
    if linter.has_package_rules() {
      if self.workspace_module_graph.is_none() {
        let module_graph_creator = self.module_graph_creator.clone();
        let packages = self.workspace_dir.jsr_packages_for_publish();
        self.workspace_module_graph = Some(
          async move {
            module_graph_creator
              .create_and_validate_publish_graph(&packages, true)
              .await
              .map(Rc::new)
              .map_err(Rc::new)
          }
          .boxed_local()
          .shared_local(),
        );
      }
      let workspace_module_graph_future =
        self.workspace_module_graph.as_ref().unwrap().clone();
      let publish_config = member_dir.maybe_package_config();
      if let Some(publish_config) = publish_config {
        let has_error = self.has_error.clone();
        let reporter_lock = self.reporter_lock.clone();
        let linter = linter.clone();
        let path_urls = paths
          .iter()
          .filter_map(|p| ModuleSpecifier::from_file_path(p).ok())
          .collect::<HashSet<_>>();
        futures.push(
          async move {
            let graph = workspace_module_graph_future
              .await
              .map_err(|err| anyhow!("{:#}", err))?;
            let export_urls =
              publish_config.config_file.resolve_export_value_urls()?;
            if !export_urls.iter().any(|url| path_urls.contains(url)) {
              return Ok(()); // entrypoint is not specified, so skip
            }
            let diagnostics = linter.lint_package(&graph, &export_urls);
            if !diagnostics.is_empty() {
              has_error.raise();
              let mut reporter = reporter_lock.lock();
              for diagnostic in &diagnostics {
                reporter.visit_diagnostic(diagnostic);
              }
            }
            Ok(())
          }
          .boxed_local(),
        );
      }
    }

    futures.push({
      let has_error = self.has_error.clone();
      let reporter_lock = self.reporter_lock.clone();
      let maybe_incremental_cache = maybe_incremental_cache.clone();
      let linter = linter.clone();
      let cli_options = cli_options.clone();
      async move {
        run_parallelized(paths, {
          move |file_path| {
            let file_text =
              deno_ast::strip_bom(fs::read_to_string(&file_path)?);

            // don't bother rechecking this file if it didn't have any diagnostics before
            if let Some(incremental_cache) = &maybe_incremental_cache {
              if incremental_cache.is_file_same(&file_path, &file_text) {
                return Ok(());
              }
            }

            let r = linter.lint_file(
              &file_path,
              file_text,
              cli_options.ext_flag().as_deref(),
            );
            if let Ok((file_source, file_diagnostics)) = &r {
              if let Some(incremental_cache) = &maybe_incremental_cache {
                if file_diagnostics.is_empty() {
                  // update the incremental cache if there were no diagnostics
                  incremental_cache.update_file(
                    &file_path,
                    // ensure the returned text is used here as it may have been modified via --fix
                    file_source.text(),
                  )
                }
              }
            }

            let success = handle_lint_result(
              &file_path.to_string_lossy(),
              r,
              reporter_lock.clone(),
            );
            if !success {
              has_error.raise();
            }

            Ok(())
          }
        })
        .await
      }
      .boxed_local()
    });

    if lint_options.fix {
      // run sequentially when using `--fix` to lower the chances of weird
      // bugs where a file level fix affects a package level diagnostic though
      // it probably will happen anyway
      for future in futures {
        future.await?;
      }
    } else {
      deno_core::futures::future::try_join_all(futures).await?;
    }

    if let Some(incremental_cache) = &maybe_incremental_cache {
      incremental_cache.wait_completion().await;
    }

    Ok(())
  }

  pub fn finish(self) -> bool {
    debug!("Found {} files", self.file_count);
    self.reporter_lock.lock().close(self.file_count);
    !self.has_error.is_raised() // success
  }
}

fn collect_lint_files(
  cli_options: &CliOptions,
  files: FilePatterns,
) -> Result<Vec<PathBuf>, AnyError> {
  FileCollector::new(|e| {
    is_script_ext(e.path)
      || (e.path.extension().is_none() && cli_options.ext_flag().is_some())
  })
  .ignore_git_folder()
  .ignore_node_modules()
  .use_gitignore()
  .set_vendor_folder(cli_options.vendor_dir_path().map(ToOwned::to_owned))
  .collect_file_patterns(&deno_config::fs::RealDenoConfigFs, files)
}

#[allow(clippy::print_stdout)]
pub fn print_rules_list(json: bool, maybe_rules_tags: Option<Vec<String>>) {
  let rule_provider = LintRuleProvider::new(None, None);
  let lint_rules = rule_provider
    .resolve_lint_rules(
      LintRulesConfig {
        tags: maybe_rules_tags.clone(),
        include: None,
        exclude: None,
      },
      None,
    )
    .rules;

  if json {
    let json_output = serde_json::json!({
      "version": JSON_SCHEMA_VERSION,
      "rules": lint_rules
        .iter()
        .map(|rule| {
          serde_json::json!({
            "code": rule.code(),
            "tags": rule.tags(),
            "docs": rule.docs(),
          })
        })
        .collect::<Vec<serde_json::Value>>(),
    });
    display::write_json_to_stdout(&json_output).unwrap();
  } else {
    // The rules should still be printed even if `--quiet` option is enabled,
    // so use `println!` here instead of `info!`.
    println!("Available rules:");
    for rule in lint_rules.iter() {
      print!(" - {}", colors::cyan(rule.code()));
      if rule.tags().is_empty() {
        println!();
      } else {
        println!(" [{}]", colors::gray(rule.tags().join(", ")))
      }
      println!(
        "{}",
        colors::gray(format!("   help: {}", rule.help_docs_url()))
      );
      println!();
    }
  }
}

/// Lint stdin and write result to stdout.
/// Treats input as TypeScript.
/// Compatible with `--json` flag.
fn lint_stdin(
  file_path: &Path,
  configured_rules: ConfiguredRules,
  deno_lint_config: LintConfig,
) -> Result<(ParsedSource, Vec<LintDiagnostic>), AnyError> {
  let mut source_code = String::new();
  if stdin().read_to_string(&mut source_code).is_err() {
    return Err(generic_error("Failed to read from stdin"));
  }

  let linter = CliLinter::new(CliLinterOptions {
    fix: false,
    configured_rules,
    deno_lint_config,
  });

  linter
    .lint_file(file_path, deno_ast::strip_bom(source_code), None)
    .map_err(AnyError::from)
}

fn handle_lint_result(
  file_path: &str,
  result: Result<(ParsedSource, Vec<LintDiagnostic>), AnyError>,
  reporter_lock: Arc<Mutex<Box<dyn LintReporter + Send>>>,
) -> bool {
  let mut reporter = reporter_lock.lock();

  match result {
    Ok((source, mut file_diagnostics)) => {
      if !source.diagnostics().is_empty() {
        for parse_diagnostic in source.diagnostics() {
          log::warn!("{}: {}", colors::yellow("warn"), parse_diagnostic);
        }
      }
      file_diagnostics.sort_by(|a, b| match a.specifier.cmp(&b.specifier) {
        std::cmp::Ordering::Equal => {
          let a_start = a.range.as_ref().map(|r| r.range.start);
          let b_start = b.range.as_ref().map(|r| r.range.start);
          match a_start.cmp(&b_start) {
            std::cmp::Ordering::Equal => a.details.code.cmp(&b.details.code),
            other => other,
          }
        }
        file_order => file_order,
      });
      for d in &file_diagnostics {
        reporter.visit_diagnostic(d);
      }
      file_diagnostics.is_empty()
    }
    Err(err) => {
      reporter.visit_error(file_path, &err);
      false
    }
  }
}

#[derive(Serialize)]
struct LintError {
  file_path: String,
  message: String,
}