Skip to content

[New Refactoring] Convert ObjectProperty into ObjectMethod #1178

@nicoespeon

Description

@nicoespeon

Is this request related to a problem? Please describe.

When implementing #1175 we noticed a refactoring we did manually that we wish would have been automated.

Describe the solution you'd like

We want to convert:

function createVisitor(onMatch) {
  return {
    VariableDeclaration: onMatch
  }
}

into:

function createVisitor(onMatch) {
  return {
    VariableDeclaration(path) {
      return onMatch(path)
    }
  }
}

Of course, this is not always possible. It should only be possible if the value is a Function—which may be tricky to determine, depending if we can resolve its type.

Note that it may be challenging to determine exactly the parameters of the ObjectProperty—depends also if we can resolve the type. However, this should always be valid JS:

function createVisitor(onMatch) {
  return {
    VariableDeclaration(...params) {
      return onMatch(...params)
    }
  }
}

TS may not be happy in some cases (e.g. onMatch only expects 1 argument but params has 2, which works at runtime but makes the compiler complain), but it's a valid refactoring. Hence, it's acceptable.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions