|
| 1 | +//! JavaScript 独立入口复用项目候选扫描与稳定任务;不替代原生 ESLint。 |
| 2 | +use crate::eslint_lint_arguments::EslintLintArguments; |
| 3 | +use serde_json::{Value, json}; |
| 4 | +use std::{collections::BTreeSet, time::Instant}; |
| 5 | + |
| 6 | +/// 判断请求是否属于 JavaScript grammar 的整文件输入;不推断 module/CommonJS 语义。 |
| 7 | +pub(crate) fn supported(args: &EslintLintArguments) -> bool { |
| 8 | + args.source.extension().is_some_and(|extension| { |
| 9 | + ["js", "mjs", "cjs", "jsx"] |
| 10 | + .iter() |
| 11 | + .any(|value| extension == *value) |
| 12 | + }) |
| 13 | +} |
| 14 | + |
| 15 | +/// 在原生上下文发现为空后,以同一截止时间观察单文件并同步既有确认任务。 |
| 16 | +/// 参数为原请求及截止时间,返回无交付权威的版本化候选反馈。 |
| 17 | +pub(crate) fn observe(args: &EslintLintArguments, deadline: Instant) -> Value { |
| 18 | + let mut report = json!({"schema_version":"0.6.0","report_type":"eslint_local_feedback", |
| 19 | + "status":"incomplete","local_coherent":false,"coverage_proven":false, |
| 20 | + "delivery_decision":"not_evaluated","reason":"eslint_execution_context_missing", |
| 21 | + "findings":[],"suppressed_count":0,"workbench_status":"not_connected","workbench":null, |
| 22 | + "native":{"status":"not_run","reason":"explicit_context_missing"}, |
| 23 | + "setup":{"requirement":"required","reason":"native_confirmation_needed","task_id":null}, |
| 24 | + "syntax_candidates":{"status":"not_run","reason":"source_scope_unavailable","observations":[]}, |
| 25 | + "syntax_tasks":null, |
| 26 | + "next_action":"源码范围无法绑定;先核对源码位置及 --workspace 的已核验绝对路径,本次语法初检未运行,不修改源码"}); |
| 27 | + let source = match args.source.canonicalize() { |
| 28 | + Ok(source) => source, |
| 29 | + Err(_) => return report, |
| 30 | + }; |
| 31 | + let root = match args.workspace.as_deref().or_else(|| source.parent()) { |
| 32 | + Some(path) => match path.canonicalize() { |
| 33 | + Ok(root) |
| 34 | + if args |
| 35 | + .workspace |
| 36 | + .as_ref() |
| 37 | + .is_none_or(|workspace| *workspace == root) => |
| 38 | + { |
| 39 | + root |
| 40 | + } |
| 41 | + _ => return report, |
| 42 | + }, |
| 43 | + None => return report, |
| 44 | + }; |
| 45 | + let Some(relative) = source |
| 46 | + .strip_prefix(&root) |
| 47 | + .ok() |
| 48 | + .and_then(|path| path.to_str()) |
| 49 | + else { |
| 50 | + return report; |
| 51 | + }; |
| 52 | + let selected = BTreeSet::from([relative.to_owned()]); |
| 53 | + let empty = Value::Null; |
| 54 | + let coverage = crate::rust_native_syntax_coverage::RustNativeSyntaxCoverage::default(); |
| 55 | + let syntax = crate::check_syntax_candidates::observe_selected( |
| 56 | + &root, |
| 57 | + &selected, |
| 58 | + crate::check_syntax_candidates::NativeCoverage { |
| 59 | + node_lint: &empty, |
| 60 | + python_lint: &empty, |
| 61 | + go_lint: &empty, |
| 62 | + erlang_lint: &empty, |
| 63 | + kotlin_lint: &empty, |
| 64 | + zig_lint: &empty, |
| 65 | + swift_lint: &empty, |
| 66 | + ruby_lint: &empty, |
| 67 | + rust_targets: &coverage, |
| 68 | + go_tool: None, |
| 69 | + }, |
| 70 | + 1, |
| 71 | + deadline, |
| 72 | + None, |
| 73 | + ); |
| 74 | + report["next_action"] = json!( |
| 75 | + "查看本次候选或检查未完成原因,恢复适用的原生 JavaScript/ESLint 上下文确认;不凭候选直接修改源码或关闭任务" |
| 76 | + ); |
| 77 | + // 只有已完成本次有界扫描且没有任何候选,才将安装建议降为推荐;语法能力仍未验收。 |
| 78 | + let no_candidates = syntax["status"] == "observed_partial" |
| 79 | + && syntax["skipped_count"] == 0 |
| 80 | + && syntax["unrouted_count"] == 0 |
| 81 | + && syntax["observations"].as_array().is_some_and(|rows| { |
| 82 | + rows.len() == 1 |
| 83 | + && rows[0]["status"] == "candidate_observed" |
| 84 | + && rows[0]["reason"].is_null() |
| 85 | + && rows[0]["recovery_count"] == 0 |
| 86 | + && rows[0]["structural_observation_count"] |
| 87 | + .as_u64() |
| 88 | + .unwrap_or(0) |
| 89 | + == 0 |
| 90 | + }); |
| 91 | + if no_candidates { |
| 92 | + report["setup"]["requirement"] = json!("recommended"); |
| 93 | + report["setup"]["reason"] = json!("native_lint_recommended"); |
| 94 | + report["next_action"] = json!( |
| 95 | + "本次有界候选初检未发现疑似问题;推荐配置并运行原生 ESLint,类型、依赖、安全及完整项目质量仍未检查" |
| 96 | + ); |
| 97 | + } |
| 98 | + if codeguard_runtime::sigint_cancellation_requested() { |
| 99 | + report["reason"] = json!("request_cancelled"); |
| 100 | + } else if args.workspace.is_some() { |
| 101 | + let tasks = crate::syntax_confirmation::persist(&root, &syntax, deadline); |
| 102 | + report["workbench_status"] = tasks["status"].clone(); |
| 103 | + let mut workbench = json!({"status":tasks["status"]}); |
| 104 | + let selected_task = tasks["tasks"][0]["task_id"] |
| 105 | + .as_str() |
| 106 | + .map(str::to_owned) |
| 107 | + .or_else(|| { |
| 108 | + if no_candidates { |
| 109 | + pending_confirmation(&root, relative) |
| 110 | + } else { |
| 111 | + None |
| 112 | + } |
| 113 | + }); |
| 114 | + if let Some(id) = selected_task { |
| 115 | + report["setup"]["task_id"] = json!(id); |
| 116 | + workbench["task_id"] = json!(id); |
| 117 | + if no_candidates { |
| 118 | + // 当前初检没有候选,不代表历史原生确认已完成;保留待办及明确安装要求。 |
| 119 | + report["setup"]["requirement"] = json!("required"); |
| 120 | + report["setup"]["reason"] = json!("native_confirmation_still_pending"); |
| 121 | + } |
| 122 | + if let Ok(brief) = crate::next_command::read_task_brief(&root, &id) { |
| 123 | + report["next_action"] = brief["step"].clone(); |
| 124 | + } |
| 125 | + } |
| 126 | + if tasks["failures"] |
| 127 | + .as_array() |
| 128 | + .is_some_and(|failures| !failures.is_empty()) |
| 129 | + { |
| 130 | + report["next_action"] = json!(if tasks["failures"].as_array().is_some_and(|failures| { |
| 131 | + failures |
| 132 | + .iter() |
| 133 | + .any(|failure| failure["reason"] == "workspace_not_initialized") |
| 134 | + }) { |
| 135 | + "工作区未初始化;先对原工作区执行 codeguard init <工作区> --apply,再按原 lint 命令复检。保留初检候选,不修改源码或虚构任务" |
| 136 | + } else { |
| 137 | + "本次候选任务同步未完成;查看 syntax_tasks.failures,核查 .codeguard/reports 的路径、权限及受管工作区身份,再按原命令复检,不自行创建关闭证据" |
| 138 | + }); |
| 139 | + } |
| 140 | + report["workbench"] = workbench; |
| 141 | + report["syntax_tasks"] = tasks; |
| 142 | + } |
| 143 | + report["syntax_candidates"] = syntax; |
| 144 | + report |
| 145 | +} |
| 146 | + |
| 147 | +// 仅从已初始化工作区及经过完整工作台校验的现存任务恢复引用,不制造新任务或关闭证据。 |
| 148 | +fn pending_confirmation(root: &std::path::Path, relative: &str) -> Option<String> { |
| 149 | + let baseline = crate::workspace_refresh::read_workspace_baseline(root) |
| 150 | + .ok() |
| 151 | + .flatten()?; |
| 152 | + let fingerprint = crate::eslint_preparation::fingerprint(baseline.workspace_id()?, relative); |
| 153 | + let id = format!("CG-B-{}", &fingerprint[..32]); |
| 154 | + let brief = crate::next_command::read_task_brief(root, &id).ok()?; |
| 155 | + if brief["checker_id"] != "node.eslint.preparation" |
| 156 | + || brief["kind"] != "blocker" |
| 157 | + || brief["scope"] != relative |
| 158 | + { |
| 159 | + return None; |
| 160 | + } |
| 161 | + Some(id) |
| 162 | +} |
| 163 | + |
| 164 | +/// 输出有界脱敏语法与结构位置;参数为本轮候选报告,不输出源码片段或标识符。 |
| 165 | +pub(crate) fn print_feedback(syntax: &Value) { |
| 166 | + println!( |
| 167 | + "内置 JavaScript 候选初检:{};原生 lint 尚未完成。", |
| 168 | + syntax["status"] |
| 169 | + ); |
| 170 | + for row in syntax["observations"] |
| 171 | + .as_array() |
| 172 | + .into_iter() |
| 173 | + .flatten() |
| 174 | + .take(1) |
| 175 | + { |
| 176 | + println!( |
| 177 | + "疑似恢复节点 {},结构候选 {};检查原因 {}。", |
| 178 | + row["recovery_count"], |
| 179 | + row["structural_observation_count"].as_u64().unwrap_or(0), |
| 180 | + row["reason"] |
| 181 | + ); |
| 182 | + for recovery in row["recoveries"].as_array().into_iter().flatten().take(8) { |
| 183 | + println!( |
| 184 | + "疑似语法 {},零起始行 {}、UTF-8 字节列 {};须原生确认。", |
| 185 | + recovery["kind"], recovery["start_row"], recovery["start_column_byte"] |
| 186 | + ); |
| 187 | + } |
| 188 | + for structure in row["structural_observations"] |
| 189 | + .as_array() |
| 190 | + .into_iter() |
| 191 | + .flatten() |
| 192 | + .take(8) |
| 193 | + { |
| 194 | + println!( |
| 195 | + "疑似结构规则 {},零起始行 {}、UTF-8 字节列 {};须原生确认。", |
| 196 | + structure["rule_id"], structure["start_row"], structure["start_column_byte"] |
| 197 | + ); |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments