Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/quasilyte/astnorm
AST normalization experiment
https://github.com/quasilyte/astnorm
ast canonicalize go golang library normalization normalize source-code types
Last synced: 12 days ago
JSON representation
AST normalization experiment
- Host: GitHub
- URL: https://github.com/quasilyte/astnorm
- Owner: quasilyte
- License: mit
- Created: 2019-01-24T09:07:24.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-01-31T20:01:51.000Z (almost 6 years ago)
- Last Synced: 2024-10-11T11:48:20.557Z (28 days ago)
- Topics: ast, canonicalize, go, golang, library, normalization, normalize, source-code, types
- Language: Go
- Homepage:
- Size: 111 KB
- Stars: 45
- Watchers: 8
- Forks: 2
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Go Report Card](https://goreportcard.com/badge/github.com/Quasilyte/astnorm)](https://goreportcard.com/report/github.com/Quasilyte/astnorm)
[![GoDoc](https://godoc.org/github.com/Quasilyte/astnorm?status.svg)](https://godoc.org/github.com/Quasilyte/astnorm)
[![Build Status](https://travis-ci.org/Quasilyte/astnorm.svg?branch=master)](https://travis-ci.org/Quasilyte/astnorm)![logo](/logo.jpg)
# astnorm
Go AST normalization experiment.
> THIS IS NOT A PROPER LIBRARY (yet?).
> DO NOT USE.
> It will probably be completely re-written before it becomes usable.## Normalized code examples
1. Swap values.
Before
After
```go
tmp := xs[i]
xs[i] = ys[i]
ys[i] = tmp
```
```go
xs[i], ys[i] = ys[i], xs[i]
```
2. Remove elements that are equal to `toRemove+1`.
Before
After
```go
const toRemove = 10
var filtered []int
filtered = xs[0:0]
for i := int(0); i < len(xs); i++ {
x := xs[i]
if toRemove+1 != x {
filtered = append(filtered, x)
}
}
return (filtered)
```
```go
filtered := []int(nil)
filtered = xs[:0]
for _, x := range xs {
if x != 11 {
filtered = append(filtered, x)
}
}
return filtered
```
## Usage examples
* [cmd/go-normalize](/cmd/go-normalize): normalize given Go file
* [cmd/grepfunc](/cmd/grepfunc): turn Go code into a pattern for `gogrep` and run itPotential workflow for code searching:
### 1. Code search
* Normalize the entire Go stdlib
* Then normalize your function
* Run `grepfunc` against normalized stdlib
* If function you implemented has implementation under the stdlib, you'll probably find itBasically, instead of stdlib you can use any kind of Go corpus.
Another code search related tasks that can be simplified by `astnorm` are code similarity
evaluation and code duplication detection of any kind.### 2. Static analysis
Suppose we have `badcode.go` file:
```go
package badpkgfunc NotEqual(x1, x2 int) bool {
return (x1) != x1
}
```There is an obvious mistake there, `x1` used twice, but because of extra parenthesis, linters may not detect this issue:
```bash
$ staticcheck badcode.go
# No output
```Let's normalize the input first and then run `staticcheck`:
```bash
go-normalize badcode.go > normalized_badcode.go
staticcheck normalized_badcode.go
normalized_badcode.go:4:9: identical expressions on the left and right side of the '!=' operator (SA4000)
```And we get the warning we deserve!
No changes into `staticcheck` or any other linter are required.See also: [demo script](/example/demo.bash).