telefonicaid / iotagent-node-lib

Module to enable IoT Agent developers to build custom agents for their devices that can easily connect to NGSI Context Brokers
https://iotagent-node-lib.rtfd.io/
GNU Affero General Public License v3.0
60 stars 87 forks source link

Should every IoT Agent align to use eslint #830

Closed jason-fox closed 3 years ago

jason-fox commented 4 years ago

LoRaWAN and OPC-UA already use ES6 and are linted using ESLint. The telefonica IoT Agents code base is older and continues to use jshint

I see several advantages of swapping out the older linter and replacing it with the ES6 compliant one:

  1. Consistency - users would find it easier to navigate between the various projects.
  2. An Es6 codebase is more modern and likely to attract more contributions.
  3. EsLint can be configured to check code only and delegate formatting rules to work with prettier to avoid the need for extra jshint workarounds in the code base.
  4. Use of constructs such as const and let is supposedly more performant - well they would say that wouldn't they

The existing minimum node engine is Node 8. Node 8 is reaching EoF and is already compliant with const and let anyway . I can't see why there would there be any problem is upgrading the IoT Agents to use the newer syntax? It's just a start - ideally someone can delete the existing async library callbacks and replace them with proper native ES2015+ Promises and use async await - it would make the code a lot easier to follow. I'm not sure if the iot-node-lib eventually needs to offer promise endpoints itself or just use utils.promisify() but that is an issue for another day. This would just be a start.

As a broad mechanical automated change, it would make sense to try push this through whilst there is a lull to avoid merge conflicts with other ongoing PRs.

jason-fox commented 4 years ago

I'd like to swap out JSHint for EsLint on the long runner: https://github.com/telefonicaid/iotagent-node-lib/pull/753 as well [sigh] since it should make the maintenance of the ongoing merge conflicts easier. If this proposal is accepted I may combine prettier and eslint into a single PR (and remove all jshint work arounds.

jason-fox commented 4 years ago

Related PRs:

Still Open:

fgalan commented 4 years ago

Thank you very much for creating the issue and the explanations above. We are still discussing the issue internally and I hope we can provide feedback on it soon. Sorry for the delay.

On the meanwhile, looking to the proposed PRs, I have a couple of doubts, pls:

jason-fox commented 4 years ago

To quote Wikipedia:

eslint is a static code analysis tool for identifying problematic patterns found in JavaScript code. Rules in ESLint are configurable, and customized rules can be defined and loaded

Tâmia provides a set of such rules

This is very similar to the commonly used airbnb code rules, with one important addition/omission/change. - Tâmia does not provide any code style rules, hence it is very easy to complement this base set of linter rules with another for code styling.

As you know, I've been pushing Prettier - and they also supply a eslint config eslint-plugin-prettier - using this enforces the prettier formatting, and removes the need for any overrides where jshint formatting rules were conflicting with prettier.

The .eslintignore and .eslintrc files are described in the config documentation

fgalan commented 4 years ago

Please find some feedback:

jason-fox commented 4 years ago

I have created my own branch of #800 - expression-lib-alternative and merged my eslint branch into it:

  1. There is no conflict when running the initial merge - ff62d37
  2. Running npm run lint prettifies the code as shown: b59d0c2
  3. Fixing the remaining errors is merely replacing jshint overrides with eslint ones - c3899f75

Total time to fix - 8 minutes - I don't think this is much of a blocker.

Replacing jshint with eslint is a simple mechanical process which could be easily added into IOTA Manager - the only problem is waiting and merging in other PRs whilst still at the back of the queue takes time. If I could see movement elsewhere I could easily apply the same process to another repo.

I fully agree removing callbacks and using promises is an aspiration and not intended to be part of this PR and should be separated from this discussion.

jason-fox commented 4 years ago

This is the state of the lull across the IoT Agents

Name Open PRs Comments
iotagent-json 3 One from April, A documentation one and a new change
iotagent-ul 3 Two PRs are 11 months old - are they active? One from this month.
sigfox-iotagent 1 A documentation change from March - is it active?
lightweightm2m-iotagent 1 One 18 months old - is it active?
Name Open PRs Comments
iotagent-node-lib 10 Includes several documentation changes, a Docker change and the obsolete prettier PR
iotagent-manager 1 One outstanding PR

Which of these PRs are still active?

fgalan commented 4 years ago

Tab indentation.

As far as I remember we use plain spaces to indent in our code, not tabs. Can configuration be adjusted to align with our current indentation style?

In fact, maybe already works that way... I see in the proposed PRs configuration like:

indent_style = tab
...
indent_size = 4 

What does exactly mean?

