Skip to content

Commit 0b70274

Browse files
Replace prettier by stylistic to lint JavaScript (#3303)
In the latest versions of ESLint, more and more formatting rules were removed or declared deprecated. These rules have been integrated into the new Stylistic package (https://eslint.style/guide/why) and expanded. Stylistic acts as a better formatter for JavaScript as Prettier. With this PR there are many changes that make the code more uniform, but it may be difficult to review due to the large amount. Even if I have no worries about the changes, perhaps this would be something for the release after next. Let me know what you think.
1 parent 4e7b68a commit 0b70274

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1040
-1028
lines changed

.eslintrc.json

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"extends": ["eslint:recommended", "plugin:import/recommended", "plugin:jest/recommended", "plugin:jsdoc/recommended", "plugin:prettier/recommended"],
2+
"extends": ["eslint:recommended", "plugin:@stylistic/all-extends", "plugin:import/recommended", "plugin:jest/recommended", "plugin:jsdoc/recommended"],
33
"plugins": [],
44
"env": {
55
"browser": true,
6-
"es2022": true,
6+
"es2023": true,
77
"jest/globals": true,
88
"node": true
99
},
@@ -16,7 +16,7 @@
1616
},
1717
"parserOptions": {
1818
"sourceType": "module",
19-
"ecmaVersion": 2022,
19+
"ecmaVersion": 2023,
2020
"ecmaFeatures": {
2121
"globalReturn": true
2222
}
@@ -38,6 +38,46 @@
3838
"no-throw-literal": "error",
3939
"no-unused-vars": "off",
4040
"no-useless-return": "error",
41-
"prefer-template": "error"
42-
}
41+
"object-shorthand": ["error", "methods"],
42+
"prefer-template": "error",
43+
"@stylistic/array-element-newline": ["error", "consistent"],
44+
"@stylistic/arrow-parens": ["error", "always"],
45+
"@stylistic/brace-style": "off",
46+
"@stylistic/comma-dangle": ["error", "never"],
47+
"@stylistic/dot-location": ["error", "property"],
48+
"@stylistic/function-call-argument-newline": ["error", "consistent"],
49+
"@stylistic/function-paren-newline": ["error", "consistent"],
50+
"@stylistic/implicit-arrow-linebreak": ["error", "beside"],
51+
"@stylistic/max-statements-per-line": ["error", { "max": 2 }],
52+
"@stylistic/multiline-ternary": ["error", "always-multiline"],
53+
"@stylistic/newline-per-chained-call": ["error", { "ignoreChainWithDepth": 4 }],
54+
"@stylistic/no-extra-parens": "off",
55+
"@stylistic/no-tabs": "off",
56+
"@stylistic/object-curly-spacing": ["error", "always"],
57+
"@stylistic/object-property-newline": ["error", { "allowAllPropertiesOnSameLine": true }],
58+
"@stylistic/operator-linebreak": ["error", "before"],
59+
"@stylistic/padded-blocks": "off",
60+
"@stylistic/quote-props": ["error", "as-needed"],
61+
"@stylistic/quotes": ["error", "double"],
62+
"@stylistic/indent": ["error", "tab"],
63+
"@stylistic/semi": ["error", "always"],
64+
"@stylistic/space-before-function-paren": ["error", "always"],
65+
"@stylistic/spaced-comment": "off"
66+
},
67+
"overrides": [
68+
{
69+
"files": ["config/config.js.sample"],
70+
"rules": {
71+
"@stylistic/comma-dangle": "off",
72+
"@stylistic/indent": "off",
73+
"@stylistic/no-multi-spaces": "off"
74+
}
75+
},
76+
{
77+
"files": ["tests/configs/modules/weather/*.js"],
78+
"rules": {
79+
"@stylistic/quotes": "off"
80+
}
81+
}
82+
]
4383
}

.prettierignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.eslintignore
1+
*.js
22
.prettierignore
33
/config
44
/coverage

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ _This release is scheduled to be released on 2024-01-01._
3131
- Updated dependencies
3232
- Clock module: optionally display current moon phase in addition to rise/set times
3333
- electron is now per default started without gpu, if needed it must be enabled with new env var `ELECTRON_ENABLE_GPU=1` on startup (#3226)
34+
- Replace prettier by stylistic in ESLint config to lint JavaScript
3435

3536
### Fixed
3637

clientonly/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
/**
88
* Helper function to get server address/hostname from either the commandline or env
99
*/
10-
function getServerAddress() {
10+
function getServerAddress () {
11+
1112
/**
1213
* Get command line parameters
1314
* Assumes that a cmdline parameter is defined with `--key [value]`
1415
* @param {string} key key to look for at the command line
1516
* @param {string} defaultValue value if no key is given at the command line
1617
* @returns {string} the value of the parameter
1718
*/
18-
function getCommandLineParameter(key, defaultValue = undefined) {
19+
function getCommandLineParameter (key, defaultValue = undefined) {
1920
const index = process.argv.indexOf(`--${key}`);
2021
const value = index > -1 ? process.argv[index + 1] : undefined;
2122
return value !== undefined ? String(value) : defaultValue;
@@ -35,7 +36,7 @@
3536
* @param {string} url location where the server is running.
3637
* @returns {Promise} the config
3738
*/
38-
function getServerConfig(url) {
39+
function getServerConfig (url) {
3940
// Return new pending promise
4041
return new Promise((resolve, reject) => {
4142
// Select http or https module, depending on requested url
@@ -64,7 +65,7 @@
6465
* @param {string} message error message to print
6566
* @param {number} code error code for the exit call
6667
*/
67-
function fail(message, code = 1) {
68+
function fail (message, code = 1) {
6869
if (message !== undefined && typeof message === "string") {
6970
console.log(message);
7071
} else {
@@ -121,4 +122,4 @@
121122
} else {
122123
fail();
123124
}
124-
})();
125+
}());

config/config.js.sample

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ let config = {
1818
// - "0.0.0.0", "::" to listen on any interface
1919
// Default, when address config is left out or empty, is "localhost"
2020
port: 8080,
21-
basePath: "/", // The URL path where MagicMirror² is hosted. If you are using a Reverse proxy
22-
// you must set the sub path here. basePath must end with a /
21+
basePath: "/", // The URL path where MagicMirror² is hosted. If you are using a Reverse proxy
22+
// you must set the sub path here. basePath must end with a /
2323
ipWhitelist: ["127.0.0.1", "::ffff:127.0.0.1", "::1"], // Set [] to allow all IP addresses
24-
// or add a specific IPv4 of 192.168.1.5 :
25-
// ["127.0.0.1", "::ffff:127.0.0.1", "::1", "::ffff:192.168.1.5"],
26-
// or IPv4 range of 192.168.3.0 --> 192.168.3.15 use CIDR format :
27-
// ["127.0.0.1", "::ffff:127.0.0.1", "::1", "::ffff:192.168.3.0/28"],
24+
// or add a specific IPv4 of 192.168.1.5 :
25+
// ["127.0.0.1", "::ffff:127.0.0.1", "::1", "::ffff:192.168.1.5"],
26+
// or IPv4 range of 192.168.3.0 --> 192.168.3.15 use CIDR format :
27+
// ["127.0.0.1", "::ffff:127.0.0.1", "::1", "::ffff:192.168.3.0/28"],
2828

29-
useHttps: false, // Support HTTPS or not, default "false" will use HTTP
30-
httpsPrivateKey: "", // HTTPS private key path, only require when useHttps is true
31-
httpsCertificate: "", // HTTPS Certificate path, only require when useHttps is true
29+
useHttps: false, // Support HTTPS or not, default "false" will use HTTP
30+
httpsPrivateKey: "", // HTTPS private key path, only require when useHttps is true
31+
httpsCertificate: "", // HTTPS Certificate path, only require when useHttps is true
3232

3333
language: "en",
3434
locale: "en-US",
@@ -109,4 +109,4 @@ let config = {
109109
};
110110

111111
/*************** DO NOT EDIT THE LINE BELOW ***************/
112-
if (typeof module !== "undefined") {module.exports = config;}
112+
if (typeof module !== "undefined") { module.exports = config; }

js/animateCSS.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,12 @@ const AnimateCSSOut = [
134134
* @param {string} [animation] animation name.
135135
* @param {number} [animationTime] animation duration.
136136
*/
137-
function addAnimateCSS(element, animation, animationTime) {
137+
function addAnimateCSS (element, animation, animationTime) {
138138
const animationName = `animate__${animation}`;
139139
const node = document.getElementById(element);
140140
if (!node) {
141141
// don't execute animate: we don't find div
142-
Log.warn(`addAnimateCSS: node not found for`, element);
142+
Log.warn("addAnimateCSS: node not found for", element);
143143
return;
144144
}
145145
node.style.setProperty("--animate-duration", `${animationTime}s`);
@@ -151,12 +151,12 @@ function addAnimateCSS(element, animation, animationTime) {
151151
* @param {string} [element] div element to animate.
152152
* @param {string} [animation] animation name.
153153
*/
154-
function removeAnimateCSS(element, animation) {
154+
function removeAnimateCSS (element, animation) {
155155
const animationName = `animate__${animation}`;
156156
const node = document.getElementById(element);
157157
if (!node) {
158158
// don't execute animate: we don't find div
159-
Log.warn(`removeAnimateCSS: node not found for`, element);
159+
Log.warn("removeAnimateCSS: node not found for", element);
160160
return;
161161
}
162162
node.classList.remove("animate__animated", animationName);

js/app.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ process.on("uncaughtException", function (err) {
4747
* The core app.
4848
* @class
4949
*/
50-
function App() {
50+
function App () {
5151
let nodeHelpers = [];
5252
let httpServer;
5353

@@ -56,7 +56,7 @@ function App() {
5656
* @async
5757
* @returns {Promise<object>} the loaded config or the defaults if something goes wrong
5858
*/
59-
async function loadConfig() {
59+
async function loadConfig () {
6060
Log.log("Loading config ...");
6161
const defaults = require(`${__dirname}/defaults`);
6262

@@ -136,7 +136,7 @@ function App() {
136136
* if it encounters one option from the deprecated.js list
137137
* @param {object} userConfig The user config
138138
*/
139-
function checkDeprecatedOptions(userConfig) {
139+
function checkDeprecatedOptions (userConfig) {
140140
const deprecated = require(`${global.root_path}/js/deprecated`);
141141
const deprecatedOptions = deprecated.configs;
142142

@@ -150,7 +150,7 @@ function App() {
150150
* Loads a specific module.
151151
* @param {string} module The name of the module (including subpath).
152152
*/
153-
function loadModule(module) {
153+
function loadModule (module) {
154154
const elements = module.split("/");
155155
const moduleName = elements[elements.length - 1];
156156
let moduleFolder = `${__dirname}/../modules/${module}`;
@@ -204,7 +204,7 @@ function App() {
204204
* @param {Module[]} modules All modules to be loaded
205205
* @returns {Promise} A promise that is resolved when all modules been loaded
206206
*/
207-
async function loadModules(modules) {
207+
async function loadModules (modules) {
208208
Log.log("Loading module helpers ...");
209209

210210
for (let module of modules) {
@@ -221,7 +221,7 @@ function App() {
221221
* @returns {number} A positive number if a is larger than b, a negative
222222
* number if a is smaller and 0 if they are the same
223223
*/
224-
function cmpVersions(a, b) {
224+
function cmpVersions (a, b) {
225225
let i, diff;
226226
const regExStrip0 = /(\.0+)+$/;
227227
const segmentsA = a.replace(regExStrip0, "").split(".");

js/check_config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ const Utils = require(`${rootPath}/js/utils.js`);
2020
* Check if set by environment variable MM_CONFIG_FILE
2121
* @returns {string} path and filename of the config file
2222
*/
23-
function getConfigFile() {
23+
function getConfigFile () {
2424
// FIXME: This function should be in core. Do you want refactor me ;) ?, be good!
2525
return path.resolve(process.env.MM_CONFIG_FILE || `${rootPath}/config/config.js`);
2626
}
2727

2828
/**
2929
* Checks the config file using eslint.
3030
*/
31-
function checkConfigFile() {
31+
function checkConfigFile () {
3232
const configFileName = getConfigFile();
3333

3434
// Check if file is present

js/class.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
(function () {
1111
let initializing = false;
12-
const fnTest = /xyz/.test(function () {
12+
const fnTest = (/xyz/).test(function () {
1313
xyz;
1414
})
1515
? /\b_super\b/
@@ -36,31 +36,31 @@
3636
// Copy the properties over onto the new prototype
3737
for (const name in prop) {
3838
// Check if we're overwriting an existing function
39-
prototype[name] =
40-
typeof prop[name] === "function" && typeof _super[name] === "function" && fnTest.test(prop[name])
39+
prototype[name]
40+
= typeof prop[name] === "function" && typeof _super[name] === "function" && fnTest.test(prop[name])
4141
? (function (name, fn) {
42-
return function () {
43-
const tmp = this._super;
42+
return function () {
43+
const tmp = this._super;
4444

45-
// Add a new ._super() method that is the same method
46-
// but on the super-class
47-
this._super = _super[name];
45+
// Add a new ._super() method that is the same method
46+
// but on the super-class
47+
this._super = _super[name];
4848

49-
// The method only need to be bound temporarily, so we
50-
// remove it when we're done executing
51-
const ret = fn.apply(this, arguments);
52-
this._super = tmp;
49+
// The method only need to be bound temporarily, so we
50+
// remove it when we're done executing
51+
const ret = fn.apply(this, arguments);
52+
this._super = tmp;
5353

54-
return ret;
55-
};
56-
})(name, prop[name])
54+
return ret;
55+
};
56+
}(name, prop[name]))
5757
: prop[name];
5858
}
5959

6060
/**
6161
* The dummy class constructor
6262
*/
63-
function Class() {
63+
function Class () {
6464
// All construction is actually done in the init method
6565
if (!initializing && this.init) {
6666
this.init.apply(this, arguments);
@@ -78,14 +78,14 @@
7878

7979
return Class;
8080
};
81-
})();
81+
}());
8282

8383
/**
8484
* Define the clone method for later use. Helper Method.
8585
* @param {object} obj Object to be cloned
8686
* @returns {object} the cloned object
8787
*/
88-
function cloneObject(obj) {
88+
function cloneObject (obj) {
8989
if (obj === null || typeof obj !== "object") {
9090
return obj;
9191
}

js/electron.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ let mainWindow;
2525
/**
2626
*
2727
*/
28-
function createWindow() {
28+
function createWindow () {
2929
// see https://www.electronjs.org/docs/latest/api/screen
3030
// Create a window that fills the screen's available work area.
3131
let electronSize = (800, 600);
@@ -121,11 +121,11 @@ function createWindow() {
121121
mainWindow.webContents.session.webRequest.onHeadersReceived((details, callback) => {
122122
let curHeaders = details.responseHeaders;
123123
if (config["ignoreXOriginHeader"] || false) {
124-
curHeaders = Object.fromEntries(Object.entries(curHeaders).filter((header) => !/x-frame-options/i.test(header[0])));
124+
curHeaders = Object.fromEntries(Object.entries(curHeaders).filter((header) => !(/x-frame-options/i).test(header[0])));
125125
}
126126

127127
if (config["ignoreContentSecurityPolicy"] || false) {
128-
curHeaders = Object.fromEntries(Object.entries(curHeaders).filter((header) => !/content-security-policy/i.test(header[0])));
128+
curHeaders = Object.fromEntries(Object.entries(curHeaders).filter((header) => !(/content-security-policy/i).test(header[0])));
129129
}
130130

131131
callback({ responseHeaders: curHeaders });

js/loader.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* MIT Licensed.
88
*/
99
const Loader = (function () {
10+
1011
/* Create helper variables */
1112

1213
const loadedModuleFiles = [];
@@ -196,10 +197,11 @@ const Loader = (function () {
196197

197198
/* Public Methods */
198199
return {
200+
199201
/**
200202
* Load all modules as defined in the config.
201203
*/
202-
loadModules: async function () {
204+
async loadModules () {
203205
let moduleData = getModuleData();
204206

205207
/**
@@ -230,7 +232,7 @@ const Loader = (function () {
230232
* @param {Module} module The module that calls the loadFile function.
231233
* @returns {Promise} resolved when the file is loaded
232234
*/
233-
loadFileForModule: async function (fileName, module) {
235+
async loadFileForModule (fileName, module) {
234236
if (loadedFiles.indexOf(fileName.toLowerCase()) !== -1) {
235237
Log.log(`File already loaded: ${fileName}`);
236238
return;
@@ -256,4 +258,4 @@ const Loader = (function () {
256258
return loadFile(module.file(fileName));
257259
}
258260
};
259-
})();
261+
}());

0 commit comments

Comments
 (0)