Skip to content

Allow duplicate names in package.json parsing #1321

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions internal/packagejson/expected.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()}
}
3 changes: 2 additions & 1 deletion internal/packagejson/packagejson.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package packagejson

import (
json2 "github.com/go-json-experiment/json"
"github.com/go-json-experiment/json/jsontext"
)

type HeaderFields struct {
Expand Down Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions internal/packagejson/packagejson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ 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"
"github.com/microsoft/typescript-go/internal/parser"
"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{
Expand Down Expand Up @@ -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{},
))
})
}
}