(I have tried to find indent_style and indent_size in the link you provide https://eslint.org/docs/user-guide/configuring but I didn't find them...)

jason-fox commented 4 years ago

My mistake - that should be the editorconfig - an attempt to maintain consistent coding styles across various IDEs.

The JavaScript editorconfig should indeed match the 4 space guideline (and align with prettierrc.json)

[*.{js}]
indent_style = space
indent_size = 4
jason-fox commented 4 years ago

Given that #800 has now been merged, I thought it would be an opportune moment to ask whether these PRs could be processed. I have updated the PRs again to ensure no conflicts.

This is the state of the lull across the IoT Agents

Name Done Open PRs Comments
iotagent-json 3 A documentation one and "ignore measure" - updated and currently ready for review.
sigfox-iotagent 1 A non-conflicting documentation change from March - is it active?
lightweightm2m-iotagent 1 One 2 years-old - is it active?
iotagent-manager 1 One outstanding PR - 8 months old

For these repos I see no conflicting commits in the last 6 months and no progress on the outstanding PRs - are they active? How is the queue being dealt with?

Name Done Open PRs Comments
iotagent-node-lib :x: 10 Includes several documentation changes, a Docker change and the obsolete prettier PR
iotagent-ul 8 Two PRs are 11 months old - are they active? One from this month.

These two are more problematic, and I can see you will want to process #849 before accepting #831. Question should #753 be withdrawn? The eslint PR supersedes it.

On the UL IoT Agent all the outstanding PRs https://github.com/telefonicaid/iotagent-ul/pull/411 and https://github.com/telefonicaid/iotagent-ul/pull/412 appear to be waiting for some equivalent works - is this scheduled? The other two are approved or equivalents to approved work elsewhere - https://github.com/telefonicaid/iotagent-ul/pull/429 https://github.com/telefonicaid/iotagent-ul/pull/430

Basically just asking if it is worth continuing maintaining these PRs.

fgalan commented 4 years ago

I'm still thinking the work is valuable. Let's start the merge by the "simplest" repositories which I think are Sigfox and L2M2M.

The PR in LWM2M has been just merged. The PR in Sigfox has been commented with a couple of things I have spotted in a last review on it.

fgalan commented 4 years ago

Current status: eslint PRs on IOTA-Sigfox, IOTA-LWM2M and IOTAManager (and STH, as bonus track ;) has been already merged. Next ones are IOTA-UL and IOTA-JSON.

There is an outstanding ongoing work on leading slashes mqtt topics authored by @jmezzera. The piece of work corresponding to IOTA-JSON is pending, but given that the "parallel" work in IOTA-UL is finished (PR https://github.com/telefonicaid/iotagent-ul/pull/429) I understand it would be provided soon. @jmezzera could you provide some update on this, please? Thanks!

jmezzera commented 4 years ago

I was just working on this aspect this morning, planning to have it done by the end of the day.

jmezzera commented 4 years ago

Done https://github.com/telefonicaid/iotagent-json/pull/481/

fgalan commented 4 years ago

@jmezzera work on MQTT improvements has been just merged in IOTA-UL and IOTA-JSON (thanks!). Thus, I think once the eslint PRs on these repos gets updates with master and conflict solved, they would be ready for final review and merge.

jason-fox commented 4 years ago

Conflicts have been resolved. PRs have been updated.

fgalan commented 4 years ago

As far as I understand, all the PRs related with eslint have been already merged in every repository so we are almost done :) However, there is still one pending, in this repository itself, for the iotagent-node-lib: PR https://github.com/telefonicaid/iotagent-node-lib/pull/831.

The summary of "parallel" PRs that may conflict are the following ones:

PR number Author Does the author agree on merging PR #831 before his PR?
#601 @m4n3dw0lf ? (author asked)
#674 @gabrieledeluca Closed (overpassed by PR #920)
#688 @fgalan Yes
#753 @jason-fox Withdrawn
#780 @Jagatjot Closed (ported to #922)
#920 @jason-fox Merged
#922 @fgalan Yes
#793 @Cerfoglg Yes
#849 (and dependant #854) (*) Yes

Notes:

jason-fox commented 4 years ago

831 is a blind mechanical change - it will ES6-ify the existing master code which adds unnecessary changes over the currently ngsi-ld refactored (and already ES6-ified) files. This means a subsequent master => ngsi-ld merge to resolve conflicts would include a lot more unnecessary stuff - it will be very tricky to remove conflicts since each file will need analysis especially as I haven't looked at the code in either of these PRS in over six months.

Much easier to merge the ngsi-ld => master stuff first and then merge eslint => master after. Eslint is already up to date with master so it will be a matter of accept theirs, much easier not to miss something.

In contrast none of the other PRs are large enough to cause too many problems ( 9, 3, 1 and 10 files respectively)

fgalan commented 4 years ago

Thanks for the feedback! I have edited the table above accordingly.

jason-fox commented 4 years ago

I have fixed the merge conflicts for the NGSI-LD PR:

This makes the merge of ngsi-ld + eslint a simple matter of accept mine as an experiment have created a new branch for this see here: https://github.com/jason-fox/iotagent-node-lib/tree/feature/eslint_and_ngsi-ld

I would still prefer that the NGSI-LD is actioned first as I don't want to be maintaining three branches (eslint, ngsi-ld , eslint+ngsi-ld) whilst waiting for merge to master

fgalan commented 3 years ago

PR #831 finally merged.

After merging, let's see tonight if the newly generated IOTAs containers with the new version of the library work in the CI e2s tests. If no problem is found (we don't expect any), the issue will be closed.

fgalan commented 3 years ago

After merging, let's see tonight if the newly generated IOTAs containers with the new version of the library work in the CI e2s tests. If no problem is found (we don't expect any), the issue will be closed.

CI e2e went fine last night. So I think nothing is pending and this issue can be closed.