Skip to content

Commit 710aee7

Browse files
committed
Fix lints task
1 parent 59953b5 commit 710aee7

File tree

4 files changed

+66
-20
lines changed

4 files changed

+66
-20
lines changed

rust/src/minify.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl<'a> Minifier<'a> {
5454
#[inline]
5555
pub fn minify_crate_name(&self, name: &str) -> String {
5656
let vec: Vec<&str> = name
57-
.split(|c| c == '_' || c == '-')
57+
.split(['_', '-'])
5858
.map(|item| self.mapping.get(item).map(Deref::deref).unwrap_or(item))
5959
.collect();
6060
vec.join("_")

rust/src/tasks/crates.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl WordCollector {
5454

5555
fn collect_crate_name(&mut self, name: &str) {
5656
self.words.extend(
57-
name.split(|c| c == '_' || c == '-')
57+
name.split(['_', '-'])
5858
.filter(|c| c.len() >= 3)
5959
.map(String::from)
6060
.collect::<Vec<_>>(),

rust/src/tasks/labels.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl LabelsTask {
5959
for page in 1..=MAX_PAGE {
6060
labels.extend(
6161
client
62-
.get(&API.replace("{}", &page.to_string()))
62+
.get(API.replace("{}", &page.to_string()))
6363
.send()
6464
.await?
6565
.json::<Vec<Label>>()

rust/src/tasks/lints.rs

+63-17
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
use argh::FromArgs;
22
use rayon::prelude::*;
3-
use serde::Deserialize;
4-
use std::clone::Clone;
3+
use select::document::Document;
4+
use select::predicate::{Class, Name, Predicate};
55
use std::collections::HashMap;
6+
use std::fmt::{self, Formatter};
67
use std::fs;
78
use std::path::Path;
89
use tokio::runtime::Runtime;
910

1011
use crate::minify::Minifier;
1112
use crate::tasks::Task;
1213

13-
const LINT_URL: &str = "https://rust-lang.github.io/rust-clippy/stable/lints.json";
14-
const LINTS_INDEX_PATH: &str = "../extension/index/lints.js";
14+
const LINT_URL: &str = "https://rust-lang.github.io/rust-clippy/stable/index.html";
15+
const LINTS_INDEX_PATH: &str = "../lib/index/lints.js";
1516

1617
/// Lint task
1718
#[derive(FromArgs)]
@@ -22,8 +23,7 @@ pub struct LintsTask {
2223
dest_path: String,
2324
}
2425

25-
#[derive(Deserialize, Debug)]
26-
#[serde(rename_all = "lowercase")]
26+
#[derive(Debug)]
2727
enum LintLevel {
2828
Allow,
2929
Warn,
@@ -32,27 +32,70 @@ enum LintLevel {
3232
None,
3333
}
3434

35-
impl ToString for LintLevel {
36-
fn to_string(&self) -> String {
35+
impl fmt::Display for LintLevel {
36+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
3737
match self {
38-
Self::Allow => "Allow".to_string(),
39-
Self::Warn => "Warn".to_string(),
40-
Self::Deny => "Deny".to_string(),
41-
Self::Deprecated => "Deprecated".to_string(),
42-
Self::None => "None".to_string(),
38+
Self::Allow => write!(f, "Allow"),
39+
Self::Warn => write!(f, "Warn"),
40+
Self::Deny => write!(f, "Deny"),
41+
Self::Deprecated => write!(f, "Deprecated"),
42+
Self::None => write!(f, "None"),
4343
}
4444
}
4545
}
4646

47-
#[derive(Deserialize, Debug)]
4847
struct Lint {
4948
id: String,
5049
level: LintLevel,
5150
docs: Option<String>,
5251
}
5352

5453
async fn fetch_clippy_lints() -> crate::Result<Vec<Lint>> {
55-
let lints = reqwest::get(LINT_URL).await?.json().await?;
54+
let html = reqwest::get(LINT_URL).await?.text().await?;
55+
let document = Document::from(html.as_str());
56+
57+
// Define custom predicate for articles that have panel class
58+
let article_panel = Name("article").and(Class("panel"));
59+
60+
let lints: Vec<Lint> = document
61+
.find(article_panel)
62+
.filter_map(|article| {
63+
// Get lint ID from the panel-title-name span
64+
let id = article
65+
.find(Class("panel-title-name"))
66+
.next()?
67+
.find(Name("span"))
68+
.next()?
69+
.text()
70+
.trim()
71+
.to_string();
72+
73+
// Get lint level from label-lint-level class
74+
let level = article
75+
.find(Class("label-lint-level"))
76+
.next()?
77+
.text()
78+
.trim()
79+
.to_lowercase();
80+
81+
let level = match level.as_str() {
82+
"allow" => LintLevel::Allow,
83+
"warn" => LintLevel::Warn,
84+
"deny" => LintLevel::Deny,
85+
"deprecated" => LintLevel::Deprecated,
86+
_ => LintLevel::None,
87+
};
88+
89+
// Get full documentation from lint-doc-md class
90+
let docs = article
91+
.find(Class("lint-doc-md"))
92+
.next()
93+
.map(|doc| doc.text());
94+
95+
Some(Lint { id, level, docs })
96+
})
97+
.collect();
98+
5699
Ok(lints)
57100
}
58101

@@ -73,7 +116,7 @@ impl LintsTask {
73116
if let Some(docs) = lint
74117
.docs
75118
.as_ref()
76-
.and_then(|d| d.trim().strip_prefix("### What it does"))
119+
.and_then(|d| d.trim().strip_prefix("What it does"))
77120
{
78121
let mut desc = docs.replace(['`', '#'], "");
79122
desc.truncate(100);
@@ -90,7 +133,10 @@ impl LintsTask {
90133
})
91134
.collect();
92135

93-
let contents = format!("const lintsIndex={};export default lintsIndex;", serde_json::to_string(&lints)?);
136+
let contents = format!(
137+
"const lintsIndex={};export default lintsIndex;",
138+
serde_json::to_string(&lints)?
139+
);
94140
let path = Path::new(&self.dest_path);
95141
fs::write(path, Minifier::minify_js(&contents))?;
96142
println!("\nGenerate javascript lints index successful!");

0 commit comments

Comments
 (0)