How to use @codemod/parser - 3 common examples

To help you get started, we’ve selected a few @codemod/parser 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 codemod-js / codemod / packages / matchers / src / __tests__ / utils / parse / js.ts View on Github external
export default function js(code: string): t.File {
  return stripExtras(parse(code));
}
github codemod-js / codemod / packages / matchers / src / __tests__ / utils / builders.ts View on Github external
export function program(
  template: string
): (replacements?: R) => t.File {
  const ast = parse(template);

  return (replacements = {} as R) => {
    const unusedReplacements = new Set(Object.keys(replacements));

    traverse(ast, {
      Placeholder(path: NodePath): void {
        const name = path.node.name.name;
        const replacement = replacements[name];

        if (!replacement) {
          throw new Error(
            `no replacement found for placeholder with name: ${name}`
          );
        }

        if (Array.isArray(replacement)) {
github decaffeinate / decaffeinate / src / stages / semicolons / index.ts View on Github external
static run(content: string): StageResult {
    const log = logger(this.name);
    log(content);

    const editor = new MagicString(content);
    const ast = parse(content, {
      tokens: true
    });

    const { insertions, removals } = asi(content, ast);

    insertions.forEach(({ index, content }) => editor.appendLeft(index, content));
    removals.forEach(({ start, end }) => editor.remove(start, end));

    return {
      code: editor.toString(),
      suggestions: []
    };
  }
}

@codemod/parser

Wrapper around @babel/parser that allows parsing everything.

Apache-2.0
Latest version published 12 months ago

Package Health Score

77 / 100
Full package analysis

Popular @codemod/parser functions

Similar packages