This repository has been archived by the owner on May 19, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add comment attachment to the parser and remove dead acorn options
- Loading branch information
Showing
13 changed files
with
227 additions
and
391 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/** | ||
* Based on the comment attachment algorithm used in espree and estraverse. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY | ||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
import { Parser } from "./state"; | ||
|
||
function last(stack) { | ||
return stack[stack.length - 1]; | ||
} | ||
|
||
const pp = Parser.prototype; | ||
|
||
pp.addComment = function (comment) { | ||
this.trailingComments.push(comment); | ||
this.leadingComments.push(comment); | ||
}; | ||
|
||
pp.processComment = function (node) { | ||
var stack = this.bottomRightStack; | ||
|
||
var lastChild; | ||
var trailingComments; | ||
var i; | ||
|
||
if (this.trailingComments.length > 0) { | ||
// If the first comment in trailingComments comes after the | ||
// current node, then we're good - all comments in the array will | ||
// come after the node and so it's safe to add then as official | ||
// trailingComments. | ||
if (this.trailingComments[0].start >= node.end) { | ||
trailingComments = this.trailingComments; | ||
this.trailingComments = []; | ||
} else { | ||
// Otherwise, if the first comment doesn't come after the | ||
// current node, that means we have a mix of leading and trailing | ||
// comments in the array and that leadingComments contains the | ||
// same items as trailingComments. Reset trailingComments to | ||
// zero items and we'll handle this by evaluating leadingComments | ||
// later. | ||
this.trailingComments.length = 0; | ||
} | ||
} else { | ||
var lastInStack = last(stack); | ||
if (stack.length > 0 && lastInStack.trailingComments && lastInStack.trailingComments[0].start >= node.end) { | ||
trailingComments = lastInStack.trailingComments; | ||
lastInStack.trailingComments = null; | ||
} | ||
} | ||
|
||
// Eating the stack. | ||
while (stack.length > 0 && last(stack).start >= node.start) { | ||
lastChild = stack.pop(); | ||
} | ||
|
||
if (lastChild) { | ||
if (lastChild.leadingComments && last(lastChild.leadingComments).end <= node.start) { | ||
node.leadingComments = lastChild.leadingComments; | ||
lastChild.leadingComments = null; | ||
} | ||
} else if (this.leadingComments.length > 0) { | ||
if (last(this.leadingComments).end <= node.start) { | ||
node.leadingComments = this.leadingComments; | ||
this.leadingComments = []; | ||
} else { | ||
// https://github.com/eslint/espree/issues/2 | ||
// | ||
// In special cases, such as return (without a value) and | ||
// debugger, all comments will end up as leadingComments and | ||
// will otherwise be eliminated. This this step runs when the | ||
// bottomRightStack is empty and there are comments left | ||
// in leadingComments. | ||
// | ||
// This loop figures out the stopping point between the actual | ||
// leading and trailing comments by finding the location of the | ||
// first comment that comes after the given node. | ||
for (i = 0; i < this.leadingComments.length; i++) { | ||
if (this.leadingComments[i].end > node.start) { | ||
break; | ||
} | ||
} | ||
|
||
// Split the array based on the location of the first comment | ||
// that comes after the node. Keep in mind that this could | ||
// result in an empty array, and if so, the array must be | ||
// deleted. | ||
node.leadingComments = this.leadingComments.slice(0, i); | ||
if (node.leadingComments.length === 0) { | ||
node.leadingComments = null; | ||
} | ||
|
||
// Similarly, trailing comments are attached later. The variable | ||
// must be reset to null if there are no trailing comments. | ||
trailingComments = this.leadingComments.slice(i); | ||
if (trailingComments.length === 0) { | ||
trailingComments = null; | ||
} | ||
} | ||
} | ||
|
||
if (trailingComments) { | ||
if (trailingComments.length && trailingComments[0].start >= node.start && last(trailingComments).end <= node.end) { | ||
node.innerComments = trailingComments; | ||
} else { | ||
node.trailingComments = trailingComments; | ||
} | ||
} | ||
|
||
stack.push(node); | ||
}; |
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
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
Oops, something went wrong.