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 7a64fdb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
2 changes: 0 additions & 2 deletions lib/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -908,8 +908,6 @@ module.exports = (function() {
}
}

ast.hasShebang = !!shebang;

let eventGenerator = new NodeEventGenerator(api);

eventGenerator = new CodePathAnalyzer(eventGenerator);
Expand Down
27 changes: 18 additions & 9 deletions lib/util/source-code.js
Original file line number Diff line number Diff line change
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.isShebang) || token.type === "Block";
}

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

/*
* Shebangs are parsed as regular line comments. Adding the isShebang property
* allows for rules to quickly ascertain whether a comment is a shebang or not.
*/
const shebang = this.text.match(/^#!([^\r\n]+)/);
const shebangValue = shebang && shebang[1];

if (shebang && ast.comments.length && ast.comments[0].value === shebangValue) {
ast.comments[0].isShebang = true;
}

this.tokensAndComments = ast.tokens
.concat(ast.comments)
.sort((left, right) => left.range[0] - right.range[0]);
Expand Down Expand Up @@ -212,7 +222,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 +248,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 +259,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 7a64fdb

Please sign in to comment.