Collect comments around parentheses in expressions (#13803)

Co-authored-by: Brian Ng <bng412@gmail.com>
This commit is contained in:
Nicolò Ribaudo 2021-10-05 20:15:24 +02:00 committed by GitHub
parent 7bbdf096aa
commit 64f14b05fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 508 additions and 21 deletions

View File

@ -0,0 +1,17 @@
function assertElement(assertFn, shouldBeElement, opt_message) {
return /** @type {!Ele ment} */ (
assertType_(
assertFn,
shouldBeElement,
isElement(shouldBeElement),
'Element expected',
opt_message
)
);
}
const slot = /** @type {!HTMLSlotElement} */ (e.target);
assertElement(
/** @type {Element} */ (el),
);

View File

@ -0,0 +1,4 @@
{
"retainLines": true,
"createParenthesizedExpressions": true
}

View File

@ -0,0 +1,16 @@
function assertElement(assertFn, shouldBeElement, opt_message) {
return (/** @type {!Ele ment} */
assertType_(
assertFn,
shouldBeElement,
isElement(shouldBeElement),
'Element expected',
opt_message));
}
const slot = /** @type {!HTMLSlotElement} */e.target;
assertElement(
/** @type {Element} */el);

View File

@ -0,0 +1,17 @@
function assertElement(assertFn, shouldBeElement, opt_message) {
return /** @type {!Ele ment} */ (
assertType_(
assertFn,
shouldBeElement,
isElement(shouldBeElement),
'Element expected',
opt_message
)
);
}
const slot = /** @type {!HTMLSlotElement} */ (e.target);
assertElement(
/** @type {Element} */ (el),
);

View File

@ -0,0 +1,13 @@
function assertElement(assertFn, shouldBeElement, opt_message) {
return (
/** @type {!Ele ment} */
assertType_(assertFn, shouldBeElement, isElement(shouldBeElement), 'Element expected', opt_message)
);
}
const slot =
/** @type {!HTMLSlotElement} */
e.target;
assertElement(
/** @type {Element} */
el);

View File

@ -26,6 +26,7 @@ export type CommentWhitespace = {
trailingNode: Node | null, trailingNode: Node | null,
containingNode: Node | null, containingNode: Node | null,
}; };
/** /**
* Merge comments with node's trailingComments or assign comments to be * Merge comments with node's trailingComments or assign comments to be
* trailingComments. New comments will be placed before old comments * trailingComments. New comments will be placed before old comments
@ -42,6 +43,22 @@ function setTrailingComments(node: Node, comments: Array<Comment>) {
} }
} }
/**
* Merge comments with node's leadingComments or assign comments to be
* leadingComments. New comments will be placed before old comments
* because the commentStack is enumerated reversely.
*
* @param {Node} node
* @param {Array<Comment>} comments
*/
function setLeadingComments(node: Node, comments: Array<Comment>) {
if (node.leadingComments === undefined) {
node.leadingComments = comments;
} else {
node.leadingComments.unshift(...comments);
}
}
/** /**
* Merge comments with node's innerComments or assign comments to be * Merge comments with node's innerComments or assign comments to be
* innerComments. New comments will be placed before old comments * innerComments. New comments will be placed before old comments
@ -50,10 +67,10 @@ function setTrailingComments(node: Node, comments: Array<Comment>) {
* @param {Node} node * @param {Node} node
* @param {Array<Comment>} comments * @param {Array<Comment>} comments
*/ */
export function setInnerComments(node: Node, comments: Array<Comment> | void) { export function setInnerComments(node: Node, comments: Array<Comment>) {
if (node.innerComments === undefined) { if (node.innerComments === undefined) {
node.innerComments = comments; node.innerComments = comments;
} else if (comments !== undefined) { } else {
node.innerComments.unshift(...comments); node.innerComments.unshift(...comments);
} }
} }
@ -149,7 +166,7 @@ export default class CommentsParser extends BaseParser {
setTrailingComments(commentWS.leadingNode, comments); setTrailingComments(commentWS.leadingNode, comments);
} }
if (commentWS.trailingNode !== null) { if (commentWS.trailingNode !== null) {
commentWS.trailingNode.leadingComments = comments; setLeadingComments(commentWS.trailingNode, comments);
} }
} else { } else {
/*:: invariant(commentWS.containingNode !== null) */ /*:: invariant(commentWS.containingNode !== null) */
@ -238,4 +255,36 @@ export default class CommentsParser extends BaseParser {
commentWS.leadingNode = null; commentWS.leadingNode = null;
} }
} }
/**
* Attach a node to the comment whitespaces right before/after
* the given range.
*
* This is used to properly attach comments around parenthesized
* expressions as leading/trailing comments of the inner expression.
*
* @param {Node} node
* @param {number} start
* @param {number} end
*/
takeSurroundingComments(node: Node, start: number, end: number) {
const { commentStack } = this.state;
const commentStackLength = commentStack.length;
if (commentStackLength === 0) return;
let i = commentStackLength - 1;
for (; i >= 0; i--) {
const commentWS = commentStack[i];
const commentEnd = commentWS.end;
const commentStart = commentWS.start;
if (commentStart === end) {
commentWS.leadingNode = node;
} else if (commentEnd === start) {
commentWS.trailingNode = node;
} else if (commentEnd < start) {
break;
}
}
}
} }

