Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

feat(rome_analyzer): ARIA query #3832

Merged
merged 4 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ codegen = "run -p xtask_codegen --"
codegen-configuration = "run -p xtask_codegen --features configuration -- configuration"
codegen-schema = "run -p xtask_codegen --features schema -- schema"
codegen-bindings = "run -p xtask_codegen --features schema -- bindings"
codegen-aria = "run -p xtask_codegen --features aria -- aria"
lintdoc = "run -p xtask_lintdoc --"
documentation = """
doc \
Expand Down
13 changes: 11 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/rome_analyze/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ use crate::signals::DiagnosticSignal;
pub use crate::signals::{AnalyzerAction, AnalyzerSignal};
pub use crate::syntax::SyntaxVisitor;
pub use crate::visitor::{NodeVisitor, Visitor, VisitorContext, VisitorFinishContext};

use rome_console::markup;
use rome_diagnostics::{category, Applicability, DiagnosticTags, FileId};
use rome_rowan::{
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_analyze/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl<L: Language> RegistryVisitor<L> for MetadataRegistry {
/// after the "SemanticModel" is ready, which demands a whole transverse of the parsed tree.
pub struct RuleRegistry<L: Language> {
/// Holds a collection of rules for each phase.
phase_rules: [PhaseRules<L>; 2],
phase_rules: [PhaseRules<L>; 3],
ematipico marked this conversation as resolved.
Show resolved Hide resolved
}

impl<L: Language + Default> RuleRegistry<L> {
Expand Down
37 changes: 37 additions & 0 deletions crates/rome_analyze/src/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,17 @@ pub struct RuleDiagnostic {
pub struct RuleAdvice {
pub(crate) details: Vec<Detail>,
pub(crate) notes: Vec<(LogCategory, MarkupBuf)>,
pub(crate) suggestion_list: Option<SuggestionList>,
pub(crate) code_suggestion_list: Vec<CodeSuggestionAdvice<MarkupBuf>>,
}

#[derive(Debug, Default)]
pub struct SuggestionList {
pub(crate) message: MarkupBuf,
pub(crate) list: Vec<MarkupBuf>,
}

// TODO: this code will be hit once https://github.com/rome/tools/issues/3829 is closed. Make sure it works as expected.
ematipico marked this conversation as resolved.
Show resolved Hide resolved
impl Advices for RuleAdvice {
fn record(&self, visitor: &mut dyn Visit) -> std::io::Result<()> {
for detail in &self.details {
Expand All @@ -402,6 +410,19 @@ impl Advices for RuleAdvice {
visitor.record_log(*log_category, &markup! { {note} }.to_owned())?;
}

if let Some(suggestion_list) = &self.suggestion_list {
visitor.record_log(
LogCategory::Info,
&markup! { {suggestion_list.message} }.to_owned(),
)?;
let list: Vec<_> = suggestion_list
.list
.iter()
.map(|suggestion| suggestion as &dyn Display)
.collect();
visitor.record_list(&list)?;
}

// finally, we print possible code suggestions on how to fix the issue
for suggestion in &self.code_suggestion_list {
suggestion.record(visitor)?;
Expand Down Expand Up @@ -486,6 +507,22 @@ impl RuleDiagnostic {
self.footer(LogCategory::Info, msg)
}

/// It creates a new footer note which contains a message and a list of possible suggestions.
/// Useful when there's need to suggest a list of things inside a diagnostic.
pub fn footer_list(mut self, message: impl Display, list: &[impl Display]) -> Self {
if !list.is_empty() {
self.rule_advice.suggestion_list = Some(SuggestionList {
message: markup! { {message} }.to_owned(),
list: list
.iter()
.map(|msg| markup! { {msg} }.to_owned())
.collect(),
});
}

self
}

/// Adds a footer to this [`RuleDiagnostic`], with the `Warn` severity.
pub fn warning(self, msg: impl Display) -> Self {
self.footer(LogCategory::Warn, msg)
Expand Down
12 changes: 12 additions & 0 deletions crates/rome_aria/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "rome_aria"
version = "0.0.0"
edition = "2021"
authors = ["Rome Tools Developers and Contributors"]
repository = "https://github.com/rome/tools"
license = "MIT"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rustc-hash = "1.1.0"
140 changes: 140 additions & 0 deletions crates/rome_aria/src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
//! Metadata of:
//! - ARIA properties
//! - ARIA property types
//! - ARIA roles

pub const ARIA_PROPERTIES: [&str; 48] = [
"aria-activedescendant",
"aria-atomic",
"aria-autocomplete",
"aria-busy",
"aria-checked",
"aria-colcount",
"aria-colindex",
"aria-colspan",
"aria-controls",
"aria-current",
"aria-describedby",
"aria-details",
"aria-disabled",
"aria-dropeffect",
"aria-errormessage",
"aria-expanded",
"aria-flowto",
"aria-grabbed",
"aria-haspopup",
"aria-hidden",
"aria-invalid",
"aria-keyshortcuts",
"aria-label",
"aria-labelledby",
"aria-level",
"aria-live",
"aria-modal",
"aria-multiline",
"aria-multiselectable",
"aria-orientation",
"aria-owns",
"aria-placeholder",
"aria-posinset",
"aria-pressed",
"aria-readonly",
"aria-relevant",
"aria-required",
"aria-roledescription",
"aria-rowcount",
"aria-rowindex",
"aria-rowspan",
"aria-selected",
"aria-setsize",
"aria-sort",
"aria-valuemax",
"aria-valuemin",
"aria-valuenow",
"aria-valuetext",
];

pub const ARIA_PROPERTY_TYPE: [&str; 9] = [
"boolean",
"id",
"idlist",
"integer",
"number",
"string",
"token",
"tokenlist",
"tristate",
];

pub const ARIA_WIDGET_ROLES: [&str; 27] = [
"alert",
"alertdialog",
"button",
"checkbox",
"dialog",
"gridcell",
"link",
"log",
"marquee",
"menuitem",
"menuitemcheckbox",
"menuitemradio",
"option",
"progressbar",
"radio",
"scrollbar",
"searchbox",
"slider",
"spinbutton",
"status",
"switch",
"tab",
"tabpanel",
"textbox",
"timer",
"tooltip",
"treeitem",
];

pub const ARIA_ABSTRACT_ROLES: [&str; 12] = [
"command",
"composite",
"input",
"landmark",
"range",
"roletype",
"section",
"sectionhead",
"select",
"structure",
"widget",
"window",
];

pub const ARIA_DOCUMENT_STRUCTURE_ROLES: [&str; 25] = [
"article",
"cell",
"columnheader",
"definition",
"directory",
"document",
"feed",
"figure",
"group",
"heading",
"img",
"list",
"listitem",
"math",
"none",
"note",
"presentation",
"region",
"row",
"rowgroup",
"rowheader",
"separator",
"table",
"term",
"toolbar",
];
Loading