Skip to content

Commit b6efcd8

Browse files
authored
chore: update recheck script to check rules.other (#3908)
1 parent 22f0c55 commit b6efcd8

File tree

3 files changed

+62
-42
lines changed

3 files changed

+62
-42
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
"test:cjs": "node test/cjs-test.cjs",
8989
"test:lint": "eslint",
9090
"test:only": "npm run build && npm run test:specs:only && npm run test:unit:only",
91-
"test:redos": "node test/recheck.js > vuln.js",
91+
"test:redos": "node test/recheck.ts > vuln.js",
9292
"test:specs:only": "node --test --test-only --test-reporter=spec test/run-spec-tests.js",
9393
"test:specs": "node --test --test-reporter=spec test/run-spec-tests.js",
9494
"test:types": "tsc --project tsconfig-type-test.json && attw -P --entrypoints . --profile esm-only",

test/recheck.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

test/recheck.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)