Skip to content

Commit aff6dfc

Browse files
committed
fix review comments + linting
- rename willGenerateDescriptorInterface - create getIndentSpacing funciton - avoid similar indentLevel variable names
1 parent e7e262b commit aff6dfc

File tree

1 file changed

+60
-41
lines changed

1 file changed

+60
-41
lines changed

rostsd_gen/index.js

Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,15 @@ function savePkgInfoAsTSD(pkgInfos, fd) {
133133

134134
// generate descriptor msg/srv/action interfaces
135135
fs.writeSync(fd, ` namespace ${descriptorInterfaceNamespace} {\n`);
136-
const descriptorInterfaceType = true;
136+
const willGenerateDescriptorInterface = true;
137137
generateRosMsgInterfaces(
138138
pkgInfo,
139139
subfolder,
140140
messagesMap,
141141
servicesMap,
142142
actionsMap,
143143
fd,
144-
descriptorInterfaceType
144+
willGenerateDescriptorInterface
145145
);
146146
// close namespace descriptor declare
147147
fs.writeSync(fd, ' }\n');
@@ -230,12 +230,12 @@ function generateRosMsgInterfaces(
230230
servicesMap,
231231
actionsMap,
232232
fd,
233-
descriptorInterfaceType = false
233+
willGenerateDescriptorInterface = false
234234
) {
235-
const descriptorNamespaceName = descriptorInterfaceType
235+
const descriptorNamespaceName = willGenerateDescriptorInterface
236236
? `${descriptorInterfaceNamespace}/`
237237
: '';
238-
const descriptorNamespacePath = descriptorInterfaceType
238+
const descriptorNamespacePath = willGenerateDescriptorInterface
239239
? `${descriptorInterfaceNamespace}.`
240240
: '';
241241
for (const rosInterface of pkgInfo.subfolders.get(subfolder)) {
@@ -244,10 +244,16 @@ function generateRosMsgInterfaces(
244244
const fullInterfacePath = `${type.pkgName}.${type.subFolder}.${descriptorNamespacePath}${type.interfaceName}`;
245245
const fullInterfaceConstructor = fullInterfacePath + 'Constructor';
246246

247+
const indentStartLevel = willGenerateDescriptorInterface ? 4 : 3;
247248
if (isMsgInterface(rosInterface)) {
248249
// create message interface
249-
saveMsgAsTSD(rosInterface, fd, descriptorInterfaceType);
250-
saveMsgConstructorAsTSD(rosInterface, fd, descriptorInterfaceType);
250+
saveMsgAsTSD(
251+
rosInterface,
252+
fd,
253+
indentStartLevel,
254+
willGenerateDescriptorInterface
255+
);
256+
saveMsgConstructorAsTSD(rosInterface, fd, indentStartLevel);
251257
messagesMap[fullInterfaceName] = fullInterfacePath;
252258
} else if (isSrvInterface(rosInterface)) {
253259
if (!isValidService(rosInterface, pkgInfo.subfolders.get(subfolder))) {
@@ -259,7 +265,7 @@ function generateRosMsgInterfaces(
259265
}
260266

261267
// create service interface
262-
saveSrvAsTSD(rosInterface, fd, descriptorInterfaceType);
268+
saveSrvAsTSD(rosInterface, fd, indentStartLevel);
263269
if (!isInternalActionSrvInterface(rosInterface)) {
264270
servicesMap[fullInterfaceName] = fullInterfaceConstructor;
265271
}
@@ -273,33 +279,39 @@ function generateRosMsgInterfaces(
273279
}
274280

275281
// create action interface
276-
saveActionAsTSD(rosInterface, fd, descriptorInterfaceType);
282+
saveActionAsTSD(rosInterface, fd, indentStartLevel);
277283
actionsMap[fullInterfaceName] = fullInterfaceConstructor;
278284
}
279285
}
280286
}
281287

282-
function saveMsgAsTSD(rosMsgInterface, fd, descriptorInterfaceType = false) {
283-
const indentlevel = descriptorInterfaceType ? 8 : 6;
288+
function saveMsgAsTSD(
289+
rosMsgInterface,
290+
fd,
291+
indentLevel = 3,
292+
willGenerateDescriptorInterface = false
293+
) {
294+
const outerIndentSpacing = getIndentSpacing(indentLevel);
284295
const tmpl = indentString(
285296
`export interface ${rosMsgInterface.type().interfaceName} {\n`,
286-
indentlevel
297+
outerIndentSpacing
287298
);
288299
fs.writeSync(fd, tmpl);
289300
const useSamePkg =
290301
isInternalActionMsgInterface(rosMsgInterface) ||
291302
isInternalServiceEventMsgInterface(rosMsgInterface);
292-
const indentLevel = descriptorInterfaceType ? 10 : 8;
303+
const innerIndentLevel = indentLevel + 1;
304+
const innerIndentSpacing = getIndentSpacing(innerIndentLevel);
293305
saveMsgFieldsAsTSD(
294306
rosMsgInterface,
295307
fd,
296-
indentLevel,
308+
innerIndentSpacing,
297309
';',
298310
'',
299311
useSamePkg,
300-
descriptorInterfaceType
312+
willGenerateDescriptorInterface
301313
);
302-
const tmplEnd = indentString('}\n', indentlevel);
314+
const tmplEnd = indentString('}\n', outerIndentSpacing);
303315
fs.writeSync(fd, tmplEnd);
304316
}
305317

@@ -314,7 +326,7 @@ function saveMsgAsTSD(rosMsgInterface, fd, descriptorInterfaceType = false) {
314326
* @param {string} typePrefix The prefix to put before the type name for
315327
* non-primitive types
316328
* @param {boolean} useSamePackageSubFolder Indicates if the sub folder name should be taken from the message
317-
* @param {boolean} descriptorInterfaceType Indicates if descriptor interface is being generated
329+
* @param {boolean} willGenerateDescriptorInterface Indicates if descriptor interface is being generated
318330
* when the field type comes from the same package. This is needed for action interfaces. Defaults to false.
319331
* @returns {undefined}
320332
*/
@@ -325,7 +337,7 @@ function saveMsgFieldsAsTSD(
325337
lineEnd = ',',
326338
typePrefix = '',
327339
useSamePackageSubFolder = false,
328-
descriptorInterfaceType = false
340+
willGenerateDescriptorInterface = false
329341
) {
330342
let type = rosMsgInterface.type();
331343
let fields = rosMsgInterface.ROSMessageDef.fields;
@@ -335,7 +347,11 @@ function saveMsgFieldsAsTSD(
335347
useSamePackageSubFolder && field.type.pkgName === type.pkgName
336348
? type.subFolder
337349
: 'msg';
338-
let fieldType = fieldType2JSName(field, subFolder, descriptorInterfaceType);
350+
let fieldType = fieldType2JSName(
351+
field,
352+
subFolder,
353+
willGenerateDescriptorInterface
354+
);
339355
let tp = field.type.isPrimitiveType ? '' : typePrefix;
340356
if (typePrefix === 'rclnodejs.') {
341357
fieldType = 'any';
@@ -346,11 +362,11 @@ function saveMsgFieldsAsTSD(
346362
if (field.type.isArray) {
347363
arrayString = '[]';
348364

349-
if (field.type.isFixedSizeArray && descriptorInterfaceType) {
365+
if (field.type.isFixedSizeArray && willGenerateDescriptorInterface) {
350366
arrayString = `[${field.type.arraySize}]`;
351367
}
352368

353-
if (fieldType === 'number' && !descriptorInterfaceType) {
369+
if (fieldType === 'number' && !willGenerateDescriptorInterface) {
354370
// for number[] include alternate typed-array types, e.g., number[] | uint8[]
355371
let jsTypedArrayName = fieldTypeArray2JSTypedArrayName(field.type.type);
356372

@@ -359,7 +375,7 @@ function saveMsgFieldsAsTSD(
359375
}
360376
}
361377
}
362-
const fieldString = descriptorInterfaceType
378+
const fieldString = willGenerateDescriptorInterface
363379
? `${field.name}: '${tp}${fieldType}${arrayString}'`
364380
: `${field.name}: ${tp}${fieldType}${arrayString}`;
365381
const tmpl = indentString(fieldString, indent);
@@ -370,11 +386,7 @@ function saveMsgFieldsAsTSD(
370386
}
371387
}
372388

373-
function saveMsgConstructorAsTSD(
374-
rosMsgInterface,
375-
fd,
376-
descriptorInterfaceType = false
377-
) {
389+
function saveMsgConstructorAsTSD(rosMsgInterface, fd, indentLevel = 3) {
378390
const type = rosMsgInterface.type();
379391
const msgName = type.interfaceName;
380392
let interfaceTmpl = [`export interface ${msgName}Constructor {`];
@@ -386,11 +398,11 @@ function saveMsgConstructorAsTSD(
386398
interfaceTmpl.push(` new(other?: ${msgName}): ${msgName};`);
387399
interfaceTmpl.push('}');
388400
interfaceTmpl.push('');
389-
const indentLevel = descriptorInterfaceType ? 8 : 6;
390-
fs.writeSync(fd, indentLines(interfaceTmpl, indentLevel).join('\n'));
401+
const indentSpacing = getIndentSpacing(indentLevel);
402+
fs.writeSync(fd, indentLines(interfaceTmpl, indentSpacing).join('\n'));
391403
}
392404

393-
function saveSrvAsTSD(rosSrvInterface, fd, descriptorInterfaceType = false) {
405+
function saveSrvAsTSD(rosSrvInterface, fd, indentLevel = 3) {
394406
const serviceName = rosSrvInterface.type().interfaceName;
395407

396408
const interfaceTemplate = [
@@ -400,15 +412,11 @@ function saveSrvAsTSD(rosSrvInterface, fd, descriptorInterfaceType = false) {
400412
'}',
401413
'',
402414
];
403-
const indentLevel = descriptorInterfaceType ? 8 : 6;
404-
fs.writeSync(fd, indentLines(interfaceTemplate, indentLevel).join('\n'));
415+
const indentSpacing = getIndentSpacing(indentLevel);
416+
fs.writeSync(fd, indentLines(interfaceTemplate, indentSpacing).join('\n'));
405417
}
406418

407-
function saveActionAsTSD(
408-
rosActionInterface,
409-
fd,
410-
descriptorInterfaceType = false
411-
) {
419+
function saveActionAsTSD(rosActionInterface, fd, indentLevel = 3) {
412420
const actionName = rosActionInterface.type().interfaceName;
413421

414422
const interfaceTemplate = [
@@ -419,8 +427,19 @@ function saveActionAsTSD(
419427
'}',
420428
'',
421429
];
422-
const indentLevel = descriptorInterfaceType ? 8 : 6;
423-
fs.writeSync(fd, indentLines(interfaceTemplate, indentLevel).join('\n'));
430+
const indentSpacing = getIndentSpacing(indentLevel);
431+
fs.writeSync(fd, indentLines(interfaceTemplate, indentSpacing).join('\n'));
432+
}
433+
434+
/**
435+
* Get number of indent spaces for given level
436+
*
437+
* @param {*} indentLevel Indention level
438+
* @param {*} spacesPerLevel Number of spaces per level
439+
* @returns Total number of space
440+
*/
441+
function getIndentSpacing(indentLevel, spacesPerLevel = 2) {
442+
return indentLevel * spacesPerLevel;
424443
}
425444

426445
function isMsgInterface(rosInterface) {
@@ -526,9 +545,9 @@ function isValidAction(rosActionInterface, infos) {
526545
function fieldType2JSName(
527546
fieldInfo,
528547
subFolder = 'msg',
529-
descriptorInterfaceType = false
548+
willGenerateDescriptorInterface = false
530549
) {
531-
if (descriptorInterfaceType) {
550+
if (willGenerateDescriptorInterface) {
532551
return fieldInfo.type.isPrimitiveType
533552
? `${fieldInfo.type.type}`
534553
: `${fieldInfo.type.pkgName}/${subFolder}/${fieldInfo.type.type}`;

0 commit comments

Comments
 (0)