VSCode code completion for file based rules using RuleBuilder


I try to use Visual Studio code to write file based rules based on openhab-js (JavaScript Scripting - Automation | openHAB).

I installed openhab-js to conf/automation/js.

The rules are working, but I have a usability issue:

When I write a rule with rules.JSRule({..., then I get nice code completion features from VSCode:

But when I try to use the rule builder, not even the when() method is known and there is no guidance from the IDE:
(second screenshot omitted because as a new user I can only provide one.)

Is this a missing feature of openhab-js or can I improve my configuration?


second screenshot: no guidance for RuleBuilder

RuleBuilder is definitely available in openhab-js. You can find the code under rules/rule_builder.js.

The code completion built into VSCode here has nothing to do with openHAB. It’s standard JavaScript support.

Given that JavaScript is not strongly typed, I suspect that VSCode just can’t figure out the type of rules or because of the way when is defined and exposed as a part of rules. I’m not sure what can be done about that. In general I’ve found VSCode’s ability to do code completion with JavaScript to be limited in general.

OpenHab provides TypeScript type definitions (openhab-js/types at main · openhab/openhab-js · GitHub). This enables the IntelliSense feature in VSCode.

I just wondered whether I miss something in my setup or code for the rule builder.

Have you found a way to correctly import the types?
I’m facing the same problem here.

No, I use JSRule instead.

Hello, I am one of the openHAB JavaScript library maintainers.
Your autocompletion works as supposed, we are missing type definitions for RuleBuilder because they currently don’t work properly and due to the complexity of RuleBuilder we weren’t able (yet) to provide type definitions for it.

Also see Require("openhab").rules is not exposing rule-builder methods / Typescript - #2 by florian-h05.
We improved the docs a few days ago to inform the users that we are missing type definitions for Rule Builder.

1 Like