@@ -59,6 +59,37 @@ function isIdentifier(name, ecmaVersion) {
5959 return esutils . keyword . isIdentifierES5 ( name ) ;
6060}
6161
62+ /**
63+ * Determines whether a given node is a string literal
64+ * @param {ASTNode } node The node to check
65+ * @returns {boolean } `true` if the node is a string literal
66+ */
67+ function isStringLiteral ( node ) {
68+ return node . type === "Literal" && typeof node . value === "string" ;
69+ }
70+
71+ /**
72+ * Gets the name of a property key that can be compared with a function name.
73+ * @param {ASTNode } node A `Property` or `PropertyDefinition` node.
74+ * @param {number } ecmaVersion The ECMAScript version.
75+ * @returns {string|null } The name if the key is a non-computed identifier or a string literal
76+ * that is a valid identifier, otherwise `null`.
77+ */
78+ function getKeyName ( node , ecmaVersion ) {
79+ if ( node . key . type === "Identifier" && ! node . computed ) {
80+ return node . key . name ;
81+ }
82+
83+ if (
84+ isStringLiteral ( node . key ) &&
85+ isIdentifier ( node . key . value , ecmaVersion )
86+ ) {
87+ return node . key . value ;
88+ }
89+
90+ return null ;
91+ }
92+
6293//------------------------------------------------------------------------------
6394// Rule Definition
6495//------------------------------------------------------------------------------
@@ -131,23 +162,7 @@ module.exports = {
131162 const considerPropertyDescriptor = options . considerPropertyDescriptor ;
132163 const includeModuleExports = options . includeCommonJSModuleExports ;
133164 const ecmaVersion = context . languageOptions . ecmaVersion ;
134-
135- /**
136- * Check whether node is a certain CallExpression.
137- * @param {string } objName object name
138- * @param {string } funcName function name
139- * @param {ASTNode } node The node to check
140- * @returns {boolean } `true` if node matches CallExpression
141- */
142- function isPropertyCall ( objName , funcName , node ) {
143- if ( ! node ) {
144- return false ;
145- }
146- return (
147- node . type === "CallExpression" &&
148- astUtils . isSpecificMemberAccess ( node . callee , objName , funcName )
149- ) ;
150- }
165+ const sourceCode = context . sourceCode ;
151166
152167 /**
153168 * Compares identifiers based on the nameMatches option
@@ -192,15 +207,6 @@ module.exports = {
192207 } ) ;
193208 }
194209
195- /**
196- * Determines whether a given node is a string literal
197- * @param {ASTNode } node The node to check
198- * @returns {boolean } `true` if the node is a string literal
199- */
200- function isStringLiteral ( node ) {
201- return node . type === "Literal" && typeof node . value === "string" ;
202- }
203-
204210 //--------------------------------------------------------------------------
205211 // Public
206212 //--------------------------------------------------------------------------
@@ -256,83 +262,46 @@ module.exports = {
256262 return ;
257263 }
258264
259- if ( node . key . type === "Identifier" && ! node . computed ) {
260- const functionName = node . value . id . name ;
261- let propertyName = node . key . name ;
262-
263- if (
264- considerPropertyDescriptor &&
265- propertyName === "value" &&
266- node . parent . type === "ObjectExpression"
267- ) {
268- if (
269- isPropertyCall (
270- "Object" ,
271- "defineProperty" ,
272- node . parent . parent ,
273- ) ||
274- isPropertyCall (
275- "Reflect" ,
276- "defineProperty" ,
277- node . parent . parent ,
278- )
279- ) {
280- const property = node . parent . parent . arguments [ 1 ] ;
281-
282- if (
283- isStringLiteral ( property ) &&
284- shouldWarn ( property . value , functionName )
285- ) {
286- report (
287- node ,
288- property . value ,
289- functionName ,
290- true ,
291- ) ;
292- }
293- } else if (
294- isPropertyCall (
295- "Object" ,
296- "defineProperties" ,
297- node . parent . parent . parent . parent ,
298- )
299- ) {
300- propertyName = node . parent . parent . key . name ;
301- if (
302- ! node . parent . parent . computed &&
303- shouldWarn ( propertyName , functionName )
304- ) {
305- report ( node , propertyName , functionName , true ) ;
306- }
307- } else if (
308- isPropertyCall (
309- "Object" ,
310- "create" ,
311- node . parent . parent . parent . parent ,
312- )
313- ) {
314- propertyName = node . parent . parent . key . name ;
315- if (
316- ! node . parent . parent . computed &&
317- shouldWarn ( propertyName , functionName )
318- ) {
319- report ( node , propertyName , functionName , true ) ;
320- }
321- } else if ( shouldWarn ( propertyName , functionName ) ) {
322- report ( node , propertyName , functionName , true ) ;
323- }
324- } else if ( shouldWarn ( propertyName , functionName ) ) {
325- report ( node , propertyName , functionName , true ) ;
265+ const functionName = node . value . id . name ;
266+ let propertyName = getKeyName ( node , ecmaVersion ) ;
267+
268+ if (
269+ considerPropertyDescriptor &&
270+ propertyName === "value" &&
271+ node . parent . type === "ObjectExpression" &&
272+ astUtils . isPropertyDescriptor ( node . parent , sourceCode )
273+ ) {
274+ const descriptorParent = node . parent . parent ;
275+
276+ if ( descriptorParent . type === "CallExpression" ) {
277+ /*
278+ * Object.defineProperty(obj, "name", descriptor)
279+ * Reflect.defineProperty(obj, "name", descriptor)
280+ */
281+ const key = descriptorParent . arguments [ 1 ] ;
282+
283+ propertyName =
284+ isStringLiteral ( key ) &&
285+ isIdentifier ( key . value , ecmaVersion )
286+ ? key . value
287+ : null ;
288+ } else {
289+ /*
290+ * Object.defineProperties(obj, { name: descriptor })
291+ * Object.create(proto, { name: descriptor })
292+ */
293+ propertyName = getKeyName (
294+ descriptorParent ,
295+ ecmaVersion ,
296+ ) ;
326297 }
327- return ;
328298 }
329299
330300 if (
331- isStringLiteral ( node . key ) &&
332- isIdentifier ( node . key . value , ecmaVersion ) &&
333- shouldWarn ( node . key . value , node . value . id . name )
301+ propertyName !== null &&
302+ shouldWarn ( propertyName , functionName )
334303 ) {
335- report ( node , node . key . value , node . value . id . name , true ) ;
304+ report ( node , propertyName , functionName , true ) ;
336305 }
337306 } ,
338307 } ;
0 commit comments