View File

@ -1023,9 +1023,13 @@ export default class ExpressionParser extends LValParser {
call.extra?.trailingComma, call.extra?.trailingComma,
); );
// mark inner comments of `async()` as inner comments of `async () =>` // mark inner comments of `async()` as inner comments of `async () =>`
if (call.innerComments) {
setInnerComments(node, call.innerComments); setInnerComments(node, call.innerComments);
}
// mark trailing comments of `async` to be inner comments // mark trailing comments of `async` to be inner comments
if (call.callee.trailingComments) {
setInnerComments(node, call.callee.trailingComments); setInnerComments(node, call.callee.trailingComments);
}
return node; return node;
} }
@ -1738,6 +1742,9 @@ export default class ExpressionParser extends LValParser {
if (!this.options.createParenthesizedExpressions) { if (!this.options.createParenthesizedExpressions) {
this.addExtra(val, "parenthesized", true); this.addExtra(val, "parenthesized", true);
this.addExtra(val, "parenStart", startPos); this.addExtra(val, "parenStart", startPos);
this.takeSurroundingComments(val, startPos, this.state.lastTokEnd);
return val; return val;
} }

View File

@ -0,0 +1 @@
1 + /* 0 */(/* 1 */(/* 2 */(/* 3 */i/* 4 */)/* 5 */)/* 6 */)/* 7 */;

View File

@ -0,0 +1,128 @@
{
"type": "File",
"start":0,"end":68,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":68}},
"program": {
"type": "Program",
"start":0,"end":68,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":68}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":68,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":68}},
"expression": {
"type": "BinaryExpression",
"start":0,"end":60,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":60}},
"left": {
"type": "NumericLiteral",
"start":0,"end":1,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":1}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
},
"operator": "+",
"right": {
"type": "Identifier",
"start":35,"end":36,"loc":{"start":{"line":1,"column":35},"end":{"line":1,"column":36},"identifierName":"i"},
"name": "i",
"extra": {
"parenthesized": true,
"parenStart": 11
},
"trailingComments": [
{
"type": "CommentBlock",
"value": " 4 ",
"start":36,"end":43,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":43}}
},
{
"type": "CommentBlock",
"value": " 5 ",
"start":44,"end":51,"loc":{"start":{"line":1,"column":44},"end":{"line":1,"column":51}}
},
{
"type": "CommentBlock",
"value": " 6 ",
"start":52,"end":59,"loc":{"start":{"line":1,"column":52},"end":{"line":1,"column":59}}
}
],
"leadingComments": [
{
"type": "CommentBlock",
"value": " 0 ",
"start":4,"end":11,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":11}}
},
{
"type": "CommentBlock",
"value": " 1 ",
"start":12,"end":19,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":19}}
},
{
"type": "CommentBlock",
"value": " 2 ",
"start":20,"end":27,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":27}}
},
{
"type": "CommentBlock",
"value": " 3 ",
"start":28,"end":35,"loc":{"start":{"line":1,"column":28},"end":{"line":1,"column":35}}
}
]
},
"trailingComments": [
{
"type": "CommentBlock",
"value": " 7 ",
"start":60,"end":67,"loc":{"start":{"line":1,"column":60},"end":{"line":1,"column":67}}
}
]
}
}
],
"directives": []
},
"comments": [
{
"type": "CommentBlock",
"value": " 0 ",
"start":4,"end":11,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":11}}
},
{
"type": "CommentBlock",
"value": " 1 ",
"start":12,"end":19,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":19}}
},
{
"type": "CommentBlock",
"value": " 2 ",
"start":20,"end":27,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":27}}
},
{
"type": "CommentBlock",
"value": " 3 ",
"start":28,"end":35,"loc":{"start":{"line":1,"column":28},"end":{"line":1,"column":35}}
},
{
"type": "CommentBlock",
"value": " 4 ",
"start":36,"end":43,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":43}}
},
{
"type": "CommentBlock",
"value": " 5 ",
"start":44,"end":51,"loc":{"start":{"line":1,"column":44},"end":{"line":1,"column":51}}
},
{
"type": "CommentBlock",
"value": " 6 ",
"start":52,"end":59,"loc":{"start":{"line":1,"column":52},"end":{"line":1,"column":59}}
},
{
"type": "CommentBlock",
"value": " 7 ",
"start":60,"end":67,"loc":{"start":{"line":1,"column":60},"end":{"line":1,"column":67}}
}
]
}

