-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
no-commented-out-code.ts
161 lines (150 loc) · 4.67 KB
/
no-commented-out-code.ts
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* @license Use of this source code is governed by an MIT-style license that
* can be found in the LICENSE file at https://github.com/cartant/eslint-plugin-etc
*/
import { TSESTree as es } from "@typescript-eslint/experimental-utils";
import { ruleCreator } from "../utils";
const rule = ruleCreator({
defaultOptions: [],
meta: {
docs: {
category: "Best Practices",
description: "Forbids commented-out code.",
recommended: false,
},
fixable: undefined,
messages: {
forbidden: "Commented-out code is forbidden.",
},
schema: [],
type: "problem",
},
name: "no-commented-out-code",
create: (context) => {
const { parse } = require(context.parserPath);
const { project, ...parserOptions } = context.parserOptions;
const sourceCode = context.getSourceCode();
return {
Program: () => {
const comments = context.getSourceCode().getAllComments();
const blocks = toBlocks(comments);
for (const block of blocks) {
const { content, loc } = block;
// Comments for collapsible regions can be parsed as private
// properties within class declarations, but they're not
// commented-out code.
if (isRegionComment(content)) {
continue;
}
// If the comment can be parsed as a trivial program, it's probably
// not commented-out code.
try {
const program = parse(content, parserOptions);
if (
!hasEmptyBody(program) &&
!hasIdentifierBody(program) &&
!hasLabeledStatementBody(program)
) {
context.report({
loc,
messageId: "forbidden",
});
}
continue;
} catch (error) {}
// Comments within certain nodes - e.g. class declarations - need to
// be wrapped in a similar context to determine whether or not they
// are commented-out code.
const index = sourceCode.getIndexFromLoc(loc.start);
const node = sourceCode.getNodeByRangeIndex(index);
const wrappedContent = wrapContent(content, node);
if (wrappedContent) {
try {
parse(wrappedContent, parserOptions);
context.report({
loc,
messageId: "forbidden",
});
} catch (error) {}
}
}
},
};
},
});
function hasEmptyBody(program: es.Program) {
return program.type === "Program" && program.body.length === 0;
}
function hasIdentifierBody(program: es.Program) {
return (
program.type === "Program" &&
program.body.length === 1 &&
program.body[0].type === "ExpressionStatement" &&
program.body[0].expression.type === "Identifier"
);
}
function hasLabeledStatementBody(program: es.Program) {
return (
program.type === "Program" &&
program.body.length === 1 &&
program.body[0].type === "LabeledStatement"
);
}
function isRegionComment(content: string) {
return /\s*#(end)?region/.test(content);
}
function toBlocks(comments: es.Comment[]) {
const blocks: {
content: string;
loc: es.SourceLocation;
}[] = [];
let prevLine: es.LineComment | undefined;
for (const comment of comments) {
if (comment.type === "Block") {
blocks.push({
content: comment.value.replace(/^\s*\*/, "").replace(/\n\s*\*/g, "\n"),
loc: { ...comment.loc },
});
prevLine = undefined;
} else if (comment.type === "Line") {
if (prevLine && prevLine.loc.start.line === comment.loc.start.line - 1) {
const prevBlock = blocks[blocks.length - 1];
prevBlock.content += `\n${comment.value}`;
prevBlock.loc.end = comment.loc.end;
} else {
blocks.push({
content: comment.value,
loc: { ...comment.loc },
});
}
prevLine = comment;
}
}
return blocks;
}
function wrapContent(
content: string,
node: es.Node | null
): string | undefined {
switch (node?.type) {
case "ArrayExpression":
return `let wrapper = [${content}]`;
case "ClassBody":
return `class Wrapper { ${content} }`;
case "ImportDeclaration":
return `import { ${content} } from "wrapper"`;
case "ObjectExpression":
return `let wrapper = { ${content} }`;
case "FunctionDeclaration":
return `function wrapper(${content}) {}`;
case "SwitchStatement":
return `switch (wrapper) { ${content} }`;
case "TSInterfaceBody":
return `interface Wrapper { ${content} }`;
case "TSTypeLiteral":
return `type Wrapper = { ${content} }`;
default:
return undefined;
}
}
export = rule;