Skip to content

Commit d8e8918

Browse files
fix(extgen): reset iota counter at the start of each const block
1 parent af07c17 commit d8e8918

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

internal/extgen/constparser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func (cp *ConstantParser) parse(filename string) (constants []phpConstant, err e
6161

6262
if strings.HasPrefix(line, "const (") {
6363
inConstBlock = true
64+
currentConstantValue = 0
6465
if expectConstDecl || expectClassConstDecl {
6566
exportAllInBlock = true
6667
}

internal/extgen/constparser_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,38 @@ const ANOTHER_INDIVIDUAL = "test"`
354354
assert.Equal(t, phpString, constants[3].PhpType)
355355
}
356356

357+
func TestConstantParserIotaRestartsBetweenBlocks(t *testing.T) {
358+
input := `package main
359+
360+
// export_php:const
361+
const (
362+
A = iota
363+
B
364+
C
365+
)
366+
367+
// export_php:const
368+
const (
369+
X = iota
370+
Y
371+
)`
372+
373+
tmpDir := t.TempDir()
374+
fileName := filepath.Join(tmpDir, "test.go")
375+
require.NoError(t, os.WriteFile(fileName, []byte(input), 0644))
376+
377+
parser := &ConstantParser{}
378+
constants, err := parser.parse(fileName)
379+
assert.NoError(t, err)
380+
381+
require.Len(t, constants, 5)
382+
assert.Equal(t, "0", constants[0].Value, "A should be 0")
383+
assert.Equal(t, "1", constants[1].Value, "B should be 1")
384+
assert.Equal(t, "2", constants[2].Value, "C should be 2")
385+
assert.Equal(t, "0", constants[3].Value, "X should restart at 0 in new block")
386+
assert.Equal(t, "1", constants[4].Value, "Y should be 1")
387+
}
388+
357389
func TestConstantParserClassConstBlock(t *testing.T) {
358390
input := `package main
359391

0 commit comments

Comments
 (0)