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

Implement skipForOfIteratorClosing assumption #12496

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/babel-core/src/config/validation/options.js
Expand Up @@ -337,11 +337,12 @@ export const assumptionsNames = new Set<string>([
"ignoreToPrimitiveHint",
"iterableIsArray",
"mutableTemplateObject",
"noDocumentAll",
"pureGetters",
"setClassMethods",
"setComputedProperties",
"setPublicClassFields",
"noDocumentAll",
"skipForOfIteratorClosing",
]);

function getSource(loc: NestingPath): OptionsSource {
Expand Down
27 changes: 14 additions & 13 deletions packages/babel-plugin-transform-for-of/src/index.js
Expand Up @@ -6,10 +6,8 @@ import transformWithoutHelper from "./no-helper-implementation";
export default declare((api, options) => {
api.assertVersion(7);

const { loose } = options;

{
const { assumeArray, allowArrayLike } = options;
const { assumeArray, allowArrayLike, loose } = options;

if (loose === true && assumeArray === true) {
throw new Error(
Expand All @@ -35,11 +33,14 @@ export default declare((api, options) => {
options.assumeArray ??
// Loose mode is not compatible with 'assumeArray', so we shouldn't read
// 'iterableIsArray' if 'loose' is true.
(!loose && api.assumption("iterableIsArray"));
(!options.loose && api.assumption("iterableIsArray"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could use loose instead of options.loose here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I am explicitly trying to use options.loose in my PRs so that ESLint tells me that loose is unused and I'm sure I didn't miss proposing any assumption 😬


const arrayLikeIsIterable =
options.allowArrayLike ?? api.assumption("arrayLikeIsIterable");

const skipteratorClosing =
api.assumption("skipForOfIteratorClosing") ?? options.loose;

if (iterableIsArray && arrayLikeIsIterable) {
throw new Error(
`The "iterableIsArray" and "arrayLikeIsIterable" assumptions are not compatible.`,
Expand Down Expand Up @@ -113,16 +114,16 @@ export default declare((api, options) => {
};
}

const buildForOfArray = template(`
const buildForOfArray = template`
for (var KEY = 0, NAME = ARR; KEY < NAME.length; KEY++) BODY;
`);
`;

const buildForOfLoose = template.statements(`
const buildForOfNoIteratorClosing = template.statements`
for (var ITERATOR_HELPER = CREATE_ITERATOR_HELPER(OBJECT, ARRAY_LIKE_IS_ITERABLE), STEP_KEY;
!(STEP_KEY = ITERATOR_HELPER()).done;) BODY;
`);
`;

const buildForOf = template.statements(`
const buildForOf = template.statements`
var ITERATOR_HELPER = CREATE_ITERATOR_HELPER(OBJECT, ARRAY_LIKE_IS_ITERABLE), STEP_KEY;
try {
for (ITERATOR_HELPER.s(); !(STEP_KEY = ITERATOR_HELPER.n()).done;) BODY;
Expand All @@ -131,11 +132,11 @@ export default declare((api, options) => {
} finally {
ITERATOR_HELPER.f();
}
`);
`;

const builder = loose
const builder = skipteratorClosing
? {
build: buildForOfLoose,
build: buildForOfNoIteratorClosing,
helper: "createForOfIteratorHelperLoose",
getContainer: nodes => nodes,
}
Expand Down Expand Up @@ -198,7 +199,7 @@ export default declare((api, options) => {

if (!state.availableHelper(builder.helper)) {
// Babel <7.9.0 doesn't support this helper
transformWithoutHelper(loose, path, state);
transformWithoutHelper(skipteratorClosing, path, state);
return;
}

Expand Down
@@ -0,0 +1,3 @@
for (i of arr) {

}
@@ -0,0 +1,3 @@
for (var _iterator = babelHelpers.createForOfIteratorHelperLoose(arr), _step; !(_step = _iterator()).done;) {
i = _step.value;
}
@@ -0,0 +1,6 @@
for (var i of foo) {
switch (i) {
case 1:
break;
}
}
@@ -0,0 +1,8 @@
for (var _iterator = babelHelpers.createForOfIteratorHelperLoose(foo), _step; !(_step = _iterator()).done;) {
var i = _step.value;

switch (i) {
case 1:
break;
}
}
@@ -0,0 +1,3 @@
for (let i of arr) {

}
@@ -0,0 +1,3 @@
for (var _iterator = babelHelpers.createForOfIteratorHelperLoose(arr), _step; !(_step = _iterator()).done;) {
let i = _step.value;
}
@@ -0,0 +1,3 @@
for (obj.prop of arr) {

}
@@ -0,0 +1,3 @@
for (var _iterator = babelHelpers.createForOfIteratorHelperLoose(arr), _step; !(_step = _iterator()).done;) {
obj.prop = _step.value;
}
@@ -0,0 +1,7 @@
for (var i of arr) {

}

for (var i of numbers) {

}
@@ -0,0 +1,7 @@
for (var _iterator = babelHelpers.createForOfIteratorHelperLoose(arr), _step; !(_step = _iterator()).done;) {
var i = _step.value;
}

for (var _iterator2 = babelHelpers.createForOfIteratorHelperLoose(numbers), _step2; !(_step2 = _iterator2()).done;) {
var i = _step2.value;
}
@@ -0,0 +1,5 @@
b: for (let c of d()) {
for (let e of f()) {
continue b;
}
}
@@ -0,0 +1,8 @@
b: for (var _iterator = babelHelpers.createForOfIteratorHelperLoose(d()), _step; !(_step = _iterator()).done;) {
let c = _step.value;

for (var _iterator2 = babelHelpers.createForOfIteratorHelperLoose(f()), _step2; !(_step2 = _iterator2()).done;) {
let e = _step2.value;
continue b;
}
}
@@ -0,0 +1,9 @@
{
"plugins": [
["external-helpers", { "helperVersion": "7.100.0" }],
"transform-for-of"
],
"assumptions": {
"skipForOfIteratorClosing": true
}
}
@@ -0,0 +1,3 @@
for (var i of arr) {

}
@@ -0,0 +1,3 @@
for (var _iterator = babelHelpers.createForOfIteratorHelperLoose(arr), _step; !(_step = _iterator()).done;) {
var i = _step.value;
}