/
is-same-reference.js
173 lines (145 loc) · 3.76 KB
/
is-same-reference.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'use strict';
const {getStaticValue} = require('eslint-utils');
// Copied from https://github.com/eslint/eslint/blob/94ba68d76a6940f68ff82eea7332c6505f93df76/lib/rules/utils/ast-utils.js#L392
/**
Gets the property name of a given node.
The node can be a MemberExpression, a Property, or a MethodDefinition.
If the name is dynamic, this returns `null`.
For examples:
a.b // => "b"
a["b"] // => "b"
a['b'] // => "b"
a[`b`] // => "b"
a[100] // => "100"
a[b] // => null
a["a" + "b"] // => null
a[tag`b`] // => null
a[`${b}`] // => null
let a = {b: 1} // => "b"
let a = {["b"]: 1} // => "b"
let a = {['b']: 1} // => "b"
let a = {[`b`]: 1} // => "b"
let a = {[100]: 1} // => "100"
let a = {[b]: 1} // => null
let a = {["a" + "b"]: 1} // => null
let a = {[tag`b`]: 1} // => null
let a = {[`${b}`]: 1} // => null
@param {ASTNode} node The node to get.
@returns {string|undefined} The property name if static. Otherwise, undefined.
*/
function getStaticPropertyName(node) {
let property;
switch (node?.type) {
case 'MemberExpression': {
property = node.property;
break;
}
/* c8 ignore next 2 */
case 'ChainExpression': {
return getStaticPropertyName(node.expression);
}
// Only reachable when use this to get class/object member key
/* c8 ignore next */
case 'Property':
case 'MethodDefinition': {
/* c8 ignore next 2 */
property = node.key;
break;
}
// No default
}
if (property) {
if (property.type === 'Identifier' && !node.computed) {
return property.name;
}
const staticResult = getStaticValue(property);
if (!staticResult) {
return;
}
return String(staticResult.value);
}
}
/**
Check if two literal nodes are the same value.
@param {ASTNode} left The Literal node to compare.
@param {ASTNode} right The other Literal node to compare.
@returns {boolean} `true` if the two literal nodes are the same value.
*/
function equalLiteralValue(left, right) {
// RegExp literal.
if (left.regex || right.regex) {
return Boolean(
left.regex
&& right.regex
&& left.regex.pattern === right.regex.pattern
&& left.regex.flags === right.regex.flags,
);
}
// BigInt literal.
if (left.bigint || right.bigint) {
return left.bigint === right.bigint;
}
return left.value === right.value;
}
/**
Check if two expressions reference the same value. For example:
a = a
a.b = a.b
a[0] = a[0]
a['b'] = a['b']
@param {ASTNode} left The left side of the comparison.
@param {ASTNode} right The right side of the comparison.
@returns {boolean} `true` if both sides match and reference the same value.
*/
function isSameReference(left, right) {
if (left.type !== right.type) {
// Handle `a.b` and `a?.b` are samely.
if (left.type === 'ChainExpression') {
return isSameReference(left.expression, right);
}
if (right.type === 'ChainExpression') {
return isSameReference(left, right.expression);
}
return false;
}
switch (left.type) {
case 'Super':
case 'ThisExpression': {
return true;
}
case 'Identifier':
case 'PrivateIdentifier': {
return left.name === right.name;
}
case 'Literal': {
return equalLiteralValue(left, right);
}
case 'ChainExpression': {
return isSameReference(left.expression, right.expression);
}
case 'MemberExpression': {
const nameA = getStaticPropertyName(left);
// `x.y = x["y"]`
if (typeof nameA !== 'undefined') {
return (
isSameReference(left.object, right.object)
&& nameA === getStaticPropertyName(right)
);
}
/*
`x[0] = x[0]`
`x[y] = x[y]`
`x.y = x.y`
*/
return (
left.computed === right.computed
&& isSameReference(left.object, right.object)
&& isSameReference(left.property, right.property)
);
}
default: {
return false;
}
}
}
module.exports = isSameReference;