Skip to content

Commit

Permalink
refactor(commands): make Command.parent field accessible (#5969)
Browse files Browse the repository at this point in the history
To avoid error-prone index-based access.
  • Loading branch information
vvagaytsev committed Apr 29, 2024
1 parent 053c786 commit 744ca54
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions core/src/commands/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export abstract class Command<
// E.g: const cmd = new Command(); cmd["parent"] = parentCommand.
// This is so that commands that are initialised via arguments can be cloned which is required
// for the websocket server to work properly.
private parent?: CommandGroup
parent?: CommandGroup

// FIXME: This is a little hack so that we can clone commands that are initialised with
// arbitrary parameters.
Expand Down Expand Up @@ -558,7 +558,7 @@ export abstract class Command<
// See: https://stackoverflow.com/a/64638986
const clone = new (this.constructor as new (params?: any) => this)(this._params)
if (this.parent) {
clone["parent"] = this.parent
clone.parent = this.parent
}
return clone
}
Expand Down Expand Up @@ -652,7 +652,7 @@ export abstract class CommandGroup extends Command {
getSubCommands(): Command[] {
return this.subCommands.flatMap((cls) => {
const cmd = new cls()
cmd["parent"] = this
cmd.parent = this
if (cmd instanceof CommandGroup) {
return cmd.getSubCommands()
} else {
Expand Down
8 changes: 4 additions & 4 deletions core/test/unit/src/commands/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ describe("Command", () => {

const cmd = new TestCommand(new TestGroup())
// FIXME: This is needs to be set "manually" for now to work around issues with cloning commands.
cmd["parent"] = new TestGroup()
cmd.parent = new TestGroup()
expect(cmd.getPaths()).to.eql([["test-group", "test-command"]])
})

Expand All @@ -184,7 +184,7 @@ describe("Command", () => {

const cmd = new TestCommand(new TestGroup())
// FIXME: This is needs to be set "manually" for now to work around issues with cloning commands.
cmd["parent"] = new TestGroup()
cmd.parent = new TestGroup()
expect(cmd.getPaths()).to.eql([
["test-group", "test-command"],
["group-alias", "test-command"],
Expand Down Expand Up @@ -213,7 +213,7 @@ describe("Command", () => {

const cmd = new TestCommand(new TestGroup())
// FIXME: This is needs to be set "manually" for now to work around issues with cloning commands.
cmd["parent"] = new TestGroup()
cmd.parent = new TestGroup()
expect(cmd.getPaths()).to.eql([
["test-group", "test-command"],
["test-group", "command-alias"],
Expand Down Expand Up @@ -243,7 +243,7 @@ describe("Command", () => {

const cmd = new TestCommand(new TestGroup())
// FIXME: This is needs to be set "manually" for now to work around issues with cloning commands.
cmd["parent"] = new TestGroup()
cmd.parent = new TestGroup()
expect(cmd.getPaths()).to.eql([
["test-group", "test-command"],
["test-group", "command-alias"],
Expand Down

0 comments on commit 744ca54

Please sign in to comment.