Skip to content

Commit

Permalink
Change arrow-function to transfer comments from the old AST (cpojer#50)
Browse files Browse the repository at this point in the history
For the test, my main concern is that every comment in the input appears
somewhere in the output (even if the order changes).
  • Loading branch information
alangpierce authored and cpojer committed Jul 5, 2016
1 parent 3632fa2 commit 90a0081
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
13 changes: 13 additions & 0 deletions transforms/__testfixtures__/arrow-function.input.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ var fn6 = (function() {
};
}).bind(this);

var fn7 = /*1*/(/*2*/function/*3*/(/*4*/)/*5*/ {/*6*/
console.log('Keep');
console.log('comments');
}/*7*/)/*8*/./*9*/bind/*10*/(/*11*/this/*12*/)/*13*/;

[1, 2, 3].map(function(x) {
return x * x;
}.bind(this));
Expand All @@ -40,6 +45,10 @@ compare(1, 2, function(num1, num2) {
return num1 > num2;
});

/*1*/compare/*2*/(/*3*/1, /*4*/2, /*5*/function/*6*/(/*7*/num1/*8*/, /*9*/num2/*10*/) /*11*/{
/*12*/return /*13*/num1 > num2/*14*/;/*15*/
}/*16*/)/*17*/;/*18*/

Promise.resolve()
.then(function() {
console.log('foo');
Expand All @@ -48,6 +57,10 @@ Promise.resolve()
return 4;
});

foo(function() /*1*/{
/*2*/console.log('Keep comments when inlining single expressions');
/*3*/}/*4*/);

foo(function(a) {
this.bar(function() {
return a + this.b;
Expand Down
10 changes: 10 additions & 0 deletions transforms/__testfixtures__/arrow-function.output.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,28 @@ var fn6 = () => ({
a: 1,
});

var fn7 = /*2*//*1*/() => /*3*//*4*//*5*/ {/*6*/
console.log('Keep');
console.log('comments');
}/*7*//*8*//*10*//*9*//*11*//*12*//*13*/;

[1, 2, 3].map(x => x * x);

[1, 2, 3].map(x => x * x);

compare(1, 2, (num1, num2) => num1 > num2);

/*1*/compare/*2*/(/*3*/1, /*4*/2, /*5*/(/*6*//*7*/num1/*8*/, /*9*/num2/*10*/) => /*13*//*11*//*12*/num1 > num2/*14*//*15*//*16*/)/*17*/;/*18*/

Promise.resolve()
.then(function() {
console.log('foo');
}.bind(this, 'a'))
.then(a => 4);

foo(() => /*1*//*2*/console.log('Keep comments when inlining single expressions')
/*3*//*4*/);

foo(function(a) {
this.bar(function() {
return a + this.b;
Expand Down
35 changes: 25 additions & 10 deletions transforms/arrow-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ module.exports = (file, api, options) => {
fn.body.body.length == 1
) {
const inner = fn.body.body[0];
const comments = (fn.body.comments || []).concat(inner.comments || []);

if (
options['inline-single-expressions'] &&
inner.type == 'ExpressionStatement'
) {
inner.expression.comments = (inner.expression.comments || []).concat(comments);
return inner.expression;
} else if (inner.type == 'ReturnStatement') {
const lineStart = fn.loc.start.line;
Expand All @@ -29,18 +31,23 @@ module.exports = (file, api, options) => {
const tooLong = maxWidth && newLength > maxWidth;

if (!tooLong) {
inner.argument.comments = (inner.argument.comments || []).concat(comments);
return inner.argument;
}
}
}
return fn.body;
};

const createArrowFunctionExpression = fn => j.arrowFunctionExpression(
fn.params,
getBodyStatement(fn),
false
);
const createArrowFunctionExpression = fn => {
const arrowFunction = j.arrowFunctionExpression(
fn.params,
getBodyStatement(fn),
false
);
arrowFunction.comments = fn.comments;
return arrowFunction;
};

const replacedBoundFunctions = root
.find(j.CallExpression, {
Expand All @@ -61,11 +68,19 @@ module.exports = (file, api, options) => {
path.value.arguments.length == 1 &&
path.value.arguments[0].type == 'ThisExpression'
))
.forEach(path =>
j(path).replaceWith(
createArrowFunctionExpression(path.value.callee.object)
)
)
.forEach(path => {
const comments = path.value.comments || [];
for (const node of [path.value.callee, path.value.callee.property, path.value.arguments[0]]) {
for (const comment of node.comments || []) {
comment.leading = false;
comment.trailing = true;
comments.push(comment);
}
}
const arrowFunction = createArrowFunctionExpression(path.value.callee.object);
arrowFunction.comments = (arrowFunction.comments || []).concat(comments);
j(path).replaceWith(arrowFunction);
})
.size() > 0;

const replacedCallbacks = root
Expand Down

0 comments on commit 90a0081

Please sign in to comment.