Comments (5)
I'm thinking about fixing this one as a good first issue. @nicoespeon, I assume since it isn't assigned that it isn't being actively worked? Just want to make sure I don't do work someone else is already doing.
from abracadabra.
Indeed, that would be doable.
And I think that's something contributors could help with, it's not too complex.
Code example
So this code:
const array1 = ['a', 'b', 'c'];
for (const element of array1) {
console.log(element);
}
Should be convertable to this code:
const object1 = { 'a': 1, 'b': 2, 'c': 3 };
array1.forEach((element) => {
console.log(element);
})
Where to do the change
The refactoring already exist, everything is here: https://github.com/nicoespeon/abracadabra/tree/master/src/refactorings/convert-for-to-foreach
What you need to do:
- Create at least one new test case to illustrate that it doesn't work today:
- Make it work!
Warning, edge-cases
Actually, for…of
is something that can be used on any Iterable, not just Arrays: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Instructions/for...of
Another example of natural iterables are Strings. If we don't pay attention, we can break the code:
const iterable = 'boo';
// This works!
for (const value of iterable) {
console.log(value);
}
// This doesn't, `String.prototype.forEach()` doesn't exist
iterable.forEach((value) => {
console.log(value);
});
Check MDN documentation for all existing iterables and make sure that we handle them properly. It's acceptable to not perform the refactoring for the non-supported iterables.
But we shouldn't make it possible to break the code 😉
from abracadabra.
I was actually looking for an automatic refactoring of "forEach" into "for (... of ...)" because "forEach" can be dangerous (doing something else than expected) when used with promises (or async/await) while "for of" works fine.
from abracadabra.
@ajanian indeed, no-one in working on that (or I'm not aware). If you start working on that let me know and I'll "assign" it to you so it clarifies it's in progress.
Thanks for your help by the way, that's awesome 😃
from abracadabra.
This will be available in the next release. I'll make it happen before the end of the week ;-)
from abracadabra.
Related Issues (20)
- Automatically "Convert to Template String" if we write ${} in a string HOT 2
- Convert getter / setter to member functions
- Inlining JSX removes curly braces HOT 1
- Extract refactoring runs on JSX attribute HOT 3
- Inline async arrow function misses braces HOT 1
- Abracadabra replaces system key binding HOT 3
- Extract Variable insert position doesn't right HOT 2
- change activationEvent to onLanguage
- Idea: Migrate to TypeScript plugin HOT 4
- "Change Signature" doesn't work on a nested function HOT 3
- "Add explicit type" refactoring HOT 1
- Inline variable if ternary misses parens HOT 5
- split declaration & init when working inside class
- JS and TS code without `;` is broken after refactoring HOT 3
- Abracadabra extension causes performance issues with other extensions (prettier formatting, etc.) HOT 7
- More inline variable missing parentheses edge-cases HOT 1
- Can't move class to an existing file HOT 2
- Move to new file: configure case of the new filename HOT 3
- Upgrade babel/traverse ti eliminate vulnerability. HOT 1
- Convert to Arrow Function: Does not work with a `export default function` HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from abracadabra.