Skip to content

Commit 66199ad

Browse files
committed
feat(Instance)!: parser throw when default async is true and called with async at false
BREAKING CHANGE: parser now throw an error when `defaultConfig` is modified by an extension to make the returned value a Promise and the we call parser with `async` option at false. It can be skipped with `silent` to true, making it still working for library that use it to modify the config globally but still calling each instance with the option `async` at false.
1 parent 784a9a9 commit 66199ad

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

src/Instance.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -273,17 +273,13 @@ export class Marked {
273273
const origOpt: MarkedOptions = { ...options };
274274
const opt: MarkedOptions = { ...this.defaults, ...origOpt };
275275

276-
// Show warning if an extension set async to true but the parse was called with async: false
277-
if (isAsyncOptions(this.defaults) && isSyncOptions(origOpt)) {
278-
if (!opt.silent) {
279-
console.warn('marked(): The async option was set to true by an extension. The async: false option sent to parse will be ignored.');
280-
}
276+
const throwError = this.#onError(!!opt.silent, !!opt.async);
281277

282-
opt.async = true;
278+
if (isAsyncOptions(this.defaults) && isSyncOptions(origOpt)) {
279+
// Throw an error if an extension set async to true but the parse was called with async: false
280+
return throwError(new Error('marked(): The async option was set to true by an extension. Remove the async: false option to continue.'));
283281
}
284282

285-
const throwError = this.#onError(!!opt.silent, !!opt.async);
286-
287283
// throw error in case of non string input
288284
if (typeof src === 'undefined' || src === null) {
289285
return throwError(new Error('marked(): input parameter is undefined or null'));

src/MarkedOptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export interface MarkedAsyncOptions extends _MarkedOptions {
162162
export type MarkedOptions = MarkedSyncOptions | MarkedAsyncOptions;
163163

164164
export function isAsyncOptions(options: MarkedOptions): options is MarkedAsyncOptions {
165-
return 'async' in options && options.async === true;
165+
return options.async === true;
166166
}
167167

168168
export function isSyncOptions(options: MarkedOptions): options is MarkedSyncOptions {

test/types/marked.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,25 @@ marked.use(asyncExtension);
261261
const md = '# foobar';
262262
const asyncMarked: string = await marked(md, { async: true });
263263
const promiseMarked: Promise<string> = marked(md, { async: true });
264-
const notAsyncMarked: string = marked(md, { async: false });
264+
const notAsyncMarked: string = marked(md, { async: false, silent: true });
265265
const defaultMarked: string = marked(md);
266266

267267
const asyncMarkedParse: string = await marked.parse(md, { async: true });
268268
const promiseMarkedParse: Promise<string> = marked.parse(md, { async: true });
269-
const notAsyncMarkedParse: string = marked.parse(md, { async: false });
269+
const notAsyncMarkedParse: string = marked.parse(md, { async: false, silent: true });
270270
const defaultMarkedParse: string = marked.parse(md);
271+
272+
try {
273+
const notAsyncMarkedThrow: string = marked(md, { async: false, silent: false });
274+
} catch {
275+
console.log('expected throw');
276+
}
277+
278+
try {
279+
const notAsyncMarkedParseThrow: string = marked.parse(md, { async: false, silent: false });
280+
} catch {
281+
console.log('expected throw');
282+
}
271283
})();
272284

273285
// Tests for List and ListItem

test/unit/marked.test.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,19 @@ used extension2 walked</p>
632632
it('should return Promise if async is set by extension', () => {
633633
marked.use({ async: true });
634634

635-
assert.ok(marked.parse('test', { async: false }) instanceof Promise);
635+
assert.ok(marked.parse('test') instanceof Promise);
636+
});
637+
638+
it('should throw an if async is set by extension and a different async parameter is set', () => {
639+
marked.use({ async: true });
640+
641+
assert.throws(() => marked.parse('test', { async: false }), /The async option was set to true by an extension/);
642+
});
643+
644+
it('should return a string error message if async is set by extension and a different async parameter is set and the silent parameter is set', () => {
645+
marked.use({ async: true });
646+
647+
assert.match(marked.parse('test', { async: false, silent: true }), /The async option was set to true by an extension/);
636648
});
637649

638650
it('should allow deleting/editing tokens', () => {

0 commit comments

Comments
 (0)