Skip to content

Commit

Permalink
feat(isolated-declarations): treat AssignmentPattern as optional (#3748)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Jun 19, 2024
1 parent 887da40 commit 9ea30c4
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 21 deletions.
15 changes: 8 additions & 7 deletions crates/oxc_isolated_declarations/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl<'a> IsolatedDeclarations<'a> {
pub fn transform_formal_parameter(
&self,
param: &FormalParameter<'a>,
next_param: Option<&FormalParameter<'a>>,
is_remaining_params_have_required: bool,
) -> FormalParameter<'a> {
let is_assignment_pattern = param.pattern.kind.is_assignment_pattern();
let mut pattern =
Expand All @@ -52,9 +52,6 @@ impl<'a> IsolatedDeclarations<'a> {
};

if is_assignment_pattern || pattern.type_annotation.is_none() {
let is_next_param_optional =
next_param.map_or(true, |next_param| next_param.pattern.optional);

let type_annotation = pattern
.type_annotation
.as_ref()
Expand All @@ -70,7 +67,7 @@ impl<'a> IsolatedDeclarations<'a> {
.map(|ts_type| {
// jf next param is not optional and current param is assignment pattern
// we need to add undefined to it's type
if !is_next_param_optional {
if is_remaining_params_have_required {
if matches!(ts_type, TSType::TSTypeReference(_)) {
self.error(implicitly_adding_undefined_to_type(param.span));
} else if !ts_type.is_maybe_undefined() {
Expand All @@ -95,7 +92,7 @@ impl<'a> IsolatedDeclarations<'a> {
self.ast.copy(&pattern.kind),
type_annotation,
// if it's assignment pattern, it's optional
pattern.optional || (is_next_param_optional && is_assignment_pattern),
pattern.optional || (!is_remaining_params_have_required && is_assignment_pattern),
);
}

Expand All @@ -119,7 +116,11 @@ impl<'a> IsolatedDeclarations<'a> {

let items =
self.ast.new_vec_from_iter(params.items.iter().enumerate().map(|(index, item)| {
self.transform_formal_parameter(item, params.items.get(index + 1))
let is_remaining_params_have_required =
params.items.iter().skip(index).any(|item| {
!(item.pattern.optional || item.pattern.kind.is_assignment_pattern())
});
self.transform_formal_parameter(item, is_remaining_params_have_required)
}));

if let Some(rest) = &params.rest {
Expand Down
1 change: 0 additions & 1 deletion crates/oxc_isolated_declarations/tests/fixtures/complex.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Correct
export function fnDeclGood(p: T = [], rParam = ""): void { };
export function fnDeclGood2(p: T = [], rParam?: number): void { };

// Incorrect
export function fnDeclBad<T>(p: T = [], rParam: T = "", r2: T): void { }
export function fnDeclBad2<T>(p: T = [], r2: T): void { }
export function fnDeclBad3<T>(p: T = [], rParam?: T, r2: T): void { }
1 change: 0 additions & 1 deletion crates/oxc_isolated_declarations/tests/fixtures/simple.ts

This file was deleted.

25 changes: 23 additions & 2 deletions crates/oxc_isolated_declarations/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
use std::{fs, path::Path};
use std::{fs, path::Path, sync::Arc};

use oxc_allocator::Allocator;
use oxc_codegen::CodeGenerator;
use oxc_isolated_declarations::IsolatedDeclarations;
use oxc_parser::Parser;
use oxc_span::SourceType;

fn transform(path: &Path, source_text: &str) -> String {
let allocator = Allocator::default();
let source_type = SourceType::from_path(path).unwrap();
let program = Parser::new(&allocator, source_text, source_type).parse().program;
CodeGenerator::new().build(&program).source_text

let ret = IsolatedDeclarations::new(&allocator).build(&program);
let code = CodeGenerator::new().build(&ret.program).source_text;

let mut snapshot = format!("==================== .D.TS ====================\n\n{code}\n\n");
if !ret.errors.is_empty() {
let source = Arc::new(source_text.to_string());
let error_messages = ret
.errors
.iter()
.map(|d| d.clone().with_source_code(Arc::clone(&source)))
.map(|error| format!("{error:?}"))
.collect::<Vec<_>>()
.join("\n");

snapshot.push_str(&format!(
"==================== Errors ====================\n\n{error_messages}\n\n"
));
}

snapshot
}

#[test]
Expand Down
5 changes: 0 additions & 5 deletions crates/oxc_isolated_declarations/tests/snapshots/complex.snap

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
source: crates/oxc_isolated_declarations/tests/mod.rs
input_file: crates/oxc_isolated_declarations/tests/fixtures/function-parameters.ts
---
==================== .D.TS ====================

export declare function fnDeclGood(p?: T, rParam?: string): void;
export declare function fnDeclGood2(p?: T, rParam?: number): void;
export declare function fnDeclBad<T>(p: T, rParam: T, r2: T): void;
export declare function fnDeclBad2<T>(p: T, r2: T): void;
export declare function fnDeclBad3<T>(p: T, rParam?: T, r2: T): void;


==================== Errors ====================

x Declaration emit for this parameter requires implicitly adding undefined
| to it's type. This is not supported with --isolatedDeclarations.
,-[6:30]
5 | // Incorrect
6 | export function fnDeclBad<T>(p: T = [], rParam: T = "", r2: T): void { }
: ^^^^^^^^^
7 | export function fnDeclBad2<T>(p: T = [], r2: T): void { }
`----
x Declaration emit for this parameter requires implicitly adding undefined
| to it's type. This is not supported with --isolatedDeclarations.
,-[6:41]
5 | // Incorrect
6 | export function fnDeclBad<T>(p: T = [], rParam: T = "", r2: T): void { }
: ^^^^^^^^^^^^^^
7 | export function fnDeclBad2<T>(p: T = [], r2: T): void { }
`----
x Declaration emit for this parameter requires implicitly adding undefined
| to it's type. This is not supported with --isolatedDeclarations.
,-[7:31]
6 | export function fnDeclBad<T>(p: T = [], rParam: T = "", r2: T): void { }
7 | export function fnDeclBad2<T>(p: T = [], r2: T): void { }
: ^^^^^^^^^
8 | export function fnDeclBad3<T>(p: T = [], rParam?: T, r2: T): void { }
`----
x Declaration emit for this parameter requires implicitly adding undefined
| to it's type. This is not supported with --isolatedDeclarations.
,-[8:31]
7 | export function fnDeclBad2<T>(p: T = [], r2: T): void { }
8 | export function fnDeclBad3<T>(p: T = [], rParam?: T, r2: T): void { }
: ^^^^^^^^^
`----
5 changes: 0 additions & 5 deletions crates/oxc_isolated_declarations/tests/snapshots/simple.snap

This file was deleted.

0 comments on commit 9ea30c4

Please sign in to comment.