Synvert

logo

write snippet code to rewrite your source code.

Synvert

Synvert (short for syntax + convert) provides a complete solution to rewrite (refactor) the source code.

Different than linter tools (e.g. rubocop) and codemod tools (e.g. jscodeshift), synvert allows you to write snippet code to rewrite your source code.

The best advantage is that you don’t need to learn all of the APIs, synvert can generate the snippet code for you.

Generate Snippet

It supports Typescript, Javascript and Ruby so far.

Table of Contents

Synvert Component

Node Query

It defines a NQL (node query language) and node rules to query AST nodes.

e.g. to match javascript code string.match(/unicorn/)

NQL can be

.CallExpression[expression=.PropertyAccessExpression[name=match]][arguments.length=1][arguments.0=.RegularExpressionLiteral]

Node rules can be

{ nodeType: "CallExpression", expression: { nodeType: "PropertyAccessExpression", name: "match" }, arguments: { length: 1, 0: { nodeType: "RegularExpressionLiteral" } } }

The ruby version is here.

The javascript version is here.

Node Mutation

It provides a set of APIs to rewrite AST node source code.

e.g. to replace javascript code string.match(/unicorn/) with /unicorn/.test(string).

replaceWith("{{arguments.0}}.test({{expression.expression}})");

The ruby version is here.

The javascript version is here.

Synvert Core

Synvert core defines a set of DSLs to rewrite code.

e.g. to find javascript code string.match(/unicorn/) and replace it with /unicorn/.test(string).

const Synvert = require("synvert-core");

new Synvert.Rewriter("javascript", "prefer-regexp-test", () => {
  description("string.match(/unicorn/) => /unicorn/.test(string)");

  configure({ parser: "typescript" });
  withinFiles(Synvert.ALL_FILES, () => {
    findNode(`.CallExpression[expression=.PropertyAccessExpression[name=match]][arguments.length=1][arguments.0=.RegularExpressionLiteral]`, () => {
      replaceWith("{{arguments.0}}.test({{expression.expression}})");
    });
  });
});

The ruby version is here.

The javascript version is here.

Synvert Snippets

Synvert snippets use the synvert core DSLs to define how to fine AST nodes and how to rewrite the AST node source code.

The ruby version is here.

The javascript version is here.

Synvert CLI

Synvert CLI is a command line tool to run the synvert snippet to rewrite the source code.

The ruby version is here.

The javascript version is here.

Synvert API

Synvert API is a web API to query snippets, generate snippet and parse snippet.

Synvert GUI

Synvert GUI is a Windows and Mac app to use synvert.

Features

GUI Snippet Show Screenshot

GUI Snippet New Single Screenshot GUI Snippet New Multi Second Screenshot

GUI Snippet Diff Screenshot

Download

Mac OS

Windows x64, it’s not signed and Microsoft SmartScreen will block it.

Synvert VSCode Extension

Synvert VSCode extension is a VSCode extension to use synvert.

Features

Search and Replace

Search snippet

Generate snippet

Download

Synvert VSCode extension can be downloaded here

Synvert Web (Playground)

Synvert Playground is a web interface to play with synvert.

Node Playground is a web interface to play with node query and node mutation.