|
| 1 | +import { block, inline, other } from '../src/rules.ts'; |
| 2 | +import { check } from 'recheck'; |
| 3 | + |
| 4 | +interface RegexpObj { |
| 5 | + [k: string]: RegExp | string | ((arg: number) => RegExp | string) | ((arg: string) => RegExp | string) | RegexpObj; |
| 6 | +} |
| 7 | + |
| 8 | +async function checkRegexp(obj: RegexpObj, name: string) { |
| 9 | + await Promise.all(Object.keys(obj).map(async(prop: string) => { |
| 10 | + const item = obj[prop]; |
| 11 | + const itemName = `${name}.${prop}`; |
| 12 | + let source = ''; |
| 13 | + let flags = ''; |
| 14 | + if (item instanceof RegExp) { |
| 15 | + source = item.source; |
| 16 | + flags = item.flags; |
| 17 | + } else if (typeof item === 'string') { |
| 18 | + source = item; |
| 19 | + } else if (typeof item === 'function') { |
| 20 | + // TODO: skip functions for now |
| 21 | + return; |
| 22 | + } else { |
| 23 | + return checkRegexp(item, itemName); |
| 24 | + } |
| 25 | + const gfm = itemName.includes('.gfm.'); |
| 26 | + const pedantic = itemName.includes('.pedantic.'); |
| 27 | + const recheckObj = await check(source, flags); |
| 28 | + try { |
| 29 | + console.log(`// ${itemName}: /${recheckObj.source}/${recheckObj.flags}`); |
| 30 | + switch (recheckObj.status) { |
| 31 | + case 'safe': |
| 32 | + console.log('// safe'); |
| 33 | + break; |
| 34 | + case 'vulnerable': |
| 35 | + console.log(`// marked(${recheckObj.attack.pattern}, { pedantic: ${pedantic ? 'true' : 'false'}, gfm: ${gfm ? 'true' : 'false'} });`); |
| 36 | + break; |
| 37 | + default: |
| 38 | + console.log('// error:', recheckObj.error); |
| 39 | + break; |
| 40 | + } |
| 41 | + } catch(e) { |
| 42 | + console.log(recheckObj); |
| 43 | + throw e; |
| 44 | + } |
| 45 | + })); |
| 46 | +} |
| 47 | + |
| 48 | +console.log(` |
| 49 | +import { marked } from '../lib/marked.esm.js'; |
| 50 | + |
| 51 | +const start = Date.now(); |
| 52 | +`); |
| 53 | + |
| 54 | +await Promise.all([ |
| 55 | + checkRegexp(inline, 'inline'), |
| 56 | + checkRegexp(block, 'block'), |
| 57 | + checkRegexp(other, 'other'), |
| 58 | +]); |
| 59 | + |
| 60 | +console.log(` |
| 61 | +console.log(Date.now() - start);`); |
0 commit comments