How to use the isJSXAttribute function from @babel/types

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

The @babel/types.isJSXAttribute function checks whether a given syntax node represents a JSX attribute in a JavaScript file.

212
213
214
215
216
217
218
219
220
221
  this.replaceSource(str)
  return this.keyInfo
}
buildReplaceStr (template, param) {
  let path = util.safePath(this.curRootPath)
  if (t.isJSXText(path.node) || t.isJSXAttribute(path.parent)) {
    // JSXText JSXAttribute 去除冒号 回车
    template = template.replace(/\s+|[::]\s*$/g, '')
  }
  // 一个表达式一个key
fork icon0
star icon7
watch icon4

+ 15 other calls in file

136
137
138
139
140
141
142
143
144
145
  let lang = options.lang;
  // node.name.name === 'style' (代表静态style) == node.name.name.name === 'style' (代表动态style)
  // cml语法下,不要解析 :style ,因为有可能是原生组件或者原生标签
  if (lang === 'cml' && t.isJSXAttribute(node) && node.name.name === 'style') {
    parseStyle.call({path, node, type, options});
  } else if (lang === 'vue' && t.isJSXAttribute(node) && (node.name.name === 'style' || node.name.name.name === 'style')) {
    parseStyle.call({path, node, type, options});
  }
}
exports.parseClassStatement = function parseClassStatement(path, type, options) {
fork icon700
star icon0
watch icon239

+ 55 other calls in file

How does @babel/types.isJSXAttribute work?

The @babel/types.isJSXAttribute function is part of the Babel compiler toolkit for JavaScript and is used to check whether a given syntax node represents a JSX attribute in a JavaScript file. To accomplish this, the function first checks whether the given node is not null and has a type property that matches the JSXAttribute string. If this check passes, it further examines the node to ensure that it has an name property, which represents the name of the attribute, and an value property, which represents the value of the attribute. If both of these conditions are met, the @babel/types.isJSXAttribute function returns true. Otherwise, it returns false. This function is useful when working with the ASTs (Abstract Syntax Trees) produced by the Babel compiler, and enables developers to write code that selectively operates on JSX attribute nodes in their JavaScript code.

199
200
201
202
203
204
205
206
207
208
let seenPropsSpread = false;

for (let i = 0; i < attributes.length; i++) {
  const attr = attributes[i];

  if (seenPropsSpread && t.isJSXAttribute(attr) && attr.name.name === "key") {
    return true;
  } else if (t.isJSXSpreadAttribute(attr)) {
    seenPropsSpread = true;
  }
fork icon0
star icon0
watch icon1

+ 35 other calls in file

Ai Example

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

const node = {
  type: "JSXAttribute",
  name: { type: "JSXIdentifier", name: "className" },
  value: { type: "StringLiteral", value: "container" },
};

if (t.isJSXAttribute(node)) {
  console.log("Found JSX attribute:", node.name.name);
}

In this example, we're using the @babel/types.isJSXAttribute function to check whether a given syntax node represents a JSX attribute in a JavaScript file. We define a node object that represents a JSX attribute with a name property representing the name of the attribute (className) and a value property representing the value of the attribute (container). We use the isJSXAttribute function to check whether node represents a JSX attribute, and if so, we log the name of the attribute to the console. When we run this code, it will output: yaml Copy code

Other functions in @babel/types

Sorted by popularity

function icon

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