How to use the isThisExpression function from @babel/types

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

@babel/types.isThisExpression is a method in the Babel JavaScript compiler that checks whether a node in an Abstract Syntax Tree (AST) represents a this expression.

37
38
39
40
41
42
43
44
45
46
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
      path.isProps = true
fork icon349
star icon0
watch icon91

+ 7 other calls in file

16
17
18
19
20
21
22
23
24
25

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

How does @babel/types.isThisExpression work?

@babel/types.isThisExpression is a method in the Babel JavaScript compiler that checks whether a node in an Abstract Syntax Tree (AST) represents a this expression. When you call @babel/types.isThisExpression, you pass in a node from an AST. The method then checks whether the node represents a this expression. A this expression refers to the current object or context in JavaScript. In an AST, a this expression will typically be represented as a ThisExpression node. @babel/types.isThisExpression returns true if the node is a ThisExpression node and false otherwise. This allows you to easily check whether a given node represents a this expression in your code. Note that @babel/types.isThisExpression is just one of many utility methods in the @babel/types module that allow you to work with AST nodes in Babel. By using these methods, you can manipulate and transform the code represented by an AST, allowing you to automate and customize your JavaScript development workflows.

180
181
182
183
184
185
186
187
188
189
 * 判断是否是this表达式
 * @param node
 * @return {Boolean}
 */
function isThisExpression(node) {
  return t.isThisExpression(node) || t.isIdentifier(node, { name: 'this' });
}

/**
 * 判断是否是null表达式
fork icon13
star icon88
watch icon4

+ 3 other calls in file

230
231
232
233
234
235
236
237
238
239
var thisNode = item.match[selectorName][0].node
var nodePath = item['0'].nodePath
if (thisNode.type === 'ThisExpression') {
    //是this本身
    result = 'this'
} else if (t.isMemberExpression(thisNode) && t.isThisExpression(thisNode.object)) {
    result = 'this'
} else {
    var objectName = thisNode.name
    var res = nodePath.scope.lookup(objectName)
fork icon221
star icon0
watch icon0

Ai Example

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

// Define an AST node representing a `this` expression
const thisExpr = t.thisExpression();

// Check whether the node is a `this` expression using `t.isThisExpression`
if (t.isThisExpression(thisExpr)) {
  console.log("This is a `this` expression!");
} else {
  console.log("This is not a `this` expression!");
}

In this example, we start by requiring the @babel/types module and defining an AST node called thisExpr that represents a this expression. We then use t.isThisExpression to check whether the thisExpr node is a ThisExpression node. In this case, the method will return true, indicating that thisExpr represents a this expression. Finally, we log a message to the console based on the result of the t.isThisExpression check. Note that @babel/types.isThisExpression is just one of many utility methods in the @babel/types module that allow you to work with AST nodes in Babel. By using these methods, you can manipulate and transform the code represented by an AST, allowing you to automate and customize your JavaScript development workflows.

426
427
428
429
430
431
432
433
434
435
{
	path.remove();
}
else if((sourceCode.includes("constructor") || sourceCode.includes("RegExp")) &&
	     types.isIdentifier(callee) && arguments.length == 2 && 
	     types.isThisExpression(arguments[0]) &&
	     types.isFunctionExpression(arguments[1]))
{
	let funcName = id.name;
	
fork icon0
star icon0
watch icon1

+ 71 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)