Skip to content

Commit

Permalink
Make more AST properties optional in return types
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Jan 7, 2023
1 parent c13f441 commit 3fc05a9
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 24 deletions.
8 changes: 4 additions & 4 deletions node/ast.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7025,7 +7025,7 @@ export interface ImportRule {
/**
* A media query.
*/
media: MediaList;
media?: MediaList;
/**
* An optional `supports()` condition.
*/
Expand All @@ -7050,7 +7050,7 @@ export interface StyleRule<D = Declaration> {
/**
* Nested rules within the style rule.
*/
rules: Rule<D>[];
rules?: Rule<D>[];
/**
* The selectors for the style rule.
*/
Expand All @@ -7065,11 +7065,11 @@ export interface DeclarationBlock<D = Declaration> {
/**
* A list of normal declarations in the block.
*/
declarations: D[];
declarations?: D[];
/**
* A list of `!important` declarations in the block.
*/
importantDeclarations: D[];
importantDeclarations?: D[];
}
/**
* A CSS [`<position>`](https://www.w3.org/TR/css3-values/#position) value, as used in the `background-position` property, gradients, masks, etc.
Expand Down
11 changes: 8 additions & 3 deletions node/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Angle, CssColor, Rule, CustomProperty, EnvironmentVariable, Function, Image, LengthValue, MediaQuery, Declaration, Ratio, Resolution, Selector, SupportsCondition, Time, Token, TokenOrValue, UnknownAtRule, Url, Variable } from './ast';
import type { Angle, CssColor, Rule, CustomProperty, EnvironmentVariable, Function, Image, LengthValue, MediaQuery, Declaration, Ratio, Resolution, Selector, SupportsCondition, Time, Token, TokenOrValue, UnknownAtRule, Url, Variable, StyleRule, DeclarationBlock } from './ast';
import type { Targets } from './targets';

export * from './ast';
Expand Down Expand Up @@ -69,9 +69,14 @@ type ReturnedDeclaration = Declaration | {

type FindByType<Union, Name> = Union extends { type: Name } ? Union : never;
type ReturnedRule = Rule<ReturnedDeclaration>;
type RuleVisitor<R = Rule> = ((rule: R) => ReturnedRule | ReturnedRule[] | void);
type RequiredValue<Rule> = Rule extends { value: object }
? Rule['value'] extends StyleRule
? Rule & { value: Required<StyleRule> & { declarations: Required<DeclarationBlock> } }
: Rule & { value: Required<Rule['value']> }
: Rule;
type RuleVisitor<R = RequiredValue<Rule>> = ((rule: R) => ReturnedRule | ReturnedRule[] | void);
type MappedRuleVisitors = {
[Name in Exclude<Rule['type'], 'unknown' | 'custom'>]?: RuleVisitor<FindByType<Rule, Name>>;
[Name in Exclude<Rule['type'], 'unknown' | 'custom'>]?: RuleVisitor<RequiredValue<FindByType<Rule, Name>>>;
}

type UnknownVisitors = {
Expand Down
17 changes: 3 additions & 14 deletions node/test/visitor.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -658,17 +658,8 @@ test('hover media query', () => {
test('momentum scrolling', () => {
// Similar to https://github.com/yunusga/postcss-momentum-scrolling
let visitOverflow = decl => [decl, {
property: 'custom',
value: {
name: '-webkit-overflow-scrolling',
value: [{
type: 'token',
value: {
type: 'ident',
value: 'touch'
}
}]
}
property: '-webkit-overflow-scrolling',
raw: 'touch'
}];

let res = transform({
Expand Down Expand Up @@ -836,7 +827,6 @@ test('returning string values', () => {
return {
type: 'style',
value: {
rules: [],
loc: rule.loc,
selectors: [
[{ type: 'universal' }]
Expand Down Expand Up @@ -864,8 +854,7 @@ test('returning string values', () => {
property: '-webkit-animation',
raw: '3s cubic-bezier(0.25, 0.1, 0.25, 1) foo'
}
],
importantDeclarations: [],
]
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ use cssparser::*;
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
pub struct DeclarationBlock<'i> {
/// A list of `!important` declarations in the block.
#[cfg_attr(feature = "serde", serde(borrow))]
#[cfg_attr(feature = "serde", serde(borrow, default))]
pub important_declarations: Vec<Property<'i>>,
/// A list of normal declarations in the block.
#[cfg_attr(feature = "serde", serde(default))]
pub declarations: Vec<Property<'i>>,
}

Expand Down
2 changes: 1 addition & 1 deletion src/media_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::collections::{HashMap, HashSet};
use crate::serialization::ValueWrapper;

/// A [media query list](https://drafts.csswg.org/mediaqueries/#mq-list).
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Default)]
#[cfg_attr(feature = "visitor", derive(Visit), visit(visit_media_list, MEDIA_QUERIES))]
#[cfg_attr(feature = "into_owned", derive(lightningcss_derive::IntoOwned))]
#[cfg_attr(
Expand Down
1 change: 1 addition & 0 deletions src/rules/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct ImportRule<'i> {
/// An optional `supports()` condition.
pub supports: Option<SupportsCondition<'i>>,
/// A media query.
#[cfg_attr(feature = "serde", serde(default))]
pub media: MediaList<'i>,
/// The location of the rule in the source file.
#[cfg_attr(feature = "visitor", skip_visit)]
Expand Down
2 changes: 1 addition & 1 deletion src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ impl<'i, T: ToCss> ToCss for CssRule<'i, T> {
}

/// A list of CSS rules.
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize), serde(transparent))]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
pub struct CssRuleList<'i, R = DefaultAtRule>(
Expand Down
5 changes: 5 additions & 0 deletions src/rules/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,17 @@ pub struct StyleRule<'i, R = DefaultAtRule> {
/// The declarations within the style rule.
pub declarations: DeclarationBlock<'i>,
/// Nested rules within the style rule.
#[cfg_attr(feature = "serde", serde(default = "default_rule_list::<R>"))]
pub rules: CssRuleList<'i, R>,
/// The location of the rule in the source file.
#[cfg_attr(feature = "visitor", skip_visit)]
pub loc: Location,
}

fn default_rule_list<'i, R>() -> CssRuleList<'i, R> {
CssRuleList(Vec::new())
}

impl<'i, T> StyleRule<'i, T> {
pub(crate) fn minify(
&mut self,
Expand Down

0 comments on commit 3fc05a9

Please sign in to comment.