Skip to content

Commit 021aa9b

Browse files
authored
Merge getPropertyName and getKeyName (#1671)
1 parent 784c7a8 commit 021aa9b

File tree

6 files changed

+33
-63
lines changed

6 files changed

+33
-63
lines changed

rules/no-document-cookie.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
const getPropertyName = require('./utils/get-property-name.js');
2+
const getKeyName = require('./utils/get-key-name.js');
33

44
const MESSAGE_ID = 'no-document-cookie';
55
const messages = {
@@ -17,7 +17,7 @@ const selector = [
1717
/** @param {import('eslint').Rule.RuleContext} context */
1818
const create = context => ({
1919
[selector](node) {
20-
if (getPropertyName(node, context.getScope()) !== 'cookie') {
20+
if (getKeyName(node, context.getScope()) !== 'cookie') {
2121
return;
2222
}
2323

rules/no-thenable.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22
const {getStaticValue} = require('eslint-utils');
33
const {methodCallSelector} = require('./selectors/index.js');
4-
const getPropertyName = require('./utils/get-property-name.js');
54
const getKeyName = require('./utils/get-key-name.js');
65

76
const MESSAGE_ID_OBJECT = 'no-thenable-object';
@@ -42,7 +41,7 @@ const cases = [
4241
// `foo[computedKey] = …`
4342
{
4443
selector: 'AssignmentExpression > MemberExpression.left > .property',
45-
test: (node, context) => getPropertyName(node.parent, context.getScope()) === 'then',
44+
test: (node, context) => getKeyName(node.parent, context.getScope()) === 'then',
4645
messageId: MESSAGE_ID_OBJECT,
4746
},
4847
// `Object.defineProperty(foo, 'then', …)`

rules/prefer-prototype-methods.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const {
55
emptyArraySelector,
66
matches,
77
} = require('./selectors/index.js');
8-
const getPropertyName = require('./utils/get-property-name.js');
8+
const getKeyName = require('./utils/get-key-name.js');
99
const {fixSpaceAroundKeyword} = require('./fix/index.js');
1010

1111
const messages = {
@@ -41,7 +41,7 @@ function create(context) {
4141
return {
4242
[selector](node) {
4343
const constructorName = node.object.type === 'ArrayExpression' ? 'Array' : 'Object';
44-
const methodName = getPropertyName(node, context.getScope());
44+
const methodName = getKeyName(node, context.getScope());
4545

4646
return {
4747
node,

rules/prefer-reflect-apply.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22
const isLiteralValue = require('./utils/is-literal-value.js');
3-
const getPropertyName = require('./utils/get-property-name.js');
3+
const getKeyName = require('./utils/get-key-name.js');
44
const {not, methodCallSelector} = require('./selectors/index.js');
55

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

3232
const fixDirectApplyCall = (node, sourceCode) => {
3333
if (
34-
getPropertyName(node.callee) === 'apply'
34+
getKeyName(node.callee) === 'apply'
3535
&& node.arguments.length === 2
3636
&& isApplySignature(node.arguments[0], node.arguments[1])
3737
) {
@@ -46,9 +46,9 @@ const fixDirectApplyCall = (node, sourceCode) => {
4646

4747
const fixFunctionPrototypeCall = (node, sourceCode) => {
4848
if (
49-
getPropertyName(node.callee) === 'call'
50-
&& getPropertyName(node.callee.object) === 'apply'
51-
&& getPropertyName(node.callee.object.object) === 'prototype'
49+
getKeyName(node.callee) === 'call'
50+
&& getKeyName(node.callee.object) === 'apply'
51+
&& getKeyName(node.callee.object.object) === 'prototype'
5252
&& node.callee.object.object.object
5353
&& node.callee.object.object.object.type === 'Identifier'
5454
&& node.callee.object.object.object.name === 'Function'

rules/utils/get-key-name.js

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
'use strict';
22
const {getStaticValue} = require('eslint-utils');
33

4-
// TODO[@fisker]: Merge this with `./get-property-name.js`
4+
function getKeyOrPropertyName(keyOrProperty, computed, scope) {
5+
if (!computed) {
6+
if (keyOrProperty.type === 'Identifier') {
7+
return keyOrProperty.name;
8+
}
9+
10+
/* istanbul ignore next: It could be `PrivateIdentifier`(ESTree) or `PrivateName`(Babel) when it's in `class` */
11+
return;
12+
}
13+
14+
const result = getStaticValue(keyOrProperty, scope);
15+
return result && result.value;
16+
}
517

618
/**
719
Get the key value of a node.
@@ -10,28 +22,17 @@ Get the key value of a node.
1022
@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.
1123
*/
1224
function getKeyName(node, scope) {
13-
const {type, key, computed} = node;
14-
15-
/* istanbul ignore next - Prevent unexpected case */
16-
if (
17-
type !== 'Property'
18-
&& type !== 'PropertyDefinition'
19-
&& type !== 'MethodDefinition'
20-
) {
21-
return;
22-
}
23-
24-
if (!computed) {
25-
if (key.type === 'Identifier') {
26-
return key.name;
27-
}
28-
29-
/* istanbul ignore next: It could be `PrivateIdentifier`(ESTree) or `PrivateName`(Babel) when it's in `class` */
30-
return;
25+
const {type, computed} = node;
26+
27+
switch (type) {
28+
case 'MemberExpression':
29+
return getKeyOrPropertyName(node.property, computed, scope);
30+
case 'Property':
31+
case 'PropertyDefinition':
32+
case 'MethodDefinition':
33+
return getKeyOrPropertyName(node.key, computed, scope);
34+
// No default
3135
}
32-
33-
const result = getStaticValue(key, scope);
34-
return result && result.value;
3536
}
3637

3738
module.exports = getKeyName;

rules/utils/get-property-name.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)