-
-
Notifications
You must be signed in to change notification settings - Fork 968
feat(lint/css): add noDeprecatedMediaType
#8861
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@biomejs/biome": patch | ||
| --- | ||
|
|
||
| Added the `noDeprecatedMediaType` CSS rule to flag deprecated media types like `tv` and `handheld`. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
crates/biome_css_analyze/src/lint/nursery/no_deprecated_media_type.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| use biome_analyze::{ | ||
| Ast, Rule, RuleDiagnostic, RuleSource, context::RuleContext, declare_lint_rule, | ||
| }; | ||
| use biome_console::markup; | ||
| use biome_css_syntax::CssMediaType; | ||
| use biome_rowan::AstNode; | ||
| use biome_rule_options::no_deprecated_media_type::NoDeprecatedMediaTypeOptions; | ||
| use biome_string_case::StrLikeExtension; | ||
|
|
||
| declare_lint_rule! { | ||
| /// Disallow deprecated media types. | ||
| /// | ||
| /// Several media types defined in earlier specifications have been deprecated and should | ||
| /// no longer be used. The deprecated media types are still recognized, but they match nothing. | ||
| /// | ||
| /// For details on media types, see the | ||
| /// [Media Queries Level 5 specification](https://drafts.csswg.org/mediaqueries-5/#media-types). | ||
| /// | ||
| /// ## Examples | ||
| /// | ||
| /// ### Invalid | ||
| /// | ||
| /// ```css,expect_diagnostic | ||
| /// @media tv {} | ||
| /// ``` | ||
| /// | ||
| /// ```css,expect_diagnostic | ||
| /// @media handheld and (min-width: 480px) {} | ||
| /// ``` | ||
| /// | ||
| /// ### Valid | ||
| /// | ||
| /// ```css | ||
| /// @media screen {} | ||
| /// ``` | ||
| /// | ||
| /// ```css | ||
| /// @media print and (min-resolution: 300dpi) {} | ||
| /// ``` | ||
| /// | ||
| /// ## Options | ||
| /// | ||
| /// ### `allow` | ||
| /// | ||
| /// Media types to allow (case-insensitive). | ||
| /// | ||
| /// ```json,options | ||
| /// { | ||
| /// "options": { | ||
| /// "allow": ["tv", "speech"] | ||
| /// } | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| /// #### Valid | ||
| /// | ||
| /// ```css,use_options | ||
| /// @media tv {} | ||
| /// @media speech {} | ||
| /// ``` | ||
| /// | ||
| pub NoDeprecatedMediaType { | ||
| version: "next", | ||
| name: "noDeprecatedMediaType", | ||
| language: "css", | ||
| recommended: false, | ||
| sources: &[RuleSource::Stylelint("media-type-no-deprecated").same()], | ||
| } | ||
| } | ||
|
|
||
| impl Rule for NoDeprecatedMediaType { | ||
| type Query = Ast<CssMediaType>; | ||
| type State = (); | ||
| type Signals = Option<Self::State>; | ||
| type Options = NoDeprecatedMediaTypeOptions; | ||
|
|
||
| fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
| let node = ctx.query(); | ||
| let media_type = node.value().ok().and_then(|v| v.value_token().ok())?; | ||
| let media_type = media_type.text_trimmed(); | ||
|
|
||
| // Check allow list from options | ||
| if let Some(allow_list) = &ctx.options().allow { | ||
| for allowed in allow_list { | ||
| if media_type.eq_ignore_ascii_case(allowed) { | ||
| return None; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // FIXME: Optimize to avoid allocation | ||
| let media_type = media_type.to_ascii_lowercase_cow(); | ||
| if DEPRECATED_MEDIA_TYPES | ||
| .binary_search(&media_type.as_ref()) | ||
| .is_ok() | ||
| { | ||
| return Some(()); | ||
| } | ||
|
|
||
| None | ||
| } | ||
|
|
||
| fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> { | ||
| let media_type = ctx | ||
| .query() | ||
| .value() | ||
| .ok() | ||
| .and_then(|v| v.value_token().ok())?; | ||
| let media_type = media_type.text_trimmed(); | ||
| Some( | ||
| RuleDiagnostic::new( | ||
| rule_category!(), | ||
| ctx.query().range(), | ||
| markup! { | ||
| "Unexpected deprecated media type: "<Emphasis>{ media_type }</Emphasis> | ||
| }, | ||
| ) | ||
| .note(markup! { | ||
| "Deprecated media types are recognized but match nothing; prefer using media features or recommended media types." | ||
| }) | ||
| .footer_list( | ||
| markup! { | ||
| "Recommended media types include:" | ||
| }, | ||
| ["all", "print", "screen"], | ||
| ), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| const DEPRECATED_MEDIA_TYPES: [&str; 8] = [ | ||
| "aural", | ||
| "braille", | ||
| "embossed", | ||
| "handheld", | ||
| "projection", | ||
| "speech", | ||
| "tty", | ||
| "tv", | ||
| ]; | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn builtin_list_is_sorted() { | ||
| assert!(DEPRECATED_MEDIA_TYPES.is_sorted()); | ||
|
dyc3 marked this conversation as resolved.
|
||
| } | ||
| } | ||
4 changes: 4 additions & 0 deletions
4
crates/biome_css_analyze/tests/specs/nursery/noDeprecatedMediaType/invalid.css
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| /* should generate diagnostics */ | ||
| @media tv {} | ||
| @media handheld and (min-width: 480px) {} | ||
| @media speech {} |
86 changes: 86 additions & 0 deletions
86
crates/biome_css_analyze/tests/specs/nursery/noDeprecatedMediaType/invalid.css.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| --- | ||
| source: crates/biome_css_analyze/tests/spec_tests.rs | ||
| expression: invalid.css | ||
| --- | ||
| # Input | ||
| ```css | ||
| /* should generate diagnostics */ | ||
| @media tv {} | ||
| @media handheld and (min-width: 480px) {} | ||
| @media speech {} | ||
|
|
||
| ``` | ||
|
|
||
| # Diagnostics | ||
| ``` | ||
| invalid.css:2:8 lint/nursery/noDeprecatedMediaType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i Unexpected deprecated media type: tv | ||
|
|
||
| 1 │ /* should generate diagnostics */ | ||
| > 2 │ @media tv {} | ||
| │ ^^ | ||
| 3 │ @media handheld and (min-width: 480px) {} | ||
| 4 │ @media speech {} | ||
|
|
||
| i Deprecated media types are recognized but match nothing; prefer using media features or recommended media types. | ||
|
|
||
| i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information. | ||
|
|
||
| i Recommended media types include: | ||
|
|
||
| - all | ||
| - screen | ||
|
|
||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| invalid.css:3:8 lint/nursery/noDeprecatedMediaType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i Unexpected deprecated media type: handheld | ||
|
|
||
| 1 │ /* should generate diagnostics */ | ||
| 2 │ @media tv {} | ||
| > 3 │ @media handheld and (min-width: 480px) {} | ||
| │ ^^^^^^^^ | ||
| 4 │ @media speech {} | ||
| 5 │ | ||
|
|
||
| i Deprecated media types are recognized but match nothing; prefer using media features or recommended media types. | ||
|
|
||
| i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information. | ||
|
|
||
| i Recommended media types include: | ||
|
|
||
| - all | ||
| - screen | ||
|
|
||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| invalid.css:4:8 lint/nursery/noDeprecatedMediaType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i Unexpected deprecated media type: speech | ||
|
|
||
| 2 │ @media tv {} | ||
| 3 │ @media handheld and (min-width: 480px) {} | ||
| > 4 │ @media speech {} | ||
| │ ^^^^^^ | ||
| 5 │ | ||
|
|
||
| i Deprecated media types are recognized but match nothing; prefer using media features or recommended media types. | ||
|
|
||
| i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information. | ||
|
|
||
| i Recommended media types include: | ||
|
|
||
| - all | ||
| - screen | ||
|
|
||
|
|
||
| ``` |
2 changes: 2 additions & 0 deletions
2
crates/biome_css_analyze/tests/specs/nursery/noDeprecatedMediaType/valid-allowed.css
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| /* should not generate diagnostics */ | ||
| @media speech {} |
10 changes: 10 additions & 0 deletions
10
crates/biome_css_analyze/tests/specs/nursery/noDeprecatedMediaType/valid-allowed.css.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| --- | ||
| source: crates/biome_css_analyze/tests/spec_tests.rs | ||
| expression: valid-allowed.css | ||
| --- | ||
| # Input | ||
| ```css | ||
| /* should not generate diagnostics */ | ||
| @media speech {} | ||
|
|
||
| ``` |
15 changes: 15 additions & 0 deletions
15
...es/biome_css_analyze/tests/specs/nursery/noDeprecatedMediaType/valid-allowed.options.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "$schema": "../../../../../../packages/@biomejs/biome/configuration_schema.json", | ||
| "linter": { | ||
| "rules": { | ||
| "nursery": { | ||
| "noDeprecatedMediaType": { | ||
| "level": "error", | ||
| "options": { | ||
| "allow": ["speech"] | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
5 changes: 5 additions & 0 deletions
5
crates/biome_css_analyze/tests/specs/nursery/noDeprecatedMediaType/valid.css
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| /* should not generate diagnostics */ | ||
| @media screen {} | ||
| @media print and (min-resolution: 300dpi) {} | ||
| @media (max-width: 600px) {} | ||
| @media (min-width: 900px) {} |
13 changes: 13 additions & 0 deletions
13
crates/biome_css_analyze/tests/specs/nursery/noDeprecatedMediaType/valid.css.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| --- | ||
| source: crates/biome_css_analyze/tests/spec_tests.rs | ||
| expression: valid.css | ||
| --- | ||
| # Input | ||
| ```css | ||
| /* should not generate diagnostics */ | ||
| @media screen {} | ||
| @media print and (min-resolution: 300dpi) {} | ||
| @media (max-width: 600px) {} | ||
| @media (min-width: 900px) {} | ||
|
|
||
| ``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| use biome_deserialize_macros::{Deserializable, Merge}; | ||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| #[derive(Default, Clone, Debug, Deserialize, Deserializable, Merge, Eq, PartialEq, Serialize)] | ||
| #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] | ||
| #[serde(rename_all = "camelCase", deny_unknown_fields, default)] | ||
| pub struct NoDeprecatedMediaTypeOptions { | ||
| /// Media types to allow (case-insensitive). | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| pub allow: Option<Box<[String]>>, | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.