How to use the babel-types.stringLiteral function in babel-types

To help you get started, we’ve selected a few babel-types examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Tencent / omi / packages / mps / _scripts / jsx2wxml / index.js View on Github external
TemplateLiteral(path) {
            const nodes = [];
            const { quasis, expressions } = path.node;
            let index = 0;
            if (path.parentPath.isTaggedTemplateExpression()) {
                return;
            }
            for (const elem of quasis) {
                if (elem.value.cooked) {
                    nodes.push(t.stringLiteral(elem.value.cooked));
                }
                if (index < expressions.length) {
                    const expr = expressions[index++];
                    if (!t.isStringLiteral(expr, { value: '' })) {
                        nodes.push(expr);
                    }
                }
            }
            // + 号连接符必须保证第一和第二个 node 都是字符串
            if (!t.isStringLiteral(nodes[0]) && !t.isStringLiteral(nodes[1])) {
                nodes.unshift(t.stringLiteral(''));
            }
            let root = nodes[0];
            for (let i = 1; i < nodes.length; i++) {
                root = t.binaryExpression('+', root, nodes[i]);
            }
github trivago / babel-plugin-cloudinary / lib / index.js View on Github external
return allPluginParameters[param];
      } else if (PLUGIN_PARAMETERS[param].default && PLUGIN_PARAMETERS[param].defaultType) {
        const babelConstructor = t[PLUGIN_PARAMETERS[param].defaultType];

        return babelConstructor(PLUGIN_PARAMETERS[param].default);
      }
    });
  const expressions = [...baseExpressions, ...astHelpers.filterExpressions(extraExpressions), t.stringLiteral("")];
  const quasis = [...baseQuasis, ...Object.keys(extraExpressions).map(() => astHelpers.templateElement(""))];

  // keep pairing between expressions and quasis with empty literals
  if (expressions.length < quasis.length) {
    const offset = quasis.length - expressions.length;

    for (let i = 0; i < offset; i++) {
      expressions.push(t.stringLiteral(""));
    }
  }

  path.replaceWith(t.expressionStatement(t.templateLiteral(quasis, expressions)));
}
github NervJS / taro / packages / taro-cli / src / weapp.js View on Github external
mediaFiles.push(vpath)
        }
        const specifiers = node.specifiers
        let defaultSpecifier = null
        specifiers.forEach(item => {
          if (item.type === 'ImportDefaultSpecifier') {
            defaultSpecifier = item.local.name
          }
        })
        let sourceDirPath = sourceDir
        if (NODE_MODULES_REG.test(vpath)) {
          sourceDirPath = nodeModulesPath
        }

        if (defaultSpecifier) {
          astPath.replaceWith(t.variableDeclaration('const', [t.variableDeclarator(t.identifier(defaultSpecifier), t.stringLiteral(vpath.replace(sourceDirPath, '').replace(/\\/g, '/')))]))
        } else {
          astPath.remove()
        }
      } else if (Util.REG_STYLE.test(valueExtname)) {
        const stylePath = path.resolve(path.dirname(sourceFilePath), value)
        if (styleFiles.indexOf(stylePath) < 0) {
          styleFiles.push(stylePath)
        }
        astPath.remove()
      } else {
        let vpath = Util.resolveScriptPath(path.resolve(sourceFilePath, '..', value))
        let outputVpath
        if (NODE_MODULES_REG.test(vpath)) {
          outputVpath = vpath.replace(nodeModulesPath, npmOutputDir)
        } else {
          outputVpath = vpath.replace(sourceDir, outputDir)
github Tencent / omi / packages / mps / _scripts / jsx2wxml / jsx.js View on Github external
function buildRefTemplate(name, refName, loop, key) {
    const attrs = [
        t.jSXAttribute(t.jSXIdentifier('is'), t.stringLiteral(name)),
        t.jSXAttribute(t.jSXIdentifier('data'), t.stringLiteral(`{{...${refName ? `${loop ? '' : '$$'}${refName}` : '__data'}}}`))
    ];
    if (key) {
        attrs.push(key);
    }
    return t.jSXElement(t.jSXOpeningElement(t.jSXIdentifier('template'), attrs), t.jSXClosingElement(t.jSXIdentifier('template')), []);
}
exports.buildRefTemplate = buildRefTemplate;
github trivago / melody / packages / melody-plugin-idom / src / visitors / idom.js View on Github external
function ensureKeyIsValid(state, maybeKey, hasStaticAttributes) {
    if (maybeKey) {
        return Node.isStringLiteral(maybeKey)
            ? maybeKey
            : t.binaryExpression('+', t.stringLiteral(''), maybeKey);
    }

    if (hasStaticAttributes && state.options.generateKey) {
        return t.stringLiteral(state.generateKey());
    }

    return t.nullLiteral();
}
github ben-eb / css-values / src / generators / property.js View on Github external
createConst(t.identifier(identifier), callExpression(
                    keywordFactory,
                    keywordList
                )),
            ];
        }
        /*
         * Otherwise, we need to generate a list of keywords that are used
         * as another validation condition. If we only have one keyword,
         * this can be inlined inside the `isKeyword` function - i.e.
         * `isKeyword(node, 'foo')`. Otherwise, we create a new list
         * and use it as a reference - `isKeyword(node, propertyKeywords)`,
         * where `propertyKeywords` is `const propertyKeywords = ['foo', 'bar']`
         */
        const keywordsList = t.identifier(`${identifier}Keywords`);
        const list = settings.keywords.length === 1 ? t.stringLiteral(settings.keywords[0]) : keywordsList;
        settings.validators.isKeyword = callExpression('isKeyword', nodeIdentifier, list);
        if (settings.keywords.length > 1) {
            keywords.push(createConst(
                keywordsList,
                keywordList
            ));
        }
    }

    const validatorKeys = Object.keys(settings.validators);

    const validatorList = validatorKeys.sort(k => k === 'isKeyword' ? -1 : 1).reduce((list, key) => {
        list.push(
            ...getValidatorResult(key, settings.validators[key])
        );
        return list;
github roman01la / html-to-react-components / lib / module.js View on Github external
function getCJSImportDeclaration(variable, moduleName) {
  return t.variableDeclaration("const", [
    t.variableDeclarator(
      t.identifier(variable),
      t.callExpression(t.identifier("require"), [t.stringLiteral(moduleName)])
    )
  ]);
}
github salesforce / lwc / packages / raptor-template-compiler / src / codegen / helpers.ts View on Github external
Object.keys(obj).map(key =>
            t.objectProperty(t.stringLiteral(key), valueMapper(key)),
        ),
github cssinjs / jss / packages / babel-plugin / src / utils / buildClassesNode.js View on Github external
Object.keys(sheet.classes).map(name =>
        t.objectProperty(t.stringLiteral(name), t.stringLiteral(sheet.classes[name]))
      )
github NervJS / taro / packages / taro-cli / src / h5 / index.ts View on Github external
node.body.forEach((bodyNode) => {
                if (t.isExpressionStatement(bodyNode)
                  && t.isCallExpression(bodyNode.expression)
                  && t.isIdentifier(bodyNode.expression.callee)
                  && bodyNode.expression.callee.name === 'mountApis') {
                  const mountApisOptNode = bodyNode.expression.arguments[0]
                  if (t.isObjectExpression(mountApisOptNode)) {
                    const valueNode = t.stringLiteral(addLeadingSlash(pageName))
                    let basenameNode = mountApisOptNode.properties.find((property: t.ObjectProperty) => {
                      return toVar(property.key) === 'currentPagename'
                    }) as t.ObjectProperty | undefined
                    if (basenameNode) {
                      basenameNode.value = valueNode
                    } else {
                      basenameNode = t.objectProperty(t.stringLiteral('currentPagename'), valueNode)
                      mountApisOptNode.properties.push(basenameNode)
                    }
                  }
                }
              })
            }