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 rpetrich / swift-to-js / values.ts View on Github external
export function literal(value: LiteralValue, location?: LocationSource): ExpressionValue | VariableValue {
	if (typeof value === "boolean") {
		return expr(booleanLiteral(value), location);
	} else if (typeof value === "number") {
		return expr(numericLiteral(value), location);
	} else if (typeof value === "string") {
		return expr(stringLiteral(value), location);
	} else if (value === null) {
		return expr(nullLiteral(), location);
	} else if (Array.isArray(value)) {
		return expr(arrayExpression(value.map((element) => literal(element, location).expression)), location);
	} else if (typeof value === "object") {
		return expr(objectExpression(Object.keys(value).map((key) => {
			const expression = literal((value as LiteralMap)[key], location).expression;
			if (validIdentifier.test(key)) {
				return objectProperty(identifier(key), expression);
			} else {
				// Case where key is not a valid identifier
				return objectProperty(stringLiteral(key), expression, true);
			}
		})), location) as ExpressionValue;
	} else {
		throw new TypeError(`Expected to receive a valid literal type, instead got ${typeof value}`);
github parcel-bundler / parcel / packages / shared / scope-hoisting / src / hoist.js View on Github external
function addImport(asset: MutableAsset, path) {
  // Replace with a $parcel$require call so we know where to insert side effects.
  let requireCall = REQUIRE_CALL_TEMPLATE({
    ID: t.stringLiteral(asset.id),
    SOURCE: t.stringLiteral(path.node.source.value),
  });

  // Hoist the call to the top of the file.
  let lastImport = path.scope.getData('hoistedImport');
  if (lastImport) {
    [lastImport] = lastImport.insertAfter(requireCall);
  } else {
    [lastImport] = path.parentPath.unshiftContainer('body', [requireCall]);
  }

  path.scope.setData('hoistedImport', lastImport);
}
github Jam3 / babel-plugin-static-fs / lib / fs.js View on Github external
if (enc && typeof enc === 'object' && enc.encoding) {
        enc = enc.encoding;
      }

      var result = fs.readFileSync(file, enc);

      // Submit new dependencies back to webpack/browserify.
      // This is currently a bit ugly, but it appears to be
      // a limitation of Babel's plugin architecture, there
      // is no documented/clean way of bubbling this back up
      // to the bundler.
      instance.onFile(file);

      if (isBuffer) {
        return buffer({
          CONTENT: t.stringLiteral(result),
          ENC: t.stringLiteral(enc)
        });
      }
      return t.stringLiteral(result.toString());
    }
  };
github alan-ai / alan-sdk-reactnative / testtools / node_modules / metro / src / ModuleGraph / worker / collectDependencies.js View on Github external
Program(path, state) {
      state.asyncRequireModulePathStringLiteral = types.stringLiteral(
        options.asyncRequireModulePath
      );
      state.dependencyMapIdentifier = path.scope.generateUidIdentifier(
        "dependencyMap"
      );
      state.dependencyCalls = new Set(
        ["require"].concat(_toConsumableArray(options.inlineableCalls))
      );
    }
  };
