How to use the isValidIdentifier function from @babel/types

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

3
4
5
6
7
8
9
10
11
const generateExpression = require('./generate-expression');

function objectToAST(object) {
  const properties = Object.entries(object).map(([name, value]) => {
    const isObject = typeof value === 'object';
    const isIdentifier = t.isValidIdentifier(name);

    const astValue = isObject ? objectToAST(value) : t.stringLiteral(value);
    const key = isIdentifier ? t.identifier(name) : t.stringLiteral(name);
fork icon25
star icon523
watch icon6

7
8
9
10
11
12
13
14
15
16

for (const name in obj) {
  const astValue = typeof obj[name] === 'object' ?
    astFromObject(obj[name]) :
    t.stringLiteral(obj[name]);
  const key = t.isValidIdentifier(name, false) ?
    t.identifier(name) :
    t.stringLiteral(name);

  ast.push(t.objectProperty(key, astValue));
fork icon18
star icon195
watch icon7

+ 3 other calls in file

126
127
128
129
130
131
132
133
134
135
});

// Add incoming translations that did not already exist in this locale
Object.keys(translationsLeftToWrite).forEach(id => {
  const value = translationsLeftToWrite[id];
  const keyNode = types.isValidIdentifier(id)
    ? types.identifier(id)
    : types.stringLiteral(id);
  const valueNode =
    typeof value === 'string'
fork icon1
star icon1
watch icon7

Other functions in @babel/types

Sorted by popularity

function icon

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