1"use strict"; 2 3// comment fixes 4module.exports = function(ast, comments, tokens) { 5 if (comments.length) { 6 var firstComment = comments[0]; 7 var lastComment = comments[comments.length - 1]; 8 // fixup program start 9 if (!tokens.length) { 10 // if no tokens, the program starts at the end of the last comment 11 ast.start = lastComment.end; 12 ast.loc.start.line = lastComment.loc.end.line; 13 ast.loc.start.column = lastComment.loc.end.column; 14 15 if (ast.leadingComments === null && ast.innerComments.length) { 16 ast.leadingComments = ast.innerComments; 17 } 18 } else if (firstComment.start < tokens[0].start) { 19 // if there are comments before the first token, the program starts at the first token 20 var token = tokens[0]; 21 // ast.start = token.start; 22 // ast.loc.start.line = token.loc.start.line; 23 // ast.loc.start.column = token.loc.start.column; 24 25 // estraverse do not put leading comments on first node when the comment 26 // appear before the first token 27 if (ast.body.length) { 28 var node = ast.body[0]; 29 node.leadingComments = []; 30 var firstTokenStart = token.start; 31 var len = comments.length; 32 for (var i = 0; i < len && comments[i].start < firstTokenStart; i++) { 33 node.leadingComments.push(comments[i]); 34 } 35 } 36 } 37 // fixup program end 38 if (tokens.length) { 39 var lastToken = tokens[tokens.length - 1]; 40 if (lastComment.end > lastToken.end) { 41 // If there is a comment after the last token, the program ends at the 42 // last token and not the comment 43 // ast.end = lastToken.end; 44 ast.range[1] = lastToken.end; 45 ast.loc.end.line = lastToken.loc.end.line; 46 ast.loc.end.column = lastToken.loc.end.column; 47 } 48 } 49 } else { 50 if (!tokens.length) { 51 ast.loc.start.line = 1; 52 ast.loc.end.line = 1; 53 } 54 } 55 if (ast.body && ast.body.length > 0) { 56 ast.loc.start.line = ast.body[0].loc.start.line; 57 ast.range[0] = ast.body[0].start; 58 } 59}; 60