fix(linter/next): false positives for non-custom font link (#3383)

fix: #3378

They only check google fonts. 

7e34b4cf98/packages/eslint-plugin-next/src/rules/no-page-custom-font.ts (L152-L154)
This commit is contained in:
Dunqing 2024-05-22 12:01:14 +08:00 committed by GitHub
parent 78e6326e48
commit bb2221e4f4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,5 +1,5 @@
use oxc_ast::{
ast::{Class, Function, JSXAttributeItem, JSXElementName},
ast::{Class, Function, JSXAttributeItem, JSXAttributeValue, JSXElementName},
AstKind,
};
use oxc_diagnostics::OxcDiagnostic;
@ -46,7 +46,13 @@ impl Rule for NoPageCustomFont {
if matches!(&element.name, JSXElementName::Identifier(ident) if ident.name != "link") {
return;
}
let is_custom_font = element.attributes.iter().any(|attr| matches!(&attr, JSXAttributeItem::Attribute(attr) if attr.is_identifier("href") && attr.value.is_some()));
let is_custom_font = element.attributes.iter().any(|attr| {
matches!(&attr,
JSXAttributeItem::Attribute(attr) if attr.is_identifier("href") && attr.value.as_ref().is_some_and(|value|
matches!(value, JSXAttributeValue::StringLiteral(literal) if literal.value.starts_with("https://fonts.googleapis.com/css"))
))
});
if !is_custom_font {
return;
@ -246,6 +252,23 @@ fn test() {
None,
filename.clone(),
),
(
r#"function a() {
return (
<Html>
<Head>
<link
rel="apple-touch-startup-image"
href="/assets/public/pwa/splash/apple-splash-2048-2732.jpg"
/>
</Head>
</Html>
)
}"#,
None,
None,
filename.clone(),
),
];
let fail = vec![