-
-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(isolated-declarations): false positive for setter method in `inte…
- Loading branch information
Showing
5 changed files
with
122 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use oxc_allocator::{CloneIn, Vec}; | ||
use oxc_ast::ast::{TSMethodSignatureKind, TSSignature}; | ||
use rustc_hash::FxHashMap; | ||
|
||
use crate::IsolatedDeclarations; | ||
|
||
impl<'a> IsolatedDeclarations<'a> { | ||
/// Transform setter signature or getter return type to match the other | ||
/// | ||
/// Infer get accessor return type from set accessor's param type | ||
/// Infer set accessor parameter type from get accessor return type | ||
pub fn transform_ts_signatures(&mut self, signatures: &mut Vec<'a, TSSignature<'a>>) { | ||
// <name, (requires_inference, first_param_annotation, return_type)> | ||
let mut method_annotations: FxHashMap<_, (bool, _, _)> = FxHashMap::default(); | ||
|
||
signatures.iter_mut().for_each(|signature| { | ||
if let TSSignature::TSMethodSignature(method) = signature { | ||
let Some(name) = method.key.static_name() else { | ||
return; | ||
}; | ||
match method.kind { | ||
TSMethodSignatureKind::Method => {} | ||
TSMethodSignatureKind::Set => { | ||
let Some(first_param) = method.params.items.first_mut() else { | ||
return; | ||
}; | ||
|
||
let entry = method_annotations.entry(name).or_default(); | ||
entry.0 |= first_param.pattern.type_annotation.is_some(); | ||
entry.1 = Some(&mut first_param.pattern.type_annotation); | ||
} | ||
TSMethodSignatureKind::Get => { | ||
let entry = method_annotations.entry(name).or_default(); | ||
entry.0 |= method.return_type.is_some(); | ||
entry.2 = Some(&mut method.return_type); | ||
} | ||
}; | ||
} | ||
}); | ||
|
||
for (requires_inference, param, return_type) in method_annotations.into_values() { | ||
if !requires_inference { | ||
return; | ||
} | ||
if let (Some(Some(annotation)), Some(option)) | (Some(option), Some(Some(annotation))) = | ||
(param, return_type) | ||
{ | ||
option.replace(annotation.clone_in(self.ast.allocator)); | ||
} | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
crates/oxc_isolated_declarations/tests/fixtures/signatures.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export interface X { | ||
set value(_: string); | ||
} | ||
|
||
export type A = { | ||
set value({ a, b, c }: { a: string; b: string; c: string }); | ||
get value(); | ||
}; | ||
|
||
export interface I { | ||
set value(_); | ||
get value(): string; | ||
} |
25 changes: 25 additions & 0 deletions
25
crates/oxc_isolated_declarations/tests/snapshots/signatures.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
source: crates/oxc_isolated_declarations/tests/mod.rs | ||
input_file: crates/oxc_isolated_declarations/tests/fixtures/signatures.ts | ||
--- | ||
==================== .D.TS ==================== | ||
|
||
export interface X { | ||
set value(_: string); | ||
} | ||
export type A = { | ||
set value({ a, b, c }: { | ||
a: string; | ||
b: string; | ||
c: string; | ||
}); | ||
get value(): { | ||
a: string; | ||
b: string; | ||
c: string; | ||
}; | ||
}; | ||
export interface I { | ||
set value(_: string); | ||
get value(): string; | ||
} |