From 550c412ebcd9bc85f8934cfce67ebd4dd733e9cb Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 21 Sep 2022 17:50:33 +0800 Subject: [PATCH] Use optional chaining (#1913) --- rules/ast/is-empty-node.js | 2 +- rules/ast/is-static-require.js | 4 +--- rules/consistent-function-scoping.js | 13 +++++------ rules/custom-error-definition.js | 6 +++--- rules/filename-case.js | 2 +- rules/no-for-loop.js | 8 +++---- rules/no-keyword-prefix.js | 10 +++------ rules/no-thenable.js | 7 ++---- rules/no-unsafe-regex.js | 4 ++-- rules/no-unused-properties.js | 6 ++---- rules/no-useless-promise-resolve-reject.js | 6 +++--- rules/no-useless-undefined.js | 3 +-- rules/prefer-array-find.js | 6 ++---- rules/prefer-json-parse-buffer.js | 6 ++---- rules/prefer-keyboard-event-key.js | 25 +++++++--------------- rules/prefer-native-coercion-functions.js | 9 +++----- rules/prefer-negative-index.js | 1 - rules/prefer-number-properties.js | 2 +- rules/prefer-prototype-methods.js | 7 ++---- rules/prefer-set-has.js | 8 +------ rules/prefer-string-slice.js | 2 +- rules/prefer-ternary.js | 2 +- rules/relative-url-style.js | 6 ++---- rules/template-indent.js | 2 +- rules/text-encoding-identifier-case.js | 1 - rules/utils/assert-token.js | 2 +- rules/utils/avoid-capture.js | 2 +- rules/utils/boolean.js | 2 +- rules/utils/is-number.js | 6 ++---- rules/utils/is-shadowed.js | 3 +-- rules/utils/rule.js | 9 +++----- 31 files changed, 61 insertions(+), 111 deletions(-) diff --git a/rules/ast/is-empty-node.js b/rules/ast/is-empty-node.js index 833b8dfd60..01e5a44241 100644 --- a/rules/ast/is-empty-node.js +++ b/rules/ast/is-empty-node.js @@ -10,7 +10,7 @@ function isEmptyNode(node, additionalEmpty) { return true; } - if (additionalEmpty && additionalEmpty(node)) { + if (additionalEmpty?.(node)) { return true; } diff --git a/rules/ast/is-static-require.js b/rules/ast/is-static-require.js index 25d10f3356..e69b767687 100644 --- a/rules/ast/is-static-require.js +++ b/rules/ast/is-static-require.js @@ -3,9 +3,7 @@ const {isStringLiteral} = require('./literal.js'); const isStaticRequire = node => Boolean( - node - && node.type === 'CallExpression' - && node.callee + node?.type === 'CallExpression' && node.callee.type === 'Identifier' && node.callee.name === 'require' && !node.optional diff --git a/rules/consistent-function-scoping.js b/rules/consistent-function-scoping.js index 9140102560..ffeac98d4e 100644 --- a/rules/consistent-function-scoping.js +++ b/rules/consistent-function-scoping.js @@ -21,7 +21,7 @@ function checkReferences(scope, parent, scopeManager) { const [definition] = resolved.defs; // Skip recursive function name - if (definition && definition.type === 'FunctionName' && resolved.name === definition.name.name) { + if (definition?.type === 'FunctionName' && resolved.name === definition.name.name) { return false; } @@ -92,23 +92,20 @@ const reactHooks = [ ].flatMap(hookName => [hookName, `React.${hookName}`]); const isReactHook = scope => - scope.block - && scope.block.parent - && scope.block.parent.callee + scope.block?.parent?.callee && isNodeMatches(scope.block.parent.callee, reactHooks); const isArrowFunctionWithThis = scope => scope.type === 'function' - && scope.block - && scope.block.type === 'ArrowFunctionExpression' + && scope.block?.type === 'ArrowFunctionExpression' && (scope.thisFound || scope.childScopes.some(scope => isArrowFunctionWithThis(scope))); const iifeFunctionTypes = new Set([ 'FunctionExpression', 'ArrowFunctionExpression', ]); -const isIife = node => node - && iifeFunctionTypes.has(node.type) +const isIife = node => + iifeFunctionTypes.has(node.type) && node.parent.type === 'CallExpression' && node.parent.callee === node; diff --git a/rules/custom-error-definition.js b/rules/custom-error-definition.js index f9ee755112..8fe6b3f29b 100644 --- a/rules/custom-error-definition.js +++ b/rules/custom-error-definition.js @@ -137,15 +137,15 @@ function * customErrorDefinition(context, node) { if (!nameExpression) { const nameProperty = body.find(node => isPropertyDefinition(node, 'name')); - if (!nameProperty || !nameProperty.value || nameProperty.value.value !== name) { + if (!nameProperty?.value || nameProperty.value.value !== name) { yield { - node: nameProperty && nameProperty.value ? nameProperty.value : constructorBodyNode, + node: nameProperty?.value ? nameProperty.value : constructorBodyNode, message: `The \`name\` property should be set to \`${name}\`.`, }; } } else if (nameExpression.expression.right.value !== name) { yield { - node: nameExpression ? nameExpression.expression.right : constructorBodyNode, + node: nameExpression?.expression.right ?? constructorBodyNode, message: `The \`name\` property should be set to \`${name}\`.`, }; } diff --git a/rules/filename-case.js b/rules/filename-case.js index 55dd9e9ebb..8c0041cf6b 100644 --- a/rules/filename-case.js +++ b/rules/filename-case.js @@ -107,7 +107,7 @@ function splitFilename(filename) { for (const char of tailing) { const isIgnored = isIgnoredChar(char); - if (lastWord && lastWord.ignored === isIgnored) { + if (lastWord?.ignored === isIgnored) { lastWord.word += char; } else { lastWord = { diff --git a/rules/no-for-loop.js b/rules/no-for-loop.js index b6d7173921..6feb0176d6 100644 --- a/rules/no-for-loop.js +++ b/rules/no-for-loop.js @@ -16,7 +16,7 @@ const defaultElementName = 'element'; const isLiteralZero = node => isLiteral(node, 0); const isLiteralOne = node => isLiteral(node, 1); -const isIdentifierWithName = (node, name) => node && node.type === 'Identifier' && node.name === name; +const isIdentifierWithName = (node, name) => node?.type === 'Identifier' && node.name === name; const getIndexIdentifierName = forStatement => { const {init: variableDeclaration} = forStatement; @@ -103,7 +103,7 @@ const getArrayIdentifier = (forStatement, indexIdentifierName) => { }; const isLiteralOnePlusIdentifierWithName = (node, identifierName) => { - if (node && node.type === 'BinaryExpression' && node.operator === '+') { + if (node?.type === 'BinaryExpression' && node.operator === '+') { return (isIdentifierWithName(node.left, identifierName) && isLiteralOne(node.right)) || (isIdentifierWithName(node.right, identifierName) && isLiteralOne(node.left)); } @@ -335,8 +335,8 @@ const create = context => { return true; }); - const elementNode = elementReference && elementReference.identifier.parent.parent; - const elementIdentifierName = elementNode && elementNode.id.name; + const elementNode = elementReference?.identifier.parent.parent; + const elementIdentifierName = elementNode?.id.name; const elementVariable = elementIdentifierName && resolveIdentifierName(elementIdentifierName, bodyScope); const shouldFix = !someVariablesLeakOutOfTheLoop(node, [indexVariable, elementVariable].filter(Boolean), forScope); diff --git a/rules/no-keyword-prefix.js b/rules/no-keyword-prefix.js index 42444dce96..17bdd8edc2 100644 --- a/rules/no-keyword-prefix.js +++ b/rules/no-keyword-prefix.js @@ -63,7 +63,7 @@ function checkObjectPattern(report, node, options) { report(node, keyword); } - // Prevent checking righthand side of destructured object + // Prevent checking right hand side of destructured object if (parent.key === node && parent.value !== node) { return true; } @@ -113,7 +113,7 @@ const create = context => { parent.type === 'Property' || parent.type === 'AssignmentPattern' ) { - if (parent.parent && parent.parent.type === 'ObjectPattern') { + if (parent.parent.type === 'ObjectPattern') { const finished = checkObjectPattern(report, node, options); if (finished) { return; @@ -145,11 +145,7 @@ const create = context => { ].includes(parent.type) ) { // Report only if the local imported identifier is invalid - if ( - Boolean(keyword) - && parent.local - && parent.local.name === name - ) { + if (Boolean(keyword) && parent.local?.name === name) { report(node, keyword); } diff --git a/rules/no-thenable.js b/rules/no-thenable.js index 5de7febaf4..990c12c7b0 100644 --- a/rules/no-thenable.js +++ b/rules/no-thenable.js @@ -11,11 +11,8 @@ const messages = { [MESSAGE_ID_CLASS]: 'Do not add `then` to a class.', }; -const isStringThen = (node, context) => { - const result = getStaticValue(node, context.getScope()); - - return result && result.value === 'then'; -}; +const isStringThen = (node, context) => + getStaticValue(node, context.getScope())?.value === 'then'; const cases = [ // `{then() {}}`, diff --git a/rules/no-unsafe-regex.js b/rules/no-unsafe-regex.js index 0826187cfa..143fd982c9 100644 --- a/rules/no-unsafe-regex.js +++ b/rules/no-unsafe-regex.js @@ -38,12 +38,12 @@ const create = () => ({ let flags; if (hasRegExp) { ({pattern} = arguments_[0].regex); - flags = arguments_[1] && arguments_[1].type === 'Literal' + flags = arguments_[1]?.type === 'Literal' ? arguments_[1].value : arguments_[0].regex.flags; } else { pattern = arguments_[0].value; - flags = arguments_[1] && arguments_[1].type === 'Literal' + flags = arguments_[1]?.type === 'Literal' ? arguments_[1].value : ''; } diff --git a/rules/no-unused-properties.js b/rules/no-unused-properties.js index 128044d551..35b7d64647 100644 --- a/rules/no-unused-properties.js +++ b/rules/no-unused-properties.js @@ -11,13 +11,11 @@ const getDeclaratorOrPropertyValue = declaratorOrProperty => || declaratorOrProperty.value; const isMemberExpressionCall = memberExpression => - memberExpression.parent - && memberExpression.parent.type === 'CallExpression' + memberExpression.parent.type === 'CallExpression' && memberExpression.parent.callee === memberExpression; const isMemberExpressionAssignment = memberExpression => - memberExpression.parent - && memberExpression.parent.type === 'AssignmentExpression'; + memberExpression.parent.type === 'AssignmentExpression'; const isMemberExpressionComputedBeyondPrediction = memberExpression => memberExpression.computed diff --git a/rules/no-useless-promise-resolve-reject.js b/rules/no-useless-promise-resolve-reject.js index 510ce3da57..c0e9413a77 100644 --- a/rules/no-useless-promise-resolve-reject.js +++ b/rules/no-useless-promise-resolve-reject.js @@ -101,7 +101,7 @@ function fix(callExpression, isInTryStatement, sourceCode) { } const {callee, parent, arguments: [errorOrValue]} = callExpression; - if (errorOrValue && errorOrValue.type === 'SpreadElement') { + if (errorOrValue?.type === 'SpreadElement') { return; } @@ -122,7 +122,7 @@ function fix(callExpression, isInTryStatement, sourceCode) { let text = errorOrValue ? sourceCode.getText(errorOrValue) : ''; - if (errorOrValue && errorOrValue.type === 'SequenceExpression') { + if (errorOrValue?.type === 'SequenceExpression') { text = `(${text})`; } @@ -155,7 +155,7 @@ function fix(callExpression, isInTryStatement, sourceCode) { } else if (parent.type === 'ReturnStatement') { text = `return${text ? ' ' : ''}${text};`; } else { - if (errorOrValue && errorOrValue.type === 'ObjectExpression') { + if (errorOrValue?.type === 'ObjectExpression') { text = `(${text})`; } diff --git a/rules/no-useless-undefined.js b/rules/no-useless-undefined.js index 6b9af3bf32..5579442a3a 100644 --- a/rules/no-useless-undefined.js +++ b/rules/no-useless-undefined.js @@ -63,7 +63,6 @@ const shouldIgnore = node => { } else if ( node.type === 'MemberExpression' && node.computed === false - && node.property && node.property.type === 'Identifier' ) { name = node.property.name; @@ -104,7 +103,7 @@ const create = context => { const listener = (fix, checkFunctionReturnType) => node => { if (checkFunctionReturnType) { const functionNode = getFunction(context.getScope()); - if (functionNode && functionNode.returnType) { + if (functionNode?.returnType) { return; } } diff --git a/rules/prefer-array-find.js b/rules/prefer-array-find.js index efbb3986e6..61e16436f3 100644 --- a/rules/prefer-array-find.js +++ b/rules/prefer-array-find.js @@ -239,11 +239,10 @@ const fixDestructuringAndReplaceFilter = (sourceCode, node) => { }; const isAccessingZeroIndex = node => - node.parent - && node.parent.type === 'MemberExpression' + node.parent.type === 'MemberExpression' && node.parent.computed === true && node.parent.object === node - && node.parent.property?.type === 'Literal' + && node.parent.property.type === 'Literal' && node.parent.property.raw === '0'; const isDestructuringFirstElement = node => { @@ -252,7 +251,6 @@ const isDestructuringFirstElement = node => { && right && right === node && left.type === 'ArrayPattern' - && left.elements && left.elements.length === 1 && left.elements[0].type !== 'RestElement'; }; diff --git a/rules/prefer-json-parse-buffer.js b/rules/prefer-json-parse-buffer.js index 1fc62d5490..03364c036c 100644 --- a/rules/prefer-json-parse-buffer.js +++ b/rules/prefer-json-parse-buffer.js @@ -59,10 +59,8 @@ function getIdentifierDeclaration(node, scope) { return getIdentifierDeclaration(identifier.parent.init, variable.scope); } -const isUtf8EncodingStringNode = (node, scope) => { - const staticValue = getStaticValue(node, scope); - return staticValue && isUtf8EncodingString(staticValue.value); -}; +const isUtf8EncodingStringNode = (node, scope) => + isUtf8EncodingString(getStaticValue(node, scope)?.value); const isUtf8EncodingString = value => { if (typeof value !== 'string') { diff --git a/rules/prefer-keyboard-event-key.js b/rules/prefer-keyboard-event-key.js index 5e5010ff8e..9270cd15d1 100644 --- a/rules/prefer-keyboard-event-key.js +++ b/rules/prefer-keyboard-event-key.js @@ -15,23 +15,20 @@ const keys = new Set([ ]); const isPropertyNamedAddEventListener = node => - node - && node.type === 'CallExpression' - && node.callee + node?.type === 'CallExpression' && node.callee.type === 'MemberExpression' - && node.callee.property && node.callee.property.name === 'addEventListener'; const getEventNodeAndReferences = (context, node) => { const eventListener = getMatchingAncestorOfType(node, 'CallExpression', isPropertyNamedAddEventListener); - const callback = eventListener && eventListener.arguments && eventListener.arguments[1]; - switch (callback && callback.type) { + const callback = eventListener?.arguments[1]; + switch (callback?.type) { case 'ArrowFunctionExpression': case 'FunctionExpression': { const eventVariable = context.getDeclaredVariables(callback)[0]; - const references = eventVariable && eventVariable.references; + const references = eventVariable?.references; return { - event: callback.params && callback.params[0], + event: callback.params[0], references, }; } @@ -43,10 +40,7 @@ const getEventNodeAndReferences = (context, node) => { }; const isPropertyOf = (node, eventNode) => - node - && node.parent - && node.parent.type === 'MemberExpression' - && node.parent.object + node?.parent?.type === 'MemberExpression' && node.parent.object === eventNode; // The third argument is a condition function, as one passed to `Array#filter()` @@ -133,7 +127,7 @@ const create = context => ({ Property(node) { // Destructured case - const propertyName = node.value && node.value.name; + const propertyName = node.value.name; if (!keys.has(propertyName)) { return; } @@ -147,10 +141,7 @@ const create = context => ({ node, 'VariableDeclarator', ); - const initObject - = nearestVariableDeclarator - && nearestVariableDeclarator.init - && nearestVariableDeclarator.init; + const initObject = nearestVariableDeclarator?.init; // Make sure initObject is a reference of eventVariable if ( diff --git a/rules/prefer-native-coercion-functions.js b/rules/prefer-native-coercion-functions.js index 3054bc97ac..4c6a9c148a 100644 --- a/rules/prefer-native-coercion-functions.js +++ b/rules/prefer-native-coercion-functions.js @@ -11,13 +11,11 @@ const nativeCoercionFunctionNames = new Set(['String', 'Number', 'BigInt', 'Bool const arrayMethodsWithBooleanCallback = new Set(['every', 'filter', 'find', 'findLast', 'findIndex', 'findLastIndex', 'some']); const isNativeCoercionFunctionCall = (node, firstArgumentName) => - node - && node.type === 'CallExpression' + node?.type === 'CallExpression' && !node.optional && node.callee.type === 'Identifier' && nativeCoercionFunctionNames.has(node.callee.name) - && node.arguments[0] - && node.arguments[0].type === 'Identifier' + && node.arguments[0]?.type === 'Identifier' && node.arguments[0].name === firstArgumentName; const isIdentityFunction = node => @@ -33,8 +31,7 @@ const isIdentityFunction = node => node.body.type === 'BlockStatement' && node.body.body.length === 1 && node.body.body[0].type === 'ReturnStatement' - && node.body.body[0].argument - && node.body.body[0].argument.type === 'Identifier' + && node.body.body[0].argument?.type === 'Identifier' && node.body.body[0].argument.name === node.params[0].name ); diff --git a/rules/prefer-negative-index.js b/rules/prefer-negative-index.js index 72bc032ca1..986b4e9f33 100644 --- a/rules/prefer-negative-index.js +++ b/rules/prefer-negative-index.js @@ -54,7 +54,6 @@ const getMemberName = node => { if ( type === 'MemberExpression' - && property && property.type === 'Identifier' ) { return property.name; diff --git a/rules/prefer-number-properties.js b/rules/prefer-number-properties.js index a0e0fcd955..c8081f4e23 100644 --- a/rules/prefer-number-properties.js +++ b/rules/prefer-number-properties.js @@ -24,7 +24,7 @@ const globalObjects = { const isNegative = node => { const {parent} = node; - return parent && parent.type === 'UnaryExpression' && parent.operator === '-' && parent.argument === node; + return parent.type === 'UnaryExpression' && parent.operator === '-' && parent.argument === node; }; function checkProperty({node, path: [name]}, sourceCode) { diff --git a/rules/prefer-prototype-methods.js b/rules/prefer-prototype-methods.js index d1ff985fa5..82289e6f0c 100644 --- a/rules/prefer-prototype-methods.js +++ b/rules/prefer-prototype-methods.js @@ -51,11 +51,8 @@ function create(context) { yield fixer.replaceText(node.object, `${constructorName}.prototype`); if ( - node.object - && ( - node.object.type === 'ArrayExpression' - || node.object.type === 'ObjectExpression' - ) + node.object.type === 'ArrayExpression' + || node.object.type === 'ObjectExpression' ) { yield * fixSpaceAroundKeyword(fixer, node.parent.parent, context.getSourceCode()); } diff --git a/rules/prefer-set-has.js b/rules/prefer-set-has.js index 01d38ee2e3..2361171b80 100644 --- a/rules/prefer-set-has.js +++ b/rules/prefer-set-has.js @@ -75,16 +75,10 @@ const selector = [ ].join(''); const isIncludesCall = node => { - /* c8 ignore next 3 */ - if (!node.parent || !node.parent.parent) { - return false; - } - - const {type, optional, callee, arguments: includesArguments} = node.parent.parent; + const {type, optional, callee, arguments: includesArguments} = node.parent.parent ?? {}; return ( type === 'CallExpression' && !optional - && callee && callee.type === 'MemberExpression' && !callee.computed && !callee.optional diff --git a/rules/prefer-string-slice.js b/rules/prefer-string-slice.js index e1f89e0cc7..61438c5d98 100644 --- a/rules/prefer-string-slice.js +++ b/rules/prefer-string-slice.js @@ -51,7 +51,7 @@ function * fixSubstrArguments({node, fixer, context, abort}) { const secondArgumentRange = getParenthesizedRange(secondArgument, sourceCode); const replaceSecondArgument = text => replaceArgument(fixer, secondArgument, text, sourceCode); - if (firstArgumentStaticResult && firstArgumentStaticResult.value === 0) { + if (firstArgumentStaticResult?.value === 0) { if (isNumberLiteral(secondArgument) || isLengthProperty(secondArgument)) { return; } diff --git a/rules/prefer-ternary.js b/rules/prefer-ternary.js index b9226de0ea..eaf22dcfa6 100644 --- a/rules/prefer-ternary.js +++ b/rules/prefer-ternary.js @@ -19,7 +19,7 @@ const selector = [ '[alternate]', ].join(''); -const isTernary = node => node && node.type === 'ConditionalExpression'; +const isTernary = node => node?.type === 'ConditionalExpression'; function getNodeBody(node) { /* c8 ignore next 3 */ diff --git a/rules/relative-url-style.js b/rules/relative-url-style.js index 00aaf13267..986e692a50 100644 --- a/rules/relative-url-style.js +++ b/rules/relative-url-style.js @@ -47,8 +47,7 @@ function canAddDotSlash(node, context) { const staticValueResult = getStaticValue(baseNode, context.getScope()); if ( - staticValueResult - && typeof staticValueResult.value === 'string' + typeof staticValueResult?.value === 'string' && isSafeToAddDotSlash(url, [staticValueResult.value]) ) { return true; @@ -67,8 +66,7 @@ function canRemoveDotSlash(node, context) { const staticValueResult = getStaticValue(baseNode, context.getScope()); if ( - staticValueResult - && typeof staticValueResult.value === 'string' + typeof staticValueResult?.value === 'string' && isSafeToRemoveDotSlash(node.value, [staticValueResult.value]) ) { return true; diff --git a/rules/template-indent.js b/rules/template-indent.js index 7b263fc49e..9e14ab4330 100644 --- a/rules/template-indent.js +++ b/rules/template-indent.js @@ -91,7 +91,7 @@ const create = context => { TemplateLiteral(node) { if (options.comments.length > 0) { const previousToken = sourceCode.getTokenBefore(node, {includeComments: true}); - if (previousToken && previousToken.type === 'Block' && options.comments.includes(previousToken.value.trim().toLowerCase())) { + if (previousToken?.type === 'Block' && options.comments.includes(previousToken.value.trim().toLowerCase())) { indentTemplateLiteralNode(node); return; } diff --git a/rules/text-encoding-identifier-case.js b/rules/text-encoding-identifier-case.js index c7490a8ea0..cec607971f 100644 --- a/rules/text-encoding-identifier-case.js +++ b/rules/text-encoding-identifier-case.js @@ -28,7 +28,6 @@ const isFsReadFileEncoding = node => node.parent.type === 'CallExpression' && !node.parent.optional && node.parent.arguments[1] === node - && node.parent.arguments[0] && node.parent.arguments[0].type !== 'SpreadElement' && node.parent.callee.type === 'MemberExpression' && !node.parent.callee.optional diff --git a/rules/utils/assert-token.js b/rules/utils/assert-token.js index 73f9f25979..13d1aa7e5d 100644 --- a/rules/utils/assert-token.js +++ b/rules/utils/assert-token.js @@ -2,7 +2,7 @@ const ISSUE_LINK_PREFIX = 'https://github.com/sindresorhus/eslint-plugin-unicorn/issues/new?'; function assertToken(token, {test, expected, ruleId}) { - if (test && test(token)) { + if (test?.(token)) { return; } diff --git a/rules/utils/avoid-capture.js b/rules/utils/avoid-capture.js index c80b6c311f..9a1f9faa42 100644 --- a/rules/utils/avoid-capture.js +++ b/rules/utils/avoid-capture.js @@ -99,7 +99,7 @@ function unicorn() { ``` */ const isUnresolvedName = (name, scope) => - getReferences(scope).some(({identifier, resolved}) => identifier && identifier.name === name && !resolved); + getReferences(scope).some(({identifier, resolved}) => identifier?.name === name && !resolved); const isSafeName = (name, scopes) => !scopes.some(scope => resolveVariableName(name, scope) || isUnresolvedName(name, scope)); diff --git a/rules/utils/boolean.js b/rules/utils/boolean.js index d28621d14d..41ebdaab12 100644 --- a/rules/utils/boolean.js +++ b/rules/utils/boolean.js @@ -7,7 +7,7 @@ const isLogicNotArgument = node => isLogicNot(node.parent) && node.parent.argume const isBooleanCallArgument = node => isBooleanCall(node.parent) && node.parent.arguments[0] === node; const isBooleanCall = node => node?.type === 'CallExpression' - && node.callee?.type === 'Identifier' + && node.callee.type === 'Identifier' && node.callee.name === 'Boolean' && node.arguments.length === 1; const isVueBooleanAttributeValue = node => diff --git a/rules/utils/is-number.js b/rules/utils/is-number.js index 1de04b0ea5..a541b9c348 100644 --- a/rules/utils/is-number.js +++ b/rules/utils/is-number.js @@ -101,10 +101,8 @@ const isNumberMethodCall = node => && isStaticProperties(node.callee, 'Number', numberMethods); const isGlobalParseToNumberFunctionCall = node => isFunctionCall(node, 'parseInt') || isFunctionCall(node, 'parseFloat'); -const isStaticNumber = (node, scope) => { - const staticResult = getStaticValue(node, scope); - return staticResult !== null && typeof staticResult.value === 'number'; -}; +const isStaticNumber = (node, scope) => + typeof getStaticValue(node, scope)?.value === 'number'; const isLengthProperty = node => node.type === 'MemberExpression' diff --git a/rules/utils/is-shadowed.js b/rules/utils/is-shadowed.js index cf7ae4ca0e..20c3b517d9 100644 --- a/rules/utils/is-shadowed.js +++ b/rules/utils/is-shadowed.js @@ -25,8 +25,7 @@ function isShadowed(scope, node) { const reference = findReference(scope, node); return ( - reference - && reference.resolved + reference?.resolved && reference.resolved.defs.length > 0 ); } diff --git a/rules/utils/rule.js b/rules/utils/rule.js index 411c95b8e4..60b68b9aa6 100644 --- a/rules/utils/rule.js +++ b/rules/utils/rule.js @@ -3,7 +3,7 @@ const path = require('node:path'); const fs = require('node:fs'); const getDocumentationUrl = require('./get-documentation-url.js'); -const isIterable = object => typeof object[Symbol.iterator] === 'function'; +const isIterable = object => typeof object?.[Symbol.iterator] === 'function'; class FixAbortError extends Error {} const fixOptions = { @@ -16,7 +16,7 @@ function wrapFixFunction(fix) { return fixer => { const result = fix(fixer, fixOptions); - if (result && isIterable(result)) { + if (isIterable(result)) { try { return [...result]; } catch (error) { @@ -103,10 +103,7 @@ function checkVueTemplate(create, options) { const listeners = create(context); // `vue-eslint-parser` - if ( - context.parserServices - && context.parserServices.defineTemplateBodyVisitor - ) { + if (context.parserServices?.defineTemplateBodyVisitor) { return visitScriptBlock ? context.parserServices.defineTemplateBodyVisitor(listeners, listeners) : context.parserServices.defineTemplateBodyVisitor(listeners);