Skip to content

Commit

Permalink
Fix handling of shebang comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed Nov 24, 2016
1 parent 6897183 commit be5df78
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
2 changes: 0 additions & 2 deletions lib/eslint.js
Expand Up @@ -908,8 +908,6 @@ module.exports = (function() {
}
}

ast.hasShebang = !!shebang;

let eventGenerator = new NodeEventGenerator(api);

eventGenerator = new CodePathAnalyzer(eventGenerator);
Expand Down
25 changes: 16 additions & 9 deletions lib/util/source-code.js
Expand Up @@ -78,14 +78,13 @@ function looksLikeExport(astNode) {
}

/**
* Check to see if the given token is a comment token
* @param {Token} token The token to check
* @param {Token} shebangToken The token that represents a shebang comment at the top of the file
* @returns {boolean} whether the given token is a comment token
* Check to see if the given token is a comment token.
* @param {Token} token The token to check.
* @returns {boolean} whether the given token is a comment token.
* @private
*/
function isComment(token, shebangToken) {
return (token.type === "Line" && (token !== shebangToken)) || token.type === "Block";
function isComment(token) {
return token.type === "Line" || token.type === "Block";
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -127,6 +126,15 @@ function SourceCode(text, ast) {
*/
this.lines = SourceCode.splitLines(this.text);

/*
* Shebangs are parsed as regular "Line" comments.
*/
const shebang = this.text.match(/^#!([^\r\n]+)/);

if (shebang && ast.comments.length && ast.comments[0].value === shebang[1]) {
ast.comments[0].type = "Shebang";
}

this.tokensAndComments = ast.tokens
.concat(ast.comments)
.sort((left, right) => left.range[0] - right.range[0]);
Expand Down Expand Up @@ -212,7 +220,6 @@ SourceCode.prototype = {
leading: [],
trailing: []
};
const shebangToken = this.ast.hasShebang && this.tokensAndComments[0];

/*
* Espree only leaves comments in the Program node comments
Expand All @@ -239,7 +246,7 @@ SourceCode.prototype = {
*/
let currentToken = this.getTokenOrCommentBefore(node);

while (currentToken && isComment(currentToken, shebangToken)) {
while (currentToken && isComment(currentToken)) {
if (node.parent && (currentToken.start < node.parent.start)) {
currentToken = this.getTokenOrCommentBefore(currentToken);
continue;
Expand All @@ -250,7 +257,7 @@ SourceCode.prototype = {

currentToken = this.getTokenOrCommentAfter(node);

while (currentToken && isComment(currentToken, shebangToken)) {
while (currentToken && isComment(currentToken)) {
if (node.parent && (currentToken.end > node.parent.end)) {
currentToken = this.getTokenOrCommentAfter(currentToken);
continue;
Expand Down

0 comments on commit be5df78

Please sign in to comment.