How to use the isClassMethod function from @babel/types

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

@babel/types.isClassMethod is a function in the Babel library that checks whether a given AST node represents a class method.

143
144
145
146
147
148
149
150
151
152
if (node.id) {
  return node.id.name;
}
let propertyPath;
let kind = '';
if (t.isObjectMethod(node) || t.isClassMethod(node)) {
  id = node.key;
  if (node.kind !== 'method' && node.kind !== 'constructor') {
    kind = node.kind;
  }
fork icon625
star icon0
watch icon0

+ 5 other calls in file

27
28
29
30
31
32
33
34
35
36
  t.isClassDeclaration(path) &&
  !(comment.constructorComment && comment.constructorComment.hideconstructor)
) {
  const constructor = path.node.body.body.find(item => {
    // https://github.com/babel/babylon/blob/master/ast/spec.md#classbody
    return t.isClassMethod(item) && item.kind === 'constructor';
  });
  if (constructor) {
    return inferAndCombineParams(constructor.params, comment);
  }
fork icon510
star icon0
watch icon5

+ 7 other calls in file

How does @babel/types.isClassMethod work?

@babel/types.isClassMethod works by taking an AST node as input and checking whether it represents a class method.

The function returns true if the node is an object of type ClassMethod in the AST, indicating that it is a class method, and false otherwise.

A class method is a function that is defined inside a class declaration or expression and has access to the class instance through the this keyword.

By using @babel/types.isClassMethod, developers can easily check whether a given AST node represents a class method, and use it to perform various operations on the node if it does.

This can be useful when working with complex code transformations and refactorings that involve modifying class declarations or expressions.

82
83
84
85
86
87
88
89
90
if (parentPath && isElement(parentPath.node)) break

isComponent = isComponent || (
  t.isArrowFunctionExpression(node)
  || t.isFunctionExpression(node)
  || t.isClassMethod(node)
  || t.isObjectMethod(node)
  || t.isFunctionDeclaration(node)
)
fork icon10
star icon44
watch icon10

+ 17 other calls in file

68
69
70
71
72
73
74
75
76
77

//put the params as declarations
const params = [];

path.get("params").forEach(traverseParameters(path, params))
const isClassMethod = t.isClassMethod(path.node)
let funcName;
if (path.node.id) {
    funcName = path.node.id.name
} else if (t.isVariableDeclarator(path.parent) && t.isIdentifier(path.parent.id)) {
fork icon5
star icon27
watch icon1

+ 21 other calls in file

Ai Example

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

const node = t.classMethod(
  "method",
  t.identifier("myMethod"),
  [],
  t.blockStatement([])
);

const isMethod = t.isClassMethod(node);

console.log(isMethod); // true

In this example, we use @babel/types.isClassMethod to check whether an AST node represents a class method. We first create an AST node using the t.classMethod() method, which creates a new ClassMethod node. We then pass the node to t.isClassMethod() to check whether it represents a class method. The function returns true because the node is a ClassMethod node, indicating that it represents a class method. By using @babel/types.isClassMethod in this way, we can easily check whether a given AST node represents a class method, and use it to perform various operations on the node if it does.

60
61
62
63
64
65
66
67
68
  key = t.toComputedKey(node, node.key);
}

if (t.isProperty(node)) {
  value = node.value;
} else if (t.isObjectMethod(node) || t.isClassMethod(node)) {
  value = t.functionExpression(null, node.params, node.body, node.generator, node.async);
  value.returnType = node.returnType;
}
fork icon0
star icon0
watch icon0

Other functions in @babel/types

Sorted by popularity

function icon

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