backdrop / backdrop-issues

Issue tracker for Backdrop core.
144 stars 39 forks source link

Add a GHA to check our JavaScript on pull requests (decide on JSLint / JSHint / ESLint) #6402

Open klonos opened 6 months ago

klonos commented 6 months ago

This is a follow-up to #5578, where it was proposed to remove the JSHint files from our codebase, since we aren't using them. The goal here is to add a GHA that checks our JS code, same as we have one checking our PHP code.

Available options:

I saw this in one of the sources, but wasn't able to confirm it:

ESLint is open source and licensed under the MIT License, which allows for more freedom in using and modifying the tool. JSLint, however, has a more restrictive license that places certain limitations on its use and distribution.

Another issue that would be a nice-to-have with our JS linting is to be able to detect deprecated jQuery functions and provide suggestions for these (see #6248). Here's what's available:


Sources

https://www.jslint.com https://jshint.com https://eslint.org

http://badassjs.com/post/3364925033/jshint-an-community-driven-fork-of-jslint https://web.archive.org/web/20130819215629/http://anton.kovalyov.net/2011/02/20/why-i-forked-jslint-to-jshint

D8 change record: Use ESLint to validate all Drupal JavaScript

https://stackoverflow.com/questions/6803305/should-i-use-jslint-or-jshint-javascript-validation https://www.sitepoint.com/comparison-javascript-linting-tools https://www.testim.io/blog/eslint-vs-jshint https://stackshare.io/stackups/eslint-vs-jslint https://medium.com/@sheldonled/from-jshint-to-eslint-8a0a135fa2bf https://www.quora.com/Which-is-better-JSHint-or-ESLint

klonos commented 6 months ago

I've assigned this issue to me for now, because I wanna try the JSHint GHA and configuration that @indigoxela has provided for https://github.com/backdrop-contrib/tinymce in this Zulip thread: https://backdrop.zulipchat.com/#narrow/stream/218635-Backdrop/topic/JavaScript.20linting.20tool (thank you 🙏🏼 )

@indigoxela:

After some experiments, I'm now happy with JSHint.

  • No opinionated code style weirdness (doesn't actually care much about code style)
  • Spots actual problems in code
  • Super easy to integrate in a GitHub Action workflow
  • Disadvantage: needs npm to install, but... :shrug:

Here's a workflow that integrates it: https://github.com/backdrop-contrib/tinymce/blob/1.x-1.x/.github/workflows/code-checks.yml#L35

To get nice inline annotations on Github for found errors, I had to create a custom report style, but that was easy:

https://github.com/backdrop-contrib/tinymce/blob/1.x-1.x/.github/misc/reporter.js

So if anyone else is interested in such a GHA, just go for it. :wink:

@herbdool:

Maybe for core?

@indigoxela:

Maybe for core?

Maybe... but we'd have to settle on a ruleset, which might take some time. https://github.com/backdrop-contrib/tinymce/blob/1.x-1.x/.jshintrc

And then we'd have to exclude all the third-party stuff... and finally fix all violations in our js code. Quite some effort...

Oh, almost forgot, you'll also need the options documentation to fiddle your own jshintrc:

https://jshint.com/docs/options

klonos commented 6 months ago

Here's a rough, PoC PR that runs ESLint on PRs: https://github.com/backdrop/backdrop/pull/4655

For completeness, I will also try to get a PR going that uses JSHint, and we can compare which one we like the most. I don't think that I will bother creating a PR for JSLint though.