Skip to content

Commit

Permalink
fix: fix bugs in ClassProperty matching/replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1211 committed Sep 23, 2023
1 parent 334ea11 commit e4be02e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/compileMatcher/ClassProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default function compileClassPropertyMatcher(
if (
!pattern.computed &&
!pattern.static &&
pattern.typeAnnotation == null &&
pattern.variance == null &&
pattern.value == null
) {
Expand Down
13 changes: 12 additions & 1 deletion src/compileReplacement/ClassProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,18 @@ export default function compileClassPropertyReplacement(
!pattern.computed &&
!pattern.static &&
pattern.variance == null &&
pattern.value == null
pattern.value == null &&
(pattern.typeAnnotation == null ||
(n.TypeAnnotation.check(pattern.typeAnnotation) &&
n.GenericTypeAnnotation.check(
pattern.typeAnnotation?.typeAnnotation
) &&
n.Identifier.check(pattern.typeAnnotation.typeAnnotation.id) &&
pattern.typeAnnotation.typeAnnotation.id.name === '$') ||
(n.TSTypeAnnotation.check(pattern.typeAnnotation) &&
n.TSTypeReference.check(pattern.typeAnnotation?.typeAnnotation) &&
n.Identifier.check(pattern.typeAnnotation.typeAnnotation.typeName) &&
pattern.typeAnnotation.typeAnnotation.typeName.name === '$'))
) {
const placeholderReplacement = compilePlaceholderReplacement(
path,
Expand Down
21 changes: 21 additions & 0 deletions test/findReplace/testcases/bugs_convertToDeclare.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export const input = `
class Foo {
a: number;
b: string;
}
`

export const find = `
class X { /**/ $a: $T }
`

export const replace = `
class X { /**/ declare $a: $T }
`

export const expectedReplace = `
class Foo {
declare a: number;
declare b: string;
}
`
21 changes: 16 additions & 5 deletions test/findReplace/testcases/findPromiseMethodCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,31 @@ $Or($a.then($handleValue), $a.then($handleValue, $handleError), $a.catch($handle

export const expectedFind = [
{
arrayCaptures: {
$$args: ['error => blah'],
captures: {
$a: 'thing.then(() => blah)',
$handleFinally: '() => done',
},
node: 'thing.then(() => blah).finally(() => done)',
},
{
captures: {
$a: 'thing',
$handleValue: '() => blah',
},
node: 'thing.then(() => blah)',
},
{
captures: {
$a: 'thing.then(\n value => value * 2,\n error => logged(error)\n )',
$handleError: 'error => blah',
},
node: 'thing.then(\n value => value * 2,\n error => logged(error)\n ).catch(error => blah)',
},
{
arrayCaptures: {
$$args: ['value => value * 2', 'error => logged(error)'],
},
captures: {
$a: 'thing',
$handleError: 'error => logged(error)',
$handleValue: 'value => value * 2',
},
node: 'thing.then(\n value => value * 2,\n error => logged(error)\n )',
},
Expand Down

0 comments on commit e4be02e

Please sign in to comment.