mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-08-23 05:20:03 +00:00
Switch to ESLint v9 and flat config (#3558)
Since PR #3551 was not yet complete, I made my own attempt. 1. Update to ESLint v9. 2. Replace deprecated `.eslintrc.json` and `.eslintignore` by flat config `eslint.config.mjs`. 3. Adapt `check_config.js` to use flat config. 4. Since `eslint-plugin-import` still doesn't support ESLint v9 I removed it. We can add it back when it does support v9. 5. Run tests `npm run check:js` and `npm run config:check`. 6. In order not to overload this PR, I have not yet activated more additional rules - there are some useful ones in the new plugin `@eslint/js`. @bugsounet, please don't take it as an offence that I have created a competing PR. The migration to ESLint v9 has been burning under my nails for some time.
This commit is contained in:
committed by
GitHub
parent
5ffdf9af09
commit
d3187689f0
@@ -2,13 +2,14 @@ const path = require("node:path");
|
||||
const fs = require("node:fs");
|
||||
const Ajv = require("ajv");
|
||||
const colors = require("ansis");
|
||||
const globals = require("globals");
|
||||
const { Linter } = require("eslint");
|
||||
|
||||
const rootPath = path.resolve(`${__dirname}/../`);
|
||||
const Log = require(`${rootPath}/js/logger.js`);
|
||||
const Utils = require(`${rootPath}/js/utils.js`);
|
||||
|
||||
const linter = new Linter();
|
||||
const linter = new Linter({ configType: "flat" });
|
||||
const ajv = new Ajv();
|
||||
|
||||
/**
|
||||
@@ -29,16 +30,14 @@ function checkConfigFile () {
|
||||
|
||||
// Check if file is present
|
||||
if (fs.existsSync(configFileName) === false) {
|
||||
Log.error(`File not found: ${configFileName}`);
|
||||
throw new Error("No config file present!");
|
||||
throw new Error(`File not found: ${configFileName}\nNo config file present!`);
|
||||
}
|
||||
|
||||
// Check permission
|
||||
try {
|
||||
fs.accessSync(configFileName, fs.F_OK);
|
||||
} catch (error) {
|
||||
Log.error(error);
|
||||
throw new Error("No permission to access config file!");
|
||||
throw new Error(`${error}\nNo permission to access config file!`);
|
||||
}
|
||||
|
||||
// Validate syntax of the configuration file.
|
||||
@@ -47,30 +46,39 @@ function checkConfigFile () {
|
||||
// I'm not sure if all ever is utf-8
|
||||
const configFile = fs.readFileSync(configFileName, "utf-8");
|
||||
|
||||
// Explicitly tell linter that he might encounter es2024 syntax ("let config = {...}")
|
||||
const errors = linter.verify(configFile, {
|
||||
env: {
|
||||
es2024: true
|
||||
}
|
||||
});
|
||||
const errors = linter.verify(
|
||||
configFile,
|
||||
{
|
||||
languageOptions: {
|
||||
ecmaVersion: "latest",
|
||||
globals: {
|
||||
...globals.node
|
||||
}
|
||||
}
|
||||
},
|
||||
configFileName
|
||||
);
|
||||
|
||||
if (errors.length === 0) {
|
||||
Log.info(colors.green("Your configuration file doesn't contain syntax errors :)"));
|
||||
validateModulePositions(configFileName);
|
||||
} else {
|
||||
Log.error("Your configuration file contains syntax errors :(");
|
||||
let errorMessage = "Your configuration file contains syntax errors :(";
|
||||
|
||||
for (const error of errors) {
|
||||
Log.error(`Line ${error.line} column ${error.column}: ${error.message}`);
|
||||
errorMessage += `\nLine ${error.line} column ${error.column}: ${error.message}`;
|
||||
}
|
||||
process.exit(1);
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
function validateModulePositions (configFileName) {
|
||||
Log.info("Checking modules structure configuration ...");
|
||||
|
||||
const positionList = Utils.getModulePositions();
|
||||
|
||||
// Make Ajv schema configuration of modules config
|
||||
// only scan "module" and "position"
|
||||
// Only scan "module" and "position"
|
||||
const schema = {
|
||||
type: "object",
|
||||
properties: {
|
||||
@@ -103,16 +111,21 @@ function checkConfigFile () {
|
||||
} else {
|
||||
const module = validate.errors[0].instancePath.split("/")[2];
|
||||
const position = validate.errors[0].instancePath.split("/")[3];
|
||||
|
||||
Log.error("This module configuration contains errors:");
|
||||
Log.error(`\n${JSON.stringify(data.modules[module], null, 2)}`);
|
||||
let errorMessage = "This module configuration contains errors:";
|
||||
errorMessage += `\n${JSON.stringify(data.modules[module], null, 2)}`;
|
||||
if (position) {
|
||||
Log.error(`${position}: ${validate.errors[0].message}`);
|
||||
Log.error(`\n${JSON.stringify(validate.errors[0].params.allowedValues, null, 2).slice(1, -1)}`);
|
||||
errorMessage += `\n${position}: ${validate.errors[0].message}`;
|
||||
errorMessage += `\n${JSON.stringify(validate.errors[0].params.allowedValues, null, 2).slice(1, -1)}`;
|
||||
} else {
|
||||
Log.error(validate.errors[0].message);
|
||||
errorMessage += validate.errors[0].message;
|
||||
}
|
||||
Log.error(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
checkConfigFile();
|
||||
try {
|
||||
checkConfigFile();
|
||||
} catch (error) {
|
||||
Log.error(error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
|
Reference in New Issue
Block a user