Skip to content

Commit

Permalink
feat: support remove unused statement
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Jun 14, 2024
1 parent f6194eb commit 7353716
Show file tree
Hide file tree
Showing 6 changed files with 493 additions and 87 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2211,7 +2211,11 @@ impl<'a> BindingPatternKind<'a> {
}

pub fn is_destructuring_pattern(&self) -> bool {
matches!(self, Self::ObjectPattern(_) | Self::ArrayPattern(_))
match self {
Self::ObjectPattern(_) | Self::ArrayPattern(_) => true,
Self::AssignmentPattern(pattern) => pattern.left.kind.is_destructuring_pattern(),
Self::BindingIdentifier(_) => false,
}
}

pub fn is_binding_identifier(&self) -> bool {
Expand Down
5 changes: 3 additions & 2 deletions crates/oxc_transformer_dts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ oxc_allocator = { workspace = true }
oxc_diagnostics = { workspace = true }
oxc_syntax = { workspace = true, features = ["to_js_string"] }

rustc-hash = { workspace = true }

[dev-dependencies]
oxc_parser = { workspace = true }
oxc_codegen = { workspace = true }
oxc_parser = { workspace = true }

[features]
default = []
25 changes: 23 additions & 2 deletions crates/oxc_transformer_dts/src/inferer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ impl<'a> TransformerDts<'a> {
Some(self.ctx.ast.copy(&expr.type_annotation))
}
}
Expression::ClassExpression(expr) => {
self.ctx.error(
OxcDiagnostic::error(
"
Inference from class expressions is not supported with --isolatedDeclarations.
",
)
.with_label(expr.span),
);
Some(self.ctx.ast.ts_unknown_keyword(SPAN))
}
Expression::TSNonNullExpression(expr) => {
self.infer_type_from_expression(&expr.expression)
}
Expand Down Expand Up @@ -86,14 +97,24 @@ impl<'a> TransformerDts<'a> {
return self.ctx.ast.copy(&function.return_type);
}

FunctionReturnType::infer(
let return_type = FunctionReturnType::infer(
self,
function
.body
.as_ref()
.unwrap_or_else(|| unreachable!("declare function can not have body")),
)
.map(|type_annotation| self.ctx.ast.ts_type_annotation(SPAN, type_annotation))
.map(|type_annotation| self.ctx.ast.ts_type_annotation(SPAN, type_annotation));

if return_type.is_none() {
self.ctx.error(OxcDiagnostic::error(
"Function must have an explicit return type annotation with --isolatedDeclarations.",
).with_label(function.span));

Some(self.ctx.ast.ts_type_annotation(SPAN, self.ctx.ast.ts_unknown_keyword(SPAN)))
} else {
return_type
}
}

pub fn infer_arrow_function_return_type(
Expand Down
Loading

0 comments on commit 7353716

Please sign in to comment.