|
| 1 | +//! Tidy check to ensure that rustdoc templates didn't forget a `{# #}` to strip extra whitespace |
| 2 | +//! characters. |
| 3 | +
|
| 4 | +use std::ffi::OsStr; |
| 5 | +use std::path::{Path, PathBuf}; |
| 6 | +use std::process::Command; |
| 7 | + |
| 8 | +use ignore::DirEntry; |
| 9 | + |
| 10 | +use crate::walk::walk_no_read; |
| 11 | + |
| 12 | +fn rustdoc_js_files(librustdoc_path: &Path) -> Vec<PathBuf> { |
| 13 | + let mut files = Vec::new(); |
| 14 | + walk_no_read( |
| 15 | + &[&librustdoc_path.join("html/static/js")], |
| 16 | + |path, is_dir| is_dir || path.extension().is_none_or(|ext| ext != OsStr::new("js")), |
| 17 | + &mut |path: &DirEntry| { |
| 18 | + files.push(path.path().into()); |
| 19 | + }, |
| 20 | + ); |
| 21 | + return files; |
| 22 | +} |
| 23 | + |
| 24 | +fn run_eslint(args: &[PathBuf], config_folder: PathBuf) -> Result<(), super::Error> { |
| 25 | + let mut child = Command::new("npx") |
| 26 | + .arg("eslint") |
| 27 | + .arg("-c") |
| 28 | + .arg(config_folder.join(".eslintrc.js")) |
| 29 | + .args(args) |
| 30 | + .spawn()?; |
| 31 | + match child.wait() { |
| 32 | + Ok(exit_status) => { |
| 33 | + if exit_status.success() { |
| 34 | + return Ok(()); |
| 35 | + } |
| 36 | + Err(super::Error::FailedCheck("eslint command failed")) |
| 37 | + } |
| 38 | + Err(error) => Err(super::Error::Generic(format!("eslint command failed: {error:?}"))), |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +fn get_eslint_version_inner(global: bool) -> Option<String> { |
| 43 | + let mut command = Command::new("npm"); |
| 44 | + command.arg("list").arg("--parseable").arg("--long").arg("--depth=0"); |
| 45 | + if global { |
| 46 | + command.arg("--global"); |
| 47 | + } |
| 48 | + let output = command.output().ok()?; |
| 49 | + let lines = String::from_utf8_lossy(&output.stdout); |
| 50 | + lines.lines().find_map(|l| l.split(':').nth(1)?.strip_prefix("eslint@")).map(|v| v.to_owned()) |
| 51 | +} |
| 52 | + |
| 53 | +fn get_eslint_version() -> Option<String> { |
| 54 | + get_eslint_version_inner(false).or_else(|| get_eslint_version_inner(true)) |
| 55 | +} |
| 56 | + |
| 57 | +pub(super) fn lint( |
| 58 | + librustdoc_path: &Path, |
| 59 | + tools_path: &Path, |
| 60 | + src_path: &Path, |
| 61 | +) -> Result<(), super::Error> { |
| 62 | + let eslint_version_path = src_path.join("ci/docker/host-x86_64/tidy/eslint.version"); |
| 63 | + let eslint_version = match std::fs::read_to_string(&eslint_version_path) { |
| 64 | + Ok(version) => version.trim().to_string(), |
| 65 | + Err(error) => { |
| 66 | + eprintln!("failed to read `{}`: {error:?}", eslint_version_path.display()); |
| 67 | + return Err(error.into()); |
| 68 | + } |
| 69 | + }; |
| 70 | + // Having the correct `eslint` version installed via `npm` isn't strictly necessary, since we're invoking it via `npx`, |
| 71 | + // but this check allows the vast majority that is not working on the rustdoc frontend to avoid the penalty of running |
| 72 | + // `eslint` in tidy. See also: https://github.com/rust-lang/rust/pull/142851 |
| 73 | + match get_eslint_version() { |
| 74 | + Some(version) => { |
| 75 | + if version != eslint_version { |
| 76 | + // unfortunatly we can't use `Error::Version` here becuse `str::trim` isn't const and |
| 77 | + // Version::required must be a static str |
| 78 | + return Err(super::Error::Generic(format!( |
| 79 | + "⚠️ Installed version of eslint (`{version}`) is different than the \ |
| 80 | + one used in the CI (`{eslint_version}`)\n\ |
| 81 | + You can install this version using `npm update eslint` or by using \ |
| 82 | + `npm install eslint@{eslint_version}`\n |
| 83 | +" |
| 84 | + ))); |
| 85 | + } |
| 86 | + } |
| 87 | + None => { |
| 88 | + //eprintln!("`eslint` doesn't seem to be installed. Skipping tidy check for JS files."); |
| 89 | + //eprintln!("You can install it using `npm install eslint@{eslint_version}`"); |
| 90 | + return Err(super::Error::MissingReq( |
| 91 | + "eslint", |
| 92 | + "js lint checks", |
| 93 | + Some(format!("You can install it using `npm install eslint@{eslint_version}`")), |
| 94 | + )); |
| 95 | + } |
| 96 | + } |
| 97 | + let files_to_check = rustdoc_js_files(librustdoc_path); |
| 98 | + println!("Running eslint on rustdoc JS files"); |
| 99 | + run_eslint(&files_to_check, librustdoc_path.join("html/static"))?; |
| 100 | + |
| 101 | + run_eslint(&[tools_path.join("rustdoc-js/tester.js")], tools_path.join("rustdoc-js"))?; |
| 102 | + run_eslint(&[tools_path.join("rustdoc-gui/tester.js")], tools_path.join("rustdoc-gui"))?; |
| 103 | + Ok(()) |
| 104 | +} |
| 105 | + |
| 106 | +pub(super) fn typecheck(librustdoc_path: &Path) -> Result<(), super::Error> { |
| 107 | + // use npx to ensure correct version |
| 108 | + let mut child = Command::new("npx") |
| 109 | + .arg("tsc") |
| 110 | + .arg("-p") |
| 111 | + .arg(librustdoc_path.join("html/static/js/tsconfig.json")) |
| 112 | + .spawn()?; |
| 113 | + match child.wait() { |
| 114 | + Ok(exit_status) => { |
| 115 | + if exit_status.success() { |
| 116 | + return Ok(()); |
| 117 | + } |
| 118 | + Err(super::Error::FailedCheck("tsc command failed")) |
| 119 | + } |
| 120 | + Err(error) => Err(super::Error::Generic(format!("tsc command failed: {error:?}"))), |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +pub(super) fn es_check(librustdoc_path: &Path) -> Result<(), super::Error> { |
| 125 | + let files_to_check = rustdoc_js_files(librustdoc_path); |
| 126 | + // use npx to ensure correct version |
| 127 | + let mut cmd = Command::new("npx"); |
| 128 | + cmd.arg("es-check").arg("es2019"); |
| 129 | + for f in files_to_check { |
| 130 | + cmd.arg(f); |
| 131 | + } |
| 132 | + match cmd.spawn()?.wait() { |
| 133 | + Ok(exit_status) => { |
| 134 | + if exit_status.success() { |
| 135 | + return Ok(()); |
| 136 | + } |
| 137 | + Err(super::Error::FailedCheck("es-check command failed")) |
| 138 | + } |
| 139 | + Err(error) => Err(super::Error::Generic(format!("es-check command failed: {error:?}"))), |
| 140 | + } |
| 141 | +} |
0 commit comments