Skip to content
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

Turn partiallyResolve() into a Value method #1495

Merged
merged 18 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/bright-gorillas-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@siteimprove/alfa-css": minor
---

**Added:** `Value` now expose a `partiallyResolve()` instance method.
7 changes: 7 additions & 0 deletions .changeset/dry-peaches-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@siteimprove/alfa-css": minor
---

**Breaking:** Various `Value.partiallyResolve()` functions have been removed.

Instead, use the corresponding `Value#partiallyResolve()` instance method.
5 changes: 5 additions & 0 deletions .changeset/long-plums-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@siteimprove/alfa-css": minor
---

**Breaking:** `Angle#resolve()` does not require a resolver anymore, since 100% is always 1 full turn.
134 changes: 77 additions & 57 deletions docs/review/api/alfa-css.api.md

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion docs/review/api/alfa-rules.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,6 @@ export class Version<N extends number = number> extends Tag<"version"> {

// @public (undocumented)
export namespace Version {
// (undocumented)
export function isVersion<T extends string>(value: unknown): value is Version;
// (undocumented)
export interface JSON<N extends number = number> extends Tag.JSON<"version"> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export namespace Function {
}
}

/** @public */
/** @public (knip) */
export function isCalculation(value: unknown): value is Calculation {
return value instanceof Calculation;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/alfa-css/src/calculation/math-expression/kind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ export class Kind implements Equatable, Serializable {
this._hint = hint;
}

/** @public */
/** @public (knip) */
public get kinds(): Kind.Map {
return this._kinds;
}

/** @public */
/** @public (knip) */
public get hint(): Option<Kind.Hint> {
return this._hint;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export namespace Operation {
}
}

/** @public */
/** @public (knip) */
export function isSumExpression(value: unknown): value is Sum {
return value instanceof Sum;
}
Expand Down Expand Up @@ -276,7 +276,7 @@ export namespace Operation {
}
}

/** @public */
/** @public (knip) */
export function isProductExpression(value: unknown): value is Product {
return value instanceof Product;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/alfa-css/src/calculation/numeric/numeric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export namespace Numeric {
value: number;
}

/** @public */
/** @public (knip) */
export function isNumeric(value: unknown): value is Numeric {
return value instanceof Numeric;
}
Expand Down
17 changes: 15 additions & 2 deletions packages/alfa-css/src/value/collection/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Parser } from "@siteimprove/alfa-parser";

import { type Parser as CSSParser, Token } from "../../syntax";

import type { Resolvable } from "../resolvable";
import type { PartiallyResolvable, Resolvable } from "../resolvable";
import { Value } from "../value";

const { delimited, option, map, separatedList } = Parser;
Expand All @@ -18,7 +18,11 @@ export class List<V extends Value>
extends Value<"list", Value.HasCalculation<[V]>>
implements
Iterable<V>,
Resolvable<List<Resolvable.Resolved<V>>, Resolvable.Resolver<V>>
Resolvable<List<Resolvable.Resolved<V>>, Resolvable.Resolver<V>>,
PartiallyResolvable<
List<Resolvable.PartiallyResolved<V>>,
Resolvable.PartialResolver<V>
>
{
public static of<V extends Value>(
values: Iterable<V>,
Expand Down Expand Up @@ -48,6 +52,15 @@ export class List<V extends Value>
);
}

public partiallyResolve(
resolver?: Resolvable.PartialResolver<V>,
): List<Resolvable.PartiallyResolved<V>> {
return this.map(
(value) =>
value.partiallyResolve(resolver) as Resolvable.PartiallyResolved<V>,
);
}

public map<U extends Value>(mapper: Mapper<V, U>): List<U> {
return new List(this._values.map(mapper), this._separator);
}
Expand Down
32 changes: 30 additions & 2 deletions packages/alfa-css/src/value/collection/tuple.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { Hash } from "@siteimprove/alfa-hash";
import { Serializable } from "@siteimprove/alfa-json";

import type { Resolvable } from "../resolvable";
import type { PartiallyResolvable, Resolvable } from "../resolvable";
import { Value } from "../value";

/**
* @public
*/
export class Tuple<T extends Array<Value>>
extends Value<"tuple", Value.HasCalculation<T>>
implements Resolvable<Tuple<Tuple.Resolved<T>>, Tuple.Resolver<T>>
implements
Resolvable<Tuple<Tuple.Resolved<T>>, Tuple.Resolver<T>>,
PartiallyResolvable<
Tuple<Tuple.PartiallyResolved<T>>,
Tuple.PartialResolver<T>
>
{
public static of<T extends Array<Value>>(...values: Readonly<T>): Tuple<T> {
return new Tuple(values);
Expand All @@ -32,6 +37,16 @@ export class Tuple<T extends Array<Value>>
);
}

public partiallyResolve(
resolver?: Tuple.PartialResolver<T>,
): Tuple<Tuple.PartiallyResolved<T>> {
return new Tuple<Tuple.PartiallyResolved<T>>(
this._values.map((value) =>
value.resolve(resolver),
) as Tuple.PartiallyResolved<T>,
);
}

public equals<T extends Array<Value>>(value: Tuple<T>): boolean;

public equals(value: unknown): value is this;
Expand Down Expand Up @@ -94,6 +109,13 @@ export namespace Tuple {
? [Resolvable.Resolved<Head>, ...Resolved<Tail>]
: [];

export type PartiallyResolved<T extends Array<Value>> = T extends [
infer Head extends Value,
...infer Tail extends Array<Value>,
]
? [Resolvable.PartiallyResolved<Head>, ...PartiallyResolved<Tail>]
: [];

/**
* Applying Resolver<T> to all members of a tuple, collapsing them into
* a single union
Expand All @@ -105,4 +127,10 @@ export namespace Tuple {
>
? Resolvable.Resolver<V>
: never;

export type PartialResolver<T extends Array<Value>> = T extends Array<
infer V extends Value
>
? Resolvable.PartialResolver<V>
: never;
}
5 changes: 0 additions & 5 deletions packages/alfa-css/src/value/color/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ export abstract class Format<F extends string = string>
this._format = format;
}

/** @public */
public get format(): F {
return this._format;
}

public abstract get red(): Number.Canonical | Percentage.Canonical;

public abstract get green(): Number.Canonical | Percentage.Canonical;
Expand Down
10 changes: 0 additions & 10 deletions packages/alfa-css/src/value/image/gradient/gradient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@ export namespace Gradient {

export type PartialResolver = Linear.PartialResolver & Radial.PartialResolver;

export function partiallyResolve(
resolver: PartialResolver,
): (value: Gradient) => PartiallyResolved {
return (value) =>
Selective.of(value)
.if(Linear.isLinear, Linear.partiallyResolve(resolver))
.else(Radial.partiallyResolve(resolver))
.get();
}

/**
* {@link https://drafts.csswg.org/css-images/#typedef-gradient}
*/
Expand Down
27 changes: 16 additions & 11 deletions packages/alfa-css/src/value/image/gradient/item/hint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ import { Parser } from "@siteimprove/alfa-parser";
import { Parser as CSSParser } from "../../../../syntax";

import { LengthPercentage } from "../../../numeric";
import { PartiallyResolvable, Resolvable } from "../../../resolvable";
import { Value } from "../../../value";

const { map } = Parser;

/**
* {@link https://drafts.csswg.org/css-images/#color-transition-hint}
*/
export class Hint<P extends LengthPercentage = LengthPercentage> extends Value<
"hint",
Value.HasCalculation<[P]>
> {
export class Hint<P extends LengthPercentage = LengthPercentage>
extends Value<"hint", Value.HasCalculation<[P]>>
implements
Resolvable<Hint.Canonical, Hint.Resolver>,
PartiallyResolvable<Hint.PartiallyResolved, Hint.PartialResolver>
{
public static of<P extends LengthPercentage>(position: P): Hint<P> {
return new Hint(position);
}
Expand All @@ -27,6 +30,7 @@ export class Hint<P extends LengthPercentage = LengthPercentage> extends Value<
this._position = position;
}

/** @public (knip) */
public get position(): P {
return this._position;
}
Expand All @@ -35,6 +39,14 @@ export class Hint<P extends LengthPercentage = LengthPercentage> extends Value<
return new Hint(LengthPercentage.resolve(resolver)(this._position));
}

public partiallyResolve(
resolver: Hint.PartialResolver,
): Hint.PartiallyResolved {
return new Hint(
LengthPercentage.partiallyResolve(resolver)(this._position),
);
}

public equals(value: unknown): value is this {
return value instanceof Hint && value._position.equals(this._position);
}
Expand Down Expand Up @@ -68,13 +80,6 @@ export namespace Hint {

export type PartialResolver = LengthPercentage.PartialResolver;

export function partiallyResolve(
resolver: PartialResolver,
): (value: Hint) => PartiallyResolved {
return (value) =>
Hint.of(LengthPercentage.partiallyResolve(resolver)(value.position));
}

/**
* {@link https://drafts.csswg.org/css-images/#typedef-linear-color-hint}
*/
Expand Down
9 changes: 0 additions & 9 deletions packages/alfa-css/src/value/image/gradient/item/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@ export namespace Item {

export type PartialResolver = Hint.PartialResolver & Stop.PartialResolver;

export function partiallyResolve(
resolver: PartialResolver,
): (value: Item) => PartiallyResolved {
return (value) =>
Stop.isStop(value)
? Stop.partiallyResolve(resolver)(value)
: Hint.partiallyResolve(resolver)(value);
}

// <linear-color-hint>? , <linear-color-stop>
const parse = pair(option(left(Hint.parse, Comma.parse)), Stop.parse);

Expand Down
33 changes: 20 additions & 13 deletions packages/alfa-css/src/value/image/gradient/item/stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Parser as CSSParser, Token } from "../../../../syntax";

import { Color } from "../../../color";
import { LengthPercentage } from "../../../numeric";
import { PartiallyResolvable, Resolvable } from "../../../resolvable";
import { Value } from "../../../value";

const { either, pair, map, left, right } = Parser;
Expand All @@ -14,9 +15,14 @@ const { either, pair, map, left, right } = Parser;
* {@link https://drafts.csswg.org/css-images/#color-stop}
*/
export class Stop<
C extends Color = Color,
P extends LengthPercentage = LengthPercentage,
> extends Value<"stop", Value.HasCalculation<[C, P]>> {
C extends Color = Color,
P extends LengthPercentage = LengthPercentage,
>
extends Value<"stop", Value.HasCalculation<[C, P]>>
implements
Resolvable<Stop.Canonical, Stop.Resolver>,
PartiallyResolvable<Stop.PartiallyResolved, Stop.PartialResolver>
{
public static of<C extends Color, P extends LengthPercentage>(
color: C,
position: Option<P> = None,
Expand All @@ -43,6 +49,7 @@ export class Stop<
return this._color;
}

/** @public (knip) */
public get position(): Option<P> {
return this._position;
}
Expand All @@ -54,6 +61,15 @@ export class Stop<
);
}

public partiallyResolve(
resolver: Stop.PartialResolver,
): Stop.PartiallyResolved {
return new Stop(
this._color.resolve(),
this._position.map(LengthPercentage.partiallyResolve(resolver)),
);
}

public equals(value: unknown): value is this {
return (
value instanceof Stop &&
Expand Down Expand Up @@ -98,16 +114,7 @@ export namespace Stop {

export type PartialResolver = LengthPercentage.PartialResolver;

export function partiallyResolve(
resolver: PartialResolver,
): (value: Stop) => PartiallyResolved {
return (value) =>
Stop.of(
value.color.resolve(),
value.position.map(LengthPercentage.partiallyResolve(resolver)),
);
}

/** @public (knip) */
export function isStop(value: unknown): value is Stop {
return value instanceof Stop;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/alfa-css/src/value/image/gradient/linear/corner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ export class Corner extends Value<"corner", false> {
this._horizontal = horizontal;
}

/** @public */
/** @public (knip) */
public get vertical(): Position.Vertical {
return this._vertical;
}

/** @public */
/** @public (knip) */
public get horizontal(): Position.Horizontal {
return this._horizontal;
}
Expand Down
Loading