Skip to content
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

Convert sequence of independent variables to an object #362

Open
qpwo opened this issue Aug 6, 2021 · 6 comments
Open

Convert sequence of independent variables to an object #362

qpwo opened this issue Aug 6, 2021 · 6 comments
Labels
✨ Feature New refactoring or feature

Comments

@qpwo
Copy link

qpwo commented Aug 6, 2021

Is this request related to a problem? Please describe.

I'm always frustrated when I'm converting global-variable-based code to functionalish code and I have to manually pack global variables into an object. Sometimes a function's internal variable will shadow the global, so I can't just replace-all foo to obj.foo.

Describe the solution you'd like

I'd like to select them and convert to an object and update all references correctly.

// input:

// Select the following three lines and "Abracadabra: convert variables to object"
const dogs = []
const cats = []
let preferred = ""
function addDog() {
    dogs.push("Dog")
    preferred = "dog"
}
function addCats() {
    cats.push("Cat")
    preferred = "cat"
}
function logFavFruit() {
    const preferred = "banana"
    console.log("My favorite fruit is " + preferred)
}

// output:

const data = {
    dogs: [],
    cats: [],
    preferred1: "",
}
function addDog() {
    data.dogs.push("Dog")
    data.preferred = "dog"
}
function addCats() {
    data.cats.push("Cat")
    data.preferred = "cat"
}
function logFavFruit() {
    const preferred = "banana"
    console.log("My favorite fruit is " + preferred)
}
@qpwo qpwo added the ✨ Feature New refactoring or feature label Aug 6, 2021
@nicoespeon
Copy link
Owner

Interesting. I don't have a strong feeling on this one so far.

I'll leave this one open though, so other people can chime in and add their thoughts. If that's something you'd like over other refactorings, please tell me 😄

@qpwo
Copy link
Author

qpwo commented Aug 11, 2021

How hard do you think it would be to convert function arguments to/from (destructured) objects as well? Relates to #341

@nicoespeon
Copy link
Owner

nicoespeon commented Aug 12, 2021

@qpwo great question.

For the refactoring presented here, most of the things are local to the file, so it shouldn't be too hard to retrieve the bindings of the variables and convert them.

I'm also thinking about exported variables. If one of the selected variables is exported (at the declaration or later), we need to detect that to either prevent the refactoring (could be the first iteration) or handle it.

Now re-ordering function arguments (#341) and changing them have more or less the same difficulty: we need to update code across the project. Again, a first iteration could make it work for non-exported functions only. But I suspect that will quickly limit the interest.

Current refactorings work within the scope of a single file. Or extracting things to other files.
The next milestone for Abracadabra is definitely to resolve dependencies so refactoring can be made on exported stuff. I didn't figure it out yet, but on it with #345 (same issue) 🤔

@qpwo
Copy link
Author

qpwo commented Aug 12, 2021

Thanks for all your work on this project Nicolas

@qpwo
Copy link
Author

qpwo commented Mar 11, 2022

Re:

How hard do you think it would be to convert function arguments to/from (destructured) objects as well? Relates to #341

Just occured to me a simpler way of phrasing this , and a more useful and general and simpler feature would be

Destructure/restructure object

// Destructured:
const {a, b, c} = o
const a2 = a * 2
const total = a + b + c

// "Restructured:"
const a2 = o.a * 2
const total = o.a + o.b + o.c

Maybe that should be a separate issue though

@qpwo
Copy link
Author

qpwo commented Mar 15, 2022

Forgot to put an example with a function:

// destructured
function Card({x, y, color}: {x: number, y: number, color: string) {
     return <p style={{left: x, top: y, backgroundColor: color}}>{color}</p>
}
// restructured
function Card(props: {x: number, y: number, color: string) {
     return <p style={{left: props.x, top: props.y, backgroundColor: props.color}}>{props.color}</p>
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ Feature New refactoring or feature
Projects
None yet
Development

No branches or pull requests

2 participants