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

fix: remove unknown from walkTokens return types #2955

Merged
merged 2 commits into from
Aug 26, 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
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