@@ -129,7 +129,7 @@ export class ConstructNode {
129
129
constructor ( private readonly host : Construct , scope : IConstruct , id : string ) {
130
130
id = id || '' ; // if undefined, convert to empty string
131
131
132
- this . id = id ;
132
+ this . id = sanitizeId ( id ) ;
133
133
this . scope = scope ;
134
134
135
135
// We say that scope is required, but root scopes will bypass the type
@@ -146,9 +146,6 @@ export class ConstructNode {
146
146
this . id = id ;
147
147
}
148
148
149
- // escape any path separators so they don't wreck havoc
150
- this . id = this . _escapePathSeparator ( this . id ) ;
151
-
152
149
if ( Token . isUnresolved ( id ) ) {
153
150
throw new Error ( `Cannot use tokens in construct ID: ${ id } ` ) ;
154
151
}
@@ -174,25 +171,13 @@ export class ConstructNode {
174
171
}
175
172
176
173
/**
177
- * Return a descendant by path , or undefined
174
+ * Return a direct child by id , or undefined
178
175
*
179
- * Note that if the original ID of the construct you are looking for contained
180
- * a '/', then it would have been replaced by '--'.
181
- *
182
- * @param path Relative path of a direct or indirect child
183
- * @returns a child by path or undefined if not found.
176
+ * @param id Identifier of direct child
177
+ * @returns the child if found, or undefined
184
178
*/
185
- public tryFindChild ( path : string ) : IConstruct | undefined {
186
- if ( path . startsWith ( ConstructNode . PATH_SEP ) ) {
187
- throw new Error ( 'Path must be relative' ) ;
188
- }
189
- const parts = path . split ( ConstructNode . PATH_SEP ) ;
190
-
191
- let curr : IConstruct | undefined = this . host ;
192
- while ( curr != null && parts . length > 0 ) {
193
- curr = curr . node . _children [ parts . shift ( ) ! ] ;
194
- }
195
- return curr ;
179
+ public tryFindChild ( id : string ) : IConstruct | undefined {
180
+ return this . _children [ sanitizeId ( id ) ] ;
196
181
}
197
182
198
183
/**
@@ -527,14 +512,6 @@ export class ConstructNode {
527
512
this . invokedAspects . push ( aspect ) ;
528
513
}
529
514
}
530
-
531
- /**
532
- * If the construct ID contains a path separator, it is replaced by double dash (`--`).
533
- */
534
- private _escapePathSeparator ( id : string ) {
535
- if ( ! id ) { return id ; }
536
- return id . split ( ConstructNode . PATH_SEP ) . join ( '--' ) ;
537
- }
538
515
}
539
516
540
517
/**
@@ -715,3 +692,13 @@ function ignore(_x: any) {
715
692
// Import this _after_ everything else to help node work the classes out in the correct order...
716
693
717
694
import { Reference } from './reference' ;
695
+
696
+ const PATH_SEP_REGEX = new RegExp ( `${ ConstructNode . PATH_SEP } ` , 'g' ) ;
697
+
698
+ /**
699
+ * Return a sanitized version of an arbitrary string, so it can be used as an ID
700
+ */
701
+ function sanitizeId ( id : string ) {
702
+ // Escape path seps as double dashes
703
+ return id . replace ( PATH_SEP_REGEX , '--' ) ;
704
+ }
0 commit comments