github areslabs / alita / src / tran / addEventHandler.js View on Github external
exit: path => {
            if (path.type === 'JSXOpeningElement'
                && path.node.name.name === 'view'
                && getOriginal(path).startsWith('Touchable')
            ) {

                const original = getOriginal(path)

                const diuu = getDiuu(path.node.attributes)

                path.node.attributes.push(
                    t.jsxAttribute(t.jsxIdentifier('data-diuu'), t.stringLiteral(`{{${diuu}}}`)),
                    t.jsxAttribute(t.jsxIdentifier('hover-stop-propagation'), t.stringLiteral("")),
                    t.jsxAttribute(t.jsxIdentifier('hover-start-time'), t.stringLiteral("0")),
                    t.jsxAttribute(t.jsxIdentifier('hover-stay-time'), t.stringLiteral("100")),
                )


                path.node.attributes.forEach(attri => {
                    if (attri.type === 'JSXAttribute' && attri.name.name === 'onPress') {
                        attri.name.name = "catchtap"
                        attri.value = t.stringLiteral('eventHandler')
                    }

                    if (attri.type === 'JSXAttribute' && attri.name.name === 'onLongPress') {
                        attri.name.name = "catchlongpress"
                        attri.value = t.stringLiteral('eventHandler')
                    }
                })

                if (original === 'TouchableOpacity') {
github areslabs / alita / src / tran / addEventHandler.js View on Github external
exit: path => {
            if (path.type === 'JSXOpeningElement'
                && path.node.name.name === 'view'
                && getOriginal(path).startsWith('Touchable')
            ) {

                const original = getOriginal(path)

                const diuu = getDiuu(path.node.attributes)

                path.node.attributes.push(
                    t.jsxAttribute(t.jsxIdentifier('data-diuu'), t.stringLiteral(`{{${diuu}}}`)),
                    t.jsxAttribute(t.jsxIdentifier('hover-stop-propagation'), t.stringLiteral("")),
                    t.jsxAttribute(t.jsxIdentifier('hover-start-time'), t.stringLiteral("0")),
                    t.jsxAttribute(t.jsxIdentifier('hover-stay-time'), t.stringLiteral("100")),
                )


                path.node.attributes.forEach(attri => {
                    if (attri.type === 'JSXAttribute' && attri.name.name === 'onPress') {
                        attri.name.name = "catchtap"
                        attri.value = t.stringLiteral('eventHandler')
                    }

                    if (attri.type === 'JSXAttribute' && attri.name.name === 'onLongPress') {
                        attri.name.name = "catchlongpress"
                        attri.value = t.stringLiteral('eventHandler')
                    }
                })
github lessmess-dev / react-overrides / plugin / src / index.js View on Github external
t.unaryExpression("typeof", t.identifier("props"), true),
                    t.stringLiteral("undefined")
                ),
                t.identifier("props"),
                t.conditionalExpression(
                    t.logicalExpression(
                        "&&",
                        t.thisExpression(),
                        t.binaryExpression(
                            "!==",
                            t.unaryExpression(
                                "typeof",
                                t.memberExpression(t.thisExpression(), t.identifier("props")),
                                true
                            ),
                            t.stringLiteral("undefined")
                        )
                    ),
                    t.memberExpression(t.thisExpression(), t.identifier("props")),
                    t.memberExpression(t.identifier("arguments"), t.numericLiteral(0), true)
                )
            )
        )
    ]);
    variableDeclaration.isOverridablePropsVariableDeclaration = true;
    return variableDeclaration;
};
github salesforce / lwc / packages / @lwc / template-compiler / src / codegen / index.ts View on Github external
}
                if (
                    templateContainsId &&
                    isAllowedFragOnlyUrlsXHTML(tagName, attr.name, namespaceURI) &&
                    isFragmentOnlyUrl(attr.value)
                ) {
                    return codeGen.genScopedFragId(attr.value);
                }
                if (isSvgUseHref(tagName, attr.name, namespaceURI)) {
                    return t.callExpression(t.identifier('sanitizeAttribute'), [
                        t.stringLiteral(tagName),
                        t.stringLiteral(namespaceURI),
                        t.stringLiteral(attr.name),
                        isFragmentOnlyUrl(attr.value)
                            ? codeGen.genScopedFragId(attr.value)
                            : t.stringLiteral(attr.value),
                    ]);
                }
                return t.stringLiteral(attr.value);
            }

            case IRAttributeType.Boolean:
                return t.booleanLiteral(attr.value);
        }
    }
github cube-js / cube.js / packages / cubejs-server-core / dev / templates / CredentialsSnippet.js View on Github external
replaceTokens(targetSource) {
    const apiUrl = targetSource.definitions.find(d => d.get('id').node.name === 'API_URL');
    apiUrl.get('init').replaceWith(t.stringLiteral(this.apiUrl));

    const cubejsToken = targetSource.definitions.find(d => d.get('id').node.name === 'CUBEJS_TOKEN');
    cubejsToken.get('init').replaceWith(t.stringLiteral(this.cubejsToken));
  }
}
github merceyz / babel-plugin-optimize-clsx / src / utils / strings.ts View on Github external
export function combineStringLike(
  a: t.StringLiteral | t.TemplateLiteral,
  b: t.StringLiteral | t.TemplateLiteral,
) {
  if (isStringLikeEmpty(a) && isStringLikeEmpty(b)) {
    return t.stringLiteral('');
  }

  if (t.isStringLiteral(a) && t.isStringLiteral(b)) {
    return t.stringLiteral(a.value + ' ' + b.value);
  }

  if (t.isTemplateLiteral(a) && t.isTemplateLiteral(b)) {
    const expressions = [...a.expressions, ...b.expressions];
    const quasis = [...a.quasis];

    quasis[quasis.length - 1] = templateElement(
      quasis[quasis.length - 1].value.raw + ' ' + b.quasis[0].value.raw,
    );

    quasis.push(...b.quasis.slice(1));