From 13f2e92bb0ac64f94d9b5016a33b5c34d2134204 Mon Sep 17 00:00:00 2001 From: Nikita Titov Date: Sun, 3 Nov 2024 19:32:25 +0300 Subject: [PATCH] [ci] check JavaScript code with `biome` tool (#6711) * lint js code * hotfix * Update .editorconfig Co-authored-by: James Lamb --------- Co-authored-by: James Lamb --- .ci/lint-js.sh | 5 ++ .ci/{lint-python.sh => lint-python-bash.sh} | 0 .ci/test.sh | 7 +- .editorconfig | 11 ++- biome.json | 21 ++++++ docs/_static/js/script.js | 69 +++++++++++-------- .../binary_classification/forced_splits.json | 2 +- examples/regression/forced_bins.json | 4 +- examples/regression/forced_bins2.json | 2 +- python-package/README.rst | 2 +- 10 files changed, 81 insertions(+), 42 deletions(-) create mode 100644 .ci/lint-js.sh rename .ci/{lint-python.sh => lint-python-bash.sh} (100%) create mode 100644 biome.json diff --git a/.ci/lint-js.sh b/.ci/lint-js.sh new file mode 100644 index 000000000000..534f251620e2 --- /dev/null +++ b/.ci/lint-js.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +set -e -E -u -o pipefail + +biome ci --config-path=./biome.json --diagnostic-level=info --error-on-warnings ./ diff --git a/.ci/lint-python.sh b/.ci/lint-python-bash.sh similarity index 100% rename from .ci/lint-python.sh rename to .ci/lint-python-bash.sh diff --git a/.ci/test.sh b/.ci/test.sh index f959af16ccf3..45ee65629744 100755 --- a/.ci/test.sh +++ b/.ci/test.sh @@ -103,6 +103,7 @@ if [[ $TASK == "lint" ]]; then pwsh -file "./.ci/lint-powershell.ps1" || : conda create -q -y -n "${CONDA_ENV}" \ "${CONDA_PYTHON_REQUIREMENT}" \ + 'biome>=1.9.3' \ 'cmakelint>=1.4.3' \ 'cpplint>=1.6.0' \ 'matplotlib-base>=3.9.1' \ @@ -113,12 +114,14 @@ if [[ $TASK == "lint" ]]; then 'r-lintr>=3.1.2' # shellcheck disable=SC1091 source activate "${CONDA_ENV}" - echo "Linting Python code" - bash ./.ci/lint-python.sh || exit 1 + echo "Linting Python and bash code" + bash ./.ci/lint-python-bash.sh || exit 1 echo "Linting R code" Rscript ./.ci/lint-r-code.R "${BUILD_DIRECTORY}" || exit 1 echo "Linting C++ code" bash ./.ci/lint-cpp.sh || exit 1 + echo "Linting JavaScript code" + bash ./.ci/lint-js.sh || exit 1 exit 0 fi diff --git a/.editorconfig b/.editorconfig index f4ae446b64bb..e7191b63c1d3 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,22 +1,19 @@ root = true [*] -charset=utf-8 +charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true +end_of_line = lf indent_style = space indent_size = 2 -[*.{py,sh,js,ps1}] +[*.{py,sh,ps1,js,json}] indent_size = 4 -line_length = 120 +max_line_length = 120 skip = external_libs known_first_party = lightgbm -# Placeholder files -[{*.gitkeep,__init__.py}] -insert_final_newline = none - # Tabs matter for Makefile and .gitmodules [{makefile*,Makefile*,*.mk,*.mak,*.makefile,*.Makefile,GNUmakefile,BSDmakefile,make.bat,Makevars*,*.gitmodules}] indent_style = tab diff --git a/biome.json b/biome.json new file mode 100644 index 000000000000..5029d037189e --- /dev/null +++ b/biome.json @@ -0,0 +1,21 @@ +{ + "files": { + "ignore": [".mypy_cache/"] + }, + "formatter": { + "enabled": true, + "useEditorconfig": true + }, + "organizeImports": { + "enabled": true + }, + "linter": { + "enabled": true, + "rules": { + "all": true + } + }, + "javascript": { + "globals": ["$"] + } +} diff --git a/docs/_static/js/script.js b/docs/_static/js/script.js index 3cfc90de887d..3f129501e06f 100644 --- a/docs/_static/js/script.js +++ b/docs/_static/js/script.js @@ -1,56 +1,69 @@ -$(function() { +$(() => { /* Use wider container for the page content */ - $('.wy-nav-content').each(function() { this.style.setProperty('max-width', 'none', 'important'); }); + $(".wy-nav-content").each(function () { + this.style.setProperty("max-width", "none", "important"); + }); /* List each class property item on a new line https://github.com/microsoft/LightGBM/issues/5073 */ - if(window.location.pathname.toLocaleLowerCase().indexOf('pythonapi') !== -1) { - $('.py.property').each(function() { this.style.setProperty('display', 'inline', 'important'); }); + if (window.location.pathname.toLocaleLowerCase().indexOf("pythonapi") !== -1) { + $(".py.property").each(function () { + this.style.setProperty("display", "inline", "important"); + }); } /* Collapse specified sections in the installation guide */ - if(window.location.pathname.toLocaleLowerCase().indexOf('installation-guide') !== -1) { - $('').appendTo('body'); - var collapsable = [ - '#build-threadless-version-not-recommended', - '#build-mpi-version', - '#build-gpu-version', - '#build-cuda-version', - '#build-java-wrapper', - '#build-c-unit-tests' + if (window.location.pathname.toLocaleLowerCase().indexOf("installation-guide") !== -1) { + $( + '', + ).appendTo("body"); + const collapsable = [ + "#build-threadless-version-not-recommended", + "#build-mpi-version", + "#build-gpu-version", + "#build-cuda-version", + "#build-java-wrapper", + "#build-c-unit-tests", ]; - $.each(collapsable, function(_, val) { - var header = val + ' > :header:first'; - var content = val + ' :not(:header:first)'; - $(header).addClass('closed'); + $.each(collapsable, (_, val) => { + const header = `${val} > :header:first`; + const content = `${val} :not(:header:first)`; + $(header).addClass("closed"); $(content).hide(); - $(header).click(function() { - $(header).toggleClass('closed opened'); + $(header).click(() => { + $(header).toggleClass("closed opened"); $(content).slideToggle(0); }); }); /* Uncollapse parent sections when nested section is specified in the URL or before navigate to it from navbar */ function uncollapse(section) { - section.parents().each((_, val) => { $(val).children('.closed').click(); }); + section.parents().each((_, val) => { + $(val).children(".closed").click(); + }); } uncollapse($(window.location.hash)); - $('.wy-menu.wy-menu-vertical li a.reference.internal').click(function() { - uncollapse($($(this).attr('href'))); + $(".wy-menu.wy-menu-vertical li a.reference.internal").click(function () { + uncollapse($($(this).attr("href"))); }); /* Modify src and href attrs of artifacts badge */ function modifyBadge(src, href) { - $('img[alt="download artifacts"]').each(function() { + $('img[alt="download artifacts"]').each(function () { this.src = src; this.parentNode.href = href; }); } /* Initialize artifacts badge */ - modifyBadge('./_static/images/artifacts-fetching.svg', '#'); + modifyBadge("./_static/images/artifacts-fetching.svg", "#"); /* Fetch latest buildId and construct artifacts badge */ - $.getJSON('https://dev.azure.com/lightgbm-ci/lightgbm-ci/_apis/build/builds?branchName=refs/heads/master&resultFilter=succeeded&queryOrder=finishTimeDescending&%24top=1&api-version=7.1-preview.7', function(data) { - modifyBadge('./_static/images/artifacts-download.svg', - 'https://dev.azure.com/lightgbm-ci/lightgbm-ci/_apis/build/builds/' + data['value'][0]['id'] + '/artifacts?artifactName=PackageAssets&api-version=7.1-preview.5&%24format=zip'); - }); + $.getJSON( + "https://dev.azure.com/lightgbm-ci/lightgbm-ci/_apis/build/builds?branchName=refs/heads/master&resultFilter=succeeded&queryOrder=finishTimeDescending&%24top=1&api-version=7.1-preview.7", + (data) => { + modifyBadge( + "./_static/images/artifacts-download.svg", + `https://dev.azure.com/lightgbm-ci/lightgbm-ci/_apis/build/builds/${data.value[0].id}/artifacts?artifactName=PackageAssets&api-version=7.1-preview.5&%24format=zip`, + ); + }, + ); } }); diff --git a/examples/binary_classification/forced_splits.json b/examples/binary_classification/forced_splits.json index 1ee410c9789e..b09391a87f49 100644 --- a/examples/binary_classification/forced_splits.json +++ b/examples/binary_classification/forced_splits.json @@ -1,6 +1,6 @@ { "feature": 25, - "threshold": 1.30, + "threshold": 1.3, "left": { "feature": 26, "threshold": 0.85 diff --git a/examples/regression/forced_bins.json b/examples/regression/forced_bins.json index 1ee0a49d727c..19722afbbb4b 100644 --- a/examples/regression/forced_bins.json +++ b/examples/regression/forced_bins.json @@ -1,10 +1,10 @@ [ { "feature": 0, - "bin_upper_bound": [ 0.3, 0.35, 0.4 ] + "bin_upper_bound": [0.3, 0.35, 0.4] }, { "feature": 1, - "bin_upper_bound": [ -0.1, -0.15, -0.2 ] + "bin_upper_bound": [-0.1, -0.15, -0.2] } ] diff --git a/examples/regression/forced_bins2.json b/examples/regression/forced_bins2.json index f4dca0ccaf34..d6454f8a4ae9 100644 --- a/examples/regression/forced_bins2.json +++ b/examples/regression/forced_bins2.json @@ -1,6 +1,6 @@ [ { "feature": 0, - "bin_upper_bound": [ 0.19, 0.39, 0.59, 0.79 ] + "bin_upper_bound": [0.19, 0.39, 0.59, 0.79] } ] diff --git a/python-package/README.rst b/python-package/README.rst index 0e007e5ee7ec..face6bba6b74 100644 --- a/python-package/README.rst +++ b/python-package/README.rst @@ -286,7 +286,7 @@ To check that a contribution to the package matches its style expectations, run .. code:: sh - bash .ci/lint-python.sh + bash .ci/lint-python-bash.sh .. |License| image:: https://img.shields.io/github/license/microsoft/lightgbm.svg :target: https://github.com/microsoft/LightGBM/blob/master/LICENSE