From b5e99a235425ea81bc075427f100dfe7b6b9ad91 Mon Sep 17 00:00:00 2001 From: Jean-Yves Moyen Date: Wed, 25 Oct 2023 09:37:01 +0200 Subject: [PATCH] =?UTF-8?q?Properly=20use=20`Value.HasCalculation`=20in=20?= =?UTF-8?q?CSS=E2=80=AFvalues=20(#1491)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Proprely use Value.HasCalculation * Add changeset --- .changeset/thin-melons-hope.md | 5 +++ .../alfa-css/src/value/collection/list.ts | 44 ++++++++----------- .../alfa-css/src/value/collection/tuple.ts | 29 +++++------- packages/alfa-css/src/value/resolvable.ts | 2 +- packages/alfa-css/src/value/shadow.ts | 35 ++++++--------- 5 files changed, 49 insertions(+), 66 deletions(-) create mode 100644 .changeset/thin-melons-hope.md diff --git a/.changeset/thin-melons-hope.md b/.changeset/thin-melons-hope.md new file mode 100644 index 0000000000..bf9fd45a48 --- /dev/null +++ b/.changeset/thin-melons-hope.md @@ -0,0 +1,5 @@ +--- +"@siteimprove/alfa-css": minor +--- + +**Breaking:** CSS `Value` types no longer accept a `CALC` parameter; it is automatically inferred diff --git a/packages/alfa-css/src/value/collection/list.ts b/packages/alfa-css/src/value/collection/list.ts index 9106dc08d1..44bb19cb0d 100644 --- a/packages/alfa-css/src/value/collection/list.ts +++ b/packages/alfa-css/src/value/collection/list.ts @@ -14,25 +14,24 @@ const { delimited, option, map, separatedList } = Parser; /** * @public */ -export class List - extends Value<"list", CALC> +export class List + extends Value<"list", Value.HasCalculation<[V]>> implements Iterable, - Resolvable, false>, Resolvable.Resolver> + Resolvable>, Resolvable.Resolver> { public static of( values: Iterable, - separator = " " - ): List> { - const array = Array.from(values); - return new List(array, separator, Value.hasCalculation(...array)); + separator = " ", + ): List { + return new List(Array.from(values), separator); } private readonly _values: Array; private readonly _separator: string; - private constructor(values: Array, separator: string, calculation: CALC) { - super("list", calculation); + private constructor(values: Array, separator: string) { + super("list", Value.hasCalculation(...values)); this._values = values; this._separator = separator; } @@ -42,22 +41,15 @@ export class List } public resolve( - resolver?: Resolvable.Resolver - ): List, false> { + resolver?: Resolvable.Resolver, + ): List> { return this.map( - (value) => value.resolve(resolver) as Resolvable.Resolved + (value) => value.resolve(resolver) as Resolvable.Resolved, ); } - public map( - mapper: Mapper - ): List ? false : true> { - const array = this._values.map(mapper); - const calculation = array.some((value) => - value.hasCalculation() - ) as U extends Value ? false : true; - - return new List(array, this._separator, calculation); + public map(mapper: Mapper): List { + return new List(this._values.map(mapper), this._separator); } public equals(value: List): boolean; @@ -88,7 +80,7 @@ export class List return { ...super.toJSON(), values: this._values.map( - (value) => value.toJSON() as Serializable.ToJSON + (value) => value.toJSON() as Serializable.ToJSON, ), separator: this._separator, }; @@ -121,11 +113,11 @@ export namespace List { separator: string, parseSeparator: CSSParser, lower: number, - upper: number + upper: number, ): CSSParser> { return map( separatedList(parseValue, parseSeparator, lower, upper), - (values) => List.of(values, separator) + (values) => List.of(values, separator), ); } @@ -136,12 +128,12 @@ export namespace List { export const parseCommaSeparated = ( parseValue: CSSParser, lower: number = 1, - upper: number = Infinity + upper: number = Infinity, ) => parse(parseValue, ", ", parseComma, lower, upper); export const parseSpaceSeparated = ( parseValue: CSSParser, lower: number = 1, - upper: number = Infinity + upper: number = Infinity, ) => parse(parseValue, " ", parseSpace, lower, upper); } diff --git a/packages/alfa-css/src/value/collection/tuple.ts b/packages/alfa-css/src/value/collection/tuple.ts index 99211ad66a..05bf231cb8 100644 --- a/packages/alfa-css/src/value/collection/tuple.ts +++ b/packages/alfa-css/src/value/collection/tuple.ts @@ -7,20 +7,18 @@ import { Value } from "../value"; /** * @public */ -export class Tuple, CALC extends boolean = boolean> - extends Value<"tuple", CALC> - implements Resolvable, false>, Tuple.Resolver> +export class Tuple> + extends Value<"tuple", Value.HasCalculation> + implements Resolvable>, Tuple.Resolver> { - public static of>( - ...values: Readonly - ): Tuple> { - return new Tuple(values, Value.hasCalculation(...values)); + public static of>(...values: Readonly): Tuple { + return new Tuple(values); } private readonly _values: Readonly; - private constructor(values: Readonly, calculation: CALC) { - super("tuple", calculation); + private constructor(values: Readonly) { + super("tuple", Value.hasCalculation(...values)); this._values = values; } @@ -28,12 +26,9 @@ export class Tuple, CALC extends boolean = boolean> return this._values; } - public resolve( - resolver?: Tuple.Resolver - ): Tuple, false> { - return new Tuple, false>( + public resolve(resolver?: Tuple.Resolver): Tuple> { + return new Tuple>( this._values.map((value) => value.resolve(resolver)) as Tuple.Resolved, - false ); } @@ -61,7 +56,7 @@ export class Tuple, CALC extends boolean = boolean> return { ...super.toJSON(), values: this._values.map((value) => - value.toJSON() + value.toJSON(), ) as Serializable.ToJSON, }; } @@ -80,7 +75,7 @@ export namespace Tuple { } export function isTuple>( - value: unknown + value: unknown, ): value is Tuple { return value instanceof Tuple; } @@ -94,7 +89,7 @@ export namespace Tuple { */ export type Resolved> = T extends [ infer Head extends Value, - ...infer Tail extends Array + ...infer Tail extends Array, ] ? [Resolvable.Resolved, ...Resolved] : []; diff --git a/packages/alfa-css/src/value/resolvable.ts b/packages/alfa-css/src/value/resolvable.ts index f07bf4a70e..862ba471f7 100644 --- a/packages/alfa-css/src/value/resolvable.ts +++ b/packages/alfa-css/src/value/resolvable.ts @@ -16,7 +16,7 @@ export namespace Resolvable { */ export type Resolved = V extends Resolvable ? U - : V; + : never; /** * @privateRemarks diff --git a/packages/alfa-css/src/value/shadow.ts b/packages/alfa-css/src/value/shadow.ts index d0cfbeabcb..f11155be83 100644 --- a/packages/alfa-css/src/value/shadow.ts +++ b/packages/alfa-css/src/value/shadow.ts @@ -21,9 +21,8 @@ export class Shadow< B extends Length = Length, S extends Length = Length, C extends Color = Color, - CALC extends boolean = boolean > - extends Value<"shadow", CALC> + extends Value<"shadow", Value.HasCalculation<[H, V, B, S, C]>> implements Resolvable { public static of< @@ -31,24 +30,16 @@ export class Shadow< V extends Length = H, B extends Length = Length, S extends Length = Length, - C extends Color = Color + C extends Color = Color, >( horizontal: H, vertical: V, blur: B, spread: S, color: C, - isInset: boolean - ): Shadow> { - return new Shadow( - horizontal, - vertical, - blur, - spread, - color, - isInset, - Value.hasCalculation(horizontal, vertical, blur, spread) - ); + isInset: boolean, + ): Shadow { + return new Shadow(horizontal, vertical, blur, spread, color, isInset); } private readonly _horizontal: H; @@ -65,9 +56,11 @@ export class Shadow< spread: S, color: C, isInset: boolean, - hasCalculation: CALC ) { - super("shadow", hasCalculation); + super( + "shadow", + Value.hasCalculation(horizontal, vertical, blur, spread, color), + ); this._horizontal = horizontal; this._vertical = vertical; this._blur = blur; @@ -108,7 +101,6 @@ export class Shadow< this._spread.resolve(resolver), this._color.resolve(), this._isInset, - false ); } public equals(value: unknown): value is this { @@ -161,8 +153,7 @@ export namespace Shadow { Length.Canonical, Length.Canonical, Length.Canonical, - Color.Canonical, - false + Color.Canonical >; export interface JSON extends Value.JSON<"shadow"> { horizontal: Length.JSON; @@ -188,7 +179,7 @@ export namespace Shadow { : [T, T, T, T]; function checkLength( - max: N + max: N, ): (array: Array) => array is Sized { return (array): array is Sized => array.length >= 2 && array.length <= max; @@ -198,7 +189,7 @@ export namespace Shadow { return parseIf( checkLength(max), separatedList(Length.parse, Token.parseWhitespace), - () => `Shadows must have between 2 and ${max} lengths` + () => `Shadows must have between 2 and ${max} lengths`, ); } @@ -273,7 +264,7 @@ export namespace Shadow { blur ?? Length.of(0, "px"), spread ?? Length.of(0, "px"), color ?? Keyword.of("currentcolor"), - isInset ?? false + isInset ?? false, ), ]); };