2 if (typeof exports == "object" && typeof module == "object") // CommonJS
4 if (typeof define == "function" && define.amd) // AMD
5 return define(["exports"], mod);
6 mod(tern.comment || (tern.comment = {}));
9 return (ch < 14 && ch > 8) || ch === 32 || ch === 160;
12 function onOwnLine(text, pos) {
13 for (; pos > 0; --pos) {
14 var ch = text.charCodeAt(pos - 1);
16 if (!isSpace(ch)) return false;
21 // Gather comments directly before a function
22 exports.commentsBefore = function(text, pos) {
23 var found = null, emptyLines = 0, topIsLineComment;
24 out: while (pos > 0) {
25 var prev = text.charCodeAt(pos - 1);
27 for (var scan = --pos, sawNonWS = false; scan > 0; --scan) {
28 prev = text.charCodeAt(scan - 1);
29 if (prev == 47 && text.charCodeAt(scan - 2) == 47) {
30 if (!onOwnLine(text, scan - 2)) break out;
31 var content = text.slice(scan, pos);
32 if (!emptyLines && topIsLineComment) found[0] = content + "\n" + found[0];
33 else (found || (found = [])).unshift(content);
34 topIsLineComment = true;
38 } else if (prev == 10) {
39 if (!sawNonWS && ++emptyLines > 1) break out;
41 } else if (!sawNonWS && !isSpace(prev)) {
45 } else if (prev == 47 && text.charCodeAt(pos - 2) == 42) {
46 for (var scan = pos - 2; scan > 1; --scan) {
47 if (text.charCodeAt(scan - 1) == 42 && text.charCodeAt(scan - 2) == 47) {
48 if (!onOwnLine(text, scan - 2)) break out;
49 (found || (found = [])).unshift(text.slice(scan, pos - 2));
50 topIsLineComment = false;
56 } else if (isSpace(prev)) {
65 exports.commentAfter = function(text, pos) {
66 while (pos < text.length) {
67 var next = text.charCodeAt(pos);
69 var after = text.charCodeAt(pos + 1), end;
70 if (after == 47) // line comment
71 end = text.indexOf("\n", pos + 2);
72 else if (after == 42) // block comment
73 end = text.indexOf("*/", pos + 2);
76 return text.slice(pos + 2, end < 0 ? text.length : end);
77 } else if (isSpace(next)) {
83 exports.ensureCommentsBefore = function(text, node) {
84 if (node.hasOwnProperty("commentsBefore")) return node.commentsBefore;
85 return node.commentsBefore = exports.commentsBefore(text, node.start);