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 2 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
39 changes: 39 additions & 0 deletions internal/packagejson/packagejson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
"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,40 @@
})
}
}

func TestParse(t *testing.T) {

Check failure on line 65 in internal/packagejson/packagejson_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

Function TestParse missing the call to method parallel (paralleltest)
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 {

Check failure on line 87 in internal/packagejson/packagejson_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

Range statement for test TestParse missing the call to method parallel in test Run (paralleltest)
t.Run(tt.name, func(t *testing.T) {
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{},
))
})
}
}
Loading