Skip to content

Commit e4be02e

Browse files
committed
fix: fix bugs in ClassProperty matching/replacement
1 parent 334ea11 commit e4be02e

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

src/compileMatcher/ClassProperty.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default function compileClassPropertyMatcher(
1313
if (
1414
!pattern.computed &&
1515
!pattern.static &&
16+
pattern.typeAnnotation == null &&
1617
pattern.variance == null &&
1718
pattern.value == null
1819
) {

src/compileReplacement/ClassProperty.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,18 @@ export default function compileClassPropertyReplacement(
1313
!pattern.computed &&
1414
!pattern.static &&
1515
pattern.variance == null &&
16-
pattern.value == null
16+
pattern.value == null &&
17+
(pattern.typeAnnotation == null ||
18+
(n.TypeAnnotation.check(pattern.typeAnnotation) &&
19+
n.GenericTypeAnnotation.check(
20+
pattern.typeAnnotation?.typeAnnotation
21+
) &&
22+
n.Identifier.check(pattern.typeAnnotation.typeAnnotation.id) &&
23+
pattern.typeAnnotation.typeAnnotation.id.name === '$') ||
24+
(n.TSTypeAnnotation.check(pattern.typeAnnotation) &&
25+
n.TSTypeReference.check(pattern.typeAnnotation?.typeAnnotation) &&
26+
n.Identifier.check(pattern.typeAnnotation.typeAnnotation.typeName) &&
27+
pattern.typeAnnotation.typeAnnotation.typeName.name === '$'))
1728
) {
1829
const placeholderReplacement = compilePlaceholderReplacement(
1930
path,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export const input = `
2+
class Foo {
3+
a: number;
4+
b: string;
5+
}
6+
`
7+
8+
export const find = `
9+
class X { /**/ $a: $T }
10+
`
11+
12+
export const replace = `
13+
class X { /**/ declare $a: $T }
14+
`
15+
16+
export const expectedReplace = `
17+
class Foo {
18+
declare a: number;
19+
declare b: string;
20+
}
21+
`

test/findReplace/testcases/findPromiseMethodCalls.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,31 @@ $Or($a.then($handleValue), $a.then($handleValue, $handleError), $a.catch($handle
1414

1515
export const expectedFind = [
1616
{
17-
arrayCaptures: {
18-
$$args: ['error => blah'],
17+
captures: {
18+
$a: 'thing.then(() => blah)',
19+
$handleFinally: '() => done',
1920
},
21+
node: 'thing.then(() => blah).finally(() => done)',
22+
},
23+
{
24+
captures: {
25+
$a: 'thing',
26+
$handleValue: '() => blah',
27+
},
28+
node: 'thing.then(() => blah)',
29+
},
30+
{
2031
captures: {
2132
$a: 'thing.then(\n value => value * 2,\n error => logged(error)\n )',
33+
$handleError: 'error => blah',
2234
},
2335
node: 'thing.then(\n value => value * 2,\n error => logged(error)\n ).catch(error => blah)',
2436
},
2537
{
26-
arrayCaptures: {
27-
$$args: ['value => value * 2', 'error => logged(error)'],
28-
},
2938
captures: {
3039
$a: 'thing',
40+
$handleError: 'error => logged(error)',
41+
$handleValue: 'value => value * 2',
3142
},
3243
node: 'thing.then(\n value => value * 2,\n error => logged(error)\n )',
3344
},

0 commit comments

Comments
 (0)