@@ -937,10 +937,8 @@ export class Block implements IASTNodeLocation {
937937 */
938938 setEditable ( editable : boolean ) {
939939 this . editable = editable ;
940- for ( let i = 0 , input ; ( input = this . inputList [ i ] ) ; i ++ ) {
941- for ( let j = 0 , field ; ( field = input . fieldRow [ j ] ) ; j ++ ) {
942- field . updateEditable ( ) ;
943- }
940+ for ( const field of this . getFields ( ) ) {
941+ field . updateEditable ( ) ;
944942 }
945943 }
946944
@@ -1107,29 +1105,37 @@ export class Block implements IASTNodeLocation {
11071105 ' instead' ,
11081106 ) ;
11091107 }
1110- for ( let i = 0 , input ; ( input = this . inputList [ i ] ) ; i ++ ) {
1111- for ( let j = 0 , field ; ( field = input . fieldRow [ j ] ) ; j ++ ) {
1112- if ( field . name === name ) {
1113- return field ;
1114- }
1108+ for ( const field of this . getFields ( ) ) {
1109+ if ( field . name === name ) {
1110+ return field ;
11151111 }
11161112 }
11171113 return null ;
11181114 }
11191115
1116+ /**
1117+ * Returns a generator that provides every field on the block.
1118+ *
1119+ * @yields A generator that can be used to iterate the fields on the block.
1120+ */
1121+ * getFields ( ) : Generator < Field > {
1122+ for ( const input of this . inputList ) {
1123+ for ( const field of input . fieldRow ) {
1124+ yield field ;
1125+ }
1126+ }
1127+ }
1128+
11201129 /**
11211130 * Return all variables referenced by this block.
11221131 *
11231132 * @returns List of variable ids.
11241133 */
11251134 getVars ( ) : string [ ] {
11261135 const vars : string [ ] = [ ] ;
1127- for ( let i = 0 , input ; ( input = this . inputList [ i ] ) ; i ++ ) {
1128- for ( let j = 0 , field ; ( field = input . fieldRow [ j ] ) ; j ++ ) {
1129- if ( field . referencesVariables ( ) ) {
1130- // NOTE: This only applies to `FieldVariable`, a `Field<string>`
1131- vars . push ( field . getValue ( ) as string ) ;
1132- }
1136+ for ( const field of this . getFields ( ) ) {
1137+ if ( field . referencesVariables ( ) ) {
1138+ vars . push ( field . getValue ( ) ) ;
11331139 }
11341140 }
11351141 return vars ;
@@ -1143,17 +1149,15 @@ export class Block implements IASTNodeLocation {
11431149 */
11441150 getVarModels ( ) : IVariableModel < IVariableState > [ ] {
11451151 const vars = [ ] ;
1146- for ( let i = 0 , input ; ( input = this . inputList [ i ] ) ; i ++ ) {
1147- for ( let j = 0 , field ; ( field = input . fieldRow [ j ] ) ; j ++ ) {
1148- if ( field . referencesVariables ( ) ) {
1149- const model = this . workspace . getVariableById (
1150- field . getValue ( ) as string ,
1151- ) ;
1152- // Check if the variable actually exists (and isn't just a potential
1153- // variable).
1154- if ( model ) {
1155- vars . push ( model ) ;
1156- }
1152+ for ( const field of this . getFields ( ) ) {
1153+ if ( field . referencesVariables ( ) ) {
1154+ const model = this . workspace . getVariableById (
1155+ field . getValue ( ) as string ,
1156+ ) ;
1157+ // Check if the variable actually exists (and isn't just a potential
1158+ // variable).
1159+ if ( model ) {
1160+ vars . push ( model ) ;
11571161 }
11581162 }
11591163 }
@@ -1168,14 +1172,12 @@ export class Block implements IASTNodeLocation {
11681172 * @internal
11691173 */
11701174 updateVarName ( variable : IVariableModel < IVariableState > ) {
1171- for ( let i = 0 , input ; ( input = this . inputList [ i ] ) ; i ++ ) {
1172- for ( let j = 0 , field ; ( field = input . fieldRow [ j ] ) ; j ++ ) {
1173- if (
1174- field . referencesVariables ( ) &&
1175- variable . getId ( ) === field . getValue ( )
1176- ) {
1177- field . refreshVariableName ( ) ;
1178- }
1175+ for ( const field of this . getFields ( ) ) {
1176+ if (
1177+ field . referencesVariables ( ) &&
1178+ variable . getId ( ) === field . getValue ( )
1179+ ) {
1180+ field . refreshVariableName ( ) ;
11791181 }
11801182 }
11811183 }
@@ -1189,11 +1191,9 @@ export class Block implements IASTNodeLocation {
11891191 * updated name.
11901192 */
11911193 renameVarById ( oldId : string , newId : string ) {
1192- for ( let i = 0 , input ; ( input = this . inputList [ i ] ) ; i ++ ) {
1193- for ( let j = 0 , field ; ( field = input . fieldRow [ j ] ) ; j ++ ) {
1194- if ( field . referencesVariables ( ) && oldId === field . getValue ( ) ) {
1195- field . setValue ( newId ) ;
1196- }
1194+ for ( const field of this . getFields ( ) ) {
1195+ if ( field . referencesVariables ( ) && oldId === field . getValue ( ) ) {
1196+ field . setValue ( newId ) ;
11971197 }
11981198 }
11991199 }
0 commit comments