Logo

dev-resources.site

for different kinds of informations.

Code search and refactoring tools - `Code Recycle`

Published at
3/16/2024
Categories
search
ast
refactor
Author
wszgrcy
Categories
3 categories in total
search
open
ast
open
refactor
open
Author
7 person written this
wszgrcy
open
Code search and refactoring tools - `Code Recycle`
  • Programmers often rely on text search when querying code during the development process, although this is useful in most cases, it may not fully meet our needs
let a    =     1;let b=`let a=1`
Enter fullscreen mode Exit fullscreen mode
  • It is usually difficult to search for the above code statement let a = 1 because on one hand, there may be string interference, and on the other hand, the format may be different, making it impossible to directly determine the unique statement

  • However, when we use code cycle, we can directly match this statement with let a=1 because it is based on syntax trees for matching. One characteristic is that it is not sensitive to blank content, and the other is that all characters are treated as node content for querying

  • In this way, we can use the same search content as before to match more accurate results

Quickly exchange parameters?

  • If you want to exchange the parameters of function calls, it may be a big project that requires checking each function call and manually handling the positions of the two parameters
  • Would it be more convenient to exchange all the code with just a few lines of code?
  • That's right, you can write it like this in code cycle
  {
    query: `CallExpression:like(test1( [[{^]] [[$p1]] [[...]] [[$p4]] [[$}]] ) )`,
    multi: true,
    replace: {
      p1: `{{''|ctxInferValue:'p4'}}`,
      p4: `{{''|ctxInferValue:'p1'}}`,
    },
   },
Enter fullscreen mode Exit fullscreen mode

switch

  • The above demonstration involves exchanging the first and last parameters. If you have already determined the number of parameters, the writing method can be simpler

Rewrite the parameter transfer structure?

  • When there are changes in the design, it is usually necessary to modify the parameter transfer structure and place the previous parameters into the new structure
  • At this point, you can do this
{
    query: `CallExpression:like(test1( [[{^]]  [[...]] [[$p4]] [[$}]] ) )`,
    multi: true,
    replace: {
      p4: `{value:{{''|ctxInferValue:'p4'}}}`,
    },
}
Enter fullscreen mode Exit fullscreen mode
  • The last parameter becomes an object, and the original parameter value will be in the object

Other uses

  • console.[[$method:/log|info/]]() Query the specified calling method for console
  • class [[$className]] { [[{]] test(){} [[}]] }Query classes containing test methods
  • {key:[[$value]]}Query the value of the key

More?

  • The tool currently supports the execution of CLI and VSCode Extension. Script Support yaml/js/ts
  • The above example is an introduction to the basic usage of code cycle using the script language typescript as an example. The tool currently supports over 400 languages/syntax parsing,You can view document learn more
  • If you want to see more instances, you can visit the repository view and Run
  • If you are already interested, you can start quickly
ast Article's
30 articles in total
Favicon
Understanding Abstract Syntax Trees
Favicon
ESLint Plugin. What was missed in the doc?
Favicon
Code search and refactoring tools - `Code Recycle`
Favicon
Code cycle: may be the syntax query that currently supports the most languages
Favicon
Python, ast, and redbaron
Favicon
Generate references table from code comments
Favicon
Effective Refactoring with Codemods
Favicon
TagTide library: make your HTML editor-friendly and more
Favicon
What is AST?
Favicon
Analyzing AST in Go with JSON tools
Favicon
Deciphering Python: How to use Abstract Syntax Trees (AST) to understand code
Favicon
Revealing the magic of AST by writing babel plugins
Favicon
From Parse Tree to Evaluator (featuring Sarah Withee)
Favicon
Digging deeper into the analysis of Go-code
Favicon
Take a walk the Go AST
Favicon
OpenTracing for Go Projects
Favicon
My journey optimizing the Go Compiler
Favicon
Static Code Analysis: What it is? How to use it?
Favicon
How to find what is the dependency of a function, class, or variable in ES6 via AST
Favicon
JS-X-Ray 1.0
Favicon
How to work happier with QA with the help of AST
Favicon
AST Finder – Finding AST nodes from code
Favicon
Building AST nodes from source code
Favicon
Adding Contexts via Go AST (Code Instrumentation)
Favicon
Manipulating AST with JavaScript
Favicon
Easier TypeScript tooling with TSQuery
Favicon
Converting TypeScript decorators into static code!
Favicon
Babel macros
Favicon
How do template literals in JavaScript work under the hood?
Favicon
Creating custom JavaScript syntax with Babel

Featured ones: