How to use the isMemberExpression function from @babel/types

Find comprehensive JavaScript @babel/types.isMemberExpression code examples handpicked from public code repositorys.

@babel/types.isMemberExpression is a function that checks whether a given AST node represents a MemberExpression.

1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
    }
  }
}

if (
  types.isMemberExpression(path.node.callee) ||
  (es6ClassDeclarations == 0 &&
    (path.node.callee.object?.type == "Super" ||
      path.node.callee.type == "Super"))
) {
fork icon257
star icon758
watch icon0

+ 21 other calls in file

17
18
19
20
21
22
23
24
25
26

function match(path, ctx, explicitOnly) {
    const node = path.node;
    const isMethodCall = (
        t.isCallExpression(node) &&
            t.isMemberExpression(node.callee) &&
            node.callee.computed === false
        );

    if(isMethodCall && ngInject.inspectComment(path, ctx)){
fork icon156
star icon244
watch icon6

+ 3 other calls in file

How does @babel/types.isMemberExpression work?

@babel/types.isMemberExpression is a function provided by the @babel/types package in Babel that determines whether a given AST node represents a MemberExpression. To use it, you pass an AST node object as an argument to the function, and it will return true if the node represents a MemberExpression, or false otherwise. A MemberExpression is a type of AST node that represents a member access operation, like accessing a property of an object using dot notation (e.g. object.property) or bracket notation (e.g. object['property']). A MemberExpression has two child nodes: the object being accessed, and the property being accessed. @babel/types.isMemberExpression checks whether the given AST node has a type property that is set to the string "MemberExpression", and if it has a property property. If both of these conditions are true, the function returns true. Otherwise, it returns false. This function can be useful when working with Babel plugins that manipulate or analyze AST nodes, as it allows you to quickly determine whether a given node represents a MemberExpression without having to manually check its properties.

149
150
151
152
153
154
155
156
157
158
  },
}
const visitor = {
  CallExpression(path) {
    if (
      t.isMemberExpression(path.node.callee) &&
      path.node.callee.property.name === 'setState'
    ) {
      path.node.callee.property.name = 'setData'
    }
fork icon23
star icon143
watch icon13

+ 11 other calls in file

15
16
17
18
19
20
21
22
23
24
const DEFAULT_WEBPACK_PATH = require('./util').DEFAULT_WEBPACK_PATH;

const hookVisitor = {
    AssignmentExpression(path) {
        if (
            t.isMemberExpression(path.node.left)
            && t.isThisExpression(path.node.left.object)
            && t.isIdentifier(path.node.left.property)
            && path.node.left.property.name === 'hooks'
            && t.isObjectExpression(path.node.right)
fork icon22
star icon151
watch icon8

+ 7 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const t = require("@babel/types");

// Example AST node representing "obj.prop"
const node = {
  type: "MemberExpression",
  object: { type: "Identifier", name: "obj" },
  property: { type: "Identifier", name: "prop" },
};

// Check if the node represents a MemberExpression
if (t.isMemberExpression(node)) {
  console.log("Node represents a MemberExpression!");
} else {
  console.log("Node does not represent a MemberExpression.");
}

In this example, we first import the @babel/types package and alias it as t for convenience. We then create an example AST node that represents the obj.prop expression. The node object has a type property set to "MemberExpression", an object property representing the obj identifier, and a property property representing the prop identifier. We then use t.isMemberExpression to check whether the node object represents a MemberExpression. Since it does, the program will log "Node represents a MemberExpression!" to the console.

76
77
78
79
80
81
82
83
84


function isAopBlock(path, block, AOP, cb) {
    t.isExpressionStatement(block)
    && t.isCallExpression(block.expression)
    && t.isMemberExpression(get(block, 'expression.callee'))
    && get(block, 'expression.callee.property.name') === 'after'
    && cb(path, block, AOP);
}
fork icon17
star icon123
watch icon10

+ 3 other calls in file

52
53
54
55
56
57
58
59
60
61
},

CallExpression: (path) => {
  const { node } = path;
  if (
    t.isMemberExpression(node.callee)
    && t.isIdentifier(node.callee.property, { name: 'define' })
  ) {
    node.arguments[0] = t.stringLiteral(definition.modelName);
  }
fork icon23
star icon115
watch icon4

+ 7 other calls in file

29
30
31
32
33
34
35
36
37
38
CallExpression(path) {
  const { callee } = path.node;

  // Only inspect calls to `I18N.textById`.
  if (
    types.isMemberExpression(callee) &&
    types.isIdentifier(callee.object, { name: 'I18N' }) &&
    types.isIdentifier(callee.property, { name: 'textById' })
  ) {
    // Validate the translation text.
fork icon5
star icon24
watch icon13

+ 9 other calls in file

79
80
81
82
83
84
85
86
87
88
//path.getPrevSibling(),获取当前节点的上一级节点
//先判断while节点中switch()中的值,判断是列表的调用还是值
switchNode = path.node.body.body[0]
//判断switchNode是否为空  switchNode的discriminant.type是否是MemberExpression类型以及property.operator是否是++

if (switchNode != null && types.isMemberExpression(switchNode.discriminant) && switchNode.discriminant.property.operator == "++") {
    //获取当前节点的上一级节点
    lastNode = path.getPrevSibling().node
    //判断上一级属性的type是否是VariableDeclaration,declarations的长度是否为2,declarations[0]的init是否是Literal declarations[1]的init是否是数组
    if (types.isVariableDeclaration(lastNode) && lastNode.declarations.length == 2 && types.isLiteral(lastNode.declarations[0].init) && types.isArrayExpression(lastNode.declarations[1].init)) {
fork icon6
star icon18
watch icon1

+ 8 other calls in file

47
48
49
50
51
52
53
54
55
_getMainArray() {
  traverse(this._ast, {
    AssignmentExpression: (path) => {
      const { node } = path;
      if (
        t.isMemberExpression(node.left) &&
        node.left.property.name === MAIN_ARRAY_NAME
      ) {
        const elementsArray = t.arrayExpression(node.right.elements).elements;
fork icon1
star icon10
watch icon1

+ 31 other calls in file

101
102
103
104
105
106
107
108
109
110
  // are being built up, leaving this will be clearer
} else if ( t.isCallExpression(path.node.init) ) {
  // No handling for ^^ right now
  // One example is var U2 = document.createElement("canvas");
  // could be good to make that clearer later
} else if ( t.isMemberExpression(path.node.init) ){
  if ( path.node.id.name in path.scope.bindings ) {
    let refsInScope = path.scope.bindings[path.node.id.name].referencePaths
    refsInScope.forEach((n) => {
      n.replaceWith(
fork icon1
star icon6
watch icon7

109
110
111
112
113
114
115
116
117

var func_body = node_.body.body;
if (func_body.length == 1  && t.isReturnStatement(func_body[0])){
    var body_return = func_body[0];

    if (t.isCallExpression(body_return.argument) && t.isMemberExpression(body_return.argument.callee)){
        let tmp_obj_name = body_return.argument.callee.object.name;

        let tmp_obj_proterty = body_return.argument.callee.property.value;
fork icon1
star icon4
watch icon1

+ 27 other calls in file

164
165
166
167
168
169
170
171
172
  childNode = parseElement(returnEl);
  const itemParam = args[0].params[0];
  const indexParam = args[0].params[1];
  if (itemParam) itemName = itemParam.name;
  if (indexParam) indexName = indexParam.name;
} else if (t.isIdentifier(args[0]) || t.isMemberExpression(args[0])) {
  // { foo.map(this.xxx) }
  throw new Error(`目前暂不支持对 ${generateCodeByExpression(expression)} 的语法转换,请使用内联函数。`);
}
fork icon663
star icon0
watch icon2

+ 3 other calls in file

158
159
160
161
162
163
164
165
166
167
    jsxList.generated = true;
    listBlock.__jsxlist = jsxList;

    parentPath.replaceWith(listBlock);
    returnElPath.replaceWith(t.objectExpression(properties));
  } else if (t.isIdentifier(mapCallbackFn) || t.isMemberExpression(mapCallbackFn)) {
    // { foo.map(this.xxx) }
    throw new Error(`The syntax conversion for ${genExpression(node)} is currently not supported. Please use inline functions.`);
  }
}
fork icon663
star icon0
watch icon0

+ 13 other calls in file

221
222
223
224
225
226
227
228
229
230

  return name;
}

function isAnyMemberExpression(node: Ast): boolean {
  return t.isMemberExpression(node) || t.isJSXMemberExpression(node);
}

function isAnyIdentifier(node: Ast): boolean {
  return t.isIdentifier(node) || t.isJSXIdentifier(node);
fork icon623
star icon0
watch icon0

+ 5 other calls in file

36
37
38
39
40
41
42
43
44
45
// 标记收集props数据
CallExpression: {
  enter (path) {
    const callee = path.node.callee
    if (
      t.isMemberExpression(callee) &&
      t.isThisExpression(callee.object) &&
      (callee.property.name === '_p' || callee.property.value === '_p')
    ) {
      isProps = true
fork icon349
star icon0
watch icon1

+ 3 other calls in file

136
137
138
139
140
141
142
143
144
145
  let message = `${filePath}:${path.node.loc.start.line + startLine - 1}:${path.node.loc.start.column}: SyntaxError: invalid ObjectProperty \`${path.node.name}\`\n`;
  wccError = new error.WccError(error.CODE.INVALID_OBJECTPROPERTY, message);
  path.stop();
  return;
}
if (path.isIdentifier() && WhiteVariableIdentifier.indexOf(path.node.name) > -1 && babelTypes.isMemberExpression(path.parent) && path.parent.property === path.node && !path.parent.computed) {
  //对象属性访问 a.NaN
  let message = `${filePath}:${path.node.loc.start.line + startLine - 1}:${path.node.loc.start.column}: SyntaxError: invalid MemberExpression \`${path.node.name}\`\n`;
  wccError = new error.WccError(error.CODE.INVALID_MEMBEREXPRESSION, message);
  path.stop();
fork icon10
star icon0
watch icon0

+ 5 other calls in file

29
30
31
32
33
34
35
36
37
38
id.properties.forEach(objProperty => {
  templateVariables[t.isRestElement(objProperty) ?
    objProperty.argument.name :
    objProperty.value.name
  ] = {
    source: t.isMemberExpression(init)
      ? init.property.name
      : init.name,
  };
});
fork icon663
star icon0
watch icon2

+ 3 other calls in file

209
210
211
212
213
214
215
216
217
218
// <tag>{fn()}</tag> => <tag>{{ _d0 }}</tag> _d0 = fn();
case 'CallExpression':
  if (type === ATTR) {
    if (
      isEventHandler(attributeName)
      && t.isMemberExpression(expression.callee)
      && t.isIdentifier(expression.callee.property, { name: 'bind' })
    ) {
      // function bounds
      const callExp = node.expression;
fork icon663
star icon0
watch icon0

+ 3 other calls in file

354
355
356
357
358
359
360
361
362
363
        //确定是this的别名
        item.replaceBy(object)
    }
}

if (keyword === "globalData" && t.isMemberExpression(nodePath.parentPath.node)) {
    var property = nodePath.parentPath.node.property
    var propertyName = (property.name || property.value) || ""
    if (propertyName && !globalDataNameList.includes(propertyName)) {
        undefinedNameList.add(propertyName)
fork icon220
star icon0
watch icon0

100
101
102
103
104
105
106
107
108
109
    node: await getReturnValue({ node: node.callee, ast, cwd }),
    ast,
    cwd,
  });

case types.isMemberExpression(node):
  return createDescriptor({
    node: await getMemberProperty({ node: node, ast, cwd }),
    ast,
    cwd,
fork icon2
star icon11
watch icon222

+ 7 other calls in file

Other functions in @babel/types

Sorted by popularity

function icon

@babel/types.identifier is the most popular function in @babel/types (20936 examples)