Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@

### Fixes

- [#4347](https://github.com/ignite/cli/pull/4347) Fix `ts-client` generation
- [#4577](https://github.com/ignite/cli/pull/4577) Add proto version to query path.
- [#4579](https://github.com/ignite/cli/pull/4579) Fix empty params response.
- [#4585](https://github.com/ignite/cli/pull/4585) Fix faucet cmd issue.
- [#4587](https://github.com/ignite/cli/pull/4587) Add missing light clients routes to IBC client keeper.
- [#4595](https://github.com/ignite/cli/pull/4595) Fix wrong InterfaceRegistry for IBC modules.
- [#4609](https://github.com/ignite/cli/pull/4609) Add work dir for relayer integration tests.

### Bug Fixes

- [#4347](https://github.com/ignite/cli/pull/4347) Fix `ts-client` generation
- [#4658](https://github.com/ignite/cli/pull/4658) Fix indentation for params scaffolded into a struct.
- [#4582](https://github.com/ignite/cli/issues/4582) Fix xast misplacing comments.
- [#4660](https://github.com/ignite/cli/pull/4660) Fix xast test case indentation.

Expand Down Expand Up @@ -120,7 +118,7 @@
- [#4633](https://github.com/ignite/cli/pull/4633) Loosen faucet check when indexer disabled.
- [#4586](https://github.com/ignite/cli/pull/4586) Remove network as default plugin.

### Bug Fixes
### Fixes

- [#4347](https://github.com/ignite/cli/pull/4347) Fix `ts-client` generation.

Expand All @@ -133,7 +131,7 @@

## [`v28.8.1`](https://github.com/ignite/cli/releases/tag/v28.8.1)

### Bug Fixes
### Fixes

- [#4532](https://github.com/ignite/cli/pull/4532) Fix non working _shortcuts_ in validator home config
- [#4538](https://github.com/ignite/cli/pull/4538) Create a simple spinner for non-terminal interactions
Expand All @@ -152,7 +150,7 @@
- [#4471](https://github.com/ignite/cli/pull/4471) Bump Ignite & chain minimum Go version to 1.23.
- [#4529](https://github.com/ignite/cli/pull/4531) Bump Cosmos SDK to v0.50.12.

### Bug Fixes
### Fixes

- [#4474](https://github.com/ignite/cli/pull/4474) Fix issue in `build --release` command
- [#4479](https://github.com/ignite/cli/pull/4479) Scaffold an `uint64 type crashs Ignite
Expand Down
67 changes: 42 additions & 25 deletions ignite/pkg/xast/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ type (
name string // Name of the struct type.
param string // Name of the struct field.
code string // Code to insert.
index int // Position to insert at.
}
functionStructs []functionStruct
functionStructsMap map[string]functionStructs
Expand Down Expand Up @@ -157,13 +156,12 @@ func AppendInsideFuncCall(callName, code string, index int) FunctionOptions {
// AppendFuncStruct adds a field to a struct literal. For instance,
// // the struct has only one parameter 'Params{Param1: param1}' and we want to add
// // the param2 the result will be 'Params{Param1: param1, Param2: param2}'.
func AppendFuncStruct(name, param, code string, index int) FunctionOptions {
func AppendFuncStruct(name, param, code string) FunctionOptions {
return func(c *functionOpts) {
c.insideStruct = append(c.insideStruct, functionStruct{
name: name,
param: param,
code: code,
index: index,
})
}
}
Expand Down Expand Up @@ -431,29 +429,51 @@ func addFunctionCall(expr *ast.CallExpr, calls functionCalls) error {
}

// addStructs modifies struct literal fields.
func addStructs(expr *ast.CompositeLit, structs functionStructs) error {
for _, s := range structs {
var newArg ast.Expr = ast.NewIdent(s.code)
func addStructs(fileSet *token.FileSet, expr *ast.CompositeLit, structs functionStructs) {
// Find the current max offset to avoid reused positions
file := fileSet.File(expr.Pos())
maxOffset := file.Offset(expr.Rbrace)
for _, elt := range expr.Elts {
if pos := elt.End(); pos.IsValid() {
offset := file.Offset(pos)
if offset > maxOffset {
maxOffset = offset
}
}
}

for i, s := range structs {
// Advance position
insertOffset := maxOffset + i
insertPos := file.Pos(insertOffset)

value := ast.NewIdent(s.code)
value.NamePos = insertPos

var newArg ast.Expr = value
if s.param != "" {
key := ast.NewIdent(s.param)
key.NamePos = insertPos + token.Pos(i)

newArg = &ast.KeyValueExpr{
Key: ast.NewIdent(s.param),
Colon: token.Pos(s.index),
Value: ast.NewIdent(s.code),
Key: key,
Value: value,
Colon: insertPos,
}
}

switch {
case s.index == -1:
// Append at end
expr.Elts = append(expr.Elts, newArg)
case s.index >= 0 && s.index <= len(expr.Elts):
// Insert at index
expr.Elts = append(expr.Elts[:s.index], append([]ast.Expr{newArg}, expr.Elts[s.index:]...)...)
default:
return errors.Errorf("function call index %d out of range", s.index)
expr.Elts = append(expr.Elts, newArg)
expr.Rbrace += token.Pos(i + 1)
}

// Ensure closing brace is on a new line
if len(expr.Elts) > 0 {
last := expr.Elts[len(expr.Elts)-1]
if file.Line(expr.Rbrace) == file.Line(last.End()) {
// Force a new line before Rbrace
file.AddLine(file.Offset(expr.Rbrace))
}
}
return nil
}

// codeToBlockStmt parses code string into AST block statement.
Expand All @@ -472,9 +492,9 @@ func toCode(code string) string {
}

// formatNode formats an AST node into Go source code.
func formatNode(fileSet *token.FileSet, f *ast.File) (string, error) {
func formatNode(fileSet *token.FileSet, n ast.Node) (string, error) {
var buf bytes.Buffer
if err := format.Node(&buf, fileSet, f); err != nil {
if err := format.Node(&buf, fileSet, n); err != nil {
return "", err
}

Expand Down Expand Up @@ -578,10 +598,7 @@ func applyFunctionOptions(fileSet *token.FileSet, f *ast.FuncDecl, opts *functio
return true
}

if err := addStructs(expr, structs); err != nil {
errInspect = err
return false
}
addStructs(fileSet, expr, structs)
delete(structMapCheck, name)

default:
Expand Down
33 changes: 24 additions & 9 deletions ignite/pkg/xast/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,15 @@ func TestValidate(t *testing.T) {
return false
}`, 2),
AppendFuncCode(`fmt.Println("Appended code.")`),
AppendFuncCode(`Param{Baz: baz, Foo: foo}`),
AppendFuncCode(`Param{
Baz: baz,
Foo: foo,
}`),
NewFuncReturn("1"),
AppendInsideFuncCall("SimpleCall", "baz", 0),
AppendInsideFuncCall("SimpleCall", "bla", -1),
AppendInsideFuncCall("Println", strconv.Quote("test"), -1),
AppendFuncStruct("Param", "Bar", strconv.Quote("bar"), -1),
AppendFuncStruct("Param", "Bar", strconv.Quote("bar")),
AppendFuncTestCase(`{
desc: "valid first genesis state",
genState: GenesisState{},
Expand Down Expand Up @@ -118,7 +121,11 @@ func anotherFunction(param1 string) bool {
return false
}
fmt.Println("Appended code.", "test")
Param{Baz: baz, Foo: foo, Bar: "bar"}
Param{
Baz: baz,
Foo: foo,
Bar: "bar",
}
return 1
}

Expand Down Expand Up @@ -474,7 +481,10 @@ import (

// anotherFunction another function
func anotherFunction() bool {
Param{Baz: baz, Foo: foo}
Param{
Baz: baz,
Foo: foo,
}
Client{baz, foo}
// return always true
return true
Expand Down Expand Up @@ -505,9 +515,9 @@ func TestValidate(t *testing.T) {
`,
functionName: "anotherFunction",
functions: []FunctionOptions{
AppendFuncStruct("Param", "Bar", "bar", -1),
AppendFuncStruct("Param", "Bla", "bla", 1),
AppendFuncStruct("Client", "", "bar", 0),
AppendFuncStruct("Param", "Bar", "bar"),
AppendFuncStruct("Param", "Bla", "bla"),
AppendFuncStruct("Client", "", "bar"),
},
},
want: `package main
Expand All @@ -518,8 +528,13 @@ import (

// anotherFunction another function
func anotherFunction() bool {
Param{Baz: baz, Bla: bla, Foo: foo, Bar: bar}
Client{bar, baz, foo}
Param{
Baz: baz,
Foo: foo,
Bar: bar,
Bla: bla,
}
Client{baz, foo, bar}
// return always true
return true
}
Expand Down
2 changes: 1 addition & 1 deletion ignite/templates/module/create/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func genesisTypesModify(opts *CreateOptions) genny.RunFn {
content, err = xast.ModifyFunction(
content,
"DefaultGenesis",
xast.AppendFuncStruct("GenesisState", "PortId", "PortID", -1),
xast.AppendFuncStruct("GenesisState", "PortId", "PortID"),
)
if err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion ignite/templates/module/create/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ func paramsTypesModify(opts ParamsOptions) genny.RunFn {
"Params",
param.Name.UpperCamel,
param.ProtoFieldName(),
-1,
),
)

Expand Down
5 changes: 0 additions & 5 deletions ignite/templates/typed/list/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ func genesisTypesModify(opts *typed.Options) genny.RunFn {
"GenesisState",
fmt.Sprintf("%[1]vList", opts.TypeName.UpperCamel),
fmt.Sprintf("[]%[1]v{}", opts.TypeName.PascalCase),
-1,
))
if err != nil {
return err
Expand Down Expand Up @@ -204,13 +203,11 @@ require.Equal(t, genesisState.%[1]vCount, got.%[1]vCount)`, opts.TypeName.UpperC
"GenesisState",
fmt.Sprintf("%[1]vList", opts.TypeName.UpperCamel),
fmt.Sprintf("[]types.%[1]v{{ Id: 0 }, { Id: 1 }}", opts.TypeName.PascalCase),
-1,
),
xast.AppendFuncStruct(
"GenesisState",
fmt.Sprintf("%[1]vCount", opts.TypeName.UpperCamel),
"2",
-1,
),
xast.AppendFuncCode(replacementAssert),
)
Expand Down Expand Up @@ -279,13 +276,11 @@ func genesisTypesTestsModify(opts *typed.Options) genny.RunFn {
"GenesisState",
fmt.Sprintf("%[1]vList", opts.TypeName.UpperCamel),
fmt.Sprintf("[]types.%[1]v{{ Id: 0 }, { Id: 1 }}", opts.TypeName.PascalCase),
-1,
),
xast.AppendFuncStruct(
"GenesisState",
fmt.Sprintf("%[1]vCount", opts.TypeName.UpperCamel),
"2",
-1,
),
xast.AppendFuncTestCase(replacementTestDuplicated),
xast.AppendFuncTestCase(replacementInvalidCount),
Expand Down
12 changes: 5 additions & 7 deletions ignite/templates/typed/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,22 +334,20 @@ func keeperModify(opts *typed.Options) genny.RunFn {
"NewKeeper",
xast.AppendFuncStruct(
"Keeper",
fmt.Sprintf("%[1]vSeq", opts.TypeName.UpperCamel),
fmt.Sprintf(`collections.NewSequence(sb, types.%[2]vCountKey, "%[3]vSequence")`,
opts.TypeName.UpperCamel,
opts.TypeName.UpperCamel,
fmt.Sprintf(`collections.NewMap(sb, types.%[1]vKey, "%[2]v", collections.Uint64Key, codec.CollValue[types.%[1]v](cdc))`,
opts.TypeName.PascalCase,
opts.TypeName.LowerCamel,
),
-1,
),
xast.AppendFuncStruct(
"Keeper",
opts.TypeName.UpperCamel,
fmt.Sprintf(`collections.NewMap(sb, types.%[1]vKey, "%[2]v", collections.Uint64Key, codec.CollValue[types.%[1]v](cdc))`,
fmt.Sprintf("%[1]vSeq", opts.TypeName.UpperCamel),
fmt.Sprintf(`collections.NewSequence(sb, types.%[2]vCountKey, "%[3]vSequence")`,
opts.TypeName.UpperCamel,
opts.TypeName.PascalCase,
opts.TypeName.LowerCamel,
),
-1,
),
)
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions ignite/templates/typed/list/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@ func moduleSimulationModify(opts *typed.Options) genny.RunFn {
opts.TypeName.PascalCase,
msgField,
),
-1,
),
xast.AppendFuncStruct(
"GenesisState",
fmt.Sprintf("%[1]vCount", opts.TypeName.UpperCamel),
"2",
-1,
),
)
if err != nil {
Expand Down
4 changes: 0 additions & 4 deletions ignite/templates/typed/map/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ func keeperModify(opts *typed.Options) genny.RunFn {
opts.TypeName.LowerCamel,
opts.Index.CollectionsKeyValueType(),
),
-1,
),
)
if err != nil {
Expand Down Expand Up @@ -368,7 +367,6 @@ func genesisTypesModify(opts *typed.Options) genny.RunFn {
"GenesisState",
fmt.Sprintf("%[1]vMap", opts.TypeName.UpperCamel),
fmt.Sprintf("[]%[1]v{}", opts.TypeName.PascalCase),
-1,
))
if err != nil {
return err
Expand Down Expand Up @@ -488,7 +486,6 @@ func genesisTestsModify(opts *typed.Options) genny.RunFn {
sampleIndexes[0],
sampleIndexes[1],
),
-1,
),
xast.AppendFuncCode(fmt.Sprintf("require.ElementsMatch(t, genesisState.%[1]vMap, got.%[1]vMap)", opts.TypeName.UpperCamel)),
)
Expand Down Expand Up @@ -548,7 +545,6 @@ func genesisTypesTestsModify(opts *typed.Options) genny.RunFn {
sampleIndexes[0],
sampleIndexes[1],
),
-1,
),
xast.AppendFuncTestCase(replacementDuplicated),
)
Expand Down
1 change: 0 additions & 1 deletion ignite/templates/typed/map/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func moduleSimulationModify(opts *typed.Options) genny.RunFn {
sampleIndexes[0],
sampleIndexes[1],
),
-1,
),
)
if err != nil {
Expand Down
4 changes: 0 additions & 4 deletions ignite/templates/typed/singleton/singleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ func keeperModify(opts *typed.Options) genny.RunFn {
opts.TypeName.PascalCase,
opts.TypeName.LowerCamel,
),
-1,
),
)
if err != nil {
Expand Down Expand Up @@ -287,7 +286,6 @@ func genesisTypesModify(opts *typed.Options) genny.RunFn {
"GenesisState",
fmt.Sprintf("%[1]v", opts.TypeName.UpperCamel),
"nil",
-1,
))
if err != nil {
return err
Expand Down Expand Up @@ -323,7 +321,6 @@ func genesisTestsModify(opts *typed.Options) genny.RunFn {
"GenesisState",
opts.TypeName.UpperCamel,
fmt.Sprintf("&types.%[1]v{ %[2]v }", opts.TypeName.PascalCase, sampleFields),
-1,
),
xast.AppendFuncCode(
fmt.Sprintf("require.Equal(t, genesisState.%[1]v, got.%[1]v)", opts.TypeName.UpperCamel),
Expand Down Expand Up @@ -364,7 +361,6 @@ func genesisTypesTestsModify(opts *typed.Options) genny.RunFn {
"GenesisState",
opts.TypeName.UpperCamel,
fmt.Sprintf("&types.%[1]v{ %[2]v }", opts.TypeName.PascalCase, sampleFields),
-1,
),
)
if err != nil {
Expand Down