use Array.from on single block array comprehensions - closes #199
This commit is contained in:
@@ -2,16 +2,21 @@ var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
var singleArrayExpression = function (node) {
|
||||
var single = function (node) {
|
||||
var block = node.blocks[0];
|
||||
|
||||
var templateName = "array-expression-comprehension-map";
|
||||
if (node.filter) templateName = "array-expression-comprehension-filter";
|
||||
|
||||
var right = block.right;
|
||||
if (!t.isArrayExpression(right)) {
|
||||
right = util.template("array-from", { VALUE: right });
|
||||
}
|
||||
|
||||
var result = util.template(templateName, {
|
||||
STATEMENT: node.body,
|
||||
FILTER: node.filter,
|
||||
ARRAY: block.right,
|
||||
ARRAY: right,
|
||||
KEY: block.left
|
||||
});
|
||||
|
||||
@@ -73,8 +78,8 @@ exports._build = function (node, buildBody) {
|
||||
exports.ComprehensionExpression = function (node, parent, file) {
|
||||
if (node.generator) return;
|
||||
|
||||
if (node.blocks.length === 1 && t.isArrayExpression(node.blocks[0].right)) {
|
||||
return singleArrayExpression(node);
|
||||
if (node.blocks.length === 1) {
|
||||
return single(node);
|
||||
} else {
|
||||
return multiple(node, file);
|
||||
}
|
||||
|
||||
1
test/fixtures/transformation/array-comprehension/single-if/actual.js
vendored
Normal file
1
test/fixtures/transformation/array-comprehension/single-if/actual.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
var arr = [for (i of nums) if (i > 1) i * i];
|
||||
7
test/fixtures/transformation/array-comprehension/single-if/expected.js
vendored
Normal file
7
test/fixtures/transformation/array-comprehension/single-if/expected.js
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
var arr = Array.from(nums).filter(function (i) {
|
||||
return i > 1;
|
||||
}).map(function (i) {
|
||||
return i * i;
|
||||
});
|
||||
1
test/fixtures/transformation/array-comprehension/single/actual.js
vendored
Normal file
1
test/fixtures/transformation/array-comprehension/single/actual.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
var arr = [for (i of nums) i * i];
|
||||
5
test/fixtures/transformation/array-comprehension/single/expected.js
vendored
Normal file
5
test/fixtures/transformation/array-comprehension/single/expected.js
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
"use strict";
|
||||
|
||||
var arr = Array.from(nums).map(function (i) {
|
||||
return i * i;
|
||||
});
|
||||
Reference in New Issue
Block a user