{"id":13724232,"url":"https://github.com/tamayika/gaq","last_synced_at":"2026-02-14T20:41:28.326Z","repository":{"id":144210229,"uuid":"155074067","full_name":"tamayika/gaq","owner":"tamayika","description":"GAQ(Go Ast Query) is the library to query ast.Node children like JavaScript querySelector or querySelectorAll for Go.","archived":false,"fork":false,"pushed_at":"2018-12-16T06:41:56.000Z","size":69,"stargazers_count":25,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-08-04T01:24:23.007Z","etag":null,"topics":["ast","golang","golang-library","query"],"latest_commit_sha":null,"homepage":"https://github.com/tamayika/gaq","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tamayika.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-10-28T13:28:23.000Z","updated_at":"2024-06-01T07:49:40.000Z","dependencies_parsed_at":"2024-01-06T01:03:04.570Z","dependency_job_id":"b9a84e6e-1147-49e5-8ac8-8eb9322b8263","html_url":"https://github.com/tamayika/gaq","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tamayika%2Fgaq","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tamayika%2Fgaq/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tamayika%2Fgaq/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tamayika%2Fgaq/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tamayika","download_url":"https://codeload.github.com/tamayika/gaq/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224628455,"owners_count":17343343,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ast","golang","golang-library","query"],"created_at":"2024-08-03T01:01:52.428Z","updated_at":"2026-02-14T20:41:28.296Z","avatar_url":"https://github.com/tamayika.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"[![CircleCI](https://circleci.com/gh/tamayika/gaq.svg?style=svg)](https://circleci.com/gh/tamayika/gaq)\n\n# GAQ(Go AST Query)\n\nGAQ is the library to query `ast.Node` children like JavaScript `querySelector` or `querySelectorAll`.\n\n## Table of Contents\n\n\u003c!-- TOC --\u003e\n\n- [GAQ(Go AST Query)](#gaqgo-ast-query)\n    - [Table of Contents](#table-of-contents)\n- [Install](#install)\n    - [Library](#library)\n        - [Usage](#usage)\n    - [CLI](#cli)\n        - [Usage](#usage-1)\n            - [Filter Mode](#filter-mode)\n            - [Replace Mode](#replace-mode)\n- [Query Specfication](#query-specfication)\n    - [Supported Combinators](#supported-combinators)\n    - [Supported Attribute Syntax](#supported-attribute-syntax)\n    - [Supported Pseudo Class](#supported-pseudo-class)\n\n\u003c!-- /TOC --\u003e\n\n# Install\n\n## Library\n\n```sh\ngo get github.com/tamayika/gaq/pkg/gaq\n```\n\n### Usage\n\nPlease refer [pkg/gaq/example_test.go](pkg/gaq/example_test.go)\n\n## CLI\n\n```sh\ngo get github.com/tamayika/gaq\n```\n\nor there are binaries for various os at [Releases](https://github.com/tamayika/gaq/releases).\n\n### Usage\n\nYou can see help with `--help` flag.\n\n```\n$ gaq --help\ngaq is the cli tool to query ast node.\nTypical usage is\n\n  cat \u003cgo file path\u003e | gaq \u003cQuery\u003e\n  cat \u003cgo file path\u003e | gaq -m replace \u003cQuery\u003e \u003cReplace command\u003e\n\nPlease see details at https://github.com/tamayika/gaq\n\nUsage:\n  gaq \u003cQuery\u003e [flags]\n\nFlags:\n  -f, --format string   Output format, 'text' or 'pos'. Default is 'text' (default \"text\")\n  -h, --help            help for gaq\n  -m, --mode string     Execution mode, 'filter' or 'replace'. Default is 'filter' (default \"filter\")\n      --version         version for gaq\n```\n\n#### Filter Mode\n\nDefault mode is `filter`.\n\nFor example, `File \u003e Ident` query filters package name in `main.go`\n\n```\n$ cat main.go | gaq \"File \u003e Ident\"\nmain\n```\n\n#### Replace Mode\n\nYou can replace matched node text by `replace` mode.\n\nFor example, below command exports functions except `main` function.\n\n```\n$ cat main.go | gaq -m replace \"FuncDecl \u003e Ident:not([Name='main'])\" -- sed -e \"s/^\\(.\\)/\\U\\1/\"\n```\n\nIn `replace` mode, below sequence is executed for each matched node\n\n1. command is spawned\n2. gaq passes node text as stdin\n3. wait command exit\n4. replace node text by command output\n\nYou can use any tool which gets input from stdin and puts result to stdout, `sed`, `awk`, `tr` etc.\n\n# Query Specfication\n\nHeavily inspired by CSS Selector.\n\n```\nQuery:\n    Selector [',' Selector]\n\nSelector:\n    SimpleSelector [Combinator SimpleSelector]\n\nSimpleSelector:\n    [[NodeName] [Attribute] [Pseudo]]!\n\nAttribute:\n    '[' Field [ AttributeOperator Value ] ']'\n\nPseudo:\n    ':' Name [ '(' Expression ')' ]\n```\n\nFull used example\n```\n    File \u003e Ident[Name*='test']:first-child, File \u003e Ident[Name*='test']:last-child\n```\n\nHere, NodeName is one of node type of [ast](https://golang.org/pkg/go/ast/).\nFor example, if you want to find `*ast.StructType`, NodeName is `StructType`.\nYou can also specify `*` as any node type.\n\nIf you don't know NodeName, VSCode extension [vscode-go-ast-explorer](https://github.com/tamayika/vscode-go-ast-explorer) will help you to find it out.\n\n## Supported Combinators\n\n|  Combinator  |            Name             |                                                 Meaning                                                 |\n| ------------ | --------------------------- | ------------------------------------------------------------------------------------------------------- |\n| +            | Adjacent sibling combinator | The second node directly follows the first, and both share the same parent.                             |\n| ~            | General sibling combinator  | The second node follows the first (though not necessarily immediately), and both share the same parent. |\n| \u003e            | Child combinator            | Selects nodes that are direct children of the first node.                                               |\n| (whitespace) | Descendant combinator       | Selects nodes that are descendants of the first node.                                                   |\n\n## Supported Attribute Syntax\n\n|    Syntax     |                                                                 Meaning                                                                 |\n| ------------- | --------------------------------------------------------------------------------------------------------------------------------------- |\n| `[f]`         | Represents Node with an field name of f.                                                                                                |\n| `[f=value]`   | Represents Node with an field name of f whose value is exactly value.                                                                   |\n| `[f~=value]`  | Represents Node with an field name of f whose value is a whitespace-separated list of words, one of which is exactly value.             |\n| `[f\\|=value]` | Represents Node with an field name of f whose value can be exactly value or can begin with value immediately followed by a hyphen, `-`. |\n| `[f^=value]`  | Represents Node with an field name of f whose value is prefixed (preceded) by value.                                                    |\n| `[f$=value]`  | Represents Node with an field name of f whose value is suffixed (followed) by value.                                                    |\n| `[f*=value]`  | Represents Node with an field name of f whose value contains at least one occurrence of value within the string.                        |\n\n## Supported Pseudo Class\n\n|      Syntax      |                                                                             Meaning                                                                             |\n| ---------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| `:empty`         | Represents nodes that has no children. `ast.CommentGroup` and `ast.Comment` are ignored.                                                                        |\n| `:first-child`   | Represents the first node among a group of sibling nodes.                                                                                                       |\n| `:first-of-type` | Represents the first node of its type among a group of sibling nodes.                                                                                           |\n| `:has(Query)`    | Represents a node if any of the selectors passed as parameters, match at least one node.                                                                        |\n| `:is(Query)`     | Represents nodes that can be selected by one of the selectors in that list                                                                                      |\n| `:last-child`    | Represents the last node among a group of sibling nodes.                                                                                                        |\n| `:last-of-type`  | Represents the last node of its type among a group of sibling nodes.                                                                                            |\n| `:not(Query)`    | Represents nodes that do not match a list of selectors.                                                                                                         |\n| `:root`          | Represents the root node. \u003cbr\u003eWhen `gaq.Parse(source string)` is used, the root node is `*ast.File`. \u003cbr\u003eWhen `gaq.ParseNode(n ast.Node)` is used, the root node is `n`. |","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftamayika%2Fgaq","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftamayika%2Fgaq","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftamayika%2Fgaq/lists"}