Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions crates/oxc_angular_compiler/src/class_metadata/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use oxc_allocator::{Allocator, Box, Vec as AllocVec};
use oxc_ast::ast::{
BindingPattern, ChainElement, Class, ClassElement, Declaration, Decorator, Expression,
FormalParameter, MethodDefinitionKind, ObjectPropertyKind, PropertyKey, Statement,
TSNamespaceDeclarationBody, TSType, TSTypeName, VariableDeclaration,
FormalParameter, ObjectPropertyKind, PropertyKey, Statement, TSNamespaceDeclarationBody,
TSType, TSTypeName, VariableDeclaration,
};
use oxc_span::GetSpan;
use oxc_str::Ident;
Expand Down Expand Up @@ -440,14 +440,7 @@ pub fn build_ctor_params_metadata_in<'a>(
// purely from the type annotation (`typeToValue`), never the DI token.
let _ = constructor_deps;
// Find constructor
let constructor = class.body.body.iter().find_map(|element| {
if let ClassElement::MethodDefinition(method) = element
&& method.kind == MethodDefinitionKind::Constructor
{
return method.value.params.items.as_slice().into();
}
None
})?;
let constructor = crate::util::find_constructor(class)?.value.params.items.as_slice();

// Names the type annotation may resolve to locally but not in value space:
// interfaces, type aliases, `const enum`s, and the class's type parameters —
Expand Down
18 changes: 2 additions & 16 deletions crates/oxc_angular_compiler/src/component/decorator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,14 +664,7 @@ fn extract_constructor_deps<'a>(
consts: &StringConsts<'a>,
) -> Option<Vec<'a, R3DependencyMetadata<'a>>> {
// Find the constructor method
let constructor = class.body.body.iter().find_map(|element| {
if let ClassElement::MethodDefinition(method) = element {
if method.kind == MethodDefinitionKind::Constructor {
return Some(method);
}
}
None
});
let constructor = crate::util::find_constructor(class);

match constructor {
Some(ctor) => {
Expand Down Expand Up @@ -907,14 +900,7 @@ fn for_each_constructor_param_decorator<'a>(
class: &'a Class<'a>,
mut f: impl FnMut(&'a Decorator<'a>),
) {
let constructor = class.body.body.iter().find_map(|element| match element {
ClassElement::MethodDefinition(method)
if method.kind == MethodDefinitionKind::Constructor =>
{
Some(method)
}
_ => None,
});
let constructor = crate::util::find_constructor(class);
for param in constructor.into_iter().flat_map(|ctor| &ctor.value.params.items) {
param.decorators.iter().for_each(&mut f);
}
Expand Down
26 changes: 7 additions & 19 deletions crates/oxc_angular_compiler/src/component/import_elision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
use std::path::Path;

use oxc_ast::ast::{
BindingIdentifier, ClassElement, Expression, ImportDeclarationSpecifier, MethodDefinitionKind,
Program, Statement, StringLiteral, TSType,
BindingIdentifier, ClassElement, Expression, ImportDeclarationSpecifier, Program, Statement,
StringLiteral, TSType,
};
use oxc_ast_visit::Visit;
use oxc_semantic::{ReferenceId, Semantic, SemanticBuilder, SymbolFlags};
Expand Down Expand Up @@ -311,13 +311,7 @@ impl<'a> ImportElisionAnalyzer<'a> {
return;
}
let type_params = crate::class_metadata::type_param_names_of(class);
for element in &class.body.body {
let ClassElement::MethodDefinition(method) = element else {
continue;
};
if method.kind != MethodDefinitionKind::Constructor {
continue;
}
if let Some(method) = crate::util::find_constructor(class) {
for param in &method.value.params.items {
let Some(annotation) = &param.type_annotation else {
continue;
Expand Down Expand Up @@ -554,8 +548,9 @@ impl<'a> ImportElisionAnalyzer<'a> {
/// them and compiles nothing that names them. Only on a class the transform compiles (one with an Angular
/// class decorator, see [`crate::directive::angular_class_decorator`]):
///
/// - an Angular constructor parameter decorator of the first constructor (the one
/// `collect_constructor_decorator_spans` strips and the factory reads), with the root of `@Inject(X)` /
/// - an Angular constructor parameter decorator of the constructor implementation (the one
/// `collect_constructor_decorator_spans` strips and the factory reads, see
/// [`crate::util::find_constructor`]), with the root of `@Inject(X)` /
/// `@Inject(X.sub)`: the factory names `X` through a namespace import (`i1.X.sub`), while any other
/// argument (`TOKEN['sub']`, `forwardRef(() => TOKEN)`) is copied as written
/// - the Angular member decorator itself on a `declare` property, not its arguments: a query predicate
Expand Down Expand Up @@ -622,14 +617,7 @@ impl<'a> ImportElisionAnalyzer<'a> {
}) {
continue;
}
let constructor = class.body.body.iter().find_map(|element| match element {
ClassElement::MethodDefinition(method)
if method.kind == MethodDefinitionKind::Constructor =>
{
Some(method)
}
_ => None,
});
let constructor = crate::util::find_constructor(class);
for param in constructor.into_iter().flat_map(|ctor| &ctor.value.params.items) {
for decorator in &param.decorators {
// by what it resolves to: `@I(TOKEN)` for `import { Inject as I }` is removed too
Expand Down
34 changes: 5 additions & 29 deletions crates/oxc_angular_compiler/src/component/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,15 +1038,7 @@ struct JitNonAngularMemberDecorator {
/// also skips that walk, and our single-file transform is closer to LOCAL
/// mode than to a full reflector.
fn class_has_own_constructor_params(class: &oxc_ast::ast::Class<'_>) -> bool {
use oxc_ast::ast::{ClassElement, MethodDefinitionKind};
class.body.body.iter().any(|element| {
if let ClassElement::MethodDefinition(method) = element {
method.kind == MethodDefinitionKind::Constructor
&& !method.value.params.items.is_empty()
} else {
false
}
})
crate::util::find_constructor(class).is_some_and(|ctor| !ctor.value.params.items.is_empty())
}

/// Return the `@Service` decorator on a class iff the `Service` identifier
Expand Down Expand Up @@ -1263,16 +1255,7 @@ fn extract_jit_ctor_params(
class: &oxc_ast::ast::Class<'_>,
consts: &crate::directive::StringConsts<'_>,
) -> (std::vec::Vec<JitCtorParam>, std::vec::Vec<String>) {
use oxc_ast::ast::{ClassElement, MethodDefinitionKind};

let constructor = class.body.body.iter().find_map(|element| {
if let ClassElement::MethodDefinition(method) = element {
if method.kind == MethodDefinitionKind::Constructor {
return Some(method);
}
}
None
});
let constructor = crate::util::find_constructor(class);

let Some(ctor) = constructor else {
return (std::vec::Vec::new(), std::vec::Vec::new());
Expand Down Expand Up @@ -3315,15 +3298,8 @@ pub fn transform_angular_file(
// __param, so their imports must be kept — the same result
// TypeScript's elision produces on ngtsc's rewritten AST.
let mut param_idents = JitCtorParamIdentifiers::default();
for element in &class.body.body {
if let oxc_ast::ast::ClassElement::MethodDefinition(method) = element
&& method.kind == oxc_ast::ast::MethodDefinitionKind::Constructor
{
oxc_ast_visit::Visit::visit_formal_parameters(
&mut param_idents,
&method.value.params,
);
}
if let Some(ctor) = crate::util::find_constructor(class) {
oxc_ast_visit::Visit::visit_formal_parameters(&mut param_idents, &ctor.value.params);
}
for name in param_idents.0 {
import_elision_mut.preserve(name);
Expand Down Expand Up @@ -4569,7 +4545,7 @@ pub fn transform_angular_file(

// The analyzer counts an Angular constructor parameter decorator, and the Angular decorator of a `declare`
// property, as removed on every class with an Angular class decorator. One the walk above left in place (a
// component whose template is missing, a constructor after the first) keeps what it references.
// component whose template is missing, a constructor other than the implementation) keeps what it references.
{
let stripped: rustc_hash::FxHashSet<Span> =
decorator_spans_to_remove.iter().copied().collect();
Expand Down
26 changes: 3 additions & 23 deletions crates/oxc_angular_compiler/src/directive/decorator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,7 @@ fn extract_constructor_deps<'a>(
consts: &StringConsts<'a>,
) -> Option<Vec<'a, R3DependencyMetadata<'a>>> {
// Find the constructor method
let constructor = class.body.body.iter().find_map(|element| {
if let ClassElement::MethodDefinition(method) = element {
if method.kind == MethodDefinitionKind::Constructor {
return Some(method);
}
}
None
});
let constructor = crate::util::find_constructor(class);

match constructor {
Some(ctor) => {
Expand Down Expand Up @@ -1670,21 +1663,8 @@ pub fn param_decorator_errors<'a>(
return std::vec::Vec::new();
}

class
.body
.body
.iter()
.filter_map(|element| match element {
// Upstream picks the first constructor *with a body*
// (typescript.ts `findConstructorDeclaration`): an overload
// signature's parameters are not checked.
ClassElement::MethodDefinition(m)
if m.kind == MethodDefinitionKind::Constructor && m.value.body.is_some() =>
{
Some(m)
}
_ => None,
})
crate::util::find_constructor(class)
.into_iter()
.flat_map(|ctor| ctor.value.params.items.iter())
.flat_map(|param| param.decorators.iter())
.filter(|d| super::is_angular_core_decorator(d, Some(consts)))
Expand Down
12 changes: 2 additions & 10 deletions crates/oxc_angular_compiler/src/injectable/decorator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

use oxc_allocator::{Allocator, Box, Vec};
use oxc_ast::ast::{
Argument, ArrayExpressionElement, Class, ClassElement, Decorator, Expression,
MethodDefinitionKind, ObjectPropertyKind, PropertyKey,
Argument, ArrayExpressionElement, Class, Decorator, Expression, ObjectPropertyKind, PropertyKey,
};
use oxc_span::Span;
use oxc_str::Ident;
Expand Down Expand Up @@ -570,14 +569,7 @@ pub fn extract_constructor_deps<'a>(
consts: Option<&StringConsts<'_>>,
) -> Option<Vec<'a, R3DependencyMetadata<'a>>> {
// Find the constructor method
let Some(constructor) = class.body.body.iter().find_map(|element| {
if let ClassElement::MethodDefinition(method) = element {
if method.kind == MethodDefinitionKind::Constructor {
return Some(method);
}
}
None
}) else {
let Some(constructor) = crate::util::find_constructor(class) else {
// No constructor: a class with a superclass inherits its factory
// (`None`), one without gets an implicit no-arg constructor.
// See: packages/compiler-cli/src/ngtsc/annotations/common/src/di.ts:47-52
Expand Down
12 changes: 2 additions & 10 deletions crates/oxc_angular_compiler/src/ng_module/decorator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

use oxc_allocator::{Allocator, Box, Vec};
use oxc_ast::ast::{
Argument, ArrayExpressionElement, Class, ClassElement, Decorator, Expression,
MethodDefinitionKind, ObjectPropertyKind, PropertyKey,
Argument, ArrayExpressionElement, Class, Decorator, Expression, ObjectPropertyKind, PropertyKey,
};
use oxc_span::Span;
use oxc_str::Ident;
Expand Down Expand Up @@ -503,14 +502,7 @@ pub fn extract_constructor_deps<'a>(
consts: Option<&StringConsts<'_>>,
) -> Option<Vec<'a, R3DependencyMetadata<'a>>> {
// Find the constructor method
let Some(constructor) = class.body.body.iter().find_map(|element| {
if let ClassElement::MethodDefinition(method) = element {
if method.kind == MethodDefinitionKind::Constructor {
return Some(method);
}
}
None
}) else {
let Some(constructor) = crate::util::find_constructor(class) else {
// No constructor: a class with a superclass inherits its factory
// (`None`), one without gets an implicit no-arg constructor.
// See: packages/compiler-cli/src/ngtsc/annotations/common/src/di.ts:47-52
Expand Down
14 changes: 2 additions & 12 deletions crates/oxc_angular_compiler/src/pipe/decorator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
//! on TypeScript class declarations.

use oxc_allocator::{Allocator, Box, Vec};
use oxc_ast::ast::{
Argument, Class, ClassElement, Decorator, Expression, MethodDefinitionKind, ObjectPropertyKind,
PropertyKey,
};
use oxc_ast::ast::{Argument, Class, Decorator, Expression, ObjectPropertyKind, PropertyKey};
use oxc_span::Span;
use oxc_str::Ident;

Expand Down Expand Up @@ -269,14 +266,7 @@ fn extract_constructor_deps<'a>(
consts: Option<&StringConsts<'_>>,
) -> Option<Vec<'a, R3DependencyMetadata<'a>>> {
// Find the constructor method
let Some(constructor) = class.body.body.iter().find_map(|element| {
if let ClassElement::MethodDefinition(method) = element {
if method.kind == MethodDefinitionKind::Constructor {
return Some(method);
}
}
None
}) else {
let Some(constructor) = crate::util::find_constructor(class) else {
// No constructor: a class with a superclass inherits its factory
// (`None`), one without gets an implicit no-arg constructor.
// See: packages/compiler-cli/src/ngtsc/annotations/common/src/di.ts:47-52
Expand Down
19 changes: 19 additions & 0 deletions crates/oxc_angular_compiler/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,22 @@ pub use type_extract::*;
pub fn is_metadata_property(prop: &oxc_ast::ast::ObjectProperty<'_>) -> bool {
!prop.method && prop.kind == oxc_ast::ast::PropertyKind::Init
}

/// The class's constructor implementation: the one with a body. Overload
/// signatures (no body) are skipped, since the implementation is what runs and
/// what carries the parameter decorators.
///
/// See: packages/compiler-cli/src/ngtsc/reflection/src/typescript.ts
/// (`TypeScriptReflectionHost.getConstructorParameters`)
pub fn find_constructor<'c, 'a>(
class: &'c oxc_ast::ast::Class<'a>,
) -> Option<&'c oxc_ast::ast::MethodDefinition<'a>> {
class.body.body.iter().find_map(|element| match element {
oxc_ast::ast::ClassElement::MethodDefinition(method)
if method.kind.is_constructor() && method.value.body.is_some() =>
{
Some(&**method)
}
_ => None,
})
}
Loading
Loading