From 5d06a1d6a4f0df18cffec7c91b145b062a1681d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20D=C4=9Bdi=C4=8D?= Date: Tue, 24 Oct 2023 23:49:54 +0200 Subject: [PATCH 1/2] Added eslint-plugin-prefer-arrow-functions --- .eslintrc.json | 12 ++++++++++-- package-lock.json | 10 ++++++++++ package.json | 1 + 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index d0700987..c06ef729 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -10,6 +10,7 @@ "plugins": [ "compat", "deprecation", + "prefer-arrow-functions", "@typescript-eslint", "simple-import-sort" ], @@ -20,13 +21,20 @@ "plugin:@typescript-eslint/stylistic-type-checked" ], "rules": { + "arrow-body-style": ["error", "as-needed"], "camelcase": ["error", {"allow": ["wordpress__customize"]}], + "no-warning-comments": "warn", + "strict": ["error", "never"], "compat/compat": "warn", "deprecation/deprecation": "warn", - "no-warning-comments": "warn", + "prefer-arrow-functions/prefer-arrow-functions": [ + "error", + { + "allowNamedFunctions": true + } + ], "simple-import-sort/imports": "error", "simple-import-sort/exports": "error", - "strict": ["error", "never"], "@typescript-eslint/array-type": ["error", {"default": "generic"}], "@typescript-eslint/consistent-type-exports": "error", "@typescript-eslint/consistent-type-imports": "error", diff --git a/package-lock.json b/package-lock.json index 4e7709c8..674b3d3e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,6 +22,7 @@ "eslint": "^8.52.0", "eslint-plugin-compat": "^4.2.0", "eslint-plugin-deprecation": "^2.0.0", + "eslint-plugin-prefer-arrow-functions": "^3.2.4", "eslint-plugin-simple-import-sort": "^10.0.0", "gulp": "^4.0.2", "gulp-clean-css": "^4.3.0", @@ -5419,6 +5420,15 @@ } } }, + "node_modules/eslint-plugin-prefer-arrow-functions": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/eslint-plugin-prefer-arrow-functions/-/eslint-plugin-prefer-arrow-functions-3.2.4.tgz", + "integrity": "sha512-HbPmlbO/iYQeVs2fuShNkGVJDfVfgSd84Vzxv+xlh+nIVoSsZvTj6yOqszw4mtG9JbiqMShVWqbVeoVsejE59w==", + "dev": true, + "peerDependencies": { + "eslint": ">=5.0.0" + } + }, "node_modules/eslint-plugin-prettier": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/eslint-plugin-prettier/-/eslint-plugin-prettier-5.0.0.tgz", diff --git a/package.json b/package.json index 0896c2f2..d63c9d49 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "eslint": "^8.52.0", "eslint-plugin-compat": "^4.2.0", "eslint-plugin-deprecation": "^2.0.0", + "eslint-plugin-prefer-arrow-functions": "^3.2.4", "eslint-plugin-simple-import-sort": "^10.0.0", "gulp": "^4.0.2", "gulp-clean-css": "^4.3.0", From 5224c42bf625cc9f8ae736233fb5e20fb815a459 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20D=C4=9Bdi=C4=8D?= Date: Tue, 24 Oct 2023 23:50:13 +0200 Subject: [PATCH 2/2] Using arrow functions --- gulpfile.js | 143 ++++++++++++----------- src/ts/admin/liveReload.ts | 16 +-- src/ts/admin/preset_customize_control.ts | 10 +- src/ts/admin/preset_on_activation.ts | 4 +- src/ts/frontend/blog.ts | 4 +- 5 files changed, 90 insertions(+), 87 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index f8daf844..32b16962 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -13,8 +13,8 @@ const terser = require('gulp-terser'); const ts = require('gulp-typescript'); const wpPot = require('gulp-wp-pot'); -gulp.task('build:css:main', function () { - return gulp +gulp.task('build:css:main', () => + gulp .src([ 'src/css/style.css', 'src/css/frontend/blog.css', @@ -26,21 +26,21 @@ gulp.task('build:css:main', function () { ]) .pipe(cleanCSS({ compatibility: 'ie8' })) .pipe(concat('style.css')) - .pipe(gulp.dest('dist/')); -}); + .pipe(gulp.dest('dist/')) +); -gulp.task('build:css:admin', function () { - return gulp +gulp.task('build:css:admin', () => + gulp .src('src/css/admin/**/*.css') .pipe(cleanCSS({ compatibility: 'ie8' })) .pipe(rename({ suffix: '.min' })) - .pipe(gulp.dest('dist/admin/css/')); -}); + .pipe(gulp.dest('dist/admin/css/')) +); gulp.task('build:css', gulp.parallel('build:css:main', 'build:css:admin')); -gulp.task('build:deps:npm:dripicons', function () { - return gulp +gulp.task('build:deps:npm:dripicons', () => + gulp .src( [ 'node_modules/dripicons/webfont/webfont.css', @@ -48,26 +48,26 @@ gulp.task('build:deps:npm:dripicons', function () { ], { base: 'node_modules/dripicons/webfont' } ) - .pipe(gulp.dest('dist/frontend/dripicons')); -}); + .pipe(gulp.dest('dist/frontend/dripicons')) +); gulp.task('build:deps:npm', gulp.parallel('build:deps:npm:dripicons')); gulp.task('build:deps', gulp.parallel('build:deps:npm')); -gulp.task('build:jpg:screenshot', function () { - return gulp.src('src/jpg/screenshot.jpg').pipe(gulp.dest('dist/')); -}); +gulp.task('build:jpg:screenshot', () => + gulp.src('src/jpg/screenshot.jpg').pipe(gulp.dest('dist/')) +); -gulp.task('build:jpg:frontend', function () { - return gulp +gulp.task('build:jpg:frontend', () => + gulp .src('src/jpg/frontend/**/*.jpg') - .pipe(gulp.dest('dist/frontend/images/')); -}); + .pipe(gulp.dest('dist/frontend/images/')) +); -gulp.task('build:jpg:admin', function () { - return gulp.src('src/jpg/admin/**/*.jpg').pipe(gulp.dest('dist/admin/')); -}); +gulp.task('build:jpg:admin', () => + gulp.src('src/jpg/admin/**/*.jpg').pipe(gulp.dest('dist/admin/')) +); gulp.task( 'build:jpg', @@ -94,8 +94,8 @@ function bundle(name, sources, part, jQuery = false) { .pipe(gulp.dest('dist/' + part + '/js/')); } -gulp.task('build:js', function () { - return merge( +gulp.task('build:js', () => + merge( bundle( 'preset_customize_control', ['src/ts/admin/preset_customize_control.ts'], @@ -122,55 +122,53 @@ gulp.task('build:js', function () { true ), bundle('blog', ['src/ts/frontend/blog.ts'], 'frontend', true) - ); -}); + ) +); -gulp.task('build:mo', function () { - return gulp +gulp.task('build:mo', () => + gulp .src('src/languages/*.po') .pipe(potomo({ verbose: false })) .pipe( - rename(function (path) { + rename((path) => { path.basename = path.basename.substring( path.basename.lastIndexOf('-') + 1 ); }) ) - .pipe(gulp.dest('dist/languages/')); -}); + .pipe(gulp.dest('dist/languages/')) +); -gulp.task('build:php:root', function () { - return gulp.src('src/php/*.php').pipe(gulp.dest('dist/')); -}); +gulp.task('build:php:root', () => + gulp.src('src/php/*.php').pipe(gulp.dest('dist/')) +); -gulp.task('build:php:admin', function () { - return gulp.src('src/php/admin/**/*.php').pipe(gulp.dest('dist/admin/')); -}); +gulp.task('build:php:admin', () => + gulp.src('src/php/admin/**/*.php').pipe(gulp.dest('dist/admin/')) +); -gulp.task('build:php:frontend', function () { - return gulp - .src('src/php/frontend/**/*.php') - .pipe(gulp.dest('dist/frontend/')); -}); +gulp.task('build:php:frontend', () => + gulp.src('src/php/frontend/**/*.php').pipe(gulp.dest('dist/frontend/')) +); gulp.task( 'build:php', gulp.parallel('build:php:root', 'build:php:admin', 'build:php:frontend') ); -gulp.task('build:png:frontend', function () { - return gulp +gulp.task('build:png:frontend', () => + gulp .src('src/png/frontend/**/*.png') - .pipe(gulp.dest('dist/frontend/images/')); -}); + .pipe(gulp.dest('dist/frontend/images/')) +); gulp.task('build:png', gulp.parallel('build:png:frontend')); -gulp.task('build:txt', function () { - return gulp +gulp.task('build:txt', () => + gulp .src(['src/txt/license.txt', 'src/txt/readme.txt']) - .pipe(gulp.dest('dist/')); -}); + .pipe(gulp.dest('dist/')) +); gulp.task( 'build', @@ -188,29 +186,34 @@ gulp.task( gulp.task( 'update-translations:generate-pot', - gulp.series(function () { - return gulp - .src('src/php/**/*.php') - .pipe( - wpPot({ - bugReport: 'https://github.com/skaut/crdm-modern/issues', - domain: 'crdm-modern', - package: 'crdm-modern', - relativeTo: 'src/php', - includePOTCreationDate: false, - }) - ) - .pipe(gulp.dest('src/languages/crdm-modern.pot')); - }, shell.task('msgmerge -U src/languages/crdm-modern.pot src/languages/crdm-modern.pot')) -); - -gulp.task('update-translations:update-po', function () { - return gulp + gulp.series( + () => + gulp + .src('src/php/**/*.php') + .pipe( + wpPot({ + bugReport: + 'https://github.com/skaut/crdm-modern/issues', + domain: 'crdm-modern', + package: 'crdm-modern', + relativeTo: 'src/php', + includePOTCreationDate: false, + }) + ) + .pipe(gulp.dest('src/languages/crdm-modern.pot')), + shell.task( + 'msgmerge -U src/languages/crdm-modern.pot src/languages/crdm-modern.pot' + ) + ) +); + +gulp.task('update-translations:update-po', () => + gulp .src('src/languages/*.po', { read: false }) .pipe( shell('msgmerge -U <%= file.path %> src/languages/crdm-modern.pot') - ); -}); + ) +); gulp.task( 'update-translations', diff --git a/src/ts/admin/liveReload.ts b/src/ts/admin/liveReload.ts index 9fff788b..2d4bd125 100644 --- a/src/ts/admin/liveReload.ts +++ b/src/ts/admin/liveReload.ts @@ -38,7 +38,7 @@ function setCSSInHead( mediaBegin + target.selector + ' {\n' + - $.map(target.properties, function (property) { + $.map(target.properties, (property) => { let computedValue = value; if (property.computed) { let additionalValues: Array = []; @@ -75,10 +75,10 @@ function liveReload( targets: Array, fallbacks?: Array ): void { - void wp.customize(setting, function (value) { - value.bind(function (newValue: string) { + void wp.customize(setting, (value) => { + value.bind((newValue: string) => { if (!newValue && fallbacks) { - $.each(fallbacks, function (_, fallback) { + $.each(fallbacks, (_, fallback) => { const fallbackValue = String(wp.customize(fallback).get()); if (fallbackValue) { newValue = fallbackValue; @@ -87,15 +87,15 @@ function liveReload( return true; }); } - $.each(targets, function (_, target) { + $.each(targets, (_, target) => { setCSSInHead(setting, target, newValue); }); }); }); if (fallbacks) { for (let i = 0; i < fallbacks.length; i++) { - void wp.customize(fallbacks[i], function (value) { - value.bind(function (newValue: string) { + void wp.customize(fallbacks[i], (value) => { + value.bind((newValue: string) => { if (wp.customize(setting).get() !== undefined) { return; } @@ -104,7 +104,7 @@ function liveReload( return; } } - $.each(targets, function (_, target) { + $.each(targets, (_, target) => { setCSSInHead(setting, target, newValue); }); }); diff --git a/src/ts/admin/preset_customize_control.ts b/src/ts/admin/preset_customize_control.ts index 6759b763..c34c4340 100644 --- a/src/ts/admin/preset_customize_control.ts +++ b/src/ts/admin/preset_customize_control.ts @@ -14,9 +14,9 @@ function applyPreset(control: wordpress__customize.Control): void { } const preset = crdmModernPresetCustomizeControlLocalize[chosen]; - $.each(preset, function (key, value) { + $.each(preset, (key, value) => { if (isAssoc(value)) { - $.each(value, function (innerKey, innerValue) { + $.each(value, (innerKey, innerValue) => { const innerSetting = wp.customize(key + '[' + innerKey + ']'); // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, @typescript-eslint/strict-boolean-expressions if (!innerSetting) { @@ -37,14 +37,14 @@ function applyPreset(control: wordpress__customize.Control): void { $('.generatepress-font-variant select').trigger('change'); } -void wp.customize.control('crdm_modern_preset', function (control) { +void wp.customize.control('crdm_modern_preset', (control) => { control.container .find('input[name=crdm_modern_preset]') - .on('change', function () { + .on('change', () => { control.container.find('.button').prop('disabled', false); }); - control.container.find('.button').on('click', function () { + control.container.find('.button').on('click', () => { applyPreset(control); }); }); diff --git a/src/ts/admin/preset_on_activation.ts b/src/ts/admin/preset_on_activation.ts index b1d50a00..54be1d1d 100644 --- a/src/ts/admin/preset_on_activation.ts +++ b/src/ts/admin/preset_on_activation.ts @@ -85,12 +85,12 @@ function onActivation(): void { '#TB_inline?inlineId=crdm-modern-preset-on-activation-modal' ); - $('input[name=crdm-modern-preset-on-activation]').on('change', function () { + $('input[name=crdm-modern-preset-on-activation]').on('change', () => { const applyButton = $('#crdm-modern-preset-on-activation-apply'); applyButton.removeAttr('disabled'); applyButton.on('click', applyCallback); }); - $('#crdm-modern-preset-on-activation-skip').on('click', function () { + $('#crdm-modern-preset-on-activation-skip').on('click', () => { tb_remove(); }); } diff --git a/src/ts/frontend/blog.ts b/src/ts/frontend/blog.ts index de43d976..6a8654a0 100644 --- a/src/ts/frontend/blog.ts +++ b/src/ts/frontend/blog.ts @@ -18,9 +18,9 @@ function addExcerptClickability(): void { return; } const articles = $('.site-main article .inside-article'); - articles.each(function (_, article) { + articles.each((_, article) => { const href = $(article).find('.entry-title a').attr('href') ?? ''; - $(article).on('click', function () { + $(article).on('click', () => { window.location.href = href; }); });