ast-grep-cli


Nameast-grep-cli JSON
Version 0.21.3 PyPI version JSON
download
home_pagehttps://ast-grep.github.io/
SummaryStructural Search and Rewrite code at large scale using precise AST pattern.
upload_time2024-05-03 04:43:15
maintainerNone
docs_urlNone
authorHerrington Darkholme <2883231+HerringtonDarkholme@users.noreply.github.com>
requires_pythonNone
licenseNone
keywords ast pattern codemod structural search rewrite
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <p align=center>
  <img src="https://ast-grep.github.io/logo.svg" alt="ast-grep"/>
</p>

<p align="center">
   <img src="https://github.com/ast-grep/ast-grep/actions/workflows/coverage.yaml/badge.svg" alt="coverage badge"/>
   <a href="https://app.codecov.io/gh/ast-grep/ast-grep"><img src="https://codecov.io/gh/ast-grep/ast-grep/branch/main/graph/badge.svg?token=37VX8H2EWV"/></a>
   <a href="https://discord.gg/4YZjf6htSQ" target="_blank"><img alt="Discord" src="https://img.shields.io/discord/1107749847722889217?label=Discord"></a>
   <a href="https://repology.org/project/ast-grep/versions" target="_blank"><img alt="Repology" src="https://repology.org/badge/tiny-repos/ast-grep.svg"></a>
   <img src="https://img.shields.io/github/stars/ast-grep/ast-grep?style=social" alt="Badge"/>
   <img src="https://img.shields.io/github/forks/ast-grep/ast-grep?style=social" alt="Badge"/>
   <img alt="GitHub Sponsors" src="https://img.shields.io/github/sponsors/HerringtonDarkholme?style=social">
</p>


## ast-grep(sg)

ast-grep(sg) is a CLI tool for code structural search, lint, and rewriting.

## Introduction
ast-grep is an AST-based tool to search code by pattern code. Think it as your old-friend `grep` but it matches AST nodes instead of text.
You can write patterns as if you are writing ordinary code. It will match all code that has the same syntactical structure.
You can use `$` sign + upper case letters as wildcard, e.g. `$MATCH`, to match any single AST node. Think it as REGEX dot `.`, except it is not textual.

