From c5c4b55c2b3a3fb824c3fd64e33bb30f1b011b71 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 2 Oct 2021 20:53:19 +0530 Subject: new lint: legacy_let_syntax --- lib/src/lib.rs | 43 +++++++++++++++++++++-------- lib/src/lints.rs | 1 + lib/src/lints/legacy_let_syntax.rs | 55 ++++++++++++++++++++++++++++++++++++++ lib/src/make.rs | 45 ++++++++++++++++++++++++++++--- 4 files changed, 129 insertions(+), 15 deletions(-) create mode 100644 lib/src/lints/legacy_let_syntax.rs (limited to 'lib/src') diff --git a/lib/src/lib.rs b/lib/src/lib.rs index ab4a14a..f0f26dd 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -4,7 +4,7 @@ mod make; pub use lints::LINTS; use rnix::{SyntaxElement, SyntaxKind, TextRange}; -use std::{default::Default, convert::Into}; +use std::{convert::Into, default::Default}; /// Report generated by a lint #[derive(Debug, Default)] @@ -32,11 +32,16 @@ impl Report { self } /// Add a diagnostic with a fix to this report - pub fn suggest>(mut self, at: TextRange, message: S, suggestion: Suggestion) -> Self { - self.diagnostics.push(Diagnostic::suggest(at, message, suggestion)); + pub fn suggest>( + mut self, + at: TextRange, + message: S, + suggestion: Suggestion, + ) -> Self { + self.diagnostics + .push(Diagnostic::suggest(at, message, suggestion)); self } - } /// Mapping from a bytespan to an error message. @@ -51,11 +56,19 @@ pub struct Diagnostic { impl Diagnostic { /// Construct a diagnostic. pub fn new(at: TextRange, message: String) -> Self { - Self { at, message, suggestion: None } + Self { + at, + message, + suggestion: None, + } } /// Construct a diagnostic with a fix. pub fn suggest>(at: TextRange, message: S, suggestion: Suggestion) -> Self { - Self { at, message: message.as_ref().into(), suggestion: Some(suggestion) } + Self { + at, + message: message.as_ref().into(), + suggestion: Some(suggestion), + } } } @@ -72,7 +85,7 @@ impl Suggestion { pub fn new>(at: TextRange, fix: E) -> Self { Self { at, - fix: fix.into() + fix: fix.into(), } } } @@ -86,10 +99,18 @@ pub trait Rule { /// Contains information about the lint itself. Do not implement manually, /// look at the `lint` attribute macro instead for implementing rules pub trait Metadata { - fn name() -> &'static str where Self: Sized; - fn note() -> &'static str where Self: Sized; - fn code() -> u32 where Self: Sized; - fn report() -> Report where Self: Sized; + fn name() -> &'static str + where + Self: Sized; + fn note() -> &'static str + where + Self: Sized; + fn code() -> u32 + where + Self: Sized; + fn report() -> Report + where + Self: Sized; fn match_with(&self, with: &SyntaxKind) -> bool; fn match_kind(&self) -> SyntaxKind; } diff --git a/lib/src/lints.rs b/lib/src/lints.rs index 30c1678..98aa404 100644 --- a/lib/src/lints.rs +++ b/lib/src/lints.rs @@ -5,4 +5,5 @@ lint_map! { empty_let_in, manual_inherit, manual_inherit_from, + legacy_let_syntax, } diff --git a/lib/src/lints/legacy_let_syntax.rs b/lib/src/lints/legacy_let_syntax.rs new file mode 100644 index 0000000..c5588e2 --- /dev/null +++ b/lib/src/lints/legacy_let_syntax.rs @@ -0,0 +1,55 @@ +use crate::{make, Lint, Metadata, Report, Rule, Suggestion}; + +use if_chain::if_chain; +use macros::lint; +use rnix::{ + types::{EntryHolder, Ident, Key, LegacyLet, TokenWrapper, TypedNode}, + NodeOrToken, SyntaxElement, SyntaxKind, +}; + +#[lint( + name = "legacy let syntax", + note = "Using undocumented `let` syntax", + code = 5, + match_with = SyntaxKind::NODE_LEGACY_LET +)] +struct ManualInherit; + +impl Rule for ManualInherit { + fn validate(&self, node: &SyntaxElement) -> Option { + if_chain! { + if let NodeOrToken::Node(node) = node; + if let Some(legacy_let) = LegacyLet::cast(node.clone()); + + if legacy_let + .entries() + .find(|kv| matches!(kv.key(), Some(k) if key_is_ident(&k, "body"))) + .is_some(); + + then { + let inherits = legacy_let.inherits(); + let entries = legacy_let.entries(); + let attrset = make::attrset(inherits, entries, true); + let parenthesized = make::parenthesize(&attrset.node()); + let selected = make::select(parenthesized.node(), &make::ident("body").node()); + + let at = node.text_range(); + let message = "Prefer `rec` over undocumented `let` syntax"; + let replacement = selected.node().clone(); + + Some(Self::report().suggest(at, message, Suggestion::new(at, replacement))) + } else { + None + } + } + } +} + +fn key_is_ident(key_path: &Key, ident: &str) -> bool { + if let Some(key_node) = key_path.path().next() { + if let Some(key) = Ident::cast(key_node) { + return key.as_str() == ident; + } + } + false +} diff --git a/lib/src/make.rs b/lib/src/make.rs index cf371a2..2a6450c 100644 --- a/lib/src/make.rs +++ b/lib/src/make.rs @@ -1,13 +1,20 @@ -use std::iter::IntoIterator; +use std::{fmt::Write, iter::IntoIterator}; -use rnix::{SyntaxNode, types::{TypedNode, self, TokenWrapper}}; +use rnix::{ + types::{self, TokenWrapper, TypedNode}, + SyntaxNode, +}; fn ast_from_text(text: &str) -> N { let parse = rnix::parse(text); let node = match parse.node().descendants().find_map(N::cast) { Some(it) => it, None => { - panic!("Failed to make ast node `{}` from text `{}`", std::any::type_name::(), text) + panic!( + "Failed to make ast node `{}` from text `{}`", + std::any::type_name::(), + text + ) } }; node @@ -30,7 +37,10 @@ pub fn inherit_stmt<'a>(nodes: impl IntoIterator) -> ty ast_from_text(&format!("{{ inherit {}; }}", inherited_idents)) } -pub fn inherit_from_stmt<'a>(from: SyntaxNode, nodes: impl IntoIterator) -> types::Inherit { +pub fn inherit_from_stmt<'a>( + from: SyntaxNode, + nodes: impl IntoIterator, +) -> types::Inherit { let inherited_idents = nodes .into_iter() .map(|i| i.as_str().to_owned()) @@ -38,3 +48,30 @@ pub fn inherit_from_stmt<'a>(from: SyntaxNode, nodes: impl IntoIterator, + entries: impl IntoIterator, + recursive: bool, +) -> types::AttrSet { + let mut buffer = String::new(); + + writeln!(buffer, "{}{{", if recursive { "rec " } else { "" }).unwrap(); + for inherit in inherits.into_iter() { + writeln!(buffer, " {}", inherit.node().text()).unwrap(); + } + for entry in entries.into_iter() { + writeln!(buffer, " {}", entry.node().text()).unwrap(); + } + write!(buffer, "}}").unwrap(); + + ast_from_text(&buffer) +} + +pub fn select(set: &SyntaxNode, index: &SyntaxNode) -> types::Select { + ast_from_text(&format!("{}.{}", set, index)) +} + +pub fn ident(text: &str) -> types::Ident { + ast_from_text(text) +} -- cgit v1.2.3