-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add no-commented-out-code rule.
- Loading branch information
Showing
2 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
], | ||
}, | ||
], | ||
}); |