diff --git a/docs/rules/too-many-comments.md b/docs/rules/too-many-comments.md new file mode 100644 index 0000000..e69de29 diff --git a/lib/rules/too-many-comments.js b/lib/rules/too-many-comments.js new file mode 100644 index 0000000..db4f38c --- /dev/null +++ b/lib/rules/too-many-comments.js @@ -0,0 +1,31 @@ +const create = function (context) { + const sourceCode = context.getSourceCode() + //TODO: use getCommentsInside() + const comments = sourceCode.getAllComments() + + return { + 'Program': node => { //'*' + //TODO: option + if (comments.length >= 10) { + context.report({ + node: comments[0], + message: 'too many comments in this file with {{ nbComments }} comments', + // eslint-disable-next-line id-blacklist + data: { + nbComments: comments.length + } + }) + } + }, + } + } + +module.exports = { + create, + meta: { + // type: 'suggestion', + docs: { + description: "" + }, + } +} \ No newline at end of file diff --git a/tests/lib/rules/too-many-comments.js b/tests/lib/rules/too-many-comments.js new file mode 100644 index 0000000..0bc6d98 --- /dev/null +++ b/tests/lib/rules/too-many-comments.js @@ -0,0 +1,32 @@ +var rule = require("../../../lib/rules/too-many-comments") + +const RuleTester = require('eslint').RuleTester +const ruleTester = new RuleTester() +ruleTester.run('too-many-comments', rule, { + valid: [ + {code: 'var simple = /ab/;'}, + {code: '// a simple comment'}, + ], + + invalid: [ + { + code: `// comment one + // comment two + // comment ... + // comment .... + // comment ..... + // comment ...... + // comment ....... + // comment ........ + // comment ......... + // comment .......... + // comment .......... . + // comment .......... .. + // comment .......... ... + `, + errors: [{ + message: 'too many comments in this file with 13 comments', + }], + }, + ] +})