How to use the jscodeshift.literal function in jscodeshift

To help you get started, we’ve selected a few jscodeshift 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 dvajs / dva-ast / src / api / router.js View on Github external
if (existsSync(componentFilePath)) {
    relativePath = relative(filePath, componentFilePath);
    if (relativePath.charAt(0) !== '.') {
      relativePath = './' + relativePath;
    }
    relativePath = relativePath.split(sep).join('/');  // workaround for windows
  }

  const imports = root.find(j.ImportDeclaration);
  const lastImport = imports.at(imports.size() - 1);
  lastImport.insertAfter(
    j.importDeclaration(
      [j.importDefaultSpecifier(
        j.identifier(component.componentName)
      )],
      j.literal(relativePath)
    )
  );
  writeFile(filePath, root.toSource());
}
github noahsug / json5-writer / src / writeValue.js View on Github external
function getNewPropertyKey(properties, key) {
  // if the key has invalid characters, it has to be a string literal
  if (key.match(/[^a-zA-Z0-9_]/)) {
    return j.literal(key)
  }

  // infer whether to use a literal or identifier by looking at the other keys
  const useIdentifier =
    properties.length === 0 || properties.some(p => p.key.type === 'Identifier')
  return useIdentifier ? j.identifier(key) : j.literal(key)
}
github dvajs / dva-ast / src / collections / Entry.js View on Github external
'insertAfter' :
      'insertBefore';

    const collection = points
      // get parent statement
      .map(path => path.parent)
      .at(0);

    collection[insertMethod].call(
      collection,
      j.expressionStatement(
        j.callExpression(
          j.memberExpression(object, j.identifier('model')),
          [
            j.callExpression(j.identifier('require'), [
              j.literal(modelPath)
            ])
          ]
        )
      )
    );
  },
github noahsug / json5-writer / src / writeValue.js View on Github external
function createEmptyNode(value) {
  if (isArray(value)) {
    return j.arrayExpression([])
  }
  if (isObject(value)) {
    return j.objectExpression([])
  }
  return j.literal('')
}
github Astrocoders / old-astroman / lib / utils / js / appendImport.js View on Github external
function getNewImportDeclaration({
  name,
  local,
}) {
  return j.importDeclaration([
    j.importDefaultSpecifier(j.identifier(name)),
  ], j.literal(local))
}
github casbin / node-casbin / src / model / model.ts View on Github external
mAST.find(j.Literal).forEach(path => {
        let n = path.value.value;
        if (typeof n === 'string' && n.includes('.')) {
          hasEscape = true;
          n = n.replace(/\./g, '\\.');
          j(path).replaceWith(j.literal(n));
        }
      });
github Amwam / js-import-sort / transform.js View on Github external
if (name) {
      identifier = jscodeshift.identifier(name);
      variableIds.unshift(
        jscodeshift.importDefaultSpecifier(identifier, identifier)
      );
    } else if (namespace) {
      identifier = jscodeshift.identifier(namespace);
      variableIds.unshift(
        jscodeshift.importNamespaceSpecifier(identifier, identifier)
      );
    }
  }

  declaration = jscodeshift.importDeclaration(
    variableIds,
    jscodeshift.literal(moduleName)
  );
  declaration.importKind = kind;

  return declaration;
}
github dvajs / dva-ast / src / collections / Model.js View on Github external
path.node.properties.forEach(prop => {
        if (j.Property.check(prop) && prop.key.name === 'namespace') {
          prop.value = j.literal(newNamespace);
        }
      });
    });