diff --git a/internal/packagejson/expected.go b/internal/packagejson/expected.go index 37e410fa89..1e3507dc68 100644 --- a/internal/packagejson/expected.go +++ b/internal/packagejson/expected.go @@ -68,3 +68,7 @@ func (e *Expected[T]) ExpectedJSONType() string { func (e *Expected[T]) ActualJSONType() string { return e.actualJSONType } + +func ExpectedOf[T any](value T) Expected[T] { + return Expected[T]{Value: value, Valid: true, actualJSONType: (*Expected[T])(nil).ExpectedJSONType()} +} diff --git a/internal/packagejson/packagejson.go b/internal/packagejson/packagejson.go index ef59725b74..2482bcff3c 100644 --- a/internal/packagejson/packagejson.go +++ b/internal/packagejson/packagejson.go @@ -2,6 +2,7 @@ package packagejson import ( json2 "github.com/go-json-experiment/json" + "github.com/go-json-experiment/json/jsontext" ) type HeaderFields struct { @@ -35,7 +36,7 @@ type Fields struct { func Parse(data []byte) (Fields, error) { var f Fields - if err := json2.Unmarshal(data, &f); err != nil { + if err := json2.Unmarshal(data, &f, jsontext.AllowDuplicateNames(true)); err != nil { return Fields{}, err } return f, nil diff --git a/internal/packagejson/packagejson_test.go b/internal/packagejson/packagejson_test.go index e216d9c1d2..dd7a1d82c7 100644 --- a/internal/packagejson/packagejson_test.go +++ b/internal/packagejson/packagejson_test.go @@ -6,6 +6,7 @@ import ( "testing" json2 "github.com/go-json-experiment/json" + "github.com/google/go-cmp/cmp/cmpopts" "github.com/microsoft/typescript-go/internal/ast" "github.com/microsoft/typescript-go/internal/core" "github.com/microsoft/typescript-go/internal/packagejson" @@ -13,6 +14,7 @@ import ( "github.com/microsoft/typescript-go/internal/repo" "github.com/microsoft/typescript-go/internal/testutil/filefixture" "github.com/microsoft/typescript-go/internal/tspath" + "gotest.tools/v3/assert" ) var packageJsonFixtures = []filefixture.Fixture{ @@ -59,3 +61,44 @@ func BenchmarkPackageJSON(b *testing.B) { }) } } + +func TestParse(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + content string + want packagejson.Fields + }{ + { + name: "duplicate names", + content: `{ + "name": "test-package", + "name": "test-package", + "version": "1.0.0" + }`, + want: packagejson.Fields{ + HeaderFields: packagejson.HeaderFields{ + Name: packagejson.ExpectedOf("test-package"), + Version: packagejson.ExpectedOf("1.0.0"), + }, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + got, err := packagejson.Parse([]byte(tt.content)) + assert.NilError(t, err) + assert.DeepEqual(t, got, tt.want, cmpopts.IgnoreUnexported( + packagejson.Fields{}, + packagejson.HeaderFields{}, + packagejson.Expected[string]{}, + packagejson.Expected[map[string]string]{}, + packagejson.ExportsOrImports{}, + )) + }) + } +}