Skip to content

Commit 1144cc5

Browse files
committed
[rosidl_gen] Refactor the dot used to generate the JavaScript messages (#1014)
`rclnodejs` leverages [`dot`](https://www.npmjs.com/package/dot) to generate the JavaScript classes, representing the ROS messages and wrapping the [`ref`](https://www.npmjs.com/package/@rclnodejs/ref-napi) objects that can read/write from C++ pointer. For a long time, we changed the implementation along with new features introduced into ROS2, so it makes the template itself messy and hard to understand. This patch tries to refactor the template to make it more readable and maintainable, including: 1. Add proper indents for `for`/`if` used by the template to make it easy to understand. 2. Abstract some functionalities into separate functions, like `_setDefaults()`. 3. Add more meaningful comments by ```{{/* */}}```. 4. Remove the consistency checking. Besides, the following changes are made accordingly: 1. Remove the `willCheckConsistency` usage from current JS files. 2. Remove tests related to consistency checking in `test-compound-msg-type-check.js` and `test-security-related.js`. 3. Use `prettier` to format the generated JavaScript files. 4. Pump the generator version to `0.4.0`. Fix: #1015
1 parent 38ff888 commit 1144cc5

File tree

9 files changed

+211
-343
lines changed

9 files changed

+211
-343
lines changed

lib/client.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ class Client extends Entity {
5858
request instanceof this._typeClass.Request
5959
? request
6060
: new this._typeClass.Request(request);
61-
requestToSend._willCheckConsistency = this._options.willCheckConsistency;
6261

6362
let rawRequest = requestToSend.serialize();
6463
let sequenceNumber = rclnodejs.sendRequest(this._handle, rawRequest);

lib/node.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -502,10 +502,6 @@ class Node extends rclnodejs.ShadowNode {
502502
options = Object.assign(options, { isRaw: false });
503503
}
504504

505-
if (options.willCheckConsistency === undefined) {
506-
options = Object.assign(options, { willCheckConsistency: false });
507-
}
508-
509505
return options;
510506
}
511507

@@ -592,7 +588,6 @@ class Node extends rclnodejs.ShadowNode {
592588
* @param {object} options - The options argument used to parameterize the publisher.
593589
* @param {boolean} options.enableTypedArray - The topic will use TypedArray if necessary, default: true.
594590
* @param {QoS} options.qos - ROS Middleware "quality of service" settings for the publisher, default: QoS.profileDefault.
595-
* @param {boolean} options.willCheckConsistency - Pulisher will check the consistancy of the message to be sent, default: false.
596591
* @return {Publisher} - An instance of Publisher.
597592
*/
598593
createPublisher(typeClass, topic, options) {
@@ -695,7 +690,6 @@ class Node extends rclnodejs.ShadowNode {
695690
* @param {object} options - The options argument used to parameterize the client.
696691
* @param {boolean} options.enableTypedArray - The response will use TypedArray if necessary, default: true.
697692
* @param {QoS} options.qos - ROS Middleware "quality of service" settings for the client, default: QoS.profileDefault.
698-
* @param {boolean} options.willCheckConsistency - Client will check the consistancy of the message to be sent, default: false.
699693
* @return {Client} - An instance of Client.
700694
*/
701695
createClient(typeClass, serviceName, options) {
@@ -1696,7 +1690,6 @@ Node.getDefaultOptions = function () {
16961690
isRaw: false,
16971691
qos: QoS.profileDefault,
16981692
contentFilter: undefined,
1699-
willCheckConsistency: false,
17001693
};
17011694
};
17021695

lib/publisher.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ class Publisher extends Entity {
5353
message instanceof this._typeClass
5454
? message
5555
: new this._typeClass(message);
56-
messageToSend._willCheckConsistency = this._options.willCheckConsistency;
5756
let rawMessage = messageToSend.serialize();
5857
rclnodejs.publish(this._handle, rawMessage);
5958
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
"jsdoc": "^4.0.4",
6060
"lint-staged": "^15.2.10",
6161
"mocha": "^11.0.2",
62-
"prettier": "^3.4.2",
6362
"sinon": "^19.0.2",
6463
"tree-kill": "^1.2.2",
6564
"typescript": "^5.7.2"
@@ -83,7 +82,8 @@
8382
"nan": "^2.22.0",
8483
"rimraf": "^6.0.1",
8584
"uuid": "^11.0.3",
86-
"walk": "^2.3.15"
85+
"walk": "^2.3.15",
86+
"prettier": "^3.4.2"
8787
},
8888
"husky": {
8989
"hooks": {

rosidl_gen/generator.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rosidl-generator",
3-
"version": "0.3.10",
3+
"version": "0.4.0",
44
"description": "Generate JavaScript object from ROS IDL(.msg) files",
55
"main": "index.js",
66
"authors": [

rosidl_gen/idl_generator.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
'use strict';
1616

1717
const dot = require('dot');
18+
const prettier = require('prettier');
1819
const fse = require('fs-extra');
1920
const path = require('path');
2021
const parser = require('../rosidl_parser/rosidl_parser.js');
@@ -40,6 +41,9 @@ function removeEmptyLines(str) {
4041
* @param {string} code
4142
*/
4243
async function writeGeneratedCode(dir, fileName, code) {
44+
if (fileName.endsWith('.js')) {
45+
code = await prettier.format(code, { parser: 'babel' });
46+
}
4347
await fse.mkdirs(dir);
4448
await fse.writeFile(path.join(dir, fileName), code);
4549
}

0 commit comments

Comments
 (0)