Skip to content

Commit

Permalink
feat: Add no-commented-out-code rule.
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Feb 10, 2021
1 parent 2bce3ce commit 4a7cbc5
Show file tree
Hide file tree
Showing 2 changed files with 236 additions and 0 deletions.
76 changes: 76 additions & 0 deletions source/rules/no-commented-out-code.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* @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;
return {
Program: () => {
const comments = context.getSourceCode().getAllComments();
const blocks = toBlocks(comments);
for (const block of blocks) {
try {
parse(block.content, parserOptions);
context.report({
loc: block.loc,
messageId: "forbidden",
});
} catch (error) {}
}
},
};
},
});

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;
}

export = rule;
160 changes: 160 additions & 0 deletions tests/rules/no-commented-out-code.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/**
* @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 { stripIndent } from "common-tags";
import { fromFixture } from "eslint-etc";
import rule = require("../../source/rules/no-commented-out-code");
import { ruleTester } from "../utils";

ruleTester({ types: false }).run("no-commented-out-code", rule, {
valid: [
{
code: stripIndent`
// This comment isn't code.
const answer = 42;
`,
},
{
code: stripIndent`
// This comment includes some code:
// const answer = 54;
const answer = 42;
`,
},
{
code: stripIndent`
/* This comment isn't code. */
const answer = 42;
`,
},
{
code: stripIndent`
/*
This comment includes some code:
const answer = 54;
*/
const answer = 42;
`,
},
{
code: stripIndent`
/*
* This comment isn't code.
*/
const answer = 42;
`,
},
{
code: stripIndent`
/*
* This comment includes some code:
* const answer = 54;
*/
const answer = 42;
`,
},
],
invalid: [
fromFixture(
stripIndent`
// const answer = 54;
~~~~~~~~~~~~~~~~~~~~~ [forbidden]
const answer = 42;
`
),
fromFixture(
stripIndent`
/* const answer = 54; */
~~~~~~~~~~~~~~~~~~~~~~~~ [forbidden]
const answer = 42;
`
),
{
code: stripIndent`
/*
* const answer = 54;
*/
const answer = 42;
`,
errors: [
{
column: 1,
endColumn: 4,
line: 1,
endLine: 3,
messageId: "forbidden",
},
],
},
{
code: stripIndent`
// // Wrong answer
// const answer = 54;
const answer = 42;
`,
errors: [
{
column: 1,
endColumn: 22,
line: 1,
endLine: 2,
messageId: "forbidden",
},
],
},
{
code: stripIndent`
// /* Wrong answer */
// const answer = 54;
const answer = 42;
`,
errors: [
{
column: 1,
endColumn: 22,
line: 1,
endLine: 2,
messageId: "forbidden",
},
],
},
{
code: stripIndent`
/*
// Wrong answer
const answer = 54;
*/
const answer = 42;
`,
errors: [
{
column: 1,
endColumn: 3,
line: 1,
endLine: 4,
messageId: "forbidden",
},
],
},
{
code: stripIndent`
/*
* // Wrong answer
* const answer = 54;
*/
const answer = 42;
`,
errors: [
{
column: 1,
endColumn: 4,
line: 1,
endLine: 4,
messageId: "forbidden",
},
],
},
],
});

0 comments on commit 4a7cbc5

Please sign in to comment.