1 | var espree = require('espree');
|
2 | var quote = require('quote');
|
3 |
|
4 | function isJshint(text) {
|
5 | 'use strict';
|
6 | return /^jshint\ /.test(text);
|
7 | }
|
8 | console.assert(isJshint('jshint -W098'));
|
9 | console.assert(!isJshint('not jshint'));
|
10 |
|
11 | function isSingleWord(text) {
|
12 | 'use strict';
|
13 | return /^[\w-]*$/.test(text);
|
14 | }
|
15 | console.assert(isSingleWord('fooBar'));
|
16 | console.assert(isSingleWord(''));
|
17 | console.assert(isSingleWord('click'));
|
18 | console.assert(isSingleWord('browser-specific'));
|
19 | console.assert(!isSingleWord('var bar'));
|
20 |
|
21 | function isValidCode(text) {
|
22 | 'use strict';
|
23 | if (isSingleWord(text) || isJshint(text)) {
|
24 | return false;
|
25 | }
|
26 |
|
27 | try {
|
28 | var ast = espree.parse(text);
|
29 | return !!ast;
|
30 | } catch (err) {
|
31 | return false;
|
32 | }
|
33 | }
|
34 |
|
35 | function firstLine(str) {
|
36 | 'use strict';
|
37 | return str.split('\n')[0];
|
38 | }
|
39 |
|
40 | function cut(str) {
|
41 | 'use strict';
|
42 | var line = firstLine(str);
|
43 | var MAX_LENGTH = 20;
|
44 | if (line.length > MAX_LENGTH) {
|
45 | line = line.substr(0, MAX_LENGTH) + ' ...';
|
46 | }
|
47 | return line;
|
48 | }
|
49 |
|
50 | module.exports = function (context) {
|
51 | 'use strict';
|
52 |
|
53 | var comments = context.getAllComments();
|
54 |
|
55 | comments.filter(function (comment) {
|
56 | return isValidCode(comment.value.trim());
|
57 | }).forEach(function (commentedCode) {
|
58 | var code = cut(commentedCode.value.trim());
|
59 | var lines = commentedCode.loc.end.line - commentedCode.loc.start.line + 1;
|
60 | var linesMsg = '(' + lines + ' line' + (lines === 1 ? '' : 's') + ')';
|
61 | context.report({
|
62 | loc: commentedCode.loc
|
63 | }, 'commented out code ' + quote(code) + ' ' + linesMsg);
|
64 | });
|
65 |
|
66 | return {};
|
67 | };
|