Skip to content

Commit

Permalink
fix: remove unknown from walkTokens return types (#2955)
Browse files Browse the repository at this point in the history
* fix: remove unknown from walkTokens return types

* build before testing types
  • Loading branch information
UziTech authored Aug 26, 2023
1 parent 5bc2fdb commit 65934c1
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ jobs:
run: npm run test:lint

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

Release:
permissions:
Expand Down
7 changes: 4 additions & 3 deletions src/Instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { MarkedExtension, MarkedOptions } from './MarkedOptions.ts';
import type { Token, Tokens, TokensList } from './Tokens.ts';

export type ResultCallback = (error: Error | null, parseResult?: string) => undefined | void;
export type MaybePromise = void | Promise<void>;

type UnknownFunction = (...args: unknown[]) => unknown;
type GenericRendererFunction = (...args: unknown[]) => string | false;
Expand Down Expand Up @@ -42,8 +43,8 @@ export class Marked {
/**
* Run callback for every token
*/
walkTokens <T = void>(tokens: Token[] | TokensList, callback: (token: Token) => T | T[]) {
let values: T[] = [];
walkTokens(tokens: Token[] | TokensList, callback: (token: Token) => MaybePromise | MaybePromise[]) {
let values: MaybePromise[] = [];
for (const token of tokens) {
values = values.concat(callback.call(this, token));
switch (token.type) {
Expand Down Expand Up @@ -215,7 +216,7 @@ export class Marked {
const walkTokens = this.defaults.walkTokens;
const packWalktokens = pack.walkTokens;
opts.walkTokens = function(token) {
let values: Array<Promise<void> | void | unknown> = [];
let values: MaybePromise[] = [];
values.push(packWalktokens.call(this, token));
if (walkTokens) {
values = values.concat(walkTokens.call(this, token));
Expand Down
4 changes: 2 additions & 2 deletions src/MarkedOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export interface MarkedExtension {
* Each token is passed by reference so updates are persisted when passed to the parser.
* 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>)
* @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.
Expand Down Expand Up @@ -213,5 +213,5 @@ export interface MarkedOptions extends Omit<MarkedExtension, 'renderer' | 'token
/**
* 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>)[]);
}
4 changes: 2 additions & 2 deletions src/marked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from './defaults.ts';
import type { MarkedExtension, MarkedOptions } from './MarkedOptions.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();

Expand Down Expand Up @@ -94,7 +94,7 @@ marked.use = function(...args: MarkedExtension[]) {
* 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);
};

Expand Down

1 comment on commit 65934c1

@vercel
Copy link

@vercel vercel bot commented on 65934c1 Aug 26, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.