Skip to content

Commit

Permalink
feat(lint): remove unneeded parentheses around expressions
Browse files Browse the repository at this point in the history
Modernize code using eslint advisory introduced in GH-2596. This PR focuses on fixing no-extra-parens rule with a help or a custom rule output filter.
  • Loading branch information
mvorisek authored Dec 13, 2022
1 parent df82def commit 029436f
Show file tree
Hide file tree
Showing 52 changed files with 183 additions and 133 deletions.
23 changes: 23 additions & 0 deletions .eslint/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = {
rules: {
'no-extra-parens': require('./no-extra-parens-rule'),
},
configs: {
recommended: {
plugins: [
'@internal/eslint-plugin',
],
rules: {
// https://github.com/eslint/eslint/issues/16626
// https://github.com/airbnb/javascript/blob/eslint-config-airbnb-v19.0.4/packages/eslint-config-airbnb-base/rules/errors.js#L66
'@internal/no-extra-parens': ['error', 'all', {
conditionalAssign: true,
enforceForArrowConditionals: false,
ignoreJSX: 'all',
nestedBinaryExpressions: false,
returnAssign: false,
}],
},
},
},
};
17 changes: 17 additions & 0 deletions .eslint/no-extra-parens-rule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const eslint = require('eslint');
const ruleComposer = require('eslint-rule-composer');

const rule = new eslint.Linter().getRules().get('no-extra-parens');