Try the [online playground](https://ast-grep.github.io/playground.html) for a taste!

## Demo
![demo](https://ast-grep.github.io/image/search-replace.png)

See more screenshots on the [website](https://ast-grep.github.io/).

## Installation
You can install it from [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm), [pip](https://pypi.org/), [cargo](https://doc.rust-lang.org/cargo/getting-started/installation.html), [homebrew](https://brew.sh/), [scoop](https://scoop.sh/) or [MacPorts](https://www.macports.org)!

```bash
npm install --global @ast-grep/cli
pip install ast-grep-cli
cargo install ast-grep

# install via homebrew, thank @henryhchchc
brew install ast-grep

# install via scoop, thank @brian6932
scoop install main/ast-grep

# install via MacPorts
sudo port install ast-grep
```
Or you can build ast-grep from source. You need install rustup, clone the repository and then
```bash
cargo install --path ./crates/cli
```
[Packages](https://repology.org/project/ast-grep/versions) are available on other platforms too.

## Command line usage example

ast-grep has following form.
```
sg --pattern 'var code = $PATTERN' --rewrite 'let code = new $PATTERN' --lang ts
```

### Example

* [Rewrite code in null coalescing operator](https://twitter.com/Hchan_mgn/status/1547061516993699841?s=20&t=ldDoj4U2nq-FRKQkU5GWXA)

```bash
sg -p '$A && $A()' -l ts -r '$A?.()'
```

* [Rewrite](https://twitter.com/Hchan_mgn/status/1561802312846278657) [Zodios](https://github.com/ecyrbe/zodios#migrate-to-v8)
```bash
sg -p 'new Zodios($URL,  $CONF as const,)' -l ts -r 'new Zodios($URL, $CONF)' -i
```

* [Implement eslint rule using YAML.](https://twitter.com/Hchan_mgn/status/1560108625460355073)


## Sponsor
![Sponsors](https://raw.githubusercontent.com/HerringtonDarkholme/sponsors/main/sponsorkit/sponsors.svg)

If you find ast-grep interesting and useful for your work, please [buy me a coffee](https://github.com/sponsors/HerringtonDarkholme)
so I can spend more time on the project!

## Feature Highlight

ast-grep's core is an algorithm to search and replace code based on abstract syntax tree produced by tree-sitter.
It can help you to do lightweight static analysis and massive scale code manipulation in an intuitive way.

Key highlights:

* An intuitive pattern to find and replace AST.
ast-grep's pattern looks like ordinary code you would write every day (you could say the pattern is isomorphic to code).

* jQuery like API for AST traversal and manipulation.

* YAML configuration to write new linting rules or code modification.

* Written in compiled language, with tree-sitter based parsing and utilizing multiple cores.

* Beautiful command line interface :)

ast-grep's vision is to democratize abstract syntax tree magic and to liberate one from cumbersome AST programming!

* If you are an open source library author, ast-grep can help your library users adopt breaking changes more easily.
* if you are a tech lead in your team, ast-grep can help you enforce code best practice tailored to your business need.
* If you are a security researcher, ast-grep can help you write rules much faster.


            

Raw data

            {
    "_id": null,
    "home_page": "https://ast-grep.github.io/",
    "name": "ast-grep-cli",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": "Herrington Darkholme <2883231+HerringtonDarkholme@users.noreply.github.com>",
    "keywords": "ast, pattern, codemod, structural search, rewrite",
    "author": "Herrington Darkholme <2883231+HerringtonDarkholme@users.noreply.github.com>",
    "author_email": "Herrington Darkholme <2883231+HerringtonDarkholme@users.noreply.github.com>",
    "download_url": "https://files.pythonhosted.org/packages/32/ca/0edf397d5df652c8de1fe07ca60af929c98ce182f64be2f9a06e667e10f7/ast_grep_cli-0.21.3.tar.gz",
    "platform": null,
    "description": "<p align=center>\n  <img src=\"https://ast-grep.github.io/logo.svg\" alt=\"ast-grep\"/>\n</p>\n\n<p align=\"center\">\n   <img src=\"https://github.com/ast-grep/ast-grep/actions/workflows/coverage.yaml/badge.svg\" alt=\"coverage badge\"/>\n   <a href=\"https://app.codecov.io/gh/ast-grep/ast-grep\"><img src=\"https://codecov.io/gh/ast-grep/ast-grep/branch/main/graph/badge.svg?token=37VX8H2EWV\"/></a>\n   <a href=\"https://discord.gg/4YZjf6htSQ\" target=\"_blank\"><img alt=\"Discord\" src=\"https://img.shields.io/discord/1107749847722889217?label=Discord\"></a>\n   <a href=\"https://repology.org/project/ast-grep/versions\" target=\"_blank\"><img alt=\"Repology\" src=\"https://repology.org/badge/tiny-repos/ast-grep.svg\"></a>\n   <img src=\"https://img.shields.io/github/stars/ast-grep/ast-grep?style=social\" alt=\"Badge\"/>\n   <img src=\"https://img.shields.io/github/forks/ast-grep/ast-grep?style=social\" alt=\"Badge\"/>\n   <img alt=\"GitHub Sponsors\" src=\"https://img.shields.io/github/sponsors/HerringtonDarkholme?style=social\">\n</p>\n\n\n## ast-grep(sg)\n\nast-grep(sg) is a CLI tool for code structural search, lint, and rewriting.\n\n## Introduction\nast-grep is an AST-based tool to search code by pattern code. Think it as your old-friend `grep` but it matches AST nodes instead of text.\nYou can write patterns as if you are writing ordinary code. It will match all code that has the same syntactical structure.\nYou can use `$` sign + upper case letters as wildcard, e.g. `$MATCH`, to match any single AST node. Think it as REGEX dot `.`, except it is not textual.\n\nTry the [online playground](https://ast-grep.github.io/playground.html) for a taste!\n\n## Demo\n![demo](https://ast-grep.github.io/image/search-replace.png)\n\nSee more screenshots on the [website](https://ast-grep.github.io/).\n\n## Installation\nYou can install it from [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm), [pip](https://pypi.org/), [cargo](https://doc.rust-lang.org/cargo/getting-started/installation.html), [homebrew](https://brew.sh/), [scoop](https://scoop.sh/) or [MacPorts](https://www.macports.org)!\n\n```bash\nnpm install --global @ast-grep/cli\npip install ast-grep-cli\ncargo install ast-grep\n\n# install via homebrew, thank @henryhchchc\nbrew install ast-grep\n\n# install via scoop, thank @brian6932\nscoop install main/ast-grep\n\n# install via MacPorts\nsudo port install ast-grep\n```\nOr you can build ast-grep from source. You need install rustup, clone the repository and then\n```bash\ncargo install --path ./crates/cli\n```\n[Packages](https://repology.org/project/ast-grep/versions) are available on other platforms too.\n\n## Command line usage example\n\nast-grep has following form.\n```\nsg --pattern 'var code = $PATTERN' --rewrite 'let code = new $PATTERN' --lang ts\n```\n\n### Example\n\n* [Rewrite code in null coalescing operator](https://twitter.com/Hchan_mgn/status/1547061516993699841?s=20&t=ldDoj4U2nq-FRKQkU5GWXA)\n\n```bash\nsg -p '$A && $A()' -l ts -r '$A?.()'\n```\n\n* [Rewrite](https://twitter.com/Hchan_mgn/status/1561802312846278657) [Zodios](https://github.com/ecyrbe/zodios#migrate-to-v8)\n```bash\nsg -p 'new Zodios($URL,  $CONF as const,)' -l ts -r 'new Zodios($URL, $CONF)' -i\n```\n\n* [Implement eslint rule using YAML.](https://twitter.com/Hchan_mgn/status/1560108625460355073)\n\n\n## Sponsor\n![Sponsors](https://raw.githubusercontent.com/HerringtonDarkholme/sponsors/main/sponsorkit/sponsors.svg)\n\nIf you find ast-grep interesting and useful for your work, please [buy me a coffee](https://github.com/sponsors/HerringtonDarkholme)\nso I can spend more time on the project!\n\n## Feature Highlight\n\nast-grep's core is an algorithm to search and replace code based on abstract syntax tree produced by tree-sitter.\nIt can help you to do lightweight static analysis and massive scale code manipulation in an intuitive way.\n\nKey highlights:\n\n* An intuitive pattern to find and replace AST.\nast-grep's pattern looks like ordinary code you would write every day (you could say the pattern is isomorphic to code).\n\n* jQuery like API for AST traversal and manipulation.\n\n* YAML configuration to write new linting rules or code modification.\n\n* Written in compiled language, with tree-sitter based parsing and utilizing multiple cores.\n\n* Beautiful command line interface :)\n\nast-grep's vision is to democratize abstract syntax tree magic and to liberate one from cumbersome AST programming!\n\n* If you are an open source library author, ast-grep can help your library users adopt breaking changes more easily.\n* if you are a tech lead in your team, ast-grep can help you enforce code best practice tailored to your business need.\n* If you are a security researcher, ast-grep can help you write rules much faster.\n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Structural Search and Rewrite code at large scale using precise AST pattern.",
    "version": "0.21.3",
    "project_urls": {
        "Changelog": "https://github.com/ast-grep/ast-grep/blob/main/CHANGELOG.md",
        "Documentation": "https://ast-grep.github.io/",
        "Homepage": "https://ast-grep.github.io/",
        "Repository": "https://github.com/ast-grep/ast-grep"
    },
    "split_keywords": [
        "ast",
        " pattern",
        " codemod",
        " structural search",
        " rewrite"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "997cdd0050e36a4c9161dd458ab073188b9157f5e01ea32da3ef19ed12904fcf",
                "md5": "8a25dab5c9f91f534d4da89987454028",
                "sha256": "971b0124b8fc350d54db7a188519fe6cba8c18ff041f7efcf765a3ee51269acc"
            },
            "downloads": -1,
            "filename": "ast_grep_cli-0.21.3-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl",
            "has_sig": false,
            "md5_digest": "8a25dab5c9f91f534d4da89987454028",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 26970598,
            "upload_time": "2024-05-03T04:42:52",
            "upload_time_iso_8601": "2024-05-03T04:42:52.832642Z",
            "url": "https://files.pythonhosted.org/packages/99/7c/dd0050e36a4c9161dd458ab073188b9157f5e01ea32da3ef19ed12904fcf/ast_grep_cli-0.21.3-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "cd2a111b3b1b49257e26deb82d0b350076789971adff582fa7292920f851f565",
                "md5": "61fa4df42f311da9f57f6cf504ebcc2b",
                "sha256": "dd9e22f6f5a291e678afcefc795b1b4e57fb4436731851dc9bafe6b4601ce944"
            },
            "downloads": -1,
            "filename": "ast_grep_cli-0.21.3-py3-none-macosx_10_12_x86_64.whl",
            "has_sig": false,
            "md5_digest": "61fa4df42f311da9f57f6cf504ebcc2b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 13191534,
            "upload_time": "2024-05-03T04:42:56",
            "upload_time_iso_8601": "2024-05-03T04:42:56.584180Z",
            "url": "https://files.pythonhosted.org/packages/cd/2a/111b3b1b49257e26deb82d0b350076789971adff582fa7292920f851f565/ast_grep_cli-0.21.3-py3-none-macosx_10_12_x86_64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "776b3b963e2370ac66669f8cb0849ff6cbd7344b3a99c8a923992f05f975650f",
                "md5": "cc25d3e6b83ef6905c2179daafe5c556",
                "sha256": "7f3d08d22fa0a751fa1638cfdd2946d86646b1762204e1663acccb1889fdd692"
            },
            "downloads": -1,
            "filename": "ast_grep_cli-0.21.3-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl",
            "has_sig": false,
            "md5_digest": "cc25d3e6b83ef6905c2179daafe5c556",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 13526230,
            "upload_time": "2024-05-03T04:43:00",
            "upload_time_iso_8601": "2024-05-03T04:43:00.005347Z",
            "url": "https://files.pythonhosted.org/packages/77/6b/3b963e2370ac66669f8cb0849ff6cbd7344b3a99c8a923992f05f975650f/ast_grep_cli-0.21.3-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "36b887a63a5a3b83c3e9d02e92fdd7fc0790841a8f3bafd4f70e6b07f2e0f4c4",
                "md5": "241c344cab6da07f3321947a89301dc9",
                "sha256": "c2d058aec25e2fbadceabbf7ace881aaaa016ccc545e9cdc67dfedde9ced5fb7"
            },
            "downloads": -1,
            "filename": "ast_grep_cli-0.21.3-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
            "has_sig": false,
            "md5_digest": "241c344cab6da07f3321947a89301dc9",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 13484063,
            "upload_time": "2024-05-03T04:43:03",
            "upload_time_iso_8601": "2024-05-03T04:43:03.254664Z",
            "url": "https://files.pythonhosted.org/packages/36/b8/87a63a5a3b83c3e9d02e92fdd7fc0790841a8f3bafd4f70e6b07f2e0f4c4/ast_grep_cli-0.21.3-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f7035a6e37ae84be99c6737c90c78b3e197307ca0e83e5734b845bd5f1536ea7",
                "md5": "7be6f3a3d4e028df3b0f19978eb121e4",
                "sha256": "6dc39e5fd7db88bb27db27fdc8bd6ebd354849f3b60f7ee464b14a138fdf8cbc"
            },
            "downloads": -1,
            "filename": "ast_grep_cli-0.21.3-py3-none-win32.whl",
            "has_sig": false,
            "md5_digest": "7be6f3a3d4e028df3b0f19978eb121e4",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 14464254,
            "upload_time": "2024-05-03T04:43:06",
            "upload_time_iso_8601": "2024-05-03T04:43:06.672978Z",
            "url": "https://files.pythonhosted.org/packages/f7/03/5a6e37ae84be99c6737c90c78b3e197307ca0e83e5734b845bd5f1536ea7/ast_grep_cli-0.21.3-py3-none-win32.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a5149c4009e40391aaed9f7cc6c4b5cd3ca33a425f68665792c0c0132a8d0adf",
                "md5": "1d016998b96e2c726d7c124e82b1c813",
                "sha256": "241c690d86c95af9961e4cc78b06d8fcb1e425543f03e43ad068bc831432d3aa"
            },
            "downloads": -1,
            "filename": "ast_grep_cli-0.21.3-py3-none-win_amd64.whl",
            "has_sig": false,
            "md5_digest": "1d016998b96e2c726d7c124e82b1c813",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 15068827,
            "upload_time": "2024-05-03T04:43:09",
            "upload_time_iso_8601": "2024-05-03T04:43:09.431300Z",
            "url": "https://files.pythonhosted.org/packages/a5/14/9c4009e40391aaed9f7cc6c4b5cd3ca33a425f68665792c0c0132a8d0adf/ast_grep_cli-0.21.3-py3-none-win_amd64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1da971869843394637d542bb5a5deaa2129ded64a476d2849a602649e293e296",
                "md5": "069ff9b9171d484cca72f5917942bd07",
                "sha256": "a5b9a48d9521c39b2791e1f36c2bd5c48f765343e1661b11987183ff1e1b185c"
            },
            "downloads": -1,
            "filename": "ast_grep_cli-0.21.3-py3-none-win_arm64.whl",
            "has_sig": false,
            "md5_digest": "069ff9b9171d484cca72f5917942bd07",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 13555645,
            "upload_time": "2024-05-03T04:43:12",
            "upload_time_iso_8601": "2024-05-03T04:43:12.745689Z",
            "url": "https://files.pythonhosted.org/packages/1d/a9/71869843394637d542bb5a5deaa2129ded64a476d2849a602649e293e296/ast_grep_cli-0.21.3-py3-none-win_arm64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "32ca0edf397d5df652c8de1fe07ca60af929c98ce182f64be2f9a06e667e10f7",
                "md5": "389b33fd405b2f9be3f5303e860dce20",
                "sha256": "9007b53e5d398839a92c8348469b8494c9a333900ef7b8f92d09bf661da5c742"
            },
            "downloads": -1,
            "filename": "ast_grep_cli-0.21.3.tar.gz",
            "has_sig": false,
            "md5_digest": "389b33fd405b2f9be3f5303e860dce20",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 157770,
            "upload_time": "2024-05-03T04:43:15",
            "upload_time_iso_8601": "2024-05-03T04:43:15.366614Z",
            "url": "https://files.pythonhosted.org/packages/32/ca/0edf397d5df652c8de1fe07ca60af929c98ce182f64be2f9a06e667e10f7/ast_grep_cli-0.21.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-03 04:43:15",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ast-grep",
    "github_project": "ast-grep",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "ast-grep-cli"
}
        
Elapsed time: 0.26617s