From 70dcc973e3dcbbe592f2ebddabd9228425be6240 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 17 Feb 2022 11:36:15 +0800 Subject: [PATCH] Replace `getKeyName` with utility from `eslint-utils` (#1732) --- rules/no-document-cookie.js | 4 ++-- rules/no-thenable.js | 9 ++++--- rules/prefer-json-parse-buffer.js | 7 +++--- rules/prefer-prototype-methods.js | 4 ++-- rules/prefer-reflect-apply.js | 10 ++++---- rules/utils/get-key-name.js | 39 ------------------------------- 6 files changed, 16 insertions(+), 57 deletions(-) delete mode 100644 rules/utils/get-key-name.js diff --git a/rules/no-document-cookie.js b/rules/no-document-cookie.js index c4e7d817e2..799e753d20 100644 --- a/rules/no-document-cookie.js +++ b/rules/no-document-cookie.js @@ -1,5 +1,5 @@ 'use strict'; -const getKeyName = require('./utils/get-key-name.js'); +const {getPropertyName} = require('eslint-utils'); const MESSAGE_ID = 'no-document-cookie'; const messages = { @@ -17,7 +17,7 @@ const selector = [ /** @param {import('eslint').Rule.RuleContext} context */ const create = context => ({ [selector](node) { - if (getKeyName(node, context.getScope()) !== 'cookie') { + if (getPropertyName(node, context.getScope()) !== 'cookie') { return; } diff --git a/rules/no-thenable.js b/rules/no-thenable.js index 3a49f3d509..5de7febaf4 100644 --- a/rules/no-thenable.js +++ b/rules/no-thenable.js @@ -1,7 +1,6 @@ 'use strict'; -const {getStaticValue} = require('eslint-utils'); +const {getStaticValue, getPropertyName} = require('eslint-utils'); const {methodCallSelector} = require('./selectors/index.js'); -const getKeyName = require('./utils/get-key-name.js'); const MESSAGE_ID_OBJECT = 'no-thenable-object'; const MESSAGE_ID_EXPORT = 'no-thenable-export'; @@ -25,7 +24,7 @@ const cases = [ // `{get [computedKey]() {}}`, { selector: 'ObjectExpression > Property.properties > .key', - test: (node, context) => getKeyName(node.parent, context.getScope()) === 'then', + test: (node, context) => getPropertyName(node.parent, context.getScope()) === 'then', messageId: MESSAGE_ID_OBJECT, }, // `class Foo {then}`, @@ -34,14 +33,14 @@ const cases = [ // `class Foo {static get then() {}}`, { selector: ':matches(PropertyDefinition, MethodDefinition) > .key', - test: (node, context) => getKeyName(node.parent, context.getScope()) === 'then', + test: (node, context) => getPropertyName(node.parent, context.getScope()) === 'then', messageId: MESSAGE_ID_CLASS, }, // `foo.then = …` // `foo[computedKey] = …` { selector: 'AssignmentExpression > MemberExpression.left > .property', - test: (node, context) => getKeyName(node.parent, context.getScope()) === 'then', + test: (node, context) => getPropertyName(node.parent, context.getScope()) === 'then', messageId: MESSAGE_ID_OBJECT, }, // `Object.defineProperty(foo, 'then', …)` diff --git a/rules/prefer-json-parse-buffer.js b/rules/prefer-json-parse-buffer.js index 96770feadd..7b846dfcec 100644 --- a/rules/prefer-json-parse-buffer.js +++ b/rules/prefer-json-parse-buffer.js @@ -1,8 +1,7 @@ 'use strict'; -const {findVariable, getStaticValue} = require('eslint-utils'); +const {findVariable, getStaticValue, getPropertyName} = require('eslint-utils'); const {methodCallSelector} = require('./selectors/index.js'); const {removeArgument} = require('./fix/index.js'); -const getKeyName = require('./utils/get-key-name.js'); const MESSAGE_ID = 'prefer-json-parse-buffer'; const messages = { @@ -80,7 +79,7 @@ function isUtf8Encoding(node, scope) { node.type === 'ObjectExpression' && node.properties.length === 1 && node.properties[0].type === 'Property' - && getKeyName(node.properties[0], scope) === 'encoding' + && getPropertyName(node.properties[0], scope) === 'encoding' && isUtf8EncodingStringNode(node.properties[0].value, scope) ) { return true; @@ -126,7 +125,7 @@ const create = context => ({ return; } - const method = getKeyName(node.callee, scope); + const method = getPropertyName(node.callee, scope); if (method !== 'readFile' && method !== 'readFileSync') { return; } diff --git a/rules/prefer-prototype-methods.js b/rules/prefer-prototype-methods.js index b987322d91..23bb156307 100644 --- a/rules/prefer-prototype-methods.js +++ b/rules/prefer-prototype-methods.js @@ -1,11 +1,11 @@ 'use strict'; +const {getPropertyName} = require('eslint-utils'); const { methodCallSelector, emptyObjectSelector, emptyArraySelector, matches, } = require('./selectors/index.js'); -const getKeyName = require('./utils/get-key-name.js'); const {fixSpaceAroundKeyword} = require('./fix/index.js'); const messages = { @@ -41,7 +41,7 @@ function create(context) { return { [selector](node) { const constructorName = node.object.type === 'ArrayExpression' ? 'Array' : 'Object'; - const methodName = getKeyName(node, context.getScope()); + const methodName = getPropertyName(node, context.getScope()); return { node, diff --git a/rules/prefer-reflect-apply.js b/rules/prefer-reflect-apply.js index ea40bd1afd..b0965982e6 100644 --- a/rules/prefer-reflect-apply.js +++ b/rules/prefer-reflect-apply.js @@ -1,6 +1,6 @@ 'use strict'; +const {getPropertyName} = require('eslint-utils'); const isLiteralValue = require('./utils/is-literal-value.js'); -const getKeyName = require('./utils/get-key-name.js'); const {not, methodCallSelector} = require('./selectors/index.js'); const MESSAGE_ID = 'prefer-reflect-apply'; @@ -31,7 +31,7 @@ const getReflectApplyCall = (sourceCode, target, receiver, argumentsList) => ( const fixDirectApplyCall = (node, sourceCode) => { if ( - getKeyName(node.callee) === 'apply' + getPropertyName(node.callee) === 'apply' && node.arguments.length === 2 && isApplySignature(node.arguments[0], node.arguments[1]) ) { @@ -46,9 +46,9 @@ const fixDirectApplyCall = (node, sourceCode) => { const fixFunctionPrototypeCall = (node, sourceCode) => { if ( - getKeyName(node.callee) === 'call' - && getKeyName(node.callee.object) === 'apply' - && getKeyName(node.callee.object.object) === 'prototype' + getPropertyName(node.callee) === 'call' + && getPropertyName(node.callee.object) === 'apply' + && getPropertyName(node.callee.object.object) === 'prototype' && node.callee.object.object.object && node.callee.object.object.object.type === 'Identifier' && node.callee.object.object.object.name === 'Function' diff --git a/rules/utils/get-key-name.js b/rules/utils/get-key-name.js deleted file mode 100644 index 10bbba3f76..0000000000 --- a/rules/utils/get-key-name.js +++ /dev/null @@ -1,39 +0,0 @@ -'use strict'; -const {getStaticValue} = require('eslint-utils'); - -function getKeyOrPropertyName(keyOrProperty, computed, scope) { - if (!computed) { - if (keyOrProperty.type === 'Identifier') { - return keyOrProperty.name; - } - - // It could be `PrivateIdentifier`(ESTree) or `PrivateName`(Babel) when it's in `class` - /* c8 ignore next */ - return; - } - - const result = getStaticValue(keyOrProperty, scope); - return result && result.value; -} - -/** -Get the key value of a node. - -@param {Node} node - The node. -@param {Scope} [scope] - The scope to start finding the variable. Optional. If this scope was given, it tries to resolve identifier references which are in the given node as much as possible. -*/ -function getKeyName(node, scope) { - const {type, computed} = node; - - switch (type) { - case 'MemberExpression': - return getKeyOrPropertyName(node.property, computed, scope); - case 'Property': - case 'PropertyDefinition': - case 'MethodDefinition': - return getKeyOrPropertyName(node.key, computed, scope); - // No default - } -} - -module.exports = getKeyName;