module.exports = ruleComposer.filterReports(
rule,
(problem) => {
if (problem.node.type === 'ConditionalExpression'
&& (problem.node.parent.type === 'ConditionalExpression' || problem.node.parent.type === 'SpreadElement')
) {
return false;
}

return problem;
}
);
11 changes: 11 additions & 0 deletions .eslint/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@internal/eslint-plugin",
"version": "1.0.0",
"private": true,
"dependencies": {
"eslint-rule-composer": "^0.3.0"
},
"peerDependencies": {
"eslint": "*"
}
}
20 changes: 6 additions & 14 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
extends: [
'airbnb-base',
'plugin:unicorn/recommended',
'plugin:@internal/eslint-plugin/recommended',
],
parserOptions: {
ecmaVersion: '2020',
Expand All @@ -22,10 +23,10 @@ module.exports = {
'class-methods-use-this': 'off',
'comma-dangle': ['error', {
arrays: 'always-multiline',
objects: 'always-multiline',
exports: 'always-multiline',
functions: 'never',
imports: 'always-multiline',
exports: 'always-multiline',
objects: 'always-multiline',
}],
'consistent-return': 'off',
curly: ['error', 'all'],
Expand All @@ -47,10 +48,6 @@ module.exports = {
VariableDeclarator: true,
},
}],
'no-extra-parens': 'off', /* TODO https://github.com/eslint/eslint/issues/16626#issuecomment-1341016901 ['error', 'all', {
nestedBinaryExpressions: false,
enforceForNewInMemberExpressions: false,
}], */
'no-nested-ternary': 'off',
'no-param-reassign': 'off',
'no-plusplus': 'off',
Expand All @@ -60,8 +57,8 @@ module.exports = {
'object-shorthand': ['error', 'never'],
'padding-line-between-statements': ['error', {
blankLine: 'always',
prev: '*',
next: ['continue', 'break', 'export', 'return', 'throw'],
prev: '*',
}],
'prefer-destructuring': 'off',
'prefer-template': 'off',
Expand Down Expand Up @@ -99,6 +96,7 @@ module.exports = {
'one-var': 'off',
'prefer-const': 'off',
'prefer-exponentiation-operator': 'off',
'prefer-rest-params': 'off',
'prefer-spread': 'off',
'semi-style': 'off',
'unicorn/no-array-for-each': 'off',
Expand All @@ -118,14 +116,8 @@ module.exports = {
eqeqeq: 'off', // about 300 errors to be fixed manually
'global-require': 'off', // about 30 errors to be fixed manually
'no-shadow': 'off', // about 220 errors to be fixed manually
'no-shadow-restricted-names': 'off', // TODO https://github.com/fomantic/Fomantic-UI/pull/2604
'prefer-arrow-callback': 'off', // about 350 errors (all autofixable)
'prefer-rest-params': 'off', // about 180 errors to be fixed manually

// TODO
'no-return-assign': 'off',
'no-shadow-restricted-names': 'off',
'no-use-before-define': 'off',
'unicorn/prefer-negative-index': 'off',
},
reportUnusedDisableDirectives: true,
globals: {
Expand Down
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Set-up
*******************************/

let
const
gulp = require('gulp'),

// read user config to know what task to load
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"yamljs": "^0.3.0"
},
"devDependencies": {
"@internal/eslint-plugin": "file:.eslint",
"all-contributors-cli": "^6.7.0",
"auto-changelog": "^2.4.0",
"eslint": "^8.29.0",
Expand Down
22 changes: 11 additions & 11 deletions src/definitions/behaviors/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,11 @@
variable = templatedString.indexOf('$') !== -1
? templatedString.slice(2, -1)
: templatedString.slice(1, -1),
value = ($.isPlainObject(urlData) && urlData[variable] !== undefined)
value = $.isPlainObject(urlData) && urlData[variable] !== undefined
? urlData[variable]
: (($module.data(variable) !== undefined)
: ($module.data(variable) !== undefined
? $module.data(variable)
: (($context.data(variable) !== undefined) // eslint-disable-line unicorn/no-nested-ternary
: ($context.data(variable) !== undefined // eslint-disable-line unicorn/no-nested-ternary
? $context.data(variable)
: urlData[variable]))
;
Expand All @@ -393,11 +393,11 @@
variable = templatedString.indexOf('$') !== -1
? templatedString.slice(3, -1)
: templatedString.slice(2, -1),
value = ($.isPlainObject(urlData) && urlData[variable] !== undefined)
value = $.isPlainObject(urlData) && urlData[variable] !== undefined
? urlData[variable]
: (($module.data(variable) !== undefined)
: ($module.data(variable) !== undefined
? $module.data(variable)
: (($context.data(variable) !== undefined) // eslint-disable-line unicorn/no-nested-ternary
: ($context.data(variable) !== undefined // eslint-disable-line unicorn/no-nested-ternary
? $context.data(variable)
: urlData[variable]))
;
Expand Down Expand Up @@ -748,15 +748,15 @@
},
responseFromXHR: function (xhr) {
return $.isPlainObject(xhr)
? ((module.is.expectingJSON())
? (module.is.expectingJSON()
? module.decode.json(xhr.responseText)
: xhr.responseText)
: false;
},
errorFromRequest: function (response, status, httpMessage) {
return ($.isPlainObject(response) && response.error !== undefined)
return $.isPlainObject(response) && response.error !== undefined
? response.error // use json error message
: ((settings.error[status] !== undefined) // use server error message
: (settings.error[status] !== undefined // use server error message
? settings.error[status]
: httpMessage);
},
Expand Down Expand Up @@ -836,9 +836,9 @@
}
if (settings.on == 'auto') {
if ($module.is('input')) {
return (element.oninput !== undefined)
return element.oninput !== undefined
? 'input'
: ((element.onpropertychange !== undefined)
: (element.onpropertychange !== undefined
? 'propertychange'
: 'keyup');
}
Expand Down
16 changes: 7 additions & 9 deletions src/definitions/behaviors/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,9 @@
return module.get.inputEvent();
},
inputEvent: function () {
return (document.createElement('input').oninput !== undefined)
return document.createElement('input').oninput !== undefined
? 'input'
: ((document.createElement('input').onpropertychange !== undefined)
: (document.createElement('input').onpropertychange !== undefined
? 'propertychange'
: 'keyup');
},
Expand Down Expand Up @@ -542,13 +542,11 @@
if (ancillary && ['integer', 'decimal', 'number'].indexOf(ruleName) >= 0 && ancillary.indexOf('..') >= 0) {
parts = ancillary.split('..', 2);
if (!rule.prompt) {
suffixPrompt = (
parts[0] === ''
? settings.prompt.maxValue.replace(/{ruleValue}/g, '{max}')
: (parts[1] === ''
? settings.prompt.minValue.replace(/{ruleValue}/g, '{min}')
: settings.prompt.range)
);
suffixPrompt = parts[0] === ''
? settings.prompt.maxValue.replace(/{ruleValue}/g, '{max}')
: (parts[1] === ''
? settings.prompt.minValue.replace(/{ruleValue}/g, '{min}')
: settings.prompt.range);
prompt += suffixPrompt.replace(/{name}/g, ' ' + settings.text.and);
}
prompt = prompt.replace(/{min}/g, parts[0]);
Expand Down
6 changes: 3 additions & 3 deletions src/definitions/modules/accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
toggle: function (query) {
var
$activeTitle = query !== undefined
? ((typeof query === 'number')
? (typeof query === 'number'
? $title.eq(query)
: $(query).closest(selector.title))
: $(this).closest(selector.title),
Expand All @@ -147,7 +147,7 @@
open: function (query) {
var
$activeTitle = query !== undefined
? ((typeof query === 'number')
? (typeof query === 'number'
? $title.eq(query)
: $(query).closest(selector.title))
: $(this).closest(selector.title),
Expand Down Expand Up @@ -217,7 +217,7 @@
close: function (query) {
var
$activeTitle = query !== undefined
? ((typeof query === 'number')
? (typeof query === 'number'
? $title.eq(query)
: $(query).closest(selector.title))
: $(this).closest(selector.title),
Expand Down
12 changes: 6 additions & 6 deletions src/definitions/modules/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,8 @@
var focused = module.helper.dateEqual(cellDate, focusDate, mode);
var inRange = !rangeDate
? false
: ((!!startDate && module.helper.isDateInRange(cellDate, mode, startDate, rangeDate))
|| (!!endDate && module.helper.isDateInRange(cellDate, mode, rangeDate, endDate)));
: (!!startDate && module.helper.isDateInRange(cellDate, mode, startDate, rangeDate))
|| (!!endDate && module.helper.isDateInRange(cellDate, mode, rangeDate, endDate));
$cell.toggleClass(className.focusCell, focused && (!isTouch || isTouchDown) && (!adjacent || (settings.selectAdjacentDays && adjacent)) && !disabled);

if (module.helper.isTodayButton($cell)) {
Expand Down Expand Up @@ -1112,7 +1112,7 @@
ss: ('0' + s).slice(-2),
a: a,
A: a.toUpperCase(),
S: ['th', 'st', 'nd', 'rd'][(D % 10) > 3 ? 0 : (((D % 100) - (D % 10) === 10) ? 0 : D % 10)],
S: ['th', 'st', 'nd', 'rd'][(D % 10) > 3 ? 0 : ((D % 100) - (D % 10) === 10 ? 0 : D % 10)],
w: w,
ww: ('0' + w).slice(-2),
}
Expand All @@ -1123,7 +1123,7 @@
return tokens[match];
}

return match.slice(1, match.length - 1);
return match.slice(1, -1);
});
},
isDisabled: function (date, mode) {
Expand Down Expand Up @@ -1395,9 +1395,9 @@

return !date
? date
: ((minDate && module.helper.dateDiff(date, minDate, 'minute') > 0)
: (minDate && module.helper.dateDiff(date, minDate, 'minute') > 0
? (isTimeOnly ? module.helper.mergeDateTime(date, minDate) : minDate) // eslint-disable-line unicorn/no-nested-ternary
: ((maxDate && module.helper.dateDiff(maxDate, date, 'minute') > 0) // eslint-disable-line unicorn/no-nested-ternary
: (maxDate && module.helper.dateDiff(maxDate, date, 'minute') > 0 // eslint-disable-line unicorn/no-nested-ternary
? (isTimeOnly ? module.helper.mergeDateTime(date, maxDate) : maxDate)
: date));
},
Expand Down
2 changes: 1 addition & 1 deletion src/definitions/modules/dimmer.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
moduleNamespace = 'module-' + namespace,
moduleSelector = $allModules.selector || '',

clickEvent = ('ontouchstart' in document.documentElement)
clickEvent = 'ontouchstart' in document.documentElement
? 'touchstart'
: 'click',

Expand Down
28 changes: 14 additions & 14 deletions src/definitions/modules/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -1920,8 +1920,8 @@
return '';
}

return (!module.has.selectInput() && module.is.multiple())
? ((typeof value === 'string') // delimited string
return !module.has.selectInput() && module.is.multiple()
? (typeof value === 'string' // delimited string
? (raw
? value
: module.escape.htmlEntities(value)).split(settings.delimiter)
Expand Down Expand Up @@ -1965,9 +1965,9 @@
$choice.find(selector.menuIcon).remove();
}

return ($choice.data(metadata.text) !== undefined)
return $choice.data(metadata.text) !== undefined
? $choice.data(metadata.text)
: ((preserveHTML)
: (preserveHTML
? $choice.html() && $choice.html().trim()
: $choice.text() && $choice.text().trim());
}
Expand All @@ -1978,9 +1978,9 @@
return false;
}

return ($choice.data(metadata.value) !== undefined)
return $choice.data(metadata.value) !== undefined
? String($choice.data(metadata.value))
: ((typeof choiceText === 'string')
: (typeof choiceText === 'string'
? String(
settings.ignoreSearchCase
? choiceText.toLowerCase()
Expand All @@ -1993,9 +1993,9 @@
input = $search[0]
;
if (input) {
return (input.oninput !== undefined)
return input.oninput !== undefined
? 'input'
: ((input.onpropertychange !== undefined)
: (input.onpropertychange !== undefined
? 'propertychange'
: 'keyup');
}
Expand Down Expand Up @@ -2102,14 +2102,14 @@
;
value = value !== undefined
? value
: ((module.get.values() !== undefined)
: (module.get.values() !== undefined
? module.get.values()
: module.get.text());
isMultiple = (module.is.multiple() && Array.isArray(value));
shouldSearch = (isMultiple)
isMultiple = module.is.multiple() && Array.isArray(value);
shouldSearch = isMultiple
? value.length > 0
: (value !== undefined && value !== null);
strict = (value === '' || value === false || value === true)
: value !== undefined && value !== null;
strict = value === '' || value === false || value === true
? true
: strict || false;
if (shouldSearch) {
Expand Down Expand Up @@ -2384,7 +2384,7 @@
: elementIndex < $selectableItem.length;
$nextSelectedItem = isWithinRange
? $selectableItem.eq(elementIndex)
: ((direction == 'up')
: (direction == 'up'
? $selectableItem.first()
: $selectableItem.last());
if ($nextSelectedItem.length > 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/definitions/modules/flyout.js
Original file line number Diff line number Diff line change
Expand Up @@ -1015,8 +1015,8 @@
ie: function () {
if (module.cache.isIE === undefined) {
var
isIE11 = (!(window.ActiveXObject) && 'ActiveXObject' in window),
isIE = ('ActiveXObject' in window)
isIE11 = !window.ActiveXObject && 'ActiveXObject' in window,
isIE = 'ActiveXObject' in window
;
module.cache.isIE = isIE11 || isIE;
}
Expand Down
Loading

0 comments on commit 029436f

Please sign in to comment.