-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCounter.js
141 lines (126 loc) · 6.45 KB
/
Counter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright (c) 2013 Peter Flynn, Adobe Systems Incorporated, and other contributors.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4 */
/*global define, brackets, $ */
define(function (require, exports, module) {
"use strict";
/** Quick way to bail early from countSloc() */
function Unsupported(msg, lineNum) {
var ret = new Error(msg);
Error.captureStackTrace(this, Unsupported);
ret.message = msg;
ret.lineNum = lineNum;
return ret;
}
Unsupported.prototype = Error.prototype;
/**
* Counts lines of code in given text.
* Throws 'Unsupported' exception in cases where it's not possible to give an accurate count.
* @return {{total: number, sloc: number}}
*/
function countSloc(text) {
// TODO: split on /\r\n/g so we can call getText(true) for speed?
var lines = text.split("\n");
function unsupported(msg, lineNum) {
throw new Unsupported(msg, lineNum === undefined ? lines.length - 1 : lineNum);
}
var codeLines = 0;
var inBlockComment = false;
var inString = false;
var stringDelim;
lines.forEach(function (line, lineNum) {
var i;
var sawCode = false;
for (i = 0; i < line.length; i++) {
var c = line[i];
if (inBlockComment) {
if (c === "/" && line[i - 1] === "*") {
inBlockComment = false;
}
} else if (inString) {
sawCode = true;
if (c === stringDelim) {
inString = false;
} else if (c === "\\") {
i++; // skip next char (escaped char)
}
} else {
// ignore all whitespace
if (c !== " " && c !== "\t") {
// opening of string
if (c === "\"" || c === "'") {
sawCode = true;
inString = true;
stringDelim = c;
} else if (c === "/") {
// opening of comment - MAYBE
if (line[i + 1] === "*") {
inBlockComment = true;
i++; // (no point in looking at the "*" again)
} else if (line[i + 1] === "/") {
break; // rest of line is a line comment
} else {
sawCode = true;
// A "/" also might be the start of a regexp literal. Detecting regexps is INSANELY difficult in JS
// and basically requires fully parsing the code. We care because, like a string literal, the regexp
// could contain strings like /* or " that we'd misinterpret. (Detecting where a regexp ENDS is no
// mean feat either, btw).
// So, we cheat: we only care about the rest of the line if it might contain something that affects
// how we count LATER lines. All other cases are unambiguous without us knowing whether a regexp is
// present or not.
if (line.indexOf("/*", i + 1) !== -1) {
unsupported("Potential block comment start following potential regular expression on same line", lineNum);
} else if (line.indexOf("\"", i + 1) !== -1 || line.indexOf("'", i + 1) !== -1) {
var trimmed = line.trim();
if (trimmed[trimmed.length - 1] === "\\") {
unsupported("Potential multi-line string literal following potential regular expression on same line", lineNum);
}
}
break; // ignore rest of line since we're not sure if it's a regexp and if so, where it ends
}
} else {
sawCode = true;
// mainly as a self-check, error out if we see a block-comment close when we think we're not in a block comment
if (c === "*" && line[i + 1] === "/") {
unsupported("Unexpected */ when not in a block comment", lineNum);
}
}
}
}
}
if (sawCode) {
codeLines++;
}
if (inString && line[line.length - 1] !== "\\") {
unsupported("Unclosed string at end of line", lineNum);
}
});
if (inBlockComment) {
unsupported("Unclosed block comment at end of file");
} else if (inString) {
unsupported("Unclosed string at end of file");
}
return { total: lines.length, sloc: codeLines };
}
exports.countSloc = countSloc;
exports.Unsupported = Unsupported;
});