Skip to content

Commit

Permalink
eslint[JSON]: Migrate to flat configuration model
Browse files Browse the repository at this point in the history
Openbmc repo CI uses `eslint-plugin-json` package[1] for JSON linting,
and it appears that its broken for eslint@latest versions that mandated
the use of flat config. There is some on-going work on fixing the plugin
to add flat configuration support[2], but its still a work in progress.

This commits also removed `eslint-plugin-json` package and leverages
`eslint-plugin-jsonc` package[3], which is better maintained & also uses
the same AST as Javascript for linting.Since the plugin could provide
AST & source code text back to the eslint engine, we can now use eslint
directives such as `/*eslint-disable <checks>*/` in json files. Also it
does seem like there are plans to merge[4] `eslint-plugin-json` into
`eslint-plugin-jsonc`.

Flat configuration model deprecates the use of `.eslintrc.json` and also
`.eslintignore`. Instead eslint now relies on just one java script based
configuration file `eslint.config.mjs` for everything. So we had to now
add some logic into the configuration file to append the repo specific
ignores to the global ignores, which forced us to bring yet another
node js package `fs`. Since latest versions of eslint flags a warning if
there is a presence of deprecated `.eslintignore` file in repositories,I
renamed it from `.eslintignore` to `.eslintIgnore`.

[1]: https://www.npmjs.com/package/eslint-plugin-json
[2]: azeemba/eslint-plugin-json#82
[3]: https://www.npmjs.com/package/eslint-plugin-jsonc
[4]: azeemba/eslint-plugin-json#85

Change-Id: I0fa8c928a7449e08d761022dde1f1da3ee48cf62
Signed-off-by: Manojkiran Eda <manojkiran.eda@gmail.com>
  • Loading branch information
manojkiraneda committed May 1, 2024
1 parent abb106a commit 363914c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 18 deletions.
11 changes: 0 additions & 11 deletions config/eslint-global-config.json

This file was deleted.

40 changes: 40 additions & 0 deletions config/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import eslintPluginJsonc from 'eslint-plugin-jsonc';
import fs from 'fs';

// Retrieve the default configuration
const defaultConfig = eslintPluginJsonc.configs['flat/recommended-with-jsonc'];

// Ensure defaultConfig.ignores is an array
defaultConfig.ignores = defaultConfig.ignores || [];

// Append additional ignore patterns to the default configuration
const customConfig = [
...defaultConfig,
{
ignores: [
...defaultConfig.ignores,
"**/meson-*/*.json",
"subprojects/**/*.json"
]}
];

try {
// Attempt to read the .eslintIgnore file
const eslintIgnoreContent = fs.readFileSync('.eslintIgnore', 'utf8');
// Split the content by new lines and remove empty lines
const ignorePatterns = eslintIgnoreContent.split('\n').filter(pattern => pattern.trim() !== '');
if (ignorePatterns.length > 0) {
// Ensure customConfig.ignores is an array
const ignoresIndex = customConfig.findIndex(obj => 'ignores' in obj);
if (ignoresIndex !== -1) {
customConfig[ignoresIndex].ignores = customConfig[ignoresIndex].ignores || [];
// Append the local ignores to the existing configuration
customConfig[ignoresIndex].ignores.push(...ignorePatterns);
}
}
} catch (error) {
// Handle the case where the .eslintIgnore file is not present
console.info('\x1b[32m','Repo specific ignores are not present');
}

export default customConfig;
3 changes: 2 additions & 1 deletion scripts/build-unit-test-docker
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,8 @@ RUN pip3 install --break-system-packages \
requests
RUN npm install -g \
eslint@v8.56.0 eslint-plugin-json@latest \
eslint@latest eslint-plugin-jsonc@latest \
fs@latest \
markdownlint-cli@latest \
prettier@latest
"""
Expand Down
13 changes: 7 additions & 6 deletions scripts/format-code.sh
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,15 @@ function do_black() {
black -l 79 "$@"
}

LINTER_REQUIRE+=([eslint]="eslint;.eslintrc.json;${CONFIG_PATH}/eslint-global-config.json")
LINTER_IGNORE+=([eslint]=".eslintignore")
LINTER_REQUIRE+=([eslint]="eslint;eslint.config.mjs;${CONFIG_PATH}/eslint.config.mjs")
LINTER_IGNORE+=([eslint]=".eslintIgnore")
LINTER_TYPES+=([eslint]="json")
function do_eslint() {
eslint --no-eslintrc -c "${LINTER_CONFIG[eslint]}" \
--ext .json --format=stylish \
--resolve-plugins-relative-to /usr/local/lib/node_modules \
--no-error-on-unmatched-pattern "$@"
eslint --no-config-lookup --config "${LINTER_CONFIG[eslint]}" \
--format=stylish \
--color \
--report-unused-disable-directives \
--no-error-on-unmatched-pattern "$@"
}

LINTER_REQUIRE+=([flake8]="flake8")
Expand Down

0 comments on commit 363914c

Please sign in to comment.