Skip to content

Commit

Permalink
feat(lint): nested ternary formatting and remove unneeded parentheses
Browse files Browse the repository at this point in the history
Modernize code using eslint advisory introduced in GH-2596. This PR updates ternary operator formatting for better readability and and removes unneeded parentheses around expressions.
  • Loading branch information
mvorisek authored Dec 13, 2022
1 parent 27384cb commit df82def
Show file tree
Hide file tree
Showing 35 changed files with 811 additions and 760 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ 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 Down Expand Up @@ -116,7 +120,6 @@ module.exports = {
'no-shadow': 'off', // about 220 errors to be fixed manually
'prefer-arrow-callback': 'off', // about 350 errors (all autofixable)
'prefer-rest-params': 'off', // about 180 errors to be fixed manually
'unicorn/no-nested-ternary': 'off', // about 80 errors (except 14 nested ternary operators autofixable)

// TODO
'no-return-assign': 'off',
Expand Down
4 changes: 3 additions & 1 deletion scripts/nightly-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ const getPublishedVersion = async function () {
.then((p) => {
let nightly = p['dist-tags'].nightly ?? '';
let versionInfo = p.versions[nightly] ?? {};
let buildCommit = nightly.indexOf('+') === -1 && versionInfo.gitHead ? '+' + (versionInfo.gitHead ?? '').slice(0, 7) : '';
let buildCommit = nightly.indexOf('+') === -1 && versionInfo.gitHead
? '+' + (versionInfo.gitHead ?? '').slice(0, 7)
: '';

return nightly + buildCommit;
})
Expand Down
84 changes: 42 additions & 42 deletions src/definitions/behaviors/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
return typeof obj === 'function' && typeof obj.nodeType !== 'number';
}

window = (window !== undefined && window.Math === Math)
window = window !== undefined && window.Math === Math
? window
: globalThis;

Expand All @@ -34,15 +34,15 @@
performance = [],

query = arguments[0],
methodInvoked = (typeof query === 'string'),
methodInvoked = typeof query === 'string',
queryArguments = [].slice.call(arguments, 1),

returnedValue
;

$allModules.each(function () {
var
settings = ($.isPlainObject(parameters))
settings = $.isPlainObject(parameters)
? $.extend(true, {}, $.fn.api.settings, parameters)
: $.extend({}, $.fn.api.settings),

Expand All @@ -62,7 +62,7 @@
$form = $module.closest(selector.form),

// context used for state
$context = (settings.stateContext)
$context = settings.stateContext
? ([window, document].indexOf(settings.stateContext) < 0 ? $(document).find(settings.stateContext) : $(settings.stateContext))
: $module,

Expand Down Expand Up @@ -274,13 +274,13 @@

should: {
removeError: function () {
return (settings.hideError === true || (settings.hideError === 'auto' && !module.is.form()));
return settings.hideError === true || (settings.hideError === 'auto' && !module.is.form());
},
},

is: {
disabled: function () {
return ($module.filter(selector.disabled).length > 0);
return $module.filter(selector.disabled).length > 0;
},
expectingJSON: function () {
return settings.dataType === 'json' || settings.dataType === 'jsonp';
Expand All @@ -289,14 +289,14 @@
return $module.is('form') || $context.is('form');
},
mocked: function () {
return (settings.mockResponse || settings.mockResponseAsync || settings.response || settings.responseAsync);
return settings.mockResponse || settings.mockResponseAsync || settings.response || settings.responseAsync;
},
input: function () {
return $module.is('input');
},
loading: function () {
return (module.request)
? (module.request.state() == 'pending')
return module.request
? module.request.state() == 'pending'
: false;
},
abortedRequest: function (xhr) {
Expand All @@ -311,7 +311,7 @@
return false;
},
validResponse: function (response) {
if ((!module.is.expectingJSON()) || !isFunction(settings.successTest)) {
if (!module.is.expectingJSON() || !isFunction(settings.successTest)) {
module.verbose('Response is not JSON, skipping validation', settings.successTest, response);

return true;
Expand All @@ -331,16 +331,16 @@

was: {
cancelled: function () {
return (module.cancelled || false);
return module.cancelled || false;
},
successful: function () {
return (module.request && module.request.state() == 'resolved');
return module.request && module.request.state() == 'resolved';
},
failure: function () {
return (module.request && module.request.state() == 'rejected');
return module.request && module.request.state() == 'rejected';
},
complete: function () {
return (module.request && (module.request.state() == 'resolved' || module.request.state() == 'rejected'));
return module.request && (module.request.state() == 'resolved' || module.request.state() == 'rejected');
},
},

Expand All @@ -359,16 +359,16 @@
$.each(requiredVariables, function (index, templatedString) {
var
// allow legacy {$var} style
variable = (templatedString.indexOf('$') !== -1)
variable = templatedString.indexOf('$') !== -1
? templatedString.slice(2, -1)
: templatedString.slice(1, -1),
value = ($.isPlainObject(urlData) && urlData[variable] !== undefined)
? urlData[variable]
: ($module.data(variable) !== undefined)
: (($module.data(variable) !== undefined)
? $module.data(variable)
: ($context.data(variable) !== undefined)
: (($context.data(variable) !== undefined) // eslint-disable-line unicorn/no-nested-ternary
? $context.data(variable)
: urlData[variable]
: urlData[variable]))
;
// remove value
if (value === undefined) {
Expand All @@ -379,7 +379,7 @@
}

module.verbose('Found required variable', variable, value);
value = (settings.encodeParameters)
value = settings.encodeParameters
? module.get.urlEncodedValue(value)
: value;
url = url.replace(templatedString, value);
Expand All @@ -390,16 +390,16 @@
$.each(optionalVariables, function (index, templatedString) {
var
// allow legacy {/$var} style
variable = (templatedString.indexOf('$') !== -1)
variable = templatedString.indexOf('$') !== -1
? templatedString.slice(3, -1)
: templatedString.slice(2, -1),
value = ($.isPlainObject(urlData) && urlData[variable] !== undefined)
? urlData[variable]
: ($module.data(variable) !== undefined)
: (($module.data(variable) !== undefined)
? $module.data(variable)
: ($context.data(variable) !== undefined)
: (($context.data(variable) !== undefined) // eslint-disable-line unicorn/no-nested-ternary
? $context.data(variable)
: urlData[variable]
: urlData[variable]))
;
// optional replacement
if (value !== undefined) {
Expand Down Expand Up @@ -536,15 +536,15 @@
done: function (response, textStatus, xhr) {
var
context = this,
elapsedTime = (Date.now() - requestStartTime),
timeLeft = (settings.loadingDuration - elapsedTime),
translatedResponse = (isFunction(settings.onResponse))
? module.is.expectingJSON() && !settings.rawResponse
elapsedTime = Date.now() - requestStartTime,
timeLeft = settings.loadingDuration - elapsedTime,
translatedResponse = isFunction(settings.onResponse)
? (module.is.expectingJSON() && !settings.rawResponse
? settings.onResponse.call(context, $.extend(true, {}, response))
: settings.onResponse.call(context, response)
: settings.onResponse.call(context, response))
: false
;
timeLeft = (timeLeft > 0)
timeLeft = timeLeft > 0
? timeLeft
: 0;
if (translatedResponse) {
Expand All @@ -565,10 +565,10 @@
fail: function (xhr, status, httpMessage) {
var
context = this,
elapsedTime = (Date.now() - requestStartTime),
timeLeft = (settings.loadingDuration - elapsedTime)
elapsedTime = Date.now() - requestStartTime,
timeLeft = settings.loadingDuration - elapsedTime
;
timeLeft = (timeLeft > 0)
timeLeft = timeLeft > 0
? timeLeft
: 0;
if (timeLeft > 0) {
Expand Down Expand Up @@ -748,17 +748,17 @@
},
responseFromXHR: function (xhr) {
return $.isPlainObject(xhr)
? (module.is.expectingJSON())
? ((module.is.expectingJSON())
? module.decode.json(xhr.responseText)
: xhr.responseText
: xhr.responseText)
: false;
},
errorFromRequest: function (response, status, httpMessage) {
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;
: httpMessage);
},
request: function () {
return module.request || false;
Expand Down Expand Up @@ -795,15 +795,15 @@
return runSettings;
}

return (runSettings !== undefined)
return runSettings !== undefined
? $.extend(true, {}, runSettings)
: $.extend(true, {}, settings);
},
urlEncodedValue: function (value) {
var
decodedValue = window.decodeURIComponent(value),
encodedValue = window.encodeURIComponent(value),
alreadyEncoded = (decodedValue !== value)
alreadyEncoded = decodedValue !== value
;
if (alreadyEncoded) {
module.debug('URL value is already encoded, avoiding double encoding', value);
Expand Down Expand Up @@ -838,9 +838,9 @@
if ($module.is('input')) {
return (element.oninput !== undefined)
? 'input'
: (element.onpropertychange !== undefined)
: ((element.onpropertychange !== undefined)
? 'propertychange'
: 'keyup';
: 'keyup');
}
if ($module.is('form')) {
return 'submit';
Expand Down Expand Up @@ -1004,7 +1004,7 @@
query = query.split(/[ .]/);
maxDepth = query.length - 1;
$.each(query, function (depth, value) {
var camelCaseValue = (depth != maxDepth)
var camelCaseValue = depth != maxDepth
? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
: query
;
Expand Down Expand Up @@ -1057,7 +1057,7 @@
}
});

return (returnedValue !== undefined)
return returnedValue !== undefined
? returnedValue
: this;
};
Expand Down
Loading

0 comments on commit df82def

Please sign in to comment.