View File

@ -24,22 +24,15 @@
"start":77,"end":84,"loc":{"start":{"line":1,"column":77},"end":{"line":1,"column":84}} "start":77,"end":84,"loc":{"start":{"line":1,"column":77},"end":{"line":1,"column":84}}
} }
], ],
"innerComments": [ "expression": {
"type": "SequenceExpression",
"start":27,"end":47,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":47}},
"leadingComments": [
{ {
"type": "CommentBlock", "type": "CommentBlock",
"value": " 2 ", "value": " 2 ",
"start":9,"end":16,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":16}} "start":9,"end":16,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":16}}
}, },
{
"type": "CommentBlock",
"value": " 8 ",
"start":68,"end":75,"loc":{"start":{"line":1,"column":68},"end":{"line":1,"column":75}}
}
],
"expression": {
"type": "SequenceExpression",
"start":27,"end":47,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":47}},
"leadingComments": [
{ {
"type": "CommentBlock", "type": "CommentBlock",
"value": " 3 ", "value": " 3 ",
@ -51,6 +44,11 @@
"type": "CommentBlock", "type": "CommentBlock",
"value": " 7 ", "value": " 7 ",
"start":58,"end":65,"loc":{"start":{"line":1,"column":58},"end":{"line":1,"column":65}} "start":58,"end":65,"loc":{"start":{"line":1,"column":58},"end":{"line":1,"column":65}}
},
{
"type": "CommentBlock",
"value": " 8 ",
"start":68,"end":75,"loc":{"start":{"line":1,"column":68},"end":{"line":1,"column":75}}
} }
], ],
"extra": { "extra": {

View File

@ -0,0 +1,5 @@
if (args[0] === 1 || /* istanbul ignore next */ (args[0] === 2 || args[0] === 3)) {
output = args[0] + 10;
} else {
output = 20;
}

View File

@ -0,0 +1,228 @@
{
"type": "File",
"start":0,"end":136,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}},
"program": {
"type": "Program",
"start":0,"end":136,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "IfStatement",
"start":0,"end":136,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}},
"test": {
"type": "LogicalExpression",
"start":4,"end":82,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":82}},
"left": {
"type": "BinaryExpression",
"start":4,"end":17,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":17}},
"left": {
"type": "MemberExpression",
"start":4,"end":11,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":11}},
"object": {
"type": "Identifier",
"start":4,"end":8,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":8},"identifierName":"args"},
"name": "args"
},
"computed": true,
"property": {
"type": "NumericLiteral",
"start":9,"end":10,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":10}},
"extra": {
"rawValue": 0,
"raw": "0"
},
"value": 0
}
},
"operator": "===",
"right": {
"type": "NumericLiteral",
"start":16,"end":17,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":17}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
},
"operator": "||",
"right": {
"type": "LogicalExpression",
"start":50,"end":81,"loc":{"start":{"line":1,"column":50},"end":{"line":1,"column":81}},
"left": {
"type": "BinaryExpression",
"start":50,"end":63,"loc":{"start":{"line":1,"column":50},"end":{"line":1,"column":63}},
"left": {
"type": "MemberExpression",
"start":50,"end":57,"loc":{"start":{"line":1,"column":50},"end":{"line":1,"column":57}},
"object": {
"type": "Identifier",
"start":50,"end":54,"loc":{"start":{"line":1,"column":50},"end":{"line":1,"column":54},"identifierName":"args"},
"name": "args"
},
"computed": true,
"property": {
"type": "NumericLiteral",
"start":55,"end":56,"loc":{"start":{"line":1,"column":55},"end":{"line":1,"column":56}},
"extra": {
"rawValue": 0,
"raw": "0"
},
"value": 0
}
},
"operator": "===",
"right": {
"type": "NumericLiteral",
"start":62,"end":63,"loc":{"start":{"line":1,"column":62},"end":{"line":1,"column":63}},
"extra": {
"rawValue": 2,
"raw": "2"
},
"value": 2
}
},
"operator": "||",
"right": {
"type": "BinaryExpression",
"start":68,"end":81,"loc":{"start":{"line":1,"column":68},"end":{"line":1,"column":81}},
"left": {
"type": "MemberExpression",
"start":68,"end":75,"loc":{"start":{"line":1,"column":68},"end":{"line":1,"column":75}},
"object": {
"type": "Identifier",
"start":68,"end":72,"loc":{"start":{"line":1,"column":68},"end":{"line":1,"column":72},"identifierName":"args"},
"name": "args"
},
"computed": true,
"property": {
"type": "NumericLiteral",
"start":73,"end":74,"loc":{"start":{"line":1,"column":73},"end":{"line":1,"column":74}},
"extra": {
"rawValue": 0,
"raw": "0"
},
"value": 0
}
},
"operator": "===",
"right": {
"type": "NumericLiteral",
"start":80,"end":81,"loc":{"start":{"line":1,"column":80},"end":{"line":1,"column":81}},
"extra": {
"rawValue": 3,
"raw": "3"
},
"value": 3
}
},
"extra": {
"parenthesized": true,
"parenStart": 49
},
"leadingComments": [
{
"type": "CommentBlock",
"value": " istanbul ignore next ",
"start":22,"end":48,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":48}}
}
]
}
},
"consequent": {
"type": "BlockStatement",
"start":84,"end":112,"loc":{"start":{"line":1,"column":84},"end":{"line":3,"column":1}},
"body": [
{
"type": "ExpressionStatement",
"start":88,"end":110,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":24}},
"expression": {
"type": "AssignmentExpression",
"start":88,"end":109,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":23}},
"operator": "=",
"left": {
"type": "Identifier",
"start":88,"end":94,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":8},"identifierName":"output"},
"name": "output"
},
"right": {
"type": "BinaryExpression",
"start":97,"end":109,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":23}},
"left": {
"type": "MemberExpression",
"start":97,"end":104,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":18}},
"object": {
"type": "Identifier",
"start":97,"end":101,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":15},"identifierName":"args"},
"name": "args"
},
"computed": true,
"property": {
"type": "NumericLiteral",
"start":102,"end":103,"loc":{"start":{"line":2,"column":16},"end":{"line":2,"column":17}},
"extra": {
"rawValue": 0,
"raw": "0"
},
"value": 0
}
},
"operator": "+",
"right": {
"type": "NumericLiteral",
"start":107,"end":109,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":23}},
"extra": {
"rawValue": 10,
"raw": "10"
},
"value": 10
}
}
}
}
],
"directives": []
},
"alternate": {
"type": "BlockStatement",
"start":118,"end":136,"loc":{"start":{"line":3,"column":7},"end":{"line":5,"column":1}},
"body": [
{
"type": "ExpressionStatement",
"start":122,"end":134,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":14}},
"expression": {
"type": "AssignmentExpression",
"start":122,"end":133,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":13}},
"operator": "=",
"left": {
"type": "Identifier",
"start":122,"end":128,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":8},"identifierName":"output"},
"name": "output"
},
"right": {
"type": "NumericLiteral",
"start":131,"end":133,"loc":{"start":{"line":4,"column":11},"end":{"line":4,"column":13}},
"extra": {
"rawValue": 20,
"raw": "20"
},
"value": 20
}
}
}
],
"directives": []
}
}
],
"directives": []
},
"comments": [
{
"type": "CommentBlock",
"value": " istanbul ignore next ",
"start":22,"end":48,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":48}}
}
]
}

View File

@ -2,6 +2,7 @@ var _ref, _ref2, _ref3;
const result = (_ref3 = -2.2 // -2.2 const result = (_ref3 = -2.2 // -2.2
, (_ref2 = Math.floor(_ref3) // -3 , (_ref2 = Math.floor(_ref3) // -3
, (_ref = () => Math.pow(_ref2, 5), _ref()))); // -243 , (_ref = () => Math.pow(_ref2, 5) // () => -243
, _ref()))); // -243
expect(result).toBe(-243); expect(result).toBe(-243);

View File

@ -3,7 +3,8 @@ const result = () => {
return _ref3 = -2.2 // -2.2 return _ref3 = -2.2 // -2.2
, (_ref2 = Math.floor(_ref3) // -3 , (_ref2 = Math.floor(_ref3) // -3
, (_ref = () => Math.pow(_ref2, 5), _ref())); , (_ref = () => Math.pow(_ref2, 5) // () => -243
, _ref()));
}; // -243 }; // -243

View File

@ -2,6 +2,7 @@ var _ref, _ref2, _ref3;
const result = (_ref3 = -2.2 // -2.2 const result = (_ref3 = -2.2 // -2.2
, (_ref2 = Math.floor(_ref3) // -3 , (_ref2 = Math.floor(_ref3) // -3
, (_ref = () => Math.pow(_ref2, 5), _ref()))); // -243 , (_ref = () => Math.pow(_ref2, 5) // () => -243
, _ref()))); // -243
expect(result).toBe(-243); expect(result).toBe(-243);

View File

@ -3,7 +3,8 @@ const result = () => {
return _ref3 = -2.2 // -2.2 return _ref3 = -2.2 // -2.2
, (_ref2 = Math.floor(_ref3) // -3 , (_ref2 = Math.floor(_ref3) // -3
, (_ref = () => Math.pow(_ref2, 5), _ref())); , (_ref = () => Math.pow(_ref2, 5) // () => -243
, _ref()));
}; // -243 }; // -243