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
Author
7 person written this
wszgrcy
open
- 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`
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 statementHowever, when we use
code cycle
, we can directly match this statement withlet 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 queryingIn 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'}}`,
},
},
- 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'}}}`,
},
}
- 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 forconsole
-
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
andVSCode Extension
. Script Supportyaml
/js
/ts
- The above example is an introduction to the basic usage of
code cycle
using the script languagetypescript
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
Understanding Abstract Syntax Trees
read article
ESLint Plugin. What was missed in the doc?
read article
Code search and refactoring tools - `Code Recycle`
currently reading
Code cycle: may be the syntax query that currently supports the most languages
read article
Python, ast, and redbaron
read article
Generate references table from code comments
read article
Effective Refactoring with Codemods
read article
TagTide library: make your HTML editor-friendly and more
read article
What is AST?
read article
Analyzing AST in Go with JSON tools
read article
Deciphering Python: How to use Abstract Syntax Trees (AST) to understand code
read article
Revealing the magic of AST by writing babel plugins
read article
From Parse Tree to Evaluator (featuring Sarah Withee)
read article
Digging deeper into the analysis of Go-code
read article
Take a walk the Go AST
read article
OpenTracing for Go Projects
read article
My journey optimizing the Go Compiler
read article
Static Code Analysis: What it is? How to use it?
read article
How to find what is the dependency of a function, class, or variable in ES6 via AST
read article
JS-X-Ray 1.0
read article
How to work happier with QA with the help of AST
read article
AST Finder – Finding AST nodes from code
read article
Building AST nodes from source code
read article
Adding Contexts via Go AST (Code Instrumentation)
read article
Manipulating AST with JavaScript
read article
Easier TypeScript tooling with TSQuery
read article
Converting TypeScript decorators into static code!
read article
Babel macros
read article
How do template literals in JavaScript work under the hood?
read article
Creating custom JavaScript syntax with Babel
read article
Featured ones: