write javascript code to rewrite javascript code
# synvert-core-javascript
![]()
!Main workflow

Synvert core provides a set of DSLs to rewrite javascript code. e.g.
``javascript
const Synvert = require("synvert-core");
new Synvert.Rewriter("jquery", "deprecate-event-shorthand", () => {
description('jQuery event shorthand is deprecated.');
withinFiles(Synvert.ALL_FILES, function () {
// $('#test').click(function(e) { });
// =>
// $('#test').on('click', function(e) { });
findNode(.CallExpression[callee=.MemberExpression[object IN (/^\\$/ /^jQuery/)][property=click]]
[arguments.length=1][arguments.0.type IN (FunctionExpression ArrowFunctionExpression)], () => {
replace("callee.property", { with: "on" });
insert("'click', ", { to: "arguments.0", at: "beginning" });
});
// $form.submit();
// =>
// $form.trigger('submit');
withNode(
{
nodeType: "CallExpression",
callee: { nodeType: "MemberExpression", object: /^\$/, property: 'submit' },
arguments: { length: 0 },
},
() => {
replace(["callee.property", "arguments"], { with: "trigger('submit')" });
}
);
});
});
``
Want to see more examples, check out synvert-snippets-javascript.
Want to use the CLI, check out synvert-javascript.
DSL are as follows
* configure - configure the rewriter, set sourceTyep and parser
* description - set description of the rewriter
* ifNode - check if node version is greater than or equal to the specified node version
* ifNpm - check the version of the specifid npm package
* addFile - add a new file
* addFileSync - add a new file
* removeFile - remove a file
* removeFileSync - remove a file
* renameFile - rename filepath to new filepath
* renameFileSync - rename filepath to new filepath
* withinFiles - find specified files
* withinFilesSync - find specified files
* withinFile - alias to withinFiles
* withinFileSync - alias to withinFilesSync
* addSnippet - call another snippet
* addSnippetSync - call another snippet
Scopes:
* withinNode - recursively find matching ast nodes
* withinNodeSync - recursively find matching ast nodes
* withNode - alias to withNode
* withNodeSync - alias to withNodeSync
* findNode - alias to withNode
* findNodeSync - alias to withNodeSync
* gotoNode - go to a child node
* gotoNodeSync - go to a child node
Conditions:
* ifExistNode - check if matching node exist in the child nodes
* ifExistNodeSync - check if matching node exist in the child nodes
* unlessExistNode - check if matching node does not exist in the child nodes
* unlessExistNodeSync - check if matching node does not exist in the child nodes
* ifOnlyExistNode - check if current node has only one child node and the child node matches
* ifOnlyExistNodeSync - check if current node has only one child node and the child node matches
* ifAllNodes - check if all nodes match or not
* ifAllNodesSync - check if all nodes match or not
Actions:
* append - append the code to the bottom of the current node body
* prepend - prepend the code to the top of the current node body
* insert - insert code
* insertAfter - insert the code next to the current node
* insertBefore - insert the code previous to the current node
* replace - replace the code of specified child nodes
* delete - delete code the code of specified child nodes
* remove - remove the whole code of current node
* replaceWith - replace the whole code of current node
* noop - no operation
* group - group actions
Others:
* callHelper - call a helper to run shared code
* callHelperSync - call a helper to run shared code
* wrapWithQuotes - wrap string code with single or double quotes
* appendSemicolon - append semicolon to the end of the code
* addLeadingSpaces - add leading spaces to the code
* indent - indent each line in a string code
Properties:
* filePath - get the file path
* currentNode - current ast node
* mutationAdapter - get a mutation adapter to get some helper methods