This repository has been archived by the owner on Aug 18, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #264 from hzoo/eslint2
ESLint 2
- Loading branch information
Showing
11 changed files
with
603 additions
and
85 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,57 @@ | ||
// comment fixes | ||
module.exports = function (ast, comments, tokens) { | ||
if (comments.length) { | ||
var firstComment = comments[0]; | ||
var lastComment = comments[comments.length - 1]; | ||
// fixup program start | ||
if (!tokens.length) { | ||
// if no tokens, the program starts at the end of the last comment | ||
ast.start = lastComment.end; | ||
ast.loc.start.line = lastComment.loc.end.line; | ||
ast.loc.start.column = lastComment.loc.end.column; | ||
|
||
if (ast.leadingComments === null && ast.innerComments.length) { | ||
ast.leadingComments = ast.innerComments; | ||
} | ||
} else if (firstComment.start < tokens[0].start) { | ||
// if there are comments before the first token, the program starts at the first token | ||
var token = tokens[0]; | ||
// ast.start = token.start; | ||
// ast.loc.start.line = token.loc.start.line; | ||
// ast.loc.start.column = token.loc.start.column; | ||
|
||
// estraverse do not put leading comments on first node when the comment | ||
// appear before the first token | ||
if (ast.body.length) { | ||
var node = ast.body[0]; | ||
node.leadingComments = []; | ||
var firstTokenStart = token.start; | ||
var len = comments.length; | ||
for (var i = 0; i < len && comments[i].start < firstTokenStart; i++) { | ||
node.leadingComments.push(comments[i]); | ||
} | ||
} | ||
} | ||
// fixup program end | ||
if (tokens.length) { | ||
var lastToken = tokens[tokens.length - 1]; | ||
if (lastComment.end > lastToken.end) { | ||
// If there is a comment after the last token, the program ends at the | ||
// last token and not the comment | ||
// ast.end = lastToken.end; | ||
ast.range[1] = lastToken.end; | ||
ast.loc.end.line = lastToken.loc.end.line; | ||
ast.loc.end.column = lastToken.loc.end.column; | ||
} | ||
} | ||
} else { | ||
if (!tokens.length) { | ||
ast.loc.start.line = 1; | ||
ast.loc.end.line = 1; | ||
} | ||
} | ||
if (ast.body && ast.body.length > 0) { | ||
ast.loc.start.line = ast.body[0].loc.start.line; | ||
ast.range[0] = ast.body[0].start; | ||
} | ||
}; |
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,93 @@ | ||
module.exports = function (tokens, tt) { | ||
var startingToken = 0; | ||
var currentToken = 0; | ||
var numBraces = 0; // track use of {} | ||
var numBackQuotes = 0; // track number of nested templates | ||
|
||
function isBackQuote(token) { | ||
return tokens[token].type === tt.backQuote; | ||
} | ||
|
||
function isTemplateStarter(token) { | ||
return isBackQuote(token) || | ||
// only can be a template starter when in a template already | ||
tokens[token].type === tt.braceR && numBackQuotes > 0; | ||
} | ||
|
||
function isTemplateEnder(token) { | ||
return isBackQuote(token) || | ||
tokens[token].type === tt.dollarBraceL; | ||
} | ||
|
||
// append the values between start and end | ||
function createTemplateValue(start, end) { | ||
var value = ""; | ||
while (start <= end) { | ||
if (tokens[start].value) { | ||
value += tokens[start].value; | ||
} else if (tokens[start].type !== tt.template) { | ||
value += tokens[start].type.label; | ||
} | ||
start++; | ||
} | ||
return value; | ||
} | ||
|
||
// create Template token | ||
function replaceWithTemplateType(start, end) { | ||
var templateToken = { | ||
type: "Template", | ||
value: createTemplateValue(start, end), | ||
start: tokens[start].start, | ||
end: tokens[end].end, | ||
loc: { | ||
start: tokens[start].loc.start, | ||
end: tokens[end].loc.end | ||
} | ||
}; | ||
|
||
// put new token in place of old tokens | ||
tokens.splice(start, end - start + 1, templateToken); | ||
} | ||
|
||
function trackNumBraces(token) { | ||
if (tokens[token].type === tt.braceL) { | ||
numBraces++; | ||
} else if (tokens[token].type === tt.braceR) { | ||
numBraces--; | ||
} | ||
} | ||
|
||
while (startingToken < tokens.length) { | ||
// template start: check if ` or } | ||
if (isTemplateStarter(startingToken) && numBraces === 0) { | ||
if (isBackQuote(startingToken)) { | ||
numBackQuotes++; | ||
} | ||
|
||
currentToken = startingToken + 1; | ||
|
||
// check if token after template start is "template" | ||
if (currentToken >= tokens.length - 1 || tokens[currentToken].type !== tt.template) { | ||
break; | ||
} | ||
|
||
// template end: find ` or ${ | ||
while (!isTemplateEnder(currentToken)) { | ||
if (currentToken >= tokens.length - 1) { | ||
break; | ||
} | ||
currentToken++; | ||
} | ||
|
||
if (isBackQuote(currentToken)) { | ||
numBackQuotes--; | ||
} | ||
// template start and end found: create new token | ||
replaceWithTemplateType(startingToken, currentToken); | ||
} else if (numBackQuotes > 0) { | ||
trackNumBraces(startingToken); | ||
} | ||
startingToken++; | ||
} | ||
} |
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,20 @@ | ||
exports.attachComments = require("./attachComments"); | ||
|
||
exports.toTokens = require("./toTokens"); | ||
exports.toAST = require("./toAST"); | ||
|
||
exports.convertComments = function (comments) { | ||
for (var i = 0; i < comments.length; i++) { | ||
var comment = comments[i]; | ||
if (comment.type === "CommentBlock") { | ||
comment.type = "Block"; | ||
} else if (comment.type === "CommentLine") { | ||
comment.type = "Line"; | ||
} | ||
// sometimes comments don't get ranges computed, | ||
// even with options.ranges === true | ||
if (!comment.range) { | ||
comment.range = [comment.start, comment.end]; | ||
} | ||
} | ||
} |
Oops, something went wrong.