Skip to content

Commit db8978f

Browse files
fix(injectable): don't default providedIn to 'root' when not specified (#194)
@Injectable() and @Injectable({}) were incorrectly defaulting providedIn to 'root'. Angular's actual behavior is to omit providedIn entirely when it's not explicitly specified, as confirmed by the official compliance tests.
1 parent 9b7132e commit db8978f

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

crates/oxc_angular_compiler/src/component/transform.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4310,10 +4310,10 @@ export class LocalService {}
43104310
result.code.contains("ɵɵdefineInjectable"),
43114311
"Code should contain ɵɵdefineInjectable"
43124312
);
4313-
// Should default to providedIn: "root" (Angular's default behavior)
4313+
// Should NOT have providedIn when not explicitly specified
43144314
assert!(
4315-
result.code.contains(r#"providedIn:"root""#),
4316-
"Code should contain providedIn:\"root\" by default, but got:\n{}",
4315+
!result.code.contains("providedIn"),
4316+
"Code should NOT contain providedIn when not specified, but got:\n{}",
43174317
result.code
43184318
);
43194319
}

crates/oxc_angular_compiler/src/injectable/decorator.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,12 @@ pub fn extract_injectable_metadata<'a>(
230230
_ => return None,
231231
};
232232

233-
// If no arguments, return basic metadata with default providedIn: 'root'
233+
// If no arguments, return basic metadata with no providedIn
234234
if call_expr.arguments.is_empty() {
235235
return Some(InjectableMetadata {
236236
class_name,
237237
class_span,
238-
provided_in: Some(ProvidedInValue::Root),
238+
provided_in: None,
239239
use_class: None,
240240
use_factory: None,
241241
use_value: None,
@@ -250,8 +250,8 @@ pub fn extract_injectable_metadata<'a>(
250250
_ => return None,
251251
};
252252

253-
// Extract providedIn (default to 'root' if not specified)
254-
let provided_in = extract_provided_in(allocator, config_obj).or(Some(ProvidedInValue::Root));
253+
// Extract providedIn (None if not specified)
254+
let provided_in = extract_provided_in(allocator, config_obj);
255255

256256
// Extract useClass
257257
let use_class = extract_use_class(allocator, config_obj);
@@ -681,8 +681,8 @@ mod tests {
681681
assert!(metadata.is_some());
682682
let metadata = metadata.unwrap();
683683
assert_eq!(metadata.class_name.as_str(), "MyService");
684-
// Default to 'root' when not specified (Angular's default behavior)
685-
assert!(matches!(metadata.provided_in, Some(ProvidedInValue::Root)));
684+
// No providedIn when not specified
685+
assert!(metadata.provided_in.is_none());
686686
}
687687

688688
#[test]
@@ -715,7 +715,7 @@ mod tests {
715715

716716
#[test]
717717
fn test_extract_injectable_with_empty_object() {
718-
// @Injectable({}) should default to providedIn: 'root'
718+
// @Injectable({}) should have no providedIn
719719
let allocator = Allocator::default();
720720
let source = r#"
721721
@Injectable({})
@@ -726,8 +726,8 @@ mod tests {
726726
assert!(metadata.is_some());
727727
let metadata = metadata.unwrap();
728728
assert_eq!(metadata.class_name.as_str(), "MyService");
729-
// Default to 'root' when providedIn is not specified in the object
730-
assert!(matches!(metadata.provided_in, Some(ProvidedInValue::Root)));
729+
// No providedIn when not specified in the object
730+
assert!(metadata.provided_in.is_none());
731731
}
732732

733733
#[test]

0 commit comments

Comments
 (0)