fix: remove unknown from walkTokens return types (#2955)

* fix: remove unknown from walkTokens return types

* build before testing types
This commit is contained in:
Tony Brix 2023-08-26 09:57:05 -06:00 committed by GitHub
parent 5bc2fdb7a7
commit 65934c13ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 7 deletions

View File

@ -49,6 +49,7 @@ jobs:
run: npm run test:lint run: npm run test:lint
Build: Build:
name: Build and Test Types
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Checkout Code - name: Checkout Code
@ -61,6 +62,8 @@ jobs:
run: npm ci run: npm ci
- name: Build 🗜️ - name: Build 🗜️
run: npm run build run: npm run build
- name: Run Types Tests 👩🏽‍💻
run: npm run test:types
Release: Release:
permissions: permissions:

View File

@ -14,6 +14,7 @@ import type { MarkedExtension, MarkedOptions } from './MarkedOptions.ts';
import type { Token, Tokens, TokensList } from './Tokens.ts'; import type { Token, Tokens, TokensList } from './Tokens.ts';
export type ResultCallback = (error: Error | null, parseResult?: string) => undefined | void; export type ResultCallback = (error: Error | null, parseResult?: string) => undefined | void;
export type MaybePromise = void | Promise<void>;
type UnknownFunction = (...args: unknown[]) => unknown; type UnknownFunction = (...args: unknown[]) => unknown;
type GenericRendererFunction = (...args: unknown[]) => string | false; type GenericRendererFunction = (...args: unknown[]) => string | false;
@ -42,8 +43,8 @@ export class Marked {
/** /**
* Run callback for every token * Run callback for every token
*/ */
walkTokens <T = void>(tokens: Token[] | TokensList, callback: (token: Token) => T | T[]) { walkTokens(tokens: Token[] | TokensList, callback: (token: Token) => MaybePromise | MaybePromise[]) {
let values: T[] = []; let values: MaybePromise[] = [];
for (const token of tokens) { for (const token of tokens) {
values = values.concat(callback.call(this, token)); values = values.concat(callback.call(this, token));
switch (token.type) { switch (token.type) {
@ -215,7 +216,7 @@ export class Marked {
const walkTokens = this.defaults.walkTokens; const walkTokens = this.defaults.walkTokens;
const packWalktokens = pack.walkTokens; const packWalktokens = pack.walkTokens;
opts.walkTokens = function(token) { opts.walkTokens = function(token) {
let values: Array<Promise<void> | void | unknown> = []; let values: MaybePromise[] = [];
values.push(packWalktokens.call(this, token)); values.push(packWalktokens.call(this, token));
if (walkTokens) { if (walkTokens) {
values = values.concat(walkTokens.call(this, token)); values = values.concat(walkTokens.call(this, token));

View File

@ -173,7 +173,7 @@ export interface MarkedExtension {
* Each token is passed by reference so updates are persisted when passed to the parser. * Each token is passed by reference so updates are persisted when passed to the parser.
* The return value of the function is ignored. * The return value of the function is ignored.
*/ */
walkTokens?: ((token: Token) => void | unknown | Promise<void>) | undefined | null; walkTokens?: ((token: Token) => void | Promise<void>) | undefined | null;
/** /**
* Generate closing slash for self-closing tags (<br/> instead of <br>) * Generate closing slash for self-closing tags (<br/> instead of <br>)
* @deprecated Deprecated in v5.0.0 use marked-xhtml to emit self-closing HTML tags for void elements (<br/>, <img/>, etc.) with a "/" as required by XHTML. * @deprecated Deprecated in v5.0.0 use marked-xhtml to emit self-closing HTML tags for void elements (<br/>, <img/>, etc.) with a "/" as required by XHTML.
@ -213,5 +213,5 @@ export interface MarkedOptions extends Omit<MarkedExtension, 'renderer' | 'token
/** /**
* walkTokens function returns array of values for Promise.all * walkTokens function returns array of values for Promise.all
*/ */
walkTokens?: null | ((token: Token) => void | (unknown | Promise<void>)[]); walkTokens?: null | ((token: Token) => void | Promise<void> | (void | Promise<void>)[]);
} }

View File

@ -13,7 +13,7 @@ import {
} from './defaults.ts'; } from './defaults.ts';
import type { MarkedExtension, MarkedOptions } from './MarkedOptions.ts'; import type { MarkedExtension, MarkedOptions } from './MarkedOptions.ts';
import type { Token, TokensList } from './Tokens.ts'; import type { Token, TokensList } from './Tokens.ts';
import type { ResultCallback } from './Instance.ts'; import type { ResultCallback, MaybePromise } from './Instance.ts';
const markedInstance = new Marked(); const markedInstance = new Marked();
@ -94,7 +94,7 @@ marked.use = function(...args: MarkedExtension[]) {
* Run callback for every token * Run callback for every token
*/ */
marked.walkTokens = function <T = void>(tokens: Token[] | TokensList, callback: (token: Token) => T | T[]) { marked.walkTokens = function(tokens: Token[] | TokensList, callback: (token: Token) => MaybePromise | MaybePromise[]) {
return markedInstance.walkTokens(tokens, callback); return markedInstance.walkTokens(tokens, callback);
}; };