Skip to content

Commit 4b90804

Browse files
committed
update rule no-commented-out-code: remove deprecated eslint functions
1 parent 15bd7f8 commit 4b90804

File tree

2 files changed

+52
-96
lines changed

2 files changed

+52
-96
lines changed

lib/rules/no-commented-out-code.js

+37-81
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,58 @@
1-
/**
2-
* from https://github.com/bahmutov/eslint-rules
3-
*/
41
const { parse } = require('espree');
5-
const quote = require('quote');
6-
7-
//function isJshint(text) {
8-
// return /^jshint\ /.test(text);
9-
//}
102

113
const isSingleWord = function (text) {
124
return /^[\w-]*$/.test(text);
135
}
146

15-
const isValidCode = function (text) {
16-
if (isSingleWord(text)) { // || isJshint(text)
17-
return false;
18-
}
19-
// string used to disable Flow typing for the next line
20-
if (text.trim() === "$FlowFixMe") { //TODO: add this string as a new option
7+
const isValidCode = function (text, MAX_LENGTH = 10) {
8+
if (text.length <= MAX_LENGTH) {
219
return false
2210
}
23-
// $FlowFixMe: suppressing this error until we can refactor
24-
if (/^\$FlowFixMe/.test(text)) {
25-
return false
11+
12+
if (text === " $FlowFixMe" || isSingleWord(text) ) {
13+
return false;
2614
}
27-
if (/^\$FlowTODO/.test(text)) {
15+
if (/^\$FlowFixMe/.test(text) || /^\$FlowTODO/.test(text)) {
2816
return false
2917
}
30-
//todo: read .flowconfig file
31-
// see https://flow.org/en/docs/config/options/#suppress_comment-regex-
32-
33-
//TODO: put max length value as option, in rule "no-commented-out-code"
34-
const MAX_LENGTH = 10
35-
//if (text.length <= MAX_LENGTH) {
36-
// return false
37-
//}
38-
39-
//TODO: put this as an rule option
40-
const options = {
41-
ecmaVersion: 6,
42-
}
4318

4419
try {
45-
const ast = parse(text, options);
46-
// check EmptyStatement -> if EmptyStatement exist return false
20+
const ast = parse(text, {
21+
ecmaVersion: 6,
22+
});
4723
return Boolean(ast);
48-
} catch (err) {
49-
//console.error(err)
24+
} catch (error) {
5025
return false;
5126
}
5227
}
5328

54-
const firstLine = function (str) {
55-
return str.split('\n')[0];
56-
}
57-
58-
const cutStr = function (str) {
59-
const MAX_LENGTH = 20;
60-
61-
var line = firstLine(str);
62-
if (line.length > MAX_LENGTH) {
63-
line = line.substr(0, MAX_LENGTH) + ' ...';
29+
const create = function (context) {
30+
const sourceCode = context.getSourceCode()
31+
const comments = sourceCode.getAllComments()
32+
33+
return {
34+
'Program': node => {
35+
comments.forEach(function (comment) {
36+
if (isValidCode(comment.value)) {
37+
context.report({
38+
node: comments[0],
39+
message: 'commented out code ',
40+
// eslint-disable-next-line id-blacklist
41+
data: {
42+
nbComments: comments.length
43+
}
44+
})
45+
}
46+
})
47+
},
6448
}
65-
return line;
66-
}
67-
68-
/**
69-
* @return array
70-
*/
71-
const getCommentsWithCode = function(context) {
72-
// deprecated: Use sourceCode.getAllComments() instead.
73-
var comments = context.getAllComments();
74-
75-
//TODO: array -> use map ?
76-
//TODO: can move to a function
77-
// eslint-disable-next-line no-useless-assign/no-useless-assign
78-
comments = comments.filter(function (comment) {
79-
return isValidCode(comment.value.trim());
80-
})
81-
return comments
8249
}
8350

84-
module.exports = function (context) {
85-
const comments = getCommentsWithCode(context)
86-
87-
comments.forEach(function (commentedCode) {
88-
const code = cutStr(commentedCode.value.trim()); //TODO: duplicate code : trim
89-
const lines = commentedCode.loc.end.line - commentedCode.loc.start.line + 1;
90-
const linesMsg = '(' + lines + ' line' + (lines === 1 ? '' : 's') + ')';
91-
92-
//TODO: support --fix in no-commented-out-code rule
93-
context.report({
94-
loc: commentedCode.loc
95-
}, 'commented out code ' //+ quote(code) + ' ' + linesMsg //TODO: update BIS
96-
);
97-
});
98-
99-
return {}
100-
};
101-
102-
// TODO: add recommanded: false
51+
module.exports = {
52+
create,
53+
meta: {
54+
docs: {
55+
description: "Detect commented code"
56+
},
57+
}
58+
}

tests/lib/rules/no-commented-out-code.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ruleTester.run("no-commented-out-code", rule, {
88

99
valid: [
1010
{
11-
code: "// this is a long comment about nothing",
11+
code: "var a = 0;",
1212
},
1313
{
1414
code: "//this is a long comment about nothing",
@@ -28,9 +28,6 @@ ruleTester.run("no-commented-out-code", rule, {
2828
{
2929
code: "// please don't call method getElement()",
3030
},
31-
{
32-
code: "/*\n\n\n\n\n\n\n\n\n\n\n\n*/",
33-
},
3431
{
3532
code: "//TODO",
3633
},
@@ -67,6 +64,9 @@ ruleTester.run("no-commented-out-code", rule, {
6764
{
6865
code: "/*flow-include\ntype Foo = {\nfoo: number\n};\n*/",
6966
},
67+
{
68+
code: "//; test",
69+
},
7070
],
7171

7272
invalid: [
@@ -82,12 +82,6 @@ ruleTester.run("no-commented-out-code", rule, {
8282
message: 'commented out code '
8383
}]
8484
},
85-
{
86-
code: "//; test",
87-
errors: [{
88-
message: 'commented out code '
89-
}]
90-
},
9185
{
9286
code: "// var a = 0 / 1",
9387
errors: [{
@@ -104,26 +98,32 @@ ruleTester.run("no-commented-out-code", rule, {
10498
{
10599
code: "/* var a = 1; \n const b = 2; \n var c = 3; */",
106100
errors: [{
107-
message: 'commented out code '
101+
message: 'commented out code ',
102+
type: "Block"
108103
}]
109104
},
110105
{
111106
code: "//'use strict'",
112107
errors: [{
113-
message: 'commented out code ' //`commented out code "'use strict'" (1 line)`
108+
message: 'commented out code '
114109
}]
115110
},
116111
{
117-
code: "// var a = 1",
112+
code: "// var aaaaaaaaaaaaaaaa = 1",
118113
errors: [{
119-
message: 'commented out code ' //`commented out code "var a = 1" (1 line)`
114+
message: 'commented out code '
120115
}]
121116
},
122117
{
123118
code: "// const a = 1",
124119
errors: [{
125-
message: 'commented out code ' //`commented out code "const a = 1" (1 line)`
120+
message: 'commented out code '
126121
}]
122+
}, {
123+
code: "/*\n\n\n\n\n\n\n\n\n\n\n\n var a = 0 */",
124+
errors: [{
125+
message: 'commented out code '
126+
}]
127127
},
128128
]
129129
});

0 commit comments

Comments
 (0)