|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is dual-licensed under either the MIT license found in the |
| 5 | + * LICENSE-MIT file in the root directory of this source tree or the Apache |
| 6 | + * License, Version 2.0 found in the LICENSE-APACHE file in the root directory |
| 7 | + * of this source tree. You may select, at your option, one of the |
| 8 | + * above-listed licenses. |
| 9 | + */ |
| 10 | + |
| 11 | +use anyhow::Result; |
| 12 | +use anyhow::bail; |
| 13 | +use elp::cli::Cli; |
| 14 | +use elp_ide::AnalysisHost; |
| 15 | +use elp_ide::diagnostics; |
| 16 | +use elp_ide::diagnostics::DiagnosticsConfig; |
| 17 | +use elp_ide::diagnostics::FallBackToAll; |
| 18 | +use elp_ide::diagnostics::LintConfig; |
| 19 | +use elp_ide::diagnostics::MatchSsr; |
| 20 | +use elp_project_model::buck::BuckQueryConfig; |
| 21 | + |
| 22 | +use crate::args::Ssr; |
| 23 | +use crate::lint_cli; |
| 24 | + |
| 25 | +fn normalize_ssr_pattern(pattern: &str) -> String { |
| 26 | + if pattern.starts_with("ssr:") { |
| 27 | + pattern.to_string() |
| 28 | + } else { |
| 29 | + format!("ssr: {}.", pattern) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +pub fn run_ssr_command( |
| 34 | + args: &Ssr, |
| 35 | + cli: &mut dyn Cli, |
| 36 | + query_config: &BuckQueryConfig, |
| 37 | +) -> Result<()> { |
| 38 | + // Normalize the SSR pattern |
| 39 | + let normalized_pattern = normalize_ssr_pattern(&args.ssr_spec); |
| 40 | + |
| 41 | + // Validate the SSR pattern early |
| 42 | + let analysis_host = AnalysisHost::default(); |
| 43 | + let analysis = analysis_host.analysis(); |
| 44 | + match analysis.validate_ssr_pattern(&normalized_pattern) { |
| 45 | + Ok(Ok(())) => {} |
| 46 | + Ok(Err(e)) => bail!("invalid SSR pattern '{}': {}", args.ssr_spec, e), |
| 47 | + Err(_cancelled) => bail!("SSR pattern validation was cancelled"), |
| 48 | + } |
| 49 | + |
| 50 | + // Create the lint config with the SSR pattern |
| 51 | + let mut lint_config = LintConfig::default(); |
| 52 | + let ssr_lint = diagnostics::Lint::LintMatchSsr(MatchSsr { |
| 53 | + ssr_pattern: normalized_pattern, |
| 54 | + message: None, |
| 55 | + }); |
| 56 | + lint_config.ad_hoc_lints.lints.push(ssr_lint); |
| 57 | + |
| 58 | + // Build the diagnostics config |
| 59 | + let diagnostics_config = DiagnosticsConfig::default() |
| 60 | + .configure_diagnostics( |
| 61 | + &lint_config, |
| 62 | + &Some("ad-hoc: ssr-match".to_string()), |
| 63 | + &None, |
| 64 | + FallBackToAll::Yes, |
| 65 | + )? |
| 66 | + .set_include_generated(args.include_generated) |
| 67 | + .set_experimental(false) |
| 68 | + .set_use_cli_severity(false); |
| 69 | + |
| 70 | + if diagnostics_config.enabled.all_enabled() && args.is_format_normal() { |
| 71 | + writeln!(cli, "Reporting all diagnostics codes")?; |
| 72 | + } |
| 73 | + |
| 74 | + // Convert Ssr args to Lint args to reuse lint_cli functionality |
| 75 | + let lint_args = crate::args::Lint { |
| 76 | + project: args.project.clone(), |
| 77 | + module: args.module.clone(), |
| 78 | + app: args.app.clone(), |
| 79 | + file: args.file.clone(), |
| 80 | + rebar: args.rebar, |
| 81 | + profile: args.profile.clone(), |
| 82 | + include_generated: args.include_generated, |
| 83 | + include_tests: args.include_tests, |
| 84 | + print_diags: true, |
| 85 | + format: args.format.clone(), |
| 86 | + prefix: None, |
| 87 | + include_erlc_diagnostics: false, |
| 88 | + include_ct_diagnostics: false, |
| 89 | + include_edoc_diagnostics: false, |
| 90 | + include_eqwalizer_diagnostics: false, |
| 91 | + include_suppressed: false, |
| 92 | + use_cli_severity: false, |
| 93 | + diagnostic_ignore: None, |
| 94 | + diagnostic_filter: Some("ad-hoc: ssr-match".to_string()), |
| 95 | + experimental_diags: false, |
| 96 | + read_config: false, |
| 97 | + config_file: None, |
| 98 | + apply_fix: false, |
| 99 | + ignore_fix_only: false, |
| 100 | + in_place: false, |
| 101 | + to: None, |
| 102 | + recursive: false, |
| 103 | + with_check: false, |
| 104 | + check_eqwalize_all: false, |
| 105 | + one_shot: false, |
| 106 | + report_system_stats: args.report_system_stats, |
| 107 | + ignore_apps: vec![], |
| 108 | + }; |
| 109 | + |
| 110 | + // Load the project |
| 111 | + let mut loaded = lint_cli::load_project(&lint_args, cli, query_config)?; |
| 112 | + |
| 113 | + // Run the codemod with the SSR pattern |
| 114 | + lint_cli::do_codemod(cli, &mut loaded, &diagnostics_config, &lint_args) |
| 115 | +} |
| 116 | + |
| 117 | +impl Ssr { |
| 118 | + pub fn is_format_normal(&self) -> bool { |
| 119 | + self.format.is_none() |
| 120 | + } |
| 121 | +} |
0 commit comments