Skip to content

Commit f1b1c4d

Browse files
Brooooooklynclaude
andcommitted
refactor: deduplicate create_host_directive_mappings_array across directive/component
Mirrors Angular's structure where `createHostDirectivesMappingArray` is defined once in `view/compiler.ts` and imported by `partial/directive.ts`. The shared function now lives in `directive::compiler` (pub(crate)) and is imported by `component::definition`, with the `with_capacity_in` optimization. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent da9f6d6 commit f1b1c4d

File tree

3 files changed

+9
-30
lines changed

3 files changed

+9
-30
lines changed

crates/oxc_angular_compiler/src/component/definition.rs

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ use super::metadata::{
2323
ViewEncapsulation,
2424
};
2525
use super::namespace_registry::NamespaceRegistry;
26-
use crate::directive::{create_inputs_literal, create_outputs_literal};
26+
use crate::directive::{
27+
create_host_directive_mappings_array, create_inputs_literal, create_outputs_literal,
28+
};
2729
use crate::output::ast::{
2830
FnParam, FunctionExpr, InstantiateExpr, InvokeFunctionExpr, LiteralArrayExpr, LiteralExpr,
2931
LiteralMapEntry, LiteralMapExpr, LiteralValue, OutputExpression, OutputStatement, ReadPropExpr,
@@ -1256,33 +1258,6 @@ fn create_host_directives_arg<'a>(
12561258
}
12571259
}
12581260

1259-
/// Create a host directive mappings array.
1260-
///
1261-
/// Format: ['publicName', 'internalName', 'publicName2', 'internalName2']
1262-
fn create_host_directive_mappings_array<'a>(
1263-
allocator: &'a Allocator,
1264-
mappings: &[(Atom<'a>, Atom<'a>)],
1265-
) -> OutputExpression<'a> {
1266-
let mut entries: OxcVec<'a, OutputExpression<'a>> =
1267-
OxcVec::with_capacity_in(mappings.len() * 2, allocator);
1268-
1269-
for (public_name, internal_name) in mappings {
1270-
entries.push(OutputExpression::Literal(Box::new_in(
1271-
LiteralExpr { value: LiteralValue::String(public_name.clone()), source_span: None },
1272-
allocator,
1273-
)));
1274-
entries.push(OutputExpression::Literal(Box::new_in(
1275-
LiteralExpr { value: LiteralValue::String(internal_name.clone()), source_span: None },
1276-
allocator,
1277-
)));
1278-
}
1279-
1280-
OutputExpression::LiteralArray(Box::new_in(
1281-
LiteralArrayExpr { entries, source_span: None },
1282-
allocator,
1283-
))
1284-
}
1285-
12861261
/// Generate `ɵɵExternalStylesFeature(['style.css', ...])` expression.
12871262
///
12881263
/// See: packages/compiler/src/render3/view/compiler.ts:150-155

crates/oxc_angular_compiler/src/directive/compiler.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -916,11 +916,14 @@ fn create_host_directives_feature_arg<'a>(
916916
/// Creates a host directive mappings array.
917917
///
918918
/// Format: `['publicName', 'internalName', 'publicName2', 'internalName2']`
919-
fn create_host_directive_mappings_array<'a>(
919+
///
920+
/// Shared between directive and component compilers, mirroring Angular's
921+
/// `createHostDirectivesMappingArray` in `view/compiler.ts`.
922+
pub(crate) fn create_host_directive_mappings_array<'a>(
920923
allocator: &'a Allocator,
921924
mappings: &[(Atom<'a>, Atom<'a>)],
922925
) -> OutputExpression<'a> {
923-
let mut entries = Vec::new_in(allocator);
926+
let mut entries = Vec::with_capacity_in(mappings.len() * 2, allocator);
924927

925928
for (public_name, internal_name) in mappings {
926929
entries.push(OutputExpression::Literal(Box::new_in(

crates/oxc_angular_compiler/src/directive/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mod metadata;
1919
mod property_decorators;
2020
mod query;
2121

22+
pub(crate) use compiler::create_host_directive_mappings_array;
2223
pub use compiler::{
2324
DirectiveCompileResult, compile_directive, compile_directive_from_metadata,
2425
create_inputs_literal, create_outputs_literal,

0 commit comments

Comments
 (0)