Skip to content

Commit

Permalink
feat(lint): type-safe equality operator when comparing literals
Browse files Browse the repository at this point in the history
For comparing (non empty) strings, we can use === reliably (instead of ==).
  • Loading branch information
mvorisek authored Dec 14, 2022
1 parent 029436f commit 89e62cc
Show file tree
Hide file tree
Showing 40 changed files with 484 additions and 414 deletions.
18 changes: 18 additions & 0 deletions .eslint/eqeqeq-rule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const eslint = require('eslint');
const ruleComposer = require('eslint-rule-composer');

const rule = new eslint.Linter().getRules().get('eqeqeq');

module.exports = ruleComposer.filterReports(
rule,
(problem) => {
if (problem.node.type === 'BinaryExpression'
&& (problem.node.operator === '==' || problem.node.operator === '!=')
&& problem.node.right.type === 'Literal'
) {
return problem;
}

return false;
}
);
3 changes: 3 additions & 0 deletions .eslint/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
rules: {
eqeqeq: require('./eqeqeq-rule'),
'no-extra-parens': require('./no-extra-parens-rule'),
},
configs: {
Expand All @@ -8,6 +9,8 @@ module.exports = {
'@internal/eslint-plugin',
],
rules: {
// fixes only variable == (or !=) literal expression
'@internal/eqeqeq': ['error', 'always'],
// 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', {
Expand Down
3 changes: 1 addition & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ module.exports = {
'unicorn/prefer-array-some': 'off', // https://github.com/sindresorhus/eslint-plugin-unicorn/issues/2007
'unicorn/prefer-module': 'off',
'unicorn/prevent-abbreviations': 'off',
'unicorn/switch-case-braces': ['error', 'avoid'],
'wrap-iife': ['error', 'inside'],

// TODO rules to be removed/fixed in v2.10.0 as fixes are not compatible with IE11
Expand Down Expand Up @@ -113,7 +112,7 @@ module.exports = {
'vars-on-top': 'off',

// TODO rules with a lot of errors to be fixed manually, fix in a separate PR
eqeqeq: 'off', // about 300 errors to be fixed manually
eqeqeq: 'off', // about 20 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
Expand Down
32 changes: 16 additions & 16 deletions src/definitions/behaviors/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
'use strict';

function isWindow(obj) {
return obj != null && obj === obj.window;
return obj !== null && obj === obj.window;
}

function isFunction(obj) {
Expand Down Expand Up @@ -117,7 +117,7 @@
$module
.on(triggerEvent + eventNamespace, module.event.trigger)
;
} else if (settings.on == 'now') {
} else if (settings.on === 'now') {
module.debug('Querying API endpoint immediately');
module.query();
}
Expand Down Expand Up @@ -296,7 +296,7 @@
},
loading: function () {
return module.request
? module.request.state() == 'pending'
? module.request.state() === 'pending'
: false;
},
abortedRequest: function (xhr) {
Expand Down Expand Up @@ -334,13 +334,13 @@
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 Down Expand Up @@ -478,7 +478,7 @@
while (nameKeys.length > 0) {
var k = nameKeys.pop();

if (k == '' && !Array.isArray(value)) { // foo[]
if (k === '' && !Array.isArray(value)) { // foo[]
value = build([], pushes[pushKey]++, value);
} else if (settings.regExp.fixed.test(k)) { // foo[n]
value = build([], k, value);
Expand Down Expand Up @@ -525,7 +525,7 @@
event: {
trigger: function (event) {
module.query();
if (event.type == 'submit' || event.type == 'click') {
if (event.type === 'submit' || event.type === 'click') {
event.preventDefault();
}
},
Expand Down Expand Up @@ -614,15 +614,15 @@
response = module.get.responseFromXHR(xhr),
errorMessage = module.get.errorFromRequest(response, status, httpMessage)
;
if (status == 'aborted') {
if (status === 'aborted') {
module.debug('XHR Aborted (Most likely caused by page navigation or CORS Policy)', status, httpMessage);
settings.onAbort.call(context, status, $module, xhr);

return true;
}
if (status == 'invalid') {
if (status === 'invalid') {
module.debug('JSON did not pass success test. A server-side error has most likely occurred', response);
} else if (status == 'error') {
} else if (status === 'error') {
if (xhr !== undefined) {
module.debug('XHR produced a server error', status, httpMessage);
// make sure we have an error to display to console
Expand Down Expand Up @@ -829,12 +829,12 @@
return data;
},
event: function () {
if (isWindow(element) || settings.on == 'now') {
if (isWindow(element) || settings.on === 'now') {
module.debug('API called without element, no events attached');

return false;
}
if (settings.on == 'auto') {
if (settings.on === 'auto') {
if ($module.is('input')) {
return element.oninput !== undefined
? 'input'
Expand Down Expand Up @@ -1004,17 +1004,17 @@
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
;
if ($.isPlainObject(object[camelCaseValue]) && (depth != maxDepth)) {
if ($.isPlainObject(object[camelCaseValue]) && (depth !== maxDepth)) {
object = object[camelCaseValue];
} else if (object[camelCaseValue] !== undefined) {
found = object[camelCaseValue];

return false;
} else if ($.isPlainObject(object[value]) && (depth != maxDepth)) {
} else if ($.isPlainObject(object[value]) && (depth !== maxDepth)) {
object = object[value];
} else if (object[value] !== undefined) {
found = object[value];
Expand Down
Loading

0 comments on commit 89e62cc

Please sign in to comment.