Skip to content

Commit

Permalink
Merge getPropertyName and getKeyName (#1671)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored Dec 29, 2021
1 parent 784c7a8 commit 021aa9b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 63 deletions.
4 changes: 2 additions & 2 deletions rules/no-document-cookie.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
const getPropertyName = require('./utils/get-property-name.js');
const getKeyName = require('./utils/get-key-name.js');

const MESSAGE_ID = 'no-document-cookie';
const messages = {
Expand All @@ -17,7 +17,7 @@ const selector = [
/** @param {import('eslint').Rule.RuleContext} context */
const create = context => ({
[selector](node) {
if (getPropertyName(node, context.getScope()) !== 'cookie') {
if (getKeyName(node, context.getScope()) !== 'cookie') {
return;
}

Expand Down
3 changes: 1 addition & 2 deletions rules/no-thenable.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
const {getStaticValue} = require('eslint-utils');
const {methodCallSelector} = require('./selectors/index.js');
const getPropertyName = require('./utils/get-property-name.js');
const getKeyName = require('./utils/get-key-name.js');

const MESSAGE_ID_OBJECT = 'no-thenable-object';
Expand Down Expand Up @@ -42,7 +41,7 @@ const cases = [
// `foo[computedKey] = …`
{
selector: 'AssignmentExpression > MemberExpression.left > .property',
test: (node, context) => getPropertyName(node.parent, context.getScope()) === 'then',
test: (node, context) => getKeyName(node.parent, context.getScope()) === 'then',
messageId: MESSAGE_ID_OBJECT,
},
// `Object.defineProperty(foo, 'then', …)`
Expand Down
4 changes: 2 additions & 2 deletions rules/prefer-prototype-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const {
emptyArraySelector,
matches,
} = require('./selectors/index.js');
const getPropertyName = require('./utils/get-property-name.js');
const getKeyName = require('./utils/get-key-name.js');
const {fixSpaceAroundKeyword} = require('./fix/index.js');

const messages = {
Expand Down Expand Up @@ -41,7 +41,7 @@ function create(context) {
return {
[selector](node) {
const constructorName = node.object.type === 'ArrayExpression' ? 'Array' : 'Object';
const methodName = getPropertyName(node, context.getScope());
const methodName = getKeyName(node, context.getScope());

return {
node,
Expand Down
10 changes: 5 additions & 5 deletions rules/prefer-reflect-apply.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
const isLiteralValue = require('./utils/is-literal-value.js');
const getPropertyName = require('./utils/get-property-name.js');
const getKeyName = require('./utils/get-key-name.js');
const {not, methodCallSelector} = require('./selectors/index.js');

const MESSAGE_ID = 'prefer-reflect-apply';
Expand Down Expand Up @@ -31,7 +31,7 @@ const getReflectApplyCall = (sourceCode, target, receiver, argumentsList) => (

const fixDirectApplyCall = (node, sourceCode) => {
if (
getPropertyName(node.callee) === 'apply'
getKeyName(node.callee) === 'apply'
&& node.arguments.length === 2
&& isApplySignature(node.arguments[0], node.arguments[1])
) {
Expand All @@ -46,9 +46,9 @@ const fixDirectApplyCall = (node, sourceCode) => {

const fixFunctionPrototypeCall = (node, sourceCode) => {
if (
getPropertyName(node.callee) === 'call'
&& getPropertyName(node.callee.object) === 'apply'
&& getPropertyName(node.callee.object.object) === 'prototype'
getKeyName(node.callee) === 'call'
&& getKeyName(node.callee.object) === 'apply'
&& getKeyName(node.callee.object.object) === 'prototype'
&& node.callee.object.object.object
&& node.callee.object.object.object.type === 'Identifier'
&& node.callee.object.object.object.name === 'Function'
Expand Down
45 changes: 23 additions & 22 deletions rules/utils/get-key-name.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
'use strict';
const {getStaticValue} = require('eslint-utils');

// TODO[@fisker]: Merge this with `./get-property-name.js`
function getKeyOrPropertyName(keyOrProperty, computed, scope) {
if (!computed) {
if (keyOrProperty.type === 'Identifier') {
return keyOrProperty.name;
}

/* istanbul ignore next: It could be `PrivateIdentifier`(ESTree) or `PrivateName`(Babel) when it's in `class` */
return;
}

const result = getStaticValue(keyOrProperty, scope);
return result && result.value;
}

/**
Get the key value of a node.
Expand All @@ -10,28 +22,17 @@ Get the key value of a 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, key, computed} = node;

/* istanbul ignore next - Prevent unexpected case */
if (
type !== 'Property'
&& type !== 'PropertyDefinition'
&& type !== 'MethodDefinition'
) {
return;
}

if (!computed) {
if (key.type === 'Identifier') {
return key.name;
}

/* istanbul ignore next: It could be `PrivateIdentifier`(ESTree) or `PrivateName`(Babel) when it's in `class` */
return;
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
}

const result = getStaticValue(key, scope);
return result && result.value;
}

module.exports = getKeyName;
30 changes: 0 additions & 30 deletions rules/utils/get-property-name.js

This file was deleted.

0 comments on commit 021aa9b

Please sign in to comment.