WLED icon indicating copy to clipboard operation
WLED copied to clipboard

Improve and introduce measures for consistent code style

Open Mattstir opened this issue 1 year ago • 4 comments

I'm highly passionate about clean code and a consistent code style, so I see this as a great opportunity to enhance WLED. 😉

By introducing ESLint, we can automatically enforce code style during each CI run (and hint at problems during a local npm build), ensuring that future changes adhere to the established guidelines. For now, I've aligned the rules with the suggestions in WLED's CONTRIBUTING.md and made them mandatory. This is now only a basic start, these rules can be easily expanded in the future to include more than just style.

I've also corrected the existing style issues in the .js files and js snippets in html files.

Note: This PR is purely stylistic with no logic changes. I hope you like it! 😄

Mattstir avatar Aug 09 '24 11:08 Mattstir

Also note that each style rule got introduced in its own commit so we could easily revert one of them if not wanted ;)

Mattstir avatar Aug 09 '24 11:08 Mattstir

@Mattstir interesting idea 😃

Two questions come to my mind:

  • are these rules only applied to JS and html? Because we already have a problem with flash size, and additional "beautification" in the embedded webpages could lead to increased flash size (bad).
  • do you have a list of the rules? Because I think it's better to first agree on the rule set, and then implement automated checks as a second step.

softhack007 avatar Aug 09 '24 13:08 softhack007

@softhack007 Thanks for your interest :D

  • With this pr applied to .js files and the js snippets that are inside the html files. Of course, bundle size is a very important issue and I was thinking about that when I added this. After each rule I added, I ran npm build, but nothing changed in the built files. So I assume that the minifying tool you guys use html-minifier-terser is already stripping whitespaces and comments (I haven't fully researched how you configure it now, but as I basically only changed whitespaces and these seem to be stripped again, I'm optimistic 🤩).

  • You can find the rules I added in the configuration file I added eslint.config.mjs. First, I extended the recommended rules from https://eslint.org/docs/latest/rules/ (see the rules that have ✅), but I switched some of them to off or just to warn not to flag any current code with errors. Then I added some style rules from https://eslint.style/rules

    "@stylistic/js/function-call-spacing": ["error", "never"],
    "@stylistic/js/key-spacing": "error",
    "@stylistic/js/keyword-spacing": "error",
    "@stylistic/js/no-extra-semi": "error",
    "@stylistic/js/no-whitespace-before-property": "error",
    "@stylistic/js/space-before-blocks": "error",
    "@stylistic/js/space-infix-ops": "error",
    "@stylistic/js/spaced-comment": "error"

I selected these based on the desired style in the CONTRIBUTING.md file.

Mattstir avatar Aug 09 '24 18:08 Mattstir

I'm highly passionate about clean code and a consistent code style, so I see this as a great opportunity to enhance WLED. 😉

By introducing ESLint, we can automatically enforce code style during each CI run (and hint at problems during a local npm build), ensuring that future changes adhere to the established guidelines. For now, I've aligned the rules with the suggestions in WLED's CONTRIBUTING.md and made them mandatory. This is now only a basic start, these rules can be easily expanded in the future to include more than just style.

I've also corrected the existing style issues in the .js files and js snippets in html files.

Note: This PR is purely stylistic with no logic changes. I hope you like it! 😄

WOW code formatting and cleaning is something I also would like to implement in WLED, but also for C and H files. we could use uncrustify.

I'll follow this thread!

ingDIY avatar Oct 17 '24 18:10 ingDIY

Hey! This pull request has been open for quite some time without any new comments now. It will be closed automatically in a week if no further activity occurs. Thank you for contributing to WLED! ❤️

github-actions[bot] avatar Apr 16 '25 12:04 github-actions[bot]

Walkthrough

This change set introduces project-wide JavaScript linting and enforces consistent code style using ESLint. A new ESLint configuration file is added, and the development workflow is updated to include linting steps in both the GitHub Actions workflow and the package.json scripts. Several ESLint-related packages are added as development dependencies. The contribution guidelines are updated to instruct contributors on running lint checks. Across numerous JavaScript and HTML files, code formatting is standardized, primarily through whitespace and spacing adjustments, without altering logic or functionality. A few files explicitly disable ESLint checks via comments.

Changes

Files/Paths Change Summary
.github/workflows/wled-ci.yml Renamed CI job from "Test cdata.js" to "Test & Lint"; added a separate linting step (npm run lint) after testing.
package.json Added "lint" script and modified "build" to run lint before build; introduced "devDependencies" for ESLint and related plugins.
eslint.config.mjs Added new ESLint configuration file with project-specific rules, plugin usage, and file targeting for both JavaScript and HTML files.
CONTRIBUTING.md Updated contribution guidelines to mention JavaScript linting via npm lint or npm run build.
wled00/data/iro.js
wled00/data/rangetouch.js
Added file-level ESLint disable comments at the top of each file.
tools/fps_test.htm
tools/json_test.htm
wled00/data/cpal/cpal.htm
wled00/data/dmxmap.htm
wled00/data/edit.htm
wled00/data/index.js
wled00/data/liveview.htm
wled00/data/liveviewws2D.htm
wled00/data/msg.htm
wled00/data/pixart/boxdraw.js
wled00/data/pixart/getPixelValues.js
wled00/data/pixart/pixart.js
wled00/data/pixart/statics.js
wled00/data/settings.htm
wled00/data/settings_2D.htm
wled00/data/settings_dmx.htm
wled00/data/settings_leds.htm
wled00/data/settings_sec.htm
wled00/data/settings_sync.htm
wled00/data/settings_time.htm
wled00/data/settings_ui.htm
wled00/data/settings_um.htm
wled00/data/settings_wifi.htm
wled00/data/update.htm
Applied consistent code style and formatting changes: added spaces around operators, after commas, and inside parentheses; adjusted function signatures for spacing; reformatted comments for readability. No logic or control flow changes, except for a minor addition of a missing function call in settings_leds.htm to update the UI. In msg.htm, corrected function statement termination.
✨ Finishing Touches
  • [ ] 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar Apr 16 '25 12:04 coderabbitai[bot]

Hey! This pull request has been open for quite some time without any new comments now. It will be closed automatically in a week if no further activity occurs. Thank you for contributing to WLED! ❤️

github-actions[bot] avatar Aug 15 '25 12:08 github-actions[bot]