Skip to content

Commit

Permalink
fix(planner/nodejs): Initialize map (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
pan93412 authored Aug 13, 2023
1 parent e30404a commit 52a0b9d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
17 changes: 9 additions & 8 deletions internal/nodejs/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,22 @@ type PackageJSONEngine struct {

// PackageJSON is the structure of `package.json`.
type PackageJSON struct {
PackageManager *string `json:"packageManager"`
Dependencies map[string]string `json:"dependencies"`
DevDependencies map[string]string `json:"devDependencies"`
Scripts map[string]string `json:"scripts"`
PackageManager *string `json:"packageManager,omitempty"`
Dependencies map[string]string `json:"dependencies,omitempty"`
DevDependencies map[string]string `json:"devDependencies,omitempty"`
Scripts map[string]string `json:"scripts,omitempty"`
Engines PackageJSONEngine `json:"engines"`
Main string `json:"main"`
}

// NewPackageJSON returns a new instance of PackageJson
// with some default values.
func NewPackageJSON() PackageJSON {
// we don't need to allocate an map for Dependencies,
// DevDependencies and Scripts, since we won't set them
// in the null state.
return PackageJSON{}
return PackageJSON{
Dependencies: make(map[string]string),
DevDependencies: make(map[string]string),
Scripts: make(map[string]string),
}
}

// DeserializePackageJSON deserializes a package.json file
Expand Down
11 changes: 3 additions & 8 deletions internal/nodejs/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ func TestNewPackageJson(t *testing.T) {
assert.False(t, ok)
})

// panic!
assert.Panics(t, func() {
p.Scripts["build"] = "hi"
})

// engines
assert.Empty(t, p.Engines.Node)

Expand Down Expand Up @@ -63,9 +58,9 @@ func TestDeserializePackageJson_EmptyJson(t *testing.T) {

p, err := nodejs.DeserializePackageJSON(fs)
assert.NoError(t, err)
assert.Nil(t, p.Dependencies)
assert.Nil(t, p.DevDependencies)
assert.Nil(t, p.Scripts)
assert.Empty(t, p.Dependencies)
assert.Empty(t, p.DevDependencies)
assert.Empty(t, p.Scripts)
assert.Empty(t, p.Engines.Node)
assert.Empty(t, p.Main)
}
Expand Down

0 comments on commit 52a0b9d

Please sign in to comment.