Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(js_syntax): minor clean up #4513

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion crates/biome_js_analyze/src/lint/a11y/no_access_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Rule for NoAccessKey {

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let node = ctx.query();
if node.name_value_token()?.text_trimmed() != "accessKey" {
if node.name_value_token().ok()?.text_trimmed() != "accessKey" {
return None;
}

Expand Down
4 changes: 2 additions & 2 deletions crates/biome_js_analyze/src/lint/a11y/no_autofocus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ fn find_kept_autofocus_mark(element: &AnyJsxElement) -> bool {
// 2. inside the element with the popover attribute

let is_dialog_element = match element.name_value_token() {
Some(syntax_token) => {
Ok(syntax_token) => {
let tag_name = String::from(syntax_token.text_trimmed());
tag_name.to_lowercase_cow() == "dialog"
}
None => false,
Err(_) => false,
};

let has_popover_attr = element.has_truthy_attribute("popover");
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_js_analyze/src/lint/a11y/no_blank_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl Rule for NoBlankTarget {

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let node = ctx.query();
if node.name_value_token()?.text_trimmed() != "a"
if node.name_value_token().ok()?.text_trimmed() != "a"
|| node.find_attribute_by_name("href").is_none()
{
return None;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl Rule for NoDistractingElements {

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let element = ctx.query();
let name = element.name_value_token()?;
let name = element.name_value_token().ok()?;
match name.text_trimmed() {
"marquee" | "blink" => Some(name),
_ => None,
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_js_analyze/src/lint/a11y/no_header_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Rule for NoHeaderScope {
let element = ctx.query();

if element.is_element()
&& element.name_value_token()?.text_trimmed() != "th"
&& element.name_value_token().ok()?.text_trimmed() != "th"
&& element.has_truthy_attribute("scope")
{
return Some(());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl Rule for NoLabelWithoutControl {
fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let node = ctx.query();
let options = ctx.options();
let element_name = node.name()?.name_value_token()?;
let element_name = node.name()?.name_value_token().ok()?;
let element_name = element_name.text_trimmed();
let is_allowed_element = options.has_element_name(element_name)
|| DEFAULT_LABEL_COMPONENTS.contains(&element_name);
Expand Down Expand Up @@ -237,7 +237,7 @@ impl NoLabelWithoutControlOptions {
child_iter.skip_subtree();
continue;
};
let Some(element_name) = element_name.name_value_token() else {
let Ok(element_name) = element_name.name_value_token() else {
continue;
};
let element_name = element_name.text_trimmed();
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_js_analyze/src/lint/a11y/no_redundant_alt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Rule for NoRedundantAlt {

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let node = ctx.query();
if node.name_value_token()?.text_trimmed() != "img" {
if node.name_value_token().ok()?.text_trimmed() != "img" {
return None;
}
let aria_hidden_attribute = node.find_attribute_by_name("aria-hidden");
Expand Down
4 changes: 2 additions & 2 deletions crates/biome_js_analyze/src/lint/a11y/no_svg_without_title.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl Rule for NoSvgWithoutTitle {
fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let node = ctx.query();

if node.name_value_token()?.text_trimmed() != "svg" {
if node.name_value_token().ok()?.text_trimmed() != "svg" {
return None;
}

Expand Down Expand Up @@ -196,7 +196,7 @@ fn is_valid_attribute_value(
.filter_map(|child| {
let jsx_element = child.as_jsx_element()?;
let opening_element = jsx_element.opening_element().ok()?;
let maybe_attribute = opening_element.find_attribute_by_name("id").ok()?;
let maybe_attribute = opening_element.find_attribute_by_name("id");
let child_attribute_value = maybe_attribute?.initializer()?.value().ok()?;
let is_valid = attribute_value.as_static_value()?.text()
== child_attribute_value.as_static_value()?.text();
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_js_analyze/src/lint/a11y/use_alt_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl Rule for UseAltText {
let has_aria_label = has_valid_label(element, "aria-label");
let has_aria_labelledby = has_valid_label(element, "aria-labelledby");
let aria_hidden = is_aria_hidden(element);
match element.name_value_token()?.text_trimmed() {
match element.name_value_token().ok()?.text_trimmed() {
"object" => {
let has_title = has_valid_label(element, "title");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl Rule for UseAnchorContent {

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let node = ctx.query();
let name = node.name().ok()?.name_value_token()?;
let name = node.name().ok()?.name_value_token().ok()?;

if name.text_trimmed() == "a" {
if node.has_truthy_attribute("aria-hidden") {
Expand Down
4 changes: 2 additions & 2 deletions crates/biome_js_analyze/src/lint/a11y/use_button_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl Rule for UseButtonType {
if !is_button(&name)? {
return None;
}
let type_attribute = element.find_attribute_by_name("type").ok()?;
let type_attribute = element.find_attribute_by_name("type");
let Some(attribute) = type_attribute else {
let has_spread_prop = element
.attributes()
Expand All @@ -92,7 +92,7 @@ impl Rule for UseButtonType {
if !is_button(&name)? {
return None;
}
let type_attribute = element.find_attribute_by_name("type").ok()?;
let type_attribute = element.find_attribute_by_name("type");
let Some(attribute) = type_attribute else {
let has_spread_prop = element
.attributes()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl Rule for UseHeadingContent {

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let node = ctx.query();
let name = node.name().ok()?.name_value_token()?;
let name = node.name().ok()?.name_value_token().ok()?;

if HEADING_ELEMENTS.contains(&name.text_trimmed()) {
if node.has_truthy_attribute("aria-hidden") {
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_js_analyze/src/lint/a11y/use_html_lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl Rule for UseHtmlLang {

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let element = ctx.query();
let name = element.name().ok()?.name_value_token()?;
let name = element.name().ok()?.name_value_token().ok()?;

if name.text_trimmed() == "html" {
if let Some(lang_attribute) = element.find_attribute_by_name("lang") {
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_js_analyze/src/lint/a11y/use_iframe_title.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl Rule for UseIframeTitle {

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let element = ctx.query();
let name = element.name().ok()?.name_value_token()?;
let name = element.name().ok()?.name_value_token().ok()?;

if name.text_trimmed() == "iframe" {
if let Some(lang_attribute) = element.find_attribute_by_name("title") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,9 @@ impl Rule for UseKeyWithClickEvents {
}

let attributes = element.attributes();
let on_click_attribute = attributes.find_by_name("onClick").ok()?;

#[allow(clippy::question_mark)]
if on_click_attribute.is_none() {
#[expect(clippy::question_mark)]
if attributes.find_by_name("onClick").is_none() {
return None;
}

Expand Down
8 changes: 5 additions & 3 deletions crates/biome_js_analyze/src/lint/a11y/use_media_caption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ impl Rule for UseMediaCaption {
fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let node = ctx.query();

let has_audio_or_video =
matches!(node.name_value_token()?.text_trimmed(), "video" | "audio");
let has_audio_or_video = matches!(
node.name_value_token().ok()?.text_trimmed(),
"video" | "audio"
);
let has_muted = node.find_attribute_by_name("muted").is_some();
let has_spread_prop = node
.attributes()
Expand Down Expand Up @@ -78,7 +80,7 @@ impl Rule for UseMediaCaption {
_ => None,
}?;

let has_track = any_jsx.name_value_token()?.text_trimmed() == "track";
let has_track = any_jsx.name_value_token().ok()?.text_trimmed() == "track";
let has_valid_kind = &any_jsx
.find_attribute_by_name("kind")?
.initializer()?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Rule for UseSemanticElements {

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let node = ctx.query();
let role_attribute = node.find_attribute_by_name("role").ok().flatten()?;
let role_attribute = node.find_attribute_by_name("role")?;
let role_value = role_attribute.as_static_value()?;
let role_value = role_value.as_string_constant()?;

Expand Down
2 changes: 1 addition & 1 deletion crates/biome_js_analyze/src/lint/a11y/use_valid_anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl Rule for UseValidAnchor {

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let node = ctx.query();
let name = node.name().ok()?.name_value_token()?;
let name = node.name().ok()?.name_value_token().ok()?;

if name.text_trimmed() == "a" {
let anchor_attribute = node.find_attribute_by_name("href");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,10 @@ impl Rule for NoVoidElementsWithChildren {
let name = name.as_jsx_name()?.value_token().ok()?;
let name = name.text_trimmed();
if is_void_dom_element(name) {
let dangerous_prop = opening_element
.find_attribute_by_name("dangerouslySetInnerHTML")
.ok()?;
let dangerous_prop =
opening_element.find_attribute_by_name("dangerouslySetInnerHTML");
let has_children = !element.children().is_empty();
let children_prop = opening_element.find_attribute_by_name("children").ok()?;
let children_prop = opening_element.find_attribute_by_name("children");
if dangerous_prop.is_some() || has_children || children_prop.is_some() {
let cause = NoVoidElementsWithChildrenCause::Jsx {
children_prop,
Expand All @@ -229,10 +228,8 @@ impl Rule for NoVoidElementsWithChildren {
let name = name.as_jsx_name()?.value_token().ok()?;
let name = name.text_trimmed();
if is_void_dom_element(name) {
let dangerous_prop = element
.find_attribute_by_name("dangerouslySetInnerHTML")
.ok()?;
let children_prop = element.find_attribute_by_name("children").ok()?;
let dangerous_prop = element.find_attribute_by_name("dangerouslySetInnerHTML");
let children_prop = element.find_attribute_by_name("children");
if dangerous_prop.is_some() || children_prop.is_some() {
let cause = NoVoidElementsWithChildrenCause::Jsx {
children_prop,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Rule for NoHeadElement {

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let element = ctx.query();
let name = element.name().ok()?.name_value_token()?;
let name = element.name().ok()?.name_value_token().ok()?;

if name.text_trimmed() == "head" {
let is_in_app_dir = ctx
Expand Down
4 changes: 2 additions & 2 deletions crates/biome_js_analyze/src/lint/nursery/no_img_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl Rule for NoImgElement {
fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let node = ctx.query();

if node.name().ok()?.name_value_token()?.text_trimmed() != "img"
if node.name().ok()?.name_value_token().ok()?.text_trimmed() != "img"
|| node.attributes().is_empty()
{
return None;
Expand All @@ -86,7 +86,7 @@ impl Rule for NoImgElement {
let Some(opening_element) = parent.opening_element().ok() else {
return Some(());
};
let name = opening_element.name().ok()?.name_value_token()?;
let name = opening_element.name().ok()?.name_value_token().ok()?;

if name.text_trimmed() == "picture" {
return None;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl Rule for UseGoogleFontDisplay {
fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let element = ctx.query();

if element.name().ok()?.name_value_token()?.text_trimmed() != "link" {
if element.name().ok()?.name_value_token().ok()?.text_trimmed() != "link" {
return None;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Rule for UseGoogleFontPreconnect {
fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let node = ctx.query();

if node.name().ok()?.name_value_token()?.text_trimmed() != "link" {
if node.name().ok()?.name_value_token().ok()?.text_trimmed() != "link" {
return None;
}

Expand Down Expand Up @@ -107,7 +107,7 @@ impl Rule for UseGoogleFontPreconnect {
let mut attributes: Vec<_> = node.attributes().iter().collect();

let last_attr_token = match attributes.last()? {
AnyJsxAttribute::JsxAttribute(a) => a.name_value_token()?,
AnyJsxAttribute::JsxAttribute(a) => a.name_value_token().ok()?,
AnyJsxAttribute::JsxSpreadAttribute(a) => a.l_curly_token().ok()?,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,15 @@ impl Rule for UseValidAutocomplete {
let input_components = &options.input_components;
match ctx.query() {
UseValidAutocompleteQuery::JsxOpeningElement(elem) => {
let elem_name = elem.name().ok()?.name_value_token()?;
let elem_name = elem.name().ok()?.name_value_token().ok()?;
let elem_name = elem_name.text_trimmed();
if !(elem_name == "input"
|| input_components.iter().any(|x| x.as_ref() == elem_name))
{
return None;
}
let attributes = elem.attributes();
let autocomplete = attributes.find_by_name("autocomplete").ok()??;
let autocomplete = attributes.find_by_name("autocomplete")?;
let _initializer = autocomplete.initializer()?;
let extract_attrs = ctx.extract_attributes(&attributes)?;
let autocomplete_values = extract_attrs.get("autocomplete")?;
Expand All @@ -183,15 +183,15 @@ impl Rule for UseValidAutocomplete {
Some(autocomplete.range())
}
UseValidAutocompleteQuery::JsxSelfClosingElement(elem) => {
let elem_name = elem.name().ok()?.name_value_token()?;
let elem_name = elem.name().ok()?.name_value_token().ok()?;
let elem_name = elem_name.text_trimmed();
if !(elem_name == "input"
|| input_components.iter().any(|x| x.as_ref() == elem_name))
{
return None;
}
let attributes = elem.attributes();
let autocomplete = attributes.find_by_name("autocomplete").ok()??;
let autocomplete = attributes.find_by_name("autocomplete")?;
let _initializer = autocomplete.initializer()?;
let extract_attrs = ctx.extract_attributes(&attributes)?;
let autocomplete_values = extract_attrs.get("autocomplete")?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,10 @@ impl AnyJsCreateElement {

opening_element
.find_attribute_by_name("dangerouslySetInnerHTML")
.ok()?
.map(DangerousProp::from)
}
AnyJsCreateElement::JsxSelfClosingElement(element) => element
.find_attribute_by_name("dangerouslySetInnerHTML")
.ok()?
.map(DangerousProp::from),
AnyJsCreateElement::JsCallExpression(call_expression) => {
let react_create_element =
Expand All @@ -135,12 +133,10 @@ impl AnyJsCreateElement {

opening_element
.find_attribute_by_name("children")
.ok()?
.map(DangerousProp::from)
}
AnyJsCreateElement::JsxSelfClosingElement(element) => element
.find_attribute_by_name("children")
.ok()?
.map(DangerousProp::from),
AnyJsCreateElement::JsCallExpression(call_expression) => {
let react_create_element =
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_js_analyze/src/services/aria.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl AriaServices {
if let Some(static_value) = initializer.as_static_value() {
static_value
.text()
.split_whitespace()
.split_ascii_whitespace()
.map(|s| AttributeValue::StaticValue(s.to_string()))
.collect()
} else {
Expand Down
Loading
Loading