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

Convert token-list package to TypeScript #61575

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,45 @@
* @see https://dom.spec.whatwg.org/#domtokenlist
*/
export default class TokenList {
private _currentValue: string;
private _valueAsArray: string[];

/**
* Constructs a new instance of TokenList.
*
* @param {string} initialValue Initial value to assign.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the types that have moved into TypeScript syntax should be removed from JSDoc annotations like this:

Suggested change
* @param {string} initialValue Initial value to assign.
* @param initialValue Initial value to assign.

*/
constructor( initialValue = '' ) {
constructor( initialValue: string = '' ) {
this._currentValue = '';
this._valueAsArray = [];
this.value = initialValue;

// Disable reason: These are type hints on the class.
/* eslint-disable no-unused-expressions */
/** @type {string} */
this._currentValue;

/** @type {string[]} */
this._valueAsArray;
/* eslint-enable no-unused-expressions */
}

/**
* @param {Parameters<Array<string>['entries']>} args
*/
entries( ...args ) {
entries( ...args: Parameters< Array< string >[ 'entries' ] > ) {
return this._valueAsArray.entries( ...args );
}

/**
* @param {Parameters<Array<string>['forEach']>} args
*/
forEach( ...args ) {
forEach( ...args: Parameters< Array< string >[ 'forEach' ] > ) {
return this._valueAsArray.forEach( ...args );
}

/**
* @param {Parameters<Array<string>['keys']>} args
*/
keys( ...args ) {
keys( ...args: Parameters< Array< string >[ 'keys' ] > ) {
return this._valueAsArray.keys( ...args );
}

/**
* @param {Parameters<Array<string>['values']>} args
*/
values( ...args ) {
values( ...args: Parameters< Array< string >[ 'values' ] > ) {
return this._valueAsArray.values( ...args );
}

Expand All @@ -57,7 +53,7 @@ export default class TokenList {
*
* @return {string} Token set as string.
*/
get value() {
get value(): string {
return this._currentValue;
}

Expand All @@ -68,7 +64,7 @@ export default class TokenList {
*
* @param {string} value New token set as string.
*/
set value( value ) {
set value( value: string ) {
value = String( value );
this._valueAsArray = [
...new Set( value.split( /\s+/g ).filter( Boolean ) ),
Expand All @@ -83,7 +79,7 @@ export default class TokenList {
*
* @return {number} Number of tokens.
*/
get length() {
get length(): number {
return this._valueAsArray.length;
}

Expand All @@ -95,7 +91,7 @@ export default class TokenList {
*
* @return {string} Token set as string.
*/
toString() {
toString(): string {
return this.value;
}

Expand All @@ -106,7 +102,7 @@ export default class TokenList {
*
* @return {IterableIterator<string>} TokenList iterator.
*/
*[ Symbol.iterator ]() {
*[ Symbol.iterator ](): IterableIterator< string > {
return yield* this._valueAsArray;
}

Expand All @@ -119,7 +115,7 @@ export default class TokenList {
*
* @return {string|undefined} Token at index.
*/
item( index ) {
item( index: number ): string | undefined {
return this._valueAsArray[ index ];
}

Expand All @@ -132,7 +128,7 @@ export default class TokenList {
*
* @return {boolean} Whether token is present.
*/
contains( item ) {
contains( item: string ): boolean {
return this._valueAsArray.indexOf( item ) !== -1;
}

Expand All @@ -143,7 +139,7 @@ export default class TokenList {
*
* @param {...string} items Items to add.
*/
add( ...items ) {
add( ...items: string[] ): void {
this.value += ' ' + items.join( ' ' );
}

Expand All @@ -154,7 +150,7 @@ export default class TokenList {
*
* @param {...string} items Items to remove.
*/
remove( ...items ) {
remove( ...items: string[] ): void {
this.value = this._valueAsArray
.filter( ( val ) => ! items.includes( val ) )
.join( ' ' );
Expand All @@ -173,7 +169,7 @@ export default class TokenList {
*
* @return {boolean} Whether token is present after toggle.
*/
toggle( token, force ) {
toggle( token: string, force?: boolean ): boolean {
if ( undefined === force ) {
force = ! this.contains( token );
}
Expand All @@ -198,7 +194,7 @@ export default class TokenList {
*
* @return {boolean} Whether replacement occurred.
*/
replace( token, newToken ) {
replace( token: string, newToken: string ): boolean {
if ( ! this.contains( token ) ) {
return false;
}
Expand All @@ -215,11 +211,12 @@ export default class TokenList {
*
* Always returns `true` in this implementation.
*
* @param _token
* @see https://dom.spec.whatwg.org/#dom-domtokenlist-supports
*
* @return {boolean} Whether token is supported.
*/
supports() {
supports( _token: string ): boolean {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe( 'token-list', () => {

it( 'sets to stringified value', () => {
const list = new TokenList();
list.value = undefined;
list.value = undefined as unknown as string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a case like this, I'd go with expect error. We know the type is wrong and we expect to have an error reported:

Suggested change
list.value = undefined as unknown as string;
// @ts-expect-error The value should be a string, for testing we pass a "bad" value.
list.value = undefined;


expect( list.value ).toBe( 'undefined' );
} );
Expand Down
Loading