-
Notifications
You must be signed in to change notification settings - Fork 208
Go implementation of Piranha #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
ac2707f
7ac72d1
36445bf
38274f7
fbbc08c
47b1f0e
38680bd
b37339e
6ff77de
3760732
448cabc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # PiranhaGo | ||
| PiranhaGo is runnable now. | ||
| Instructions:- | ||
| 1. To build the package run `go build -o piranha`. Dependencies will install automatically and they are given in `go.mod` file. | ||
| 2. Below are the instructions for running for the single file. | ||
| ``` | ||
| Inputs from args | ||
| Usage: ./piranha [-h] -p PROPERTIES -s SOURCE_FILE -f STALE_FLAG [-treated] [-o OUTPUT] | ||
| Required arguments: | ||
| -s SOURCE_FILE: Path of the file to be refactored | ||
| -p PROPERTIES: Configuration file (json format) for Piranha. | ||
| -f STALE_FLAG: Name of the stale flag | ||
| Optional arguments: | ||
| -h: Show the options and exit. | ||
| -treated: If this is given, then flag is treated, | ||
| otherwise it is control. | ||
| -o OUTPUT: Destination of the refactored output from piranha. If -o is not provided, then the source file is updated in place. | ||
| ``` | ||
| 3. To do a test run, run piranha on `example/testExample.go`. Run `./piranha -p properties.json -s ./example/testExample.go -o ./example/treatedExample.go -f staleFlag` command in root directory. You will get your refracted file as `/example/treatedExample.go`. | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| Copyright (c) 2021 Uber Technologies, Inc. | ||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file | ||
|
|
||
| except in compliance with the License. You may obtain a copy of the License at | ||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software distributed under the | ||
|
|
||
| License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| express or implied. See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
| package testfiles | ||
|
|
||
| import "fmt" | ||
|
|
||
| func testExpressions(ge GoExamples) { | ||
| var x, y bool = false, false | ||
|
|
||
| if ge.flagMthds.treatedBehaviour(staleFlag) || x { | ||
| fmt.Println("treated || of treatedBehaviour") | ||
|
||
| } else { | ||
| fmt.Println("control || of treatedBehaviour") | ||
| } | ||
|
|
||
| if ge.flagMthds.treatedBehaviour(staleFlag) && x { | ||
| fmt.Println("treated && of treatedBehaviour") | ||
| } else { | ||
| fmt.Println("control && of treatedBehaviour") | ||
| } | ||
|
|
||
| if ge.flagMthds.treatedBehaviour(staleFlag) && (x || y) { | ||
| fmt.Println("treated && of || of treatedBehaviour") | ||
| } else { | ||
| fmt.Println("control && of || of treatedBehaviour") | ||
| } | ||
|
|
||
| if ge.flagMthds.treatedBehaviour(staleFlag) && (x && y) { | ||
| fmt.Println("treated && of && of treatedBehaviour") | ||
| } else { | ||
| fmt.Println("control && of && of treatedBehaviour") | ||
| } | ||
|
|
||
| if ge.flagMthds.treatedBehaviour(staleFlag) && y == x { | ||
| fmt.Println("treated && equals of treatedBehaviour") | ||
| } else { | ||
| fmt.Println("control && equals of treatedBehaviour") | ||
| } | ||
|
|
||
| if ge.flagMthds.controlBehaviour(staleFlag) || y == x { | ||
| fmt.Println("treated || equals of controlBehaviour") | ||
| } else { | ||
| fmt.Println("control || equals of controlBehaviour") | ||
| } | ||
|
|
||
| if ge.flagMthds.controlBehaviour(staleFlag) && y && x { | ||
| fmt.Println("treated && and && of controlBehaviour") | ||
| } else { | ||
| fmt.Println("control && and && of controlBehaviour") | ||
| } | ||
|
|
||
| if ge.flagMthds.controlBehaviour(staleFlag) || y || x { | ||
| fmt.Println("treated || && || of controlBehaviour") | ||
| } else { | ||
| fmt.Println("control || && || of controlBehaviour") | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| Copyright (c) 2021 Uber Technologies, Inc. | ||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file | ||
|
|
||
| except in compliance with the License. You may obtain a copy of the License at | ||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software distributed under the | ||
|
|
||
| License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| express or implied. See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
| package testfiles | ||
|
|
||
| import "fmt" | ||
|
|
||
| func testExpressions(ge GoExamples) { | ||
| var x, y bool = false, false | ||
|
|
||
| if x { | ||
| fmt.Println("treated || of treatedBehaviour") | ||
| } else { | ||
| fmt.Println("control || of treatedBehaviour") | ||
| } | ||
|
|
||
| fmt.Println("control && of treatedBehaviour") | ||
| fmt.Println("control && of || of treatedBehaviour") | ||
| fmt.Println("control && of && of treatedBehaviour") | ||
| fmt.Println("control && equals of treatedBehaviour") | ||
| fmt.Println("treated || equals of controlBehaviour") | ||
|
|
||
| if y && x { | ||
| fmt.Println("treated && and && of controlBehaviour") | ||
| } else { | ||
| fmt.Println("control && and && of controlBehaviour") | ||
| } | ||
|
|
||
| fmt.Println("treated || && || of controlBehaviour") | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| module github.com/PiranhaGo | ||
|
|
||
| go 1.16 | ||
|
|
||
| require github.com/dave/dst v0.26.2 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| github.com/dave/dst v0.26.2 h1:lnxLAKI3tx7MgLNVDirFCsDTlTG9nKTk7GcptKcWSwY= | ||
| github.com/dave/dst v0.26.2/go.mod h1:UMDJuIRPfyUCC78eFuB+SV/WI8oDeyFDvM/JR6NI3IU= | ||
| github.com/dave/gopackages v0.0.0-20170318123100-46e7023ec56e/go.mod h1:i00+b/gKdIDIxuLDFob7ustLAVqhsZRk2qVZrArELGQ= | ||
| github.com/dave/jennifer v1.2.0/go.mod h1:fIb+770HOpJ2fmN9EPPKOqm1vMGhB+TwXKMZhrIygKg= | ||
| github.com/dave/kerr v0.0.0-20170318121727-bc25dd6abe8e/go.mod h1:qZqlPyPvfsDJt+3wHJ1EvSXDuVjFTK0j2p/ca+gtsb8= | ||
| github.com/dave/rebecca v0.9.1/go.mod h1:N6XYdMD/OKw3lkF3ywh8Z6wPGuwNFDNtWYEMFWEmXBA= | ||
| github.com/google/pprof v0.0.0-20181127221834-b4f47329b966/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= | ||
| github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= | ||
| github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= | ||
| github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= | ||
| github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= | ||
| github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ= | ||
| github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= | ||
| github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= | ||
| golang.org/x/arch v0.0.0-20180920145803-b19384d3c130/go.mod h1:cYlCBUl1MsqxdiKgmc4uh7TxZfWSFLOGSRR090WDxt8= | ||
| golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
| golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= | ||
| golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ= | ||
| golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= | ||
| golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= | ||
| golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= | ||
| golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= | ||
| golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
| golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
| golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
| golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
| golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
| golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
| golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= | ||
| golang.org/x/tools v0.0.0-20200509030707-2212a7e161a5 h1:MeC2gMlMdkd67dn17MEby3rGXRxZtWeiRXOnISfTQ74= | ||
| golang.org/x/tools v0.0.0-20200509030707-2212a7e161a5/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= | ||
| golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
| golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
| golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= | ||
| golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
| gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
| gopkg.in/src-d/go-billy.v4 v4.3.0/go.mod h1:tm33zBoOwxjYHZIE+OV8bxTWFMJLrconzFMd38aARFk= |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /* | ||
| Copyright (c) 2021 Uber Technologies, Inc. | ||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file | ||
|
|
||
| except in compliance with the License. You may obtain a copy of the License at | ||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software distributed under the | ||
|
|
||
| License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| express or implied. See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
| package main | ||
|
|
||
| import ( | ||
| "os" | ||
|
|
||
| "github.com/PiranhaGo/src" | ||
| ) | ||
|
|
||
| func main() { | ||
| src.RunPiranha(os.Args) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "methodProperties": [ | ||
| { | ||
| "methodName": "treatedBehaviour", | ||
| "flagType": "treated", | ||
| "argumentIndex": 0 | ||
| }, | ||
| { | ||
| "methodName": "controlBehaviour", | ||
| "flagType": "control", | ||
| "argumentIndex": 0 | ||
| }, | ||
| { | ||
| "methodName": "commonBehaviour", | ||
| "flagType": "treated", | ||
| "argumentIndex": 1 | ||
| } | ||
| ] | ||
| } | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| /* | ||
| Copyright (c) 2021 Uber Technologies, Inc. | ||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file | ||
|
|
||
| except in compliance with the License. You may obtain a copy of the License at | ||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software distributed under the | ||
|
|
||
| License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| express or implied. See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
| package src | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "go/parser" | ||
| "go/token" | ||
| "log" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/dave/dst" | ||
| "github.com/dave/dst/decorator" | ||
| ) | ||
|
|
||
| /* | ||
| Inputs from args | ||
| Usage: ./piranha [-h] -p PROPERTIES -s SOURCE_FILE -f STALE_FLAG [-treated] [-o OUTPUT] | ||
| Required arguments: | ||
| -s SOURCE_FILE: Path of the file to be refactored | ||
| -p PROPERTIES: Configuration file (json format) for Piranha. | ||
| -f STALE_FLAG: Name of the stale flag | ||
| Optional arguments: | ||
| -h: Show the options and exit. | ||
| -treated: If this is given, then flag is treated, | ||
| otherwise it is control. | ||
| -o OUTPUT: Destination of the refactored output from piranha. If -o is not provided, then the source file is updated in place. | ||
| */ | ||
| func help() { | ||
| //Help message | ||
| fmt.Println( | ||
| "Usage: ./piranha [-h] -p PROPERTIES -s SOURCE_FILE -f STALE_FLAG [-treated] [-o OUTPUT]", | ||
| "\nRequired arguments:", | ||
| "\n\t\t\t-s SOURCE_FILE: Path of the file to be refactored.", | ||
| "\n\t\t\t-p PROPERTIES: Configuration file (json format) for Piranha.", | ||
| "\n\t\t\t-f STALE_FLAG: Name of the stale flag.", | ||
| "\nOptional arguments:", | ||
| "\n\t\t\t-h: Show the options and exit.", | ||
| "\n\t\t\t-treated: If this is given, then flag is treated,", | ||
| "\n\t\t\totherwise it is control.", | ||
| "\n\t\t\t-o OUTPUT: Destination of the refactored output from piranha.", | ||
| "\n\t\t\tIf -o is not provided, then the source file is updated in place.") | ||
| } | ||
|
|
||
| // RunPiranha : the main function for the piranha tool | ||
| func RunPiranha(inArgs []string) { | ||
| var sourceFile, configFile, flagName, outputFileName string = "", "", "", "" | ||
|
||
| var isTreated = false | ||
| sizeOfArgs := len(inArgs) | ||
| if len(inArgs) < 2 { | ||
|
||
| help() | ||
| return | ||
| } | ||
| for index, arg := range inArgs { | ||
| switch arg { | ||
| case "-h": | ||
| help() | ||
| return | ||
| case "-p": | ||
| if index+1 < sizeOfArgs { | ||
| configFile = inArgs[index+1] | ||
| if !strings.HasSuffix(configFile, ".json") { | ||
| return | ||
|
||
| } | ||
| } | ||
| case "-s": | ||
| if index+1 < sizeOfArgs { | ||
| sourceFile = inArgs[index+1] | ||
| if !strings.HasSuffix(sourceFile, ".go") { | ||
| return | ||
| } | ||
| } | ||
| case "-f": | ||
| if index+1 < sizeOfArgs { | ||
| flagName = inArgs[index+1] | ||
| } | ||
| case "-treated": | ||
| isTreated = true | ||
| case "-o": | ||
| if index+1 < sizeOfArgs { | ||
| outputFileName = inArgs[index+1] | ||
| } | ||
| default: | ||
| break | ||
|
||
| } | ||
| } | ||
|
|
||
| if flagName == "" { | ||
| fmt.Println("Please provide a flag.") | ||
|
||
| } | ||
| if sourceFile == "" { | ||
| fmt.Println("Please provide a source file that is to be refactored.") | ||
| } | ||
| if configFile == "" { | ||
| fmt.Println("Please provide a config file. See README for more instructions.") | ||
| } | ||
| if flagName == "" || sourceFile == "" || configFile == "" { | ||
|
||
| fmt.Println("For more info, run ./piranha -h.") | ||
| return | ||
| } | ||
|
|
||
| fs := token.NewFileSet() | ||
| parsed, err := decorator.ParseFile(fs, sourceFile, nil, parser.ParseComments) | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
|
|
||
| var cleaner staleFlagCleaner | ||
| cleaner.init(configFile, flagName, isTreated) | ||
| newRoot := cleaner.run(parsed) | ||
| //////////////////////// | ||
| // For debugging purpose. It prints out the ast. | ||
| // spew.Dump(newRoot) | ||
| /////////////////////// | ||
|
||
|
|
||
| if outputFileName == "" { | ||
| outputFileName = sourceFile | ||
| } | ||
| outputFile, err := os.Create(outputFileName) | ||
| /* | ||
| Here we are typecasting newRoot to dst.File.It is safe because the root of AST | ||
|
||
| always starts with the dst.File type. | ||
| */ | ||
| decorator.Fprint(outputFile, newRoot.(*dst.File)) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want
controlto be implicit? I believe most other Piranha versions take an explicit string, which is eithertreatmentorcontrol(and, for some languages, and probably for Go in the future, they can also take a specific named treatment group).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can make it different. But I thought making it implicit is still fine. I think it's generally less pain to first-time users as they have to set fewer attributes initially when they are trying out. (I will answer other comments slowly and steadily:)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My thinking here is that, since we are deleting code in place, we do want the user to be somewhat explicit about which logic ought to be deleted, even if it's a bit more friction. It's also not clear that one default would be more common than the other.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I agree with you. I suggest we can ask for as like:
-mode treatedor-mode control. Is it fine?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. That makes sense to me :)