feat(lint): add warning for vf font (#1649)

* feat(lint): add warning for vf font

* feat: detect font object

---------

Co-authored-by: Myriad-Dreamin <camiyoru@gmail.com>
This commit is contained in:
Wenzhuo Liu 2025-04-12 20:55:39 +08:00 committed by GitHub
parent 1cd566aedc
commit 58ec6ab1e1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 114 additions and 0 deletions

View file

@ -195,6 +195,47 @@ impl Linter {
ty: self.ti.type_of_span(expr.span()),
}
}
fn check_variable_font<'a>(&mut self, args: impl IntoIterator<Item = ast::Arg<'a>>) {
for arg in args {
if let ast::Arg::Named(arg) = arg {
if arg.name().as_str() == "font" {
self.check_variable_font_object(arg.expr().to_untyped());
if let Some(array) = arg.expr().to_untyped().cast::<ast::Array>() {
for item in array.items() {
self.check_variable_font_object(item.to_untyped());
}
}
}
}
}
}
fn check_variable_font_object(&mut self, expr: &SyntaxNode) -> Option<()> {
if let Some(font_dict) = expr.cast::<ast::Dict>() {
for item in font_dict.items() {
if let ast::DictItem::Named(arg) = item {
if arg.name().as_str() == "name" {
self.check_variable_font_str(arg.expr().to_untyped());
}
}
}
}
self.check_variable_font_str(expr)
}
fn check_variable_font_str(&mut self, expr: &SyntaxNode) -> Option<()> {
if !expr.cast::<ast::Str>()?.get().ends_with("VF") {
return None;
}
let diag =
SourceDiagnostic::warning(expr.span(), "variable font is not supported by typst yet");
let diag = diag.with_hint("consider using a static font instead. For more information, see https://github.com/typst/typst/issues/185");
self.diag.push(diag);
Some(())
}
}
impl DataFlowVisitor for Linter {
@ -210,6 +251,11 @@ impl DataFlowVisitor for Linter {
self.expr(target);
}
self.exprs(expr.args().to_untyped().exprs());
if expr.target().to_untyped().text() == "text" {
self.check_variable_font(expr.args().items());
}
self.expr(expr.target())
}
@ -311,6 +357,15 @@ impl DataFlowVisitor for Linter {
self.check_type_compare(expr);
self.exprs([expr.lhs(), expr.rhs()].into_iter())
}
fn func_call(&mut self, expr: ast::FuncCall<'_>) -> Option<()> {
// warn if text(font: ("Font Name", "Font Name")) in which Font Name ends with
// "VF"
if expr.callee().to_untyped().text() == "text" {
self.check_variable_font(expr.args().items());
}
Some(())
}
}
struct LateFuncLinter<'a> {

View file

@ -0,0 +1,5 @@
#text(font: "DejaVu Sans Mono")[123]
#text(font: ("DejaVu Sans Mono",))[123]
#text(font: (name: "DejaVu Sans Mono"))[123]
#text(font: ((name: "DejaVu Sans Mono"),))[123]

View file

@ -0,0 +1,6 @@
---
source: crates/tinymist-query/src/analysis.rs
expression: "JsonRepr::new_redacted(result, &REDACT_LOC)"
input_file: crates/tinymist-query/src/fixtures/lint/good_font.typ
---
{}

View file

@ -0,0 +1,21 @@
---
source: crates/tinymist-query/src/analysis.rs
expression: "JsonRepr::new_redacted(result, &REDACT_LOC)"
input_file: crates/tinymist-query/src/fixtures/lint/vf.typ
---
{
"s0.typ": [
{
"message": "variable font is not supported by typst yet\nHint: consider using a static font instead. For more information, see https://github.com/typst/typst/issues/185",
"range": "0:16:0:30",
"severity": 2,
"source": "typst"
},
{
"message": "variable font is not supported by typst yet\nHint: consider using a static font instead. For more information, see https://github.com/typst/typst/issues/185",
"range": "2:22:2:36",
"severity": 2,
"source": "typst"
}
]
}

View file

@ -0,0 +1,21 @@
---
source: crates/tinymist-query/src/analysis.rs
expression: "JsonRepr::new_redacted(result, &REDACT_LOC)"
input_file: crates/tinymist-query/src/fixtures/lint/vf_object.typ
---
{
"s0.typ": [
{
"message": "variable font is not supported by typst yet\nHint: consider using a static font instead. For more information, see https://github.com/typst/typst/issues/185",
"range": "0:20:0:34",
"severity": 2,
"source": "typst"
},
{
"message": "variable font is not supported by typst yet\nHint: consider using a static font instead. For more information, see https://github.com/typst/typst/issues/185",
"range": "1:19:1:33",
"severity": 2,
"source": "typst"
}
]
}

View file

@ -0,0 +1,3 @@
#set text(font: "Noto Sans VF")
#text(font: ("Arial", "Noto Sans VF"))[123]

View file

@ -0,0 +1,3 @@
#text(font: ((name: "Noto Sans VF"),))[123]
#text(font: (name: "Noto Sans VF"))[123]