Skip to content

Commit f38c897

Browse files
committed
Detect and block Exercism stub throw in two-fer analyzer
1 parent ca7a434 commit f38c897

3 files changed

Lines changed: 45 additions & 1 deletion

File tree

src/analyzers/practice/two-fer/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ export class TwoFerAnalyzer extends AnalyzerImpl {
101101

102102
if (hasStubThrow(this.mainMethod)) {
103103
this.disapprove(REMOVE_STUB_THROW())
104+
return
104105
}
105106

106107
// Now we want to ensure that the method signature is sane and that it has

src/comments/remove_stub_throw.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ import { factory, CommentType } from '~src/comments/comment'
22

33
export const REMOVE_STUB_THROW = factory`
44
Remove this placeholder throw statement. It is dead code.
5-
`('javascript.general.remove_stub_throw', CommentType.Actionable)
5+
`('javascript.general.remove_stub_throw', CommentType.Essential)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { TwoFerAnalyzer } from '~src/analyzers/practice/two-fer'
2+
import { makeAnalyze } from '~test/helpers/smoke'
3+
4+
const analyze = makeAnalyze(() => new TwoFerAnalyzer())
5+
6+
describe('two-fer stub throw detection', () => {
7+
it('blocks canonical exercism stub throw', async () => {
8+
const solution = `
9+
export function twoFer(name = 'you') {
10+
return 'One for you, one for me.'
11+
throw new Error('Remove this line and implement the function');
12+
}
13+
`.trim()
14+
15+
const output = await analyze(solution)
16+
17+
const stub = output.comments.find(
18+
(c) => c.externalTemplate === 'javascript.general.remove_stub_throw'
19+
)
20+
21+
expect(stub).toBeDefined()
22+
expect(stub?.type).toBe('essential')
23+
})
24+
25+
it('does not block normal error throws', async () => {
26+
const solution = `
27+
export function twoFer(name = 'you') {
28+
if (name === 'bad') {
29+
throw new Error('Invalid name');
30+
}
31+
return \`One for \${name}, one for me.\`
32+
}
33+
`.trim()
34+
35+
const output = await analyze(solution)
36+
37+
const stub = output.comments.find(
38+
(c) => c.externalTemplate === 'javascript.general.remove_stub_throw'
39+
)
40+
41+
expect(stub).toBeUndefined()
42+
})
43+
})

0 commit comments

Comments
 (0)