Overhaul comment attachment (#13521)
* refactor: inline pushComment * chore: add benchmark cases * perf: overhaul comment attachment * cleanup * update test fixtures They are all bugfixes. * fix: merge HTMLComment parsing to skipSpace * perf: remove unattachedCommentStack baseline 128 nested leading comments: 11_034 ops/sec ±50.64% (0.091ms) baseline 256 nested leading comments: 6_037 ops/sec ±11.46% (0.166ms) baseline 512 nested leading comments: 3_077 ops/sec ±2.31% (0.325ms) baseline 1024 nested leading comments: 1_374 ops/sec ±3.22% (0.728ms) current 128 nested leading comments: 11_027 ops/sec ±37.41% (0.091ms) current 256 nested leading comments: 6_736 ops/sec ±1.39% (0.148ms) current 512 nested leading comments: 3_306 ops/sec ±0.69% (0.302ms) current 1024 nested leading comments: 1_579 ops/sec ±2.09% (0.633ms) baseline 128 nested trailing comments: 10_073 ops/sec ±42.95% (0.099ms) baseline 256 nested trailing comments: 6_294 ops/sec ±2.19% (0.159ms) baseline 512 nested trailing comments: 3_041 ops/sec ±0.8% (0.329ms) baseline 1024 nested trailing comments: 1_530 ops/sec ±1.18% (0.654ms) current 128 nested trailing comments: 11_461 ops/sec ±44.89% (0.087ms) current 256 nested trailing comments: 7_212 ops/sec ±1.6% (0.139ms) current 512 nested trailing comments: 3_403 ops/sec ±1% (0.294ms) current 1024 nested trailing comments: 1_539 ops/sec ±1.49% (0.65ms) * fix: do not expose CommentWhitespace type * add comments on CommentWhitespace * add test case for #11576 * fix: mark containerNode be the innermost node containing commentWS * fix: adjust trailing comma comments for Record/Tuple/OptionalCall * fix: drain comment stacks in parseExpression * docs: update comments * add a new benchmark * chore: containerNode => containingNode * add more benchmark cases * fix: avoid finishNodeAt in stmtToDirective * finalize comment right after containerNode is set * add testcase about directive * fix: finish SequenceExpression at current pos and adjust later * chore: rename test cases * add new test case on switch statement * fix: adjust comments after trailing comma of function params * add comment attachment design doc * misc fix * fix: reset previous trailing comments when parsing async method/accessor * chore: add more comment testcases * fix flow errors * fix: handle comments when parsing async arrow * fix: handle comments when "static" is a class modifier * fix flow errors * fix: handle comments when parsing async function/do * refactor: simplify resetPreviousNodeTrailingComments * update test fixtures
This commit is contained in:
6
packages/babel-parser/test/fixtures/comments/basic/switch-case/input.js
vendored
Normal file
6
packages/babel-parser/test/fixtures/comments/basic/switch-case/input.js
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
/*-1*/ foo /*0*/ : /*1*/ switch /*2*/ ( /*3*/ false /*4*/ ) /*5*/ {
|
||||
/*6*/ case /*7*/ false /*8*/ : /*9*/
|
||||
break /*10*/ foo /*11*/ ;
|
||||
/*12*/ default /*13*/ : /*14*/
|
||||
case /*15*/ false /*16*/ : /*17*/ { /*18*/ } /*19*/
|
||||
}
|
||||
342
packages/babel-parser/test/fixtures/comments/basic/switch-case/output.json
vendored
Normal file
342
packages/babel-parser/test/fixtures/comments/basic/switch-case/output.json
vendored
Normal file
@@ -0,0 +1,342 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start":0,"end":233,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":3}},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start":0,"end":233,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":3}},
|
||||
"sourceType": "script",
|
||||
"interpreter": null,
|
||||
"body": [
|
||||
{
|
||||
"type": "LabeledStatement",
|
||||
"start":7,"end":233,"loc":{"start":{"line":1,"column":7},"end":{"line":6,"column":3}},
|
||||
"leadingComments": [
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "-1",
|
||||
"start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}}
|
||||
}
|
||||
],
|
||||
"body": {
|
||||
"type": "SwitchStatement",
|
||||
"start":25,"end":233,"loc":{"start":{"line":1,"column":25},"end":{"line":6,"column":3}},
|
||||
"leadingComments": [
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "1",
|
||||
"start":19,"end":24,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":24}}
|
||||
}
|
||||
],
|
||||
"innerComments": [
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "2",
|
||||
"start":32,"end":37,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":37}}
|
||||
},
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "5",
|
||||
"start":60,"end":65,"loc":{"start":{"line":1,"column":60},"end":{"line":1,"column":65}}
|
||||
}
|
||||
],
|
||||
"discriminant": {
|
||||
"type": "BooleanLiteral",
|
||||
"start":46,"end":51,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":51}},
|
||||
"leadingComments": [
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "3",
|
||||
"start":40,"end":45,"loc":{"start":{"line":1,"column":40},"end":{"line":1,"column":45}}
|
||||
}
|
||||
],
|
||||
"trailingComments": [
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "4",
|
||||
"start":52,"end":57,"loc":{"start":{"line":1,"column":52},"end":{"line":1,"column":57}}
|
||||
}
|
||||
],
|
||||
"value": false
|
||||
},
|
||||
"cases": [
|
||||
{
|
||||
"type": "SwitchCase",
|
||||
"start":78,"end":138,"loc":{"start":{"line":2,"column":10},"end":{"line":3,"column":29}},
|
||||
"leadingComments": [
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "6",
|
||||
"start":72,"end":77,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":9}}
|
||||
}
|
||||
],
|
||||
"trailingComments": [
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "12",
|
||||
"start":143,"end":149,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":10}}
|
||||
}
|
||||
],
|
||||
"consequent": [
|
||||
{
|
||||
"type": "BreakStatement",
|
||||
"start":113,"end":138,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":29}},
|
||||
"leadingComments": [
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "9",
|
||||
"start":103,"end":108,"loc":{"start":{"line":2,"column":35},"end":{"line":2,"column":40}}
|
||||
}
|
||||
],
|
||||
"label": {
|
||||
"type": "Identifier",
|
||||
"start":126,"end":129,"loc":{"start":{"line":3,"column":17},"end":{"line":3,"column":20},"identifierName":"foo"},
|
||||
"leadingComments": [
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "10",
|
||||
"start":119,"end":125,"loc":{"start":{"line":3,"column":10},"end":{"line":3,"column":16}}
|
||||
}
|
||||
],
|
||||
"trailingComments": [
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "11",
|
||||
"start":130,"end":136,"loc":{"start":{"line":3,"column":21},"end":{"line":3,"column":27}}
|
||||
}
|
||||
],
|
||||
"name": "foo"
|
||||
}
|
||||
}
|
||||
],
|
||||
"test": {
|
||||
"type": "BooleanLiteral",
|
||||
"start":89,"end":94,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":26}},
|
||||
"leadingComments": [
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "7",
|
||||
"start":83,"end":88,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":20}}
|
||||
}
|
||||
],
|
||||
"trailingComments": [
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "8",
|
||||
"start":95,"end":100,"loc":{"start":{"line":2,"column":27},"end":{"line":2,"column":32}}
|
||||
}
|
||||
],
|
||||
"value": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "SwitchCase",
|
||||
"start":150,"end":166,"loc":{"start":{"line":4,"column":11},"end":{"line":4,"column":27}},
|
||||
"leadingComments": [
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "12",
|
||||
"start":143,"end":149,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":10}}
|
||||
}
|
||||
],
|
||||
"trailingComments": [
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "14",
|
||||
"start":167,"end":173,"loc":{"start":{"line":4,"column":28},"end":{"line":4,"column":34}}
|
||||
}
|
||||
],
|
||||
"innerComments": [
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "13",
|
||||
"start":158,"end":164,"loc":{"start":{"line":4,"column":19},"end":{"line":4,"column":25}}
|
||||
}
|
||||
],
|
||||
"consequent": [],
|
||||
"test": null
|
||||
},
|
||||
{
|
||||
"type": "SwitchCase",
|
||||
"start":178,"end":222,"loc":{"start":{"line":5,"column":4},"end":{"line":5,"column":48}},
|
||||
"leadingComments": [
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "14",
|
||||
"start":167,"end":173,"loc":{"start":{"line":4,"column":28},"end":{"line":4,"column":34}}
|
||||
}
|
||||
],
|
||||
"trailingComments": [
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "19",
|
||||
"start":223,"end":229,"loc":{"start":{"line":5,"column":49},"end":{"line":5,"column":55}}
|
||||
}
|
||||
],
|
||||
"consequent": [
|
||||
{
|
||||
"type": "BlockStatement",
|
||||
"start":212,"end":222,"loc":{"start":{"line":5,"column":38},"end":{"line":5,"column":48}},
|
||||
"leadingComments": [
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "17",
|
||||
"start":205,"end":211,"loc":{"start":{"line":5,"column":31},"end":{"line":5,"column":37}}
|
||||
}
|
||||
],
|
||||
"innerComments": [
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "18",
|
||||
"start":214,"end":220,"loc":{"start":{"line":5,"column":40},"end":{"line":5,"column":46}}
|
||||
}
|
||||
],
|
||||
"body": [],
|
||||
"directives": []
|
||||
}
|
||||
],
|
||||
"test": {
|
||||
"type": "BooleanLiteral",
|
||||
"start":190,"end":195,"loc":{"start":{"line":5,"column":16},"end":{"line":5,"column":21}},
|
||||
"leadingComments": [
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "15",
|
||||
"start":183,"end":189,"loc":{"start":{"line":5,"column":9},"end":{"line":5,"column":15}}
|
||||
}
|
||||
],
|
||||
"trailingComments": [
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "16",
|
||||
"start":196,"end":202,"loc":{"start":{"line":5,"column":22},"end":{"line":5,"column":28}}
|
||||
}
|
||||
],
|
||||
"value": false
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"label": {
|
||||
"type": "Identifier",
|
||||
"start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10},"identifierName":"foo"},
|
||||
"trailingComments": [
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "0",
|
||||
"start":11,"end":16,"loc":{"start":{"line":1,"column":11},"end":{"line":1,"column":16}}
|
||||
}
|
||||
],
|
||||
"name": "foo"
|
||||
}
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
},
|
||||
"comments": [
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "-1",
|
||||
"start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}}
|
||||
},
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "0",
|
||||
"start":11,"end":16,"loc":{"start":{"line":1,"column":11},"end":{"line":1,"column":16}}
|
||||
},
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "1",
|
||||
"start":19,"end":24,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":24}}
|
||||
},
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "2",
|
||||
"start":32,"end":37,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":37}}
|
||||
},
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "3",
|
||||
"start":40,"end":45,"loc":{"start":{"line":1,"column":40},"end":{"line":1,"column":45}}
|
||||
},
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "4",
|
||||
"start":52,"end":57,"loc":{"start":{"line":1,"column":52},"end":{"line":1,"column":57}}
|
||||
},
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "5",
|
||||
"start":60,"end":65,"loc":{"start":{"line":1,"column":60},"end":{"line":1,"column":65}}
|
||||
},
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "6",
|
||||
"start":72,"end":77,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":9}}
|
||||
},
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "7",
|
||||
"start":83,"end":88,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":20}}
|
||||
},
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "8",
|
||||
"start":95,"end":100,"loc":{"start":{"line":2,"column":27},"end":{"line":2,"column":32}}
|
||||
},
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "9",
|
||||
"start":103,"end":108,"loc":{"start":{"line":2,"column":35},"end":{"line":2,"column":40}}
|
||||
},
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "10",
|
||||
"start":119,"end":125,"loc":{"start":{"line":3,"column":10},"end":{"line":3,"column":16}}
|
||||
},
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "11",
|
||||
"start":130,"end":136,"loc":{"start":{"line":3,"column":21},"end":{"line":3,"column":27}}
|
||||
},
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "12",
|
||||
"start":143,"end":149,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":10}}
|
||||
},
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "13",
|
||||
"start":158,"end":164,"loc":{"start":{"line":4,"column":19},"end":{"line":4,"column":25}}
|
||||
},
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "14",
|
||||
"start":167,"end":173,"loc":{"start":{"line":4,"column":28},"end":{"line":4,"column":34}}
|
||||
},
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "15",
|
||||
"start":183,"end":189,"loc":{"start":{"line":5,"column":9},"end":{"line":5,"column":15}}
|
||||
},
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "16",
|
||||
"start":196,"end":202,"loc":{"start":{"line":5,"column":22},"end":{"line":5,"column":28}}
|
||||
},
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "17",
|
||||
"start":205,"end":211,"loc":{"start":{"line":5,"column":31},"end":{"line":5,"column":37}}
|
||||
},
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "18",
|
||||
"start":214,"end":220,"loc":{"start":{"line":5,"column":40},"end":{"line":5,"column":46}}
|
||||
},
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": "19",
|
||||
"start":223,"end":229,"loc":{"start":{"line":5,"column":49},"end":{"line":5,"column":55}}
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user