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
52 changes: 32 additions & 20 deletions crates/oxc_angular_compiler/src/directive/property_decorators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1940,28 +1940,40 @@ pub(crate) fn lone_surrogate_host_error<'a>(
consts: &super::StringConsts<'a>,
) -> Option<(String, Span)> {
// The members `extract_host_bindings_in` / `extract_host_listeners_in`
// compile, with their decorator's call.
// compile, with the call of each of their decorators.
let members = |name: &'static str| {
class.body.body.iter().filter_map(move |element| {
let (decorators, key, is_static) = match element {
ClassElement::PropertyDefinition(prop) => {
(&prop.decorators, &prop.key, prop.r#static)
}
ClassElement::MethodDefinition(method) => {
(&method.decorators, &method.key, method.r#static)
}
ClassElement::AccessorProperty(prop) => {
(&prop.decorators, &prop.key, prop.r#static)
class
.body
.body
.iter()
.filter_map(move |element| {
let (decorators, key, is_static) = match element {
ClassElement::PropertyDefinition(prop) => {
(&prop.decorators, &prop.key, prop.r#static)
}
ClassElement::MethodDefinition(method) => {
(&method.decorators, &method.key, method.r#static)
}
ClassElement::AccessorProperty(prop) => {
(&prop.decorators, &prop.key, prop.r#static)
}
_ => return None,
};
if is_static || get_property_key_name(key).is_none() {
return None;
}
_ => return None,
};
if is_static || get_property_key_name(key).is_none() {
return None;
}
let decorator = find_decorator_by_name(decorators, name, Some(consts))?;
let Expression::CallExpression(call) = &decorator.expression else { return None };
Some((decorator, call))
})
// Every stacked decorator is compiled, so every one is checked
Some(decorators.iter().filter_map(move |decorator| {
if angular_member_decorator(decorator, Some(consts)) != Some(name) {
return None;
}
let Expression::CallExpression(call) = &decorator.expression else {
return None;
};
Some((decorator, call))
}))
})
.flatten()
};
let suffix = Value::Dynamic.wrong_type_suffix();
let is_lone = |arg: &Argument<'_>| arg.as_expression().is_some_and(is_lone_surrogate_literal);
Expand Down
38 changes: 38 additions & 0 deletions crates/oxc_angular_compiler/tests/stacked_host_decorators_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,41 @@ export class CCmp {
);
assert!(code.contains("hostVars:3"), "Got:\n{}", result.code);
}

#[test]
fn an_unreadable_argument_on_a_later_stacked_decorator_is_reported() {
// A lone surrogate (`'\uD800'`) can't be read. Like on the first decorator, it is
// reported instead of compiling a fallback (the member name, no listener, no args).
for (member, expected) in [
(
r"@HostBinding('title') @HostBinding('\uD800') v = 'a';",
"@HostBinding's argument must be a string Value could not be determined statically.",
),
(
r"@HostListener('click') @HostListener('\uD800') a() {}",
"@HostListener's event name argument must be a string Value could not be \
determined statically.",
),
(
r"@HostListener('click') @HostListener('x', ['\uD800']) a(x: any) {}",
"Failed to resolve @HostListener.args at position 0 to a string Value could not be \
determined statically.",
),
] {
let source = format!(
"import {{ Directive, HostBinding, HostListener }} from '@angular/core';\n\
@Directive({{ selector: '[d]' }})\n\
export class D {{\n {member}\n}}"
);
let allocator = Allocator::default();
let result = transform_angular_file(
&allocator,
"test.ts",
&source,
Some(&TransformOptions::default()),
None,
);
let messages: Vec<String> = result.diagnostics.iter().map(|d| format!("{d}")).collect();
assert_eq!(messages, [expected], "`{member}`");
}
}
Loading