{"id":16218086,"url":"https://github.com/quasilyte/astnorm","last_synced_at":"2025-03-19T10:30:53.271Z","repository":{"id":57519978,"uuid":"167337132","full_name":"quasilyte/astnorm","owner":"quasilyte","description":"AST normalization experiment","archived":false,"fork":false,"pushed_at":"2019-01-31T20:01:51.000Z","size":114,"stargazers_count":45,"open_issues_count":9,"forks_count":2,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-02-28T17:57:19.708Z","etag":null,"topics":["ast","canonicalize","go","golang","library","normalization","normalize","source-code","types"],"latest_commit_sha":null,"homepage":"","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/quasilyte.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}},"created_at":"2019-01-24T09:07:24.000Z","updated_at":"2023-07-17T19:37:34.000Z","dependencies_parsed_at":"2022-09-05T09:41:33.668Z","dependency_job_id":null,"html_url":"https://github.com/quasilyte/astnorm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quasilyte%2Fastnorm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quasilyte%2Fastnorm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quasilyte%2Fastnorm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quasilyte%2Fastnorm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/quasilyte","download_url":"https://codeload.github.com/quasilyte/astnorm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243982182,"owners_count":20378605,"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","canonicalize","go","golang","library","normalization","normalize","source-code","types"],"created_at":"2024-10-10T11:48:28.064Z","updated_at":"2025-03-19T10:30:52.890Z","avatar_url":"https://github.com/quasilyte.png","language":"Go","readme":"[![Go Report Card](https://goreportcard.com/badge/github.com/Quasilyte/astnorm)](https://goreportcard.com/report/github.com/Quasilyte/astnorm)\n[![GoDoc](https://godoc.org/github.com/Quasilyte/astnorm?status.svg)](https://godoc.org/github.com/Quasilyte/astnorm)\n[![Build Status](https://travis-ci.org/Quasilyte/astnorm.svg?branch=master)](https://travis-ci.org/Quasilyte/astnorm)\n\n![logo](/logo.jpg)\n\n# astnorm\n\nGo AST normalization experiment.\n\n\u003e THIS IS NOT A PROPER LIBRARY (yet?).\u003cbr\u003e\n\u003e DO NOT USE.\u003cbr\u003e\n\u003e It will probably be completely re-written before it becomes usable.\n\n## Normalized code examples\n\n1. Swap values.\n\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003cth\u003eBefore\u003c/th\u003e\n    \u003cth\u003eAfter\u003c/th\u003e\n  \u003c/tr\u003e\n  \n  \u003ctr\u003e\u003ctd\u003e\n  \n```go\ntmp := xs[i]\nxs[i] = ys[i]\nys[i] = tmp\n```\n  \n  \u003c/td\u003e\u003ctd\u003e\n     \n ```go\nxs[i], ys[i] = ys[i], xs[i]\n```\n     \n  \u003c/td\u003e\u003c/tr\u003e\n\u003c/table\u003e\n\n2. Remove elements that are equal to `toRemove+1`.\n\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003cth\u003eBefore\u003c/th\u003e\n    \u003cth\u003eAfter\u003c/th\u003e\n  \u003c/tr\u003e\n  \n  \u003ctr\u003e\u003ctd\u003e\n  \n```go\nconst toRemove = 10\nvar filtered []int\nfiltered = xs[0:0]\nfor i := int(0); i \u003c len(xs); i++ {\n        x := xs[i]\n        if toRemove+1 != x {\n                filtered = append(filtered, x)\n        }\n}\nreturn (filtered)\n```\n  \n  \u003c/td\u003e\u003ctd\u003e\n     \n ```go\nfiltered := []int(nil)\nfiltered = xs[:0]\nfor _, x := range xs {\n        if x != 11 {\n                filtered = append(filtered, x)\n        }\n}\nreturn filtered\n```\n     \n  \u003c/td\u003e\u003c/tr\u003e\n\u003c/table\u003e\n\n## Usage examples\n\n* [cmd/go-normalize](/cmd/go-normalize): normalize given Go file\n* [cmd/grepfunc](/cmd/grepfunc): turn Go code into a pattern for `gogrep` and run it\n\nPotential workflow for code searching:\n\n### 1. Code search\n\n* Normalize the entire Go stdlib\n* Then normalize your function\n* Run `grepfunc` against normalized stdlib\n* If function you implemented has implementation under the stdlib, you'll probably find it\n\nBasically, instead of stdlib you can use any kind of Go corpus.\n\nAnother code search related tasks that can be simplified by `astnorm` are code similarity\nevaluation and code duplication detection of any kind.\n\n### 2. Static analysis\n\nSuppose we have `badcode.go` file:\n\n```go\npackage badpkg\n\nfunc NotEqual(x1, x2 int) bool {\n\treturn (x1) != x1\n}\n```\n\nThere is an obvious mistake there, `x1` used twice, but because of extra parenthesis, linters may not detect this issue:\n\n```bash\n$ staticcheck badcode.go\n# No output\n```\n\nLet's normalize the input first and then run `staticcheck`:\n\n```bash\ngo-normalize badcode.go \u003e normalized_badcode.go\nstaticcheck normalized_badcode.go\nnormalized_badcode.go:4:9: identical expressions on the left and right side of the '!=' operator (SA4000)\n```\n\nAnd we get the warning we deserve!\nNo changes into `staticcheck` or any other linter are required.\n\nSee also: [demo script](/example/demo.bash).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquasilyte%2Fastnorm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fquasilyte%2Fastnorm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquasilyte%2Fastnorm/lists"}