diff --git a/.github/workflows/coding-style.yml b/.github/workflows/coding-style.yml index 78244756..f1d06032 100644 --- a/.github/workflows/coding-style.yml +++ b/.github/workflows/coding-style.yml @@ -37,4 +37,4 @@ jobs: steps: - uses: actions/checkout@v4 - run: npm install - - run: npx eslint src/assets/netteForms.js + - run: npx eslint diff --git a/eslint.config.mjs b/eslint.config.mjs index 3e78d371..9f50e477 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -1,29 +1,43 @@ import globals from 'globals'; import pluginJs from '@eslint/js'; +import stylistic from '@stylistic/eslint-plugin'; export default [ - pluginJs.configs.recommended, { - ignores: ['**/*.min.js'], + ignores: ['*/*', '!src/*', '!tests/*', '**/*.min.js'], }, + + pluginJs.configs.recommended, + + stylistic.configs.customize({ + indent: 'tab', + braceStyle: '1tbs', + arrowParens: true, + semi: true, + jsx: false, + }), + { languageOptions: { ecmaVersion: 2021, globals: { ...globals.browser, - 'Tracy': 'writeable', - 'define': 'readable', - 'module': 'readable', + ...globals.jasmine, + Nette: 'readable', + Tracy: 'writeable', + define: 'readable', + module: 'readable', }, }, + plugins: { + '@stylistic': stylistic, + }, rules: { - indent: ['error', 'tab'], - quotes: ['error', 'single'], - semi: ['error', 'always'], - 'func-style': ['error', 'declaration', {'allowArrowFunctions': true}], + '@stylistic/no-multiple-empty-lines': ['error', { max: 2, maxEOF: 0 }], + '@stylistic/new-parens': ['error', 'never'], + '@stylistic/padded-blocks': 'off', + 'func-style': ['error', 'declaration', { allowArrowFunctions: true }], 'prefer-arrow-callback': ['error'], - 'arrow-parens': ['error'], - 'arrow-spacing': ['error'], 'no-var': ['error'], }, }, diff --git a/package.json b/package.json index 9a73a127..398de26e 100644 --- a/package.json +++ b/package.json @@ -19,8 +19,9 @@ "url": "git+https://github.com/nette/forms.git" }, "devDependencies": { - "@eslint/js": "^9.1.1", - "eslint": "^9.1.1", - "globals": "^15.1.0" + "@eslint/js": "^9.4.0", + "@stylistic/eslint-plugin": "^2.1.0", + "eslint": "^9.4.0", + "globals": "^15.3.0" } } diff --git a/src/assets/netteForms.js b/src/assets/netteForms.js index 759580c3..db8a7967 100644 --- a/src/assets/netteForms.js +++ b/src/assets/netteForms.js @@ -1,4 +1,4 @@ -/**! +/*! * NetteForms - simple form validation. * * This file is part of the Nette Framework (https://nette.org) @@ -33,7 +33,7 @@ const Nette = {}; let preventFiltering = {}; let formToggles = {}; - let toggleListeners = new window.WeakMap(); + let toggleListeners = new window.WeakMap; Nette.formErrors = []; Nette.version = '3.3.0'; @@ -129,7 +129,7 @@ } if (filter && preventFiltering[elem.name] === undefined) { preventFiltering[elem.name] = true; - let ref = {value: val}; + let ref = { value: val }; Nette.validateControl(elem, null, true, ref); val = ref.value; delete preventFiltering[elem.name]; @@ -149,7 +149,7 @@ */ Nette.validateControl = function (elem, rules, onlyCheck = false, value = null, emptyOptional = null) { rules ??= JSON.parse(elem.getAttribute('data-nette-rules') ?? '[]'); - value ??= {value: Nette.getEffectiveValue(elem)}; + value ??= { value: Nette.getEffectiveValue(elem) }; emptyOptional ??= !Nette.validateRule(elem, ':filled', null, value); for (let rule of rules) { @@ -185,7 +185,7 @@ let arr = Array.isArray(rule.arg) ? rule.arg : [rule.arg], message = rule.msg.replace( /%(value|\d+)/g, - (foo, m) => Nette.getValue(m === 'value' ? curElem : elem.form.elements.namedItem(arr[m].control)) + (foo, m) => Nette.getValue(m === 'value' ? curElem : elem.form.elements.namedItem(arr[m].control)), ); Nette.addError(curElem, message); } @@ -265,7 +265,7 @@ Nette.addError = function (elem, message) { Nette.formErrors.push({ element: elem, - message: message + message: message, }); }; @@ -343,7 +343,7 @@ return op === ':filled'; } - value ??= {value: Nette.getEffectiveValue(elem, true)}; + value ??= { value: Nette.getEffectiveValue(elem, true) }; let method = op.charAt(0) === ':' ? op.substring(1) : op; method = method.replace('::', '_').replaceAll('\\', ''); @@ -482,7 +482,7 @@ return false; }, - 'float': function (elem, arg, val, newValue) { + float: function (elem, arg, val, newValue) { val = val.replace(/ +/g, '').replace(/,/g, '.'); if ((/^-?[0-9]*\.?[0-9]+$/).test(val)) { newValue.value = parseFloat(val); @@ -535,9 +535,9 @@ return Nette.validators.mimeType(elem, arg ?? ['image/gif', 'image/png', 'image/jpeg', 'image/webp'], val); }, - 'static': function (elem, arg) { + static: function (elem, arg) { return arg; - } + }, }; @@ -572,7 +572,7 @@ */ Nette.toggleControl = function (elem, rules, success, firsttime, value = null, emptyOptional = null) { rules ??= JSON.parse(elem.getAttribute('data-nette-rules') ?? '[]'); - value ??= {value: Nette.getEffectiveValue(elem)}; + value ??= { value: Nette.getEffectiveValue(elem) }; emptyOptional ??= !Nette.validateRule(elem, ':filled', null, value); let has = false, @@ -617,7 +617,7 @@ }); } for (let id in rule.toggle ?? []) { - formToggles[id] ??= {elem: elem}; + formToggles[id] ??= { elem: elem }; formToggles[id].state ||= rule.toggle[id] ? curSuccess : !curSuccess; } } @@ -708,7 +708,7 @@ document.body.addEventListener('click', (e) => { let target = e.target; while (target) { - if (target.form && target.type in {submit: 1, image: 1}) { + if (target.form && target.type in { submit: 1, image: 1 }) { target.form['nette-submittedBy'] = target; break; } @@ -734,7 +734,7 @@ return res.replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, ''); }; - Nette.webalizeTable = {\u00e1: 'a', \u00e4: 'a', \u010d: 'c', \u010f: 'd', \u00e9: 'e', \u011b: 'e', \u00ed: 'i', \u013e: 'l', \u0148: 'n', \u00f3: 'o', \u00f4: 'o', \u0159: 'r', \u0161: 's', \u0165: 't', \u00fa: 'u', \u016f: 'u', \u00fd: 'y', \u017e: 'z'}; + Nette.webalizeTable = { \u00e1: 'a', \u00e4: 'a', \u010d: 'c', \u010f: 'd', \u00e9: 'e', \u011b: 'e', \u00ed: 'i', \u013e: 'l', \u0148: 'n', \u00f3: 'o', \u00f4: 'o', \u0159: 'r', \u0161: 's', \u0165: 't', \u00fa: 'u', \u016f: 'u', \u00fd: 'y', \u017e: 'z' }; return Nette; })); diff --git a/tests/netteForms/karma.conf.js b/tests/netteForms/karma.conf.js index d92d72c0..3712c64d 100644 --- a/tests/netteForms/karma.conf.js +++ b/tests/netteForms/karma.conf.js @@ -1,11 +1,11 @@ -module.exports = function(config) { +module.exports = function (config) { config.set({ basePath: '', frameworks: ['jasmine'], browsers: ['ChromeHeadless'], files: [ '../../src/assets/netteForms.js', - 'spec/*Spec.js' + 'spec/*Spec.js', ], autoWatch: false, singleRun: true, diff --git a/tests/netteForms/spec/Nette.validatorsSpec.js b/tests/netteForms/spec/Nette.validatorsSpec.js index cd9938a0..0db1bd35 100644 --- a/tests/netteForms/spec/Nette.validatorsSpec.js +++ b/tests/netteForms/spec/Nette.validatorsSpec.js @@ -90,7 +90,7 @@ describe('Nette.validators', () => { it('url', () => { - let v = {value: null}; + let v = { value: null }; expect(Nette.validators.url(null, null, '', v)).toBe(false); expect(Nette.validators.url(null, null, 'hello', v)).toBe(true); expect(v.value).toBe('https://hello');