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
Add attachComment
parser option to disable comment attachment
#13229
Merged
nicolo-ribaudo
merged 3 commits into
babel:main
from
JLHwung:parser-attach-comment-option
Aug 4, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
benchmark/babel-parser/many-leading-trailing-comments/attachComment-false.bench.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import Benchmark from "benchmark"; | ||
import baseline from "@babel-baseline/parser"; | ||
import current from "../../lib/index.js"; | ||
import { report } from "../util.mjs"; | ||
|
||
const suite = new Benchmark.Suite(); | ||
|
||
function createInput(length) { | ||
return "\n// c\na".repeat(length); | ||
} | ||
|
||
function benchCases(name, implementation, options) { | ||
for (const length of [256, 512, 1024, 2048]) { | ||
const input = createInput(length); | ||
const { parse } = implementation; | ||
suite.add( | ||
`${name} ${length} leading comments + ${length - 1} trailing comments`, | ||
() => { | ||
parse(input, options); | ||
} | ||
); | ||
} | ||
} | ||
|
||
benchCases("baseline", baseline, { attachComment: true }); | ||
benchCases("current + attachComment: false", current, { attachComment: false }); | ||
|
||
suite.on("cycle", report).run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import path from "path"; | ||
import { runFixtureTestsWithoutExactASTMatch } from "./helpers/runFixtureTests"; | ||
import { parseExpression } from "../lib"; | ||
import { fileURLToPath } from "url"; | ||
|
||
runFixtureTestsWithoutExactASTMatch( | ||
path.join(path.dirname(fileURLToPath(import.meta.url)), "expressions"), | ||
(input, options = {}) => { | ||
options.attachComment = false; | ||
return parseExpression(input, options); | ||
}, | ||
); | ||
18 changes: 18 additions & 0 deletions
18
...arser/test/fixtures/comments/attachComment-false/array-expression-trailing-comma/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const nonTrailing = [ | ||
"One", // One | ||
// Two | ||
"Two" // Three | ||
// Four | ||
] | ||
|
||
const trailingAfterComma = [ | ||
"One", // One | ||
// Two | ||
"Two", // Three | ||
// Four | ||
] | ||
|
||
const trailingAfterArray = [ | ||
"One", // One | ||
// Two | ||
] // Three |
187 changes: 187 additions & 0 deletions
187
...er/test/fixtures/comments/attachComment-false/array-expression-trailing-comma/output.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
{ | ||
"type": "File", | ||
"start":0,"end":229,"loc":{"start":{"line":1,"column":0},"end":{"line":18,"column":10}}, | ||
"program": { | ||
"type": "Program", | ||
"start":0,"end":229,"loc":{"start":{"line":1,"column":0},"end":{"line":18,"column":10}}, | ||
"sourceType": "script", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "VariableDeclaration", | ||
"start":0,"end":76,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":1}}, | ||
"declarations": [ | ||
{ | ||
"type": "VariableDeclarator", | ||
"start":6,"end":76,"loc":{"start":{"line":1,"column":6},"end":{"line":6,"column":1}}, | ||
"id": { | ||
"type": "Identifier", | ||
"start":6,"end":17,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":17},"identifierName":"nonTrailing"}, | ||
"name": "nonTrailing" | ||
}, | ||
"init": { | ||
"type": "ArrayExpression", | ||
"start":20,"end":76,"loc":{"start":{"line":1,"column":20},"end":{"line":6,"column":1}}, | ||
"elements": [ | ||
{ | ||
"type": "StringLiteral", | ||
"start":24,"end":29,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":7}}, | ||
"extra": { | ||
"rawValue": "One", | ||
"raw": "\"One\"" | ||
}, | ||
"value": "One" | ||
}, | ||
{ | ||
"type": "StringLiteral", | ||
"start":50,"end":55,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":7}}, | ||
"extra": { | ||
"rawValue": "Two", | ||
"raw": "\"Two\"" | ||
}, | ||
"value": "Two" | ||
} | ||
] | ||
} | ||
} | ||
], | ||
"kind": "const" | ||
}, | ||
{ | ||
"type": "VariableDeclaration", | ||
"start":78,"end":162,"loc":{"start":{"line":8,"column":0},"end":{"line":13,"column":1}}, | ||
"declarations": [ | ||
{ | ||
"type": "VariableDeclarator", | ||
"start":84,"end":162,"loc":{"start":{"line":8,"column":6},"end":{"line":13,"column":1}}, | ||
"id": { | ||
"type": "Identifier", | ||
"start":84,"end":102,"loc":{"start":{"line":8,"column":6},"end":{"line":8,"column":24},"identifierName":"trailingAfterComma"}, | ||
"name": "trailingAfterComma" | ||
}, | ||
"init": { | ||
"type": "ArrayExpression", | ||
"start":105,"end":162,"loc":{"start":{"line":8,"column":27},"end":{"line":13,"column":1}}, | ||
"extra": { | ||
"trailingComma": 140 | ||
}, | ||
"elements": [ | ||
{ | ||
"type": "StringLiteral", | ||
"start":109,"end":114,"loc":{"start":{"line":9,"column":2},"end":{"line":9,"column":7}}, | ||
"extra": { | ||
"rawValue": "One", | ||
"raw": "\"One\"" | ||
}, | ||
"value": "One" | ||
}, | ||
{ | ||
"type": "StringLiteral", | ||
"start":135,"end":140,"loc":{"start":{"line":11,"column":2},"end":{"line":11,"column":7}}, | ||
"extra": { | ||
"rawValue": "Two", | ||
"raw": "\"Two\"" | ||
}, | ||
"value": "Two" | ||
} | ||
] | ||
} | ||
} | ||
], | ||
"kind": "const" | ||
}, | ||
{ | ||
"type": "VariableDeclaration", | ||
"start":164,"end":220,"loc":{"start":{"line":15,"column":0},"end":{"line":18,"column":1}}, | ||
"declarations": [ | ||
{ | ||
"type": "VariableDeclarator", | ||
"start":170,"end":220,"loc":{"start":{"line":15,"column":6},"end":{"line":18,"column":1}}, | ||
"id": { | ||
"type": "Identifier", | ||
"start":170,"end":188,"loc":{"start":{"line":15,"column":6},"end":{"line":15,"column":24},"identifierName":"trailingAfterArray"}, | ||
"name": "trailingAfterArray" | ||
}, | ||
"init": { | ||
"type": "ArrayExpression", | ||
"start":191,"end":220,"loc":{"start":{"line":15,"column":27},"end":{"line":18,"column":1}}, | ||
"extra": { | ||
"trailingComma": 200 | ||
}, | ||
"elements": [ | ||
{ | ||
"type": "StringLiteral", | ||
"start":195,"end":200,"loc":{"start":{"line":16,"column":2},"end":{"line":16,"column":7}}, | ||
"extra": { | ||
"rawValue": "One", | ||
"raw": "\"One\"" | ||
}, | ||
"value": "One" | ||
} | ||
] | ||
} | ||
} | ||
], | ||
"kind": "const" | ||
} | ||
], | ||
"directives": [] | ||
}, | ||
"comments": [ | ||
{ | ||
"type": "CommentLine", | ||
"value": " One", | ||
"start":31,"end":37,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":15}} | ||
}, | ||
{ | ||
"type": "CommentLine", | ||
"value": " Two", | ||
"start":41,"end":47,"loc":{"start":{"line":3,"column":3},"end":{"line":3,"column":9}} | ||
}, | ||
{ | ||
"type": "CommentLine", | ||
"value": " Three", | ||
"start":56,"end":64,"loc":{"start":{"line":4,"column":8},"end":{"line":4,"column":16}} | ||
}, | ||
{ | ||
"type": "CommentLine", | ||
"value": " Four", | ||
"start":67,"end":74,"loc":{"start":{"line":5,"column":2},"end":{"line":5,"column":9}} | ||
}, | ||
{ | ||
"type": "CommentLine", | ||
"value": " One", | ||
"start":116,"end":122,"loc":{"start":{"line":9,"column":9},"end":{"line":9,"column":15}} | ||
}, | ||
{ | ||
"type": "CommentLine", | ||
"value": " Two", | ||
"start":126,"end":132,"loc":{"start":{"line":10,"column":3},"end":{"line":10,"column":9}} | ||
}, | ||
{ | ||
"type": "CommentLine", | ||
"value": " Three", | ||
"start":142,"end":150,"loc":{"start":{"line":11,"column":9},"end":{"line":11,"column":17}} | ||
}, | ||
{ | ||
"type": "CommentLine", | ||
"value": " Four", | ||
"start":153,"end":160,"loc":{"start":{"line":12,"column":2},"end":{"line":12,"column":9}} | ||
}, | ||
{ | ||
"type": "CommentLine", | ||
"value": " One", | ||
"start":202,"end":208,"loc":{"start":{"line":16,"column":9},"end":{"line":16,"column":15}} | ||
}, | ||
{ | ||
"type": "CommentLine", | ||
"value": " Two", | ||
"start":212,"end":218,"loc":{"start":{"line":17,"column":3},"end":{"line":17,"column":9}} | ||
}, | ||
{ | ||
"type": "CommentLine", | ||
"value": " Three", | ||
"start":221,"end":229,"loc":{"start":{"line":18,"column":2},"end":{"line":18,"column":10}} | ||
} | ||
] | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/babel-parser/test/fixtures/comments/attachComment-false/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"attachComment": false | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We run
parseExpression(#, { attachComment: false })
onexpressions
fixture as smoke tests. It is faster than running over the complete parser tests.