Skip to content

Commit 1df7e90

Browse files
committed
feat: update
1 parent 5bfabbb commit 1df7e90

File tree

13 files changed

+224
-70
lines changed

13 files changed

+224
-70
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ temp
1212
regex-doctor.json
1313
.nuxt
1414
test/fixtures/**/*.d.ts
15+
.regex-doctor

build.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { defineBuildConfig } from 'unbuild'
33
export default defineBuildConfig({
44
entries: [
55
'src/index',
6+
'src/register',
7+
'src/cli',
68
],
79
declaration: true,
810
clean: true,

package.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
"types": "./dist/index.d.ts",
2121
"import": "./dist/index.mjs",
2222
"require": "./dist/index.cjs"
23+
},
24+
"./register": {
25+
"types": "./dist/register.d.ts",
26+
"import": "./dist/register.mjs",
27+
"require": "./dist/register.cjs"
2328
}
2429
},
2530
"main": "./dist/index.mjs",
@@ -38,7 +43,7 @@
3843
],
3944
"scripts": {
4045
"build": "nr js:build && nr ui:build",
41-
"start": "nr build && nr -C test/fixtures/vite build && nr dev",
46+
"start": "nr js:build && nr -C ui build:doctor && nr -C test/fixtures/vite build && nr dev",
4247
"dev": "nr -C ui dev",
4348
"js:build": "unbuild",
4449
"ui:build": "nr -C ui build",
@@ -50,11 +55,12 @@
5055
"prepare": "simple-git-hooks"
5156
},
5257
"dependencies": {
53-
"error-stack-parser-es": "^0.1.3",
54-
"trace-record": "^0.1.1"
58+
"error-stack-parser-es": "^0.1.4",
59+
"exit-hook": "^4.0.0",
60+
"trace-record": "^0.1.2"
5561
},
5662
"devDependencies": {
57-
"@antfu/eslint-config": "^2.17.0",
63+
"@antfu/eslint-config": "^2.18.0",
5864
"@antfu/ni": "^0.21.12",
5965
"@antfu/utils": "^0.7.8",
6066
"@iconify-json/carbon": "^1.1.33",

pnpm-lock.yaml

Lines changed: 174 additions & 46 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cli.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {}

src/dump.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { fileURLToPath } from 'node:url'
2-
import { parse } from 'error-stack-parser-es/lite'
1+
import { parseStack } from 'error-stack-parser-es/lite'
32
import { getTrace } from 'trace-record'
43
import type { MergedRecordRegexInfo, RecordRegexInfo, RegexCallsDurations, RegexDoctorDumpFiltersOptions, RegexDoctorDumpOptions, RegexInfo } from './types'
54

@@ -18,7 +17,7 @@ export function dump(
1817
) {
1918
const {
2019
limitCalls = 5,
21-
stacktrace = false,
20+
// stacktrace = true,
2221
} = options
2322

2423
const filters = {
@@ -85,14 +84,9 @@ export function dump(
8584

8685
const files = new Set<string>()
8786

88-
const infos = calls.map((call) => {
89-
const trace = stacktrace && call.traceObj
90-
? parse(call.traceObj, { slice: [1, 10] })
91-
.filter(frame => frame.file)
92-
.map((frame) => {
93-
delete frame.raw
94-
return frame
95-
})
87+
let infos = calls.map((call) => {
88+
const trace = call.stack
89+
? parseStack(call.stack, { slice: [1, 10] }).filter(frame => frame.file)
9690
: undefined
9791

9892
if (trace && trace[0])
@@ -106,7 +100,7 @@ export function dump(
106100
})
107101

108102
if (limitCalls > 0)
109-
infos.splice(limitCalls)
103+
infos = infos.slice(0, limitCalls)
110104

111105
return {
112106
regex: {
@@ -137,7 +131,7 @@ function normalizeFilepath(filepath: string) {
137131
filepath = filepath.slice(6)
138132
// normalize file path
139133
if (filepath.startsWith('file://'))
140-
return fileURLToPath(filepath)
134+
filepath = filepath.slice(7)
141135
return filepath
142136
}
143137

src/hijack.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ export function hijack() {
3131
const result = _exec.call(this, string)
3232
const end = performance.now()
3333
const duration = end - start
34-
const call: RecordRegexCall = Object.freeze({
35-
traceObj: duration > 0.001
36-
? new Error()
34+
const call = Object.freeze(<RecordRegexCall>{
35+
stack: duration > 0.001
36+
? new Error().stack
3737
: undefined,
3838
duration,
3939
input: string,

src/register.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import fs from 'node:fs'
2+
import process from 'node:process'
3+
import exitHook from 'exit-hook'
4+
import { RegexDoctor } from './doctor'
5+
6+
const doctor = new RegexDoctor()
7+
doctor.start()
8+
9+
const cwd = process.cwd()
10+
11+
exitHook(() => {
12+
doctor.stop()
13+
fs.mkdirSync('./.regex-doctor', { recursive: true })
14+
fs.writeFileSync('./.regex-doctor/output.json', JSON.stringify(doctor.dump({
15+
stacktrace: true,
16+
cwd,
17+
}), null, 2))
18+
// eslint-disable-next-line no-console
19+
console.log('[regex-doctor] output saved to ./.regex-doctor/output.json')
20+
})

src/types/record.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export interface RecordRegexCall {
44
duration: number
55
inputLength: number
66
input?: string
7-
traceObj?: Error
7+
stack?: string
88
}
99

1010
export interface RecordRegexInfo {

test/fixtures/vite/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"vue-router": "^4.3.2"
1818
},
1919
"devDependencies": {
20-
"@antfu/eslint-config": "^2.17.0",
20+
"@antfu/eslint-config": "^2.18.0",
2121
"@iconify-json/carbon": "^1.1.33",
2222
"@types/node": "^20.12.11",
2323
"@unocss/eslint-config": "^0.60.2",

0 commit comments

Comments
 (0)