How to use the toBindingIdentifierName function from @babel/types

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

@babel/types.toBindingIdentifierName is a function in the Babel library that converts a variable name to a valid binding identifier name in JavaScript.

218
219
220
221
222
223
224
225
226
227
} else if (t.isLiteral(id) && id.value != null) {
  name = id.value + '';
}

if (name != null) {
  return t.toBindingIdentifierName(name);
}

if (t.isImport(id)) {
  name = 'import';
fork icon625
star icon0
watch icon5

+ 8 other calls in file

161
162
163
164
165
166
167
168
169
170

if (name === undefined) {
  return;
}

name = t.toBindingIdentifierName(name);
id = t.identifier(name);
id[t.NOT_LOCAL_BINDING] = true;
const state = visit(node, name, scope);
return wrap(state, node, id, scope) || node;
fork icon0
star icon1
watch icon1

+ 8 other calls in file

How does @babel/types.toBindingIdentifierName work?

Sure! @babel/types.toBindingIdentifierName is a function in the Babel library that converts a variable name to a valid binding identifier name in JavaScript. In JavaScript, variable names can include a wide range of characters, including letters, numbers, and certain special characters such as $ or _. However, there are some restrictions on what characters can be used in a binding identifier name, which is the name used to declare a variable, function, or class. The @babel/types.toBindingIdentifierName function takes a string argument representing a variable name and converts it to a valid binding identifier name. The function applies the following rules: If the input string is not a valid identifier, such as a reserved word or a string literal, the function throws an error. If the input string starts with a number, the function prepends an underscore (_) to the string. If the input string contains characters that are not valid in an identifier, such as a hyphen or a dot, the function replaces those characters with underscores. If the input string is a valid identifier, the function returns the input string unchanged. By converting variable names to valid binding identifier names, @babel/types.toBindingIdentifierName ensures that the code generated by Babel is valid and can be executed in a JavaScript environment.

Ai Example

1
2
3
4
5
6
7
const { toBindingIdentifierName } = require("@babel/types");

const variableName = "foo-bar.baz";
const bindingIdentifierName = toBindingIdentifierName(variableName);

console.log(bindingIdentifierName);
// Output: "foo_bar_baz"

In this example, we first import toBindingIdentifierName from the @babel/types library. We define a variable variableName that contains a string with characters that are not valid in a binding identifier name. We then call toBindingIdentifierName(variableName) to convert the variable name to a valid binding identifier name. The function replaces the hyphen and dot characters with underscores, and returns the resulting string "foo_bar_baz". We log the resulting binding identifier name to the console using console.log(), which outputs "foo_bar_baz". This name can now be used to declare a variable, function, or class in JavaScript.

Other functions in @babel/types

Sorted by popularity

function icon

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