-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Factor previous and next heading diagnostics into other heading * Update API documentation * Add changeset * Update changeset * Add heading position to diagnostic * Use `.tee` to store heading levels * Update .changeset/funny-ducks-approve.md Co-authored-by: Jean-Yves Moyen <[email protected]> * Address review comments * Mark exported type as public --------- Co-authored-by: Jean-Yves Moyen <[email protected]>
- Loading branch information
1 parent
9601f1b
commit 1921b0d
Showing
9 changed files
with
223 additions
and
202 deletions.
There are no files selected for viewing
This file contains 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 @@ | ||
--- | ||
"@siteimprove/alfa-rules": major | ||
--- | ||
|
||
**Breaking:** Diagnostics `WithPreviousHeading` and `WithNextHeading` have been replaced by `WithOtherHeading`. |
This file contains 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 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 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
138 changes: 138 additions & 0 deletions
138
packages/alfa-rules/src/common/diagnostic/with-other-heading.ts
This file contains 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,138 @@ | ||
import { Diagnostic } from "@siteimprove/alfa-act"; | ||
import { Element } from "@siteimprove/alfa-dom"; | ||
import { Hash } from "@siteimprove/alfa-hash"; | ||
import { Option } from "@siteimprove/alfa-option"; | ||
|
||
/** | ||
* @public | ||
*/ | ||
export type HeadingPosition = "previous" | "next"; | ||
|
||
/** | ||
* @public | ||
*/ | ||
export class WithOtherHeading extends Diagnostic { | ||
public static of(message: string): Diagnostic; | ||
|
||
public static of( | ||
message: string, | ||
otherHeading: Option<Element>, | ||
currentLevel: number, | ||
otherLevel: number, | ||
otherPosition: HeadingPosition, | ||
): WithOtherHeading; | ||
|
||
public static of( | ||
message: string, | ||
otherHeading?: Option<Element>, | ||
currentLevel?: number, | ||
otherLevel?: number, | ||
otherPosition?: HeadingPosition, | ||
): Diagnostic { | ||
return otherHeading === undefined || | ||
currentLevel === undefined || | ||
otherLevel === undefined || | ||
otherPosition === undefined | ||
? Diagnostic.of(message) | ||
: new WithOtherHeading( | ||
message, | ||
otherHeading, | ||
currentLevel, | ||
otherLevel, | ||
otherPosition, | ||
); | ||
} | ||
|
||
private readonly _otherHeading: Option<Element>; | ||
private readonly _currentLevel: number; | ||
private readonly _otherLevel: number; | ||
private readonly _otherPosition: HeadingPosition; | ||
|
||
private constructor( | ||
message: string, | ||
otherHeading: Option<Element>, | ||
currentLevel: number, | ||
otherLevel: number, | ||
otherPosition: HeadingPosition, | ||
) { | ||
super(message); | ||
this._otherHeading = otherHeading; | ||
this._currentLevel = currentLevel; | ||
this._otherLevel = otherLevel; | ||
this._otherPosition = otherPosition; | ||
} | ||
|
||
public get otherHeading(): Option<Element> { | ||
return this._otherHeading; | ||
} | ||
|
||
public get currentHeadingLevel(): number { | ||
return this._currentLevel; | ||
} | ||
|
||
public get otherHeadingLevel(): number { | ||
return this._otherLevel; | ||
} | ||
|
||
public get otherPosition(): HeadingPosition { | ||
return this._otherPosition; | ||
} | ||
|
||
public equals(value: WithOtherHeading): boolean; | ||
|
||
public equals(value: unknown): value is this; | ||
|
||
public equals(value: unknown): boolean { | ||
return ( | ||
value instanceof WithOtherHeading && | ||
value._message === this._message && | ||
value._otherHeading.equals(this._otherHeading) && | ||
value._currentLevel === this._currentLevel && | ||
value._otherLevel === this._otherLevel && | ||
value._otherPosition === this._otherPosition | ||
); | ||
} | ||
|
||
public hash(hash: Hash) { | ||
super.hash(hash); | ||
hash.writeNumber(this._currentLevel); | ||
hash.writeNumber(this._otherLevel); | ||
hash.writeString(this._otherPosition); | ||
this._otherHeading.hash(hash); | ||
} | ||
|
||
public toJSON(): WithOtherHeading.JSON { | ||
return { | ||
...super.toJSON(), | ||
otherHeading: this._otherHeading.toJSON(), | ||
currentHeadingLevel: this._currentLevel, | ||
otherHeadingLevel: this._otherLevel, | ||
otherPosition: this._otherPosition, | ||
}; | ||
} | ||
} | ||
|
||
/** | ||
* @public | ||
*/ | ||
export namespace WithOtherHeading { | ||
export interface JSON extends Diagnostic.JSON { | ||
otherHeading: Option.JSON<Element>; | ||
currentHeadingLevel: number; | ||
otherHeadingLevel: number; | ||
otherPosition: HeadingPosition; | ||
} | ||
|
||
export function isWithOtherHeading( | ||
value: Diagnostic, | ||
): value is WithOtherHeading; | ||
|
||
export function isWithOtherHeading(value: unknown): value is WithOtherHeading; | ||
|
||
/**@public */ | ||
export function isWithOtherHeading( | ||
value: unknown, | ||
): value is WithOtherHeading { | ||
return value instanceof WithOtherHeading; | ||
} | ||
} |
This file contains 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
Oops, something went wrong.