-
-
Notifications
You must be signed in to change notification settings - Fork 353
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
isMemberExpression
to replace selectors (#2093)
- Loading branch information
Showing
6 changed files
with
137 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
'use strict'; | ||
|
||
/** | ||
@param { | ||
{ | ||
property?: string, | ||
properties?: string[], | ||
object?: string, | ||
objects?: string[], | ||
optional?: boolean, | ||
computed?: boolean | ||
} | string | string[] | ||
} [options] | ||
@returns {string} | ||
*/ | ||
function isMemberExpression(node, options) { | ||
if (node?.type !== 'MemberExpression') { | ||
return false; | ||
} | ||
|
||
if (typeof options === 'string') { | ||
options = {properties: [options]}; | ||
} | ||
|
||
if (Array.isArray(options)) { | ||
options = {properties: options}; | ||
} | ||
|
||
let { | ||
property, | ||
properties, | ||
object, | ||
objects, | ||
optional, | ||
computed, | ||
} = { | ||
path: '', | ||
property: '', | ||
properties: [], | ||
object: '', | ||
...options, | ||
}; | ||
|
||
if (property) { | ||
properties = [property]; | ||
} | ||
|
||
if (object) { | ||
objects = [object]; | ||
} | ||
|
||
if ( | ||
(computed === true && (node.computed !== computed)) | ||
|| ( | ||
computed === false | ||
// `node.computed` can be `undefined` in some parsers | ||
&& node.computed | ||
) | ||
|| (optional === true && (node.optional !== optional)) | ||
|| ( | ||
optional === false | ||
// `node.optional` can be `undefined` in some parsers | ||
&& node.optional | ||
) | ||
) { | ||
return false; | ||
} | ||
|
||
if ( | ||
Array.isArray(properties) | ||
&& properties.length > 0 | ||
&& ( | ||
node.property.type !== 'Identifier' | ||
|| !properties.includes(node.property.name) | ||
) | ||
) { | ||
return false; | ||
} | ||
|
||
if ( | ||
Array.isArray(objects) | ||
&& objects.length > 0 | ||
&& ( | ||
node.object.type !== 'Identifier' | ||
|| !objects.includes(node.object.name) | ||
) | ||
) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
module.exports = isMemberExpression; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters