Skip to content

Commit d4ee776

Browse files
frankieyanclaude
andauthored
fix: report precise error line numbers in --show-errors (#51)
* chore(deps): add minimatch as direct dependency minimatch was being imported directly but only available as a transitive dependency through glob. This caused build failures with pnpm's strict dependency resolution. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix: report precise error line numbers in --show-errors Previously, --show-errors reported the line number where the function starts (event.fnLoc) rather than the exact line where the React compiler violation occurs. Now uses: - event.detail.primaryLocation() for CompileError events - event.loc for CompileSkip events This provides users with accurate line numbers pointing to the exact location of violations, making it easier to find and fix issues. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 83696d3 commit d4ee776

3 files changed

Lines changed: 39 additions & 8 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"@babel/core": "7.28.6",
6868
"@babel/preset-react": "7.28.5",
6969
"@babel/preset-typescript": "7.28.5",
70-
"glob": "13.0.0"
70+
"glob": "13.0.0",
71+
"minimatch": "10.1.1"
7172
}
7273
}

src/index.integration.test.mts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ describe('CLI', () => {
4747

4848
expect(output).toContain('Found 4 React Compiler issues')
4949
expect(output).toContain('Errors:')
50-
expect(output).toMatch(/Line \d+:/)
50+
expect(output).toContain(
51+
'src/bad-hook.ts: Line 9: Cannot access refs during render (x2)',
52+
)
53+
expect(output).toContain('src/bad-hook.ts: Line 10: Cannot access refs during render')
54+
expect(output).toContain(
55+
'src/bad-component.tsx: Line 9: Hooks must always be called in a consistent order',
56+
)
5157
})
5258
})
5359

@@ -85,8 +91,9 @@ describe('CLI', () => {
8591
expect(output).toContain('React Compiler errors have increased')
8692
expect(output).toContain('Errors:')
8793
expect(output).toContain(
88-
'src/bad-hook.ts: Line 6: Cannot access refs during render (x3)',
94+
'src/bad-hook.ts: Line 9: Cannot access refs during render (x2)',
8995
)
96+
expect(output).toContain('src/bad-hook.ts: Line 10: Cannot access refs during render')
9097
})
9198

9299
it('--show-errors shows errors even when no increase', () => {
@@ -95,8 +102,9 @@ describe('CLI', () => {
95102

96103
expect(output).toContain('Errors:')
97104
expect(output).toContain(
98-
'src/bad-hook.ts: Line 6: Cannot access refs during render (x3)',
105+
'src/bad-hook.ts: Line 9: Cannot access refs during render (x2)',
99106
)
107+
expect(output).toContain('src/bad-hook.ts: Line 10: Cannot access refs during render')
100108
expect(output).not.toContain('React Compiler errors have increased')
101109
expect(output).toContain('✅ No new React Compiler errors')
102110
})
@@ -131,7 +139,13 @@ describe('CLI', () => {
131139

132140
expect(output).toContain('Records saved to')
133141
expect(output).toContain('Errors:')
134-
expect(output).toMatch(/Line \d+:/)
142+
expect(output).toContain(
143+
'src/bad-hook.ts: Line 9: Cannot access refs during render (x2)',
144+
)
145+
expect(output).toContain('src/bad-hook.ts: Line 10: Cannot access refs during render')
146+
expect(output).toContain(
147+
'src/bad-component.tsx: Line 9: Hooks must always be called in a consistent order',
148+
)
135149
})
136150
})
137151

@@ -162,7 +176,10 @@ describe('CLI', () => {
162176

163177
expect(output).toContain('React Compiler errors have increased')
164178
expect(output).toContain('Errors:')
165-
expect(output).toMatch(/Line \d+:/)
179+
expect(output).toContain(
180+
'src/bad-hook.ts: Line 9: Cannot access refs during render (x2)',
181+
)
182+
expect(output).toContain('src/bad-hook.ts: Line 10: Cannot access refs during render')
166183
})
167184

168185
it('--stage-record-file --show-errors shows errors even when no increase', () => {
@@ -171,8 +188,9 @@ describe('CLI', () => {
171188

172189
expect(output).toContain('Errors:')
173190
expect(output).toContain(
174-
'src/bad-hook.ts: Line 6: Cannot access refs during render (x3)',
191+
'src/bad-hook.ts: Line 9: Cannot access refs during render (x2)',
175192
)
193+
expect(output).toContain('src/bad-hook.ts: Line 10: Cannot access refs during render')
176194
expect(output).not.toContain('React Compiler errors have increased')
177195
})
178196

src/index.mts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,25 @@ const customReactCompilerLogger: ReactCompilerLogger = {
3636
current[event.kind] = (current[event.kind] ?? 0) + 1
3737
compilerErrors.set(relativePath, current)
3838

39-
const line = event.fnLoc?.start.line ?? null
39+
let line: number | null
4040
let reason: string
41+
4142
if (event.kind === 'CompileError') {
43+
// Use primaryLocation() for precise error location, fall back to fnLoc
44+
// primaryLocation() may return a symbol (GeneratedSource), so check for object
45+
const loc = event.detail.primaryLocation()
46+
line =
47+
(loc && typeof loc === 'object' ? loc.start.line : null) ??
48+
event.fnLoc?.start.line ??
49+
null
4250
reason = event.detail.reason
4351
} else if (event.kind === 'CompileSkip') {
52+
// CompileSkip has its own loc field for the precise location
53+
line = event.loc?.start.line ?? event.fnLoc?.start.line ?? null
4454
reason = event.reason
4555
} else {
56+
// PipelineError only has fnLoc
57+
line = event.fnLoc?.start.line ?? null
4658
reason = String(event.data)
4759
}
4860

0 commit comments

Comments
 (0)