Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ChainExpression type and update MemberExpression #104

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/values/expressions/ChainExpression.js
@@ -0,0 +1,12 @@
/**
* Extractor function for a ChainExpression type value node.
* A member expression is accessing a property on an object `obj.property`.
*
* @param - value - AST Value object with type `ChainExpression`
* @returns - The extracted value converted to correct type
*/
export default function extractValueFromChainExpression(value) {
// eslint-disable-next-line global-require
const getValue = require('./index.js').default;
return getValue(value.expression);
}
6 changes: 4 additions & 2 deletions src/values/expressions/MemberExpression.js
Expand Up @@ -4,10 +4,12 @@
*
* @param - value - AST Value object with type `MemberExpression`
* @returns - The extracted value converted to correct type
* and maintaing `obj.property` convention.
* and maintaing `obj.property` convention,
* or `obj?.property` if it occurs in a `ChainExpression`.
*/
export default function extractValueFromMemberExpression(value) {
// eslint-disable-next-line global-require
const getValue = require('./index.js').default;
return `${getValue(value.object)}.${getValue(value.property)}`;
const separator = value.optional ? '?.' : '.';
return `${getValue(value.object)}${separator}${getValue(value.property)}`;
}
2 changes: 2 additions & 0 deletions src/values/expressions/index.js
Expand Up @@ -6,6 +6,7 @@ import TemplateLiteral from './TemplateLiteral';
import FunctionExpression from './FunctionExpression';
import LogicalExpression from './LogicalExpression';
import MemberExpression from './MemberExpression';
import ChainExpression from './ChainExpression';
import OptionalCallExpression from './OptionalCallExpression';
import OptionalMemberExpression from './OptionalMemberExpression';
import CallExpression from './CallExpression';
Expand All @@ -32,6 +33,7 @@ const TYPES = {
FunctionExpression,
LogicalExpression,
MemberExpression,
ChainExpression,
OptionalCallExpression,
OptionalMemberExpression,
CallExpression,
Expand Down