From 20ceb377c1b872bf32b927e817c7de6b57ec27a0 Mon Sep 17 00:00:00 2001 From: Lachlan Hunt Date: Sun, 7 Aug 2022 11:25:27 +1000 Subject: [PATCH 01/10] Implement support for skipping decoration characters in no-warning-comments --- lib/rules/no-warning-comments.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/rules/no-warning-comments.js b/lib/rules/no-warning-comments.js index 4f867cbb707..d38049d56a1 100644 --- a/lib/rules/no-warning-comments.js +++ b/lib/rules/no-warning-comments.js @@ -37,6 +37,9 @@ module.exports = { }, location: { enum: ["start", "anywhere"] + }, + decoration: { + type: "string" } }, additionalProperties: false @@ -53,6 +56,7 @@ module.exports = { configuration = context.options[0] || {}, warningTerms = configuration.terms || ["todo", "fixme", "xxx"], location = configuration.location || "start", + decoration = configuration.decoration || "", selfConfigRegEx = /\bno-warning-comments\b/u; /** @@ -64,6 +68,7 @@ module.exports = { */ function convertToRegExp(term) { const escaped = escapeRegExp(term); + const escapedDecoration = escapeRegExp(decoration); /* * When matching at the start, ignore leading whitespace, and @@ -74,18 +79,23 @@ module.exports = { * e.g. terms ["TODO"] matches `//TODO something` * $ handles any terms at the end of a comment * e.g. terms ["TODO"] matches `// something TODO` - * \s* handles optional leading spaces (for "start" location only) - * e.g. terms ["TODO"] matches `// TODO something` * \b handles terms preceded/followed by word boundary * e.g. terms: ["!FIX", "FIX!"] matches `// FIX!something` or `// something!FIX` * terms: ["FIX"] matches `// FIX!` or `// !FIX`, but not `// fixed or affix` + * + * For location start: + * [\s]* handles optional leading spaces + * e.g. terms ["TODO"] matches `// TODO something` + * [\s\*] (where "\*" is the escaped string of decoration) handles + * handles optional leading spaces or decoration characters (for "start" location only) + * e.g. terms ["TODO"] matches `/**** TODO something ... ` */ const wordBoundary = "\\b"; let prefix = ""; if (location === "start") { - prefix = "^\\s*"; + prefix = `^[\\s${escapedDecoration}]*`; } else if (/^\w/u.test(term)) { prefix = wordBoundary; } @@ -95,12 +105,15 @@ module.exports = { /* * For location "start", the typical regex is: - * /^\s*ESCAPED_TERM\b/iu. + * /^[\s]*ESCAPED_TERM\b/iu. + * Or if decoration characters are specified (e.g. "*"), then any of + * those characters may appear in any order at the start: + * /^[\s\*]*ESCAPED_TERM\b/iu. * * For location "anywhere" the typical regex is * /\bESCAPED_TERM\b/iu * - * If it starts or ends with non-word character, the prefix and suffix empty, respectively. + * If it starts or ends with non-word character, the prefix and suffix are empty, respectively. */ return new RegExp(`${prefix}${escaped}${suffix}`, flags); } From 9e0a4985e480d00ccae080609ac2dd33d9c11bb9 Mon Sep 17 00:00:00 2001 From: Lachlan Hunt Date: Sun, 7 Aug 2022 11:25:50 +1000 Subject: [PATCH 02/10] Update unit tests for no-warning-comments --- tests/lib/rules/no-warning-comments.js | 68 +++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/tests/lib/rules/no-warning-comments.js b/tests/lib/rules/no-warning-comments.js index 6d4137a5e84..829963df65b 100644 --- a/tests/lib/rules/no-warning-comments.js +++ b/tests/lib/rules/no-warning-comments.js @@ -37,7 +37,8 @@ ruleTester.run("no-warning-comments", rule, { { code: "// special regex characters don't cause a problem", options: [{ terms: ["[aeiou]"], location: "anywhere" }] }, "/*eslint no-warning-comments: [2, { \"terms\": [\"todo\", \"fixme\", \"any other term\"], \"location\": \"anywhere\" }]*/\n\nvar x = 10;\n", { code: "/*eslint no-warning-comments: [2, { \"terms\": [\"todo\", \"fixme\", \"any other term\"], \"location\": \"anywhere\" }]*/\n\nvar x = 10;\n", options: [{ location: "anywhere" }] }, - { code: "// foo", options: [{ terms: ["foo-bar"] }] } + { code: "// foo", options: [{ terms: ["foo-bar"] }] }, + "/** multi-line block comment with lines starting with\nTODO\nFIXME or\nXXX\n*/" ], invalid: [ { @@ -387,6 +388,71 @@ ruleTester.run("no-warning-comments", rule, { } } ] + }, + { + code: "/*\nTODO undecorated multi-line block comment (start)\n*/", + options: [{ terms: ["todo"], location: "start" }], + errors: [ + { + messageId: "unexpectedComment", + data: { + matchedTerm: "todo", + comment: "TODO undecorated multi-line block..." + } + } + ] + }, + { + code: "/** TODO decorated single line block comment (start) */", + options: [{ terms: ["todo"], location: "start", decoration: "*" }], + errors: [ + { + messageId: "unexpectedComment", + data: { + matchedTerm: "todo", + comment: "* TODO decorated single line block..." + } + } + ] + }, + { + code: "/**\n * TODO decorated multi-line block comment (start) \n */", + options: [{ terms: ["todo"], location: "start", decoration: "*" }], + errors: [ + { + messageId: "unexpectedComment", + data: { + matchedTerm: "todo", + comment: "* * TODO decorated multi-line block..." + } + } + ] + }, + { + code: "///// TODO decorated single-line comment (start) \n /////", + options: [{ terms: ["todo"], location: "start", decoration: "*/" }], + errors: [ + { + messageId: "unexpectedComment", + data: { + matchedTerm: "todo", + comment: "/// TODO decorated single-line comment..." + } + } + ] + }, + { + code: "///*/*/ TODO decorated single-line comment with multiple decoration characters (start) \n /////", + options: [{ terms: ["todo"], location: "start", decoration: "*/" }], + errors: [ + { + messageId: "unexpectedComment", + data: { + matchedTerm: "todo", + comment: "/*/*/ TODO decorated single-line comment..." + } + } + ] } ] }); From c888de3295df6f5c6fa8ed9087d505edda1ea6ad Mon Sep 17 00:00:00 2001 From: Lachlan Hunt Date: Sun, 7 Aug 2022 11:26:08 +1000 Subject: [PATCH 03/10] Update docs for no-warning-comments --- docs/src/rules/no-warning-comments.md | 47 +++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/docs/src/rules/no-warning-comments.md b/docs/src/rules/no-warning-comments.md index 6ddef3c5de4..9e75bd2ec7b 100644 --- a/docs/src/rules/no-warning-comments.md +++ b/docs/src/rules/no-warning-comments.md @@ -21,8 +21,9 @@ This rule reports comments that include any of the predefined terms specified in This rule has an options object literal: -* `"terms"`: optional array of terms to match. Defaults to `["todo", "fixme", "xxx"]`. Terms are matched case-insensitive and as whole words: `fix` would match `FIX` but not `fixing`. Terms can consist of multiple words: `really bad idea`. -* `"location"`: optional string that configures where in your comments to check for matches. Defaults to `"start"`. The other value is match `anywhere` in comments. +* `"terms"`: optional array of terms to match. Defaults to `["todo", "fixme", "xxx"]`. Terms are matched case-insensitively and as whole words: `fix` would match `FIX` but not `fixing`. Terms can consist of multiple words: `really bad idea`. +* `"location"`: optional string that configures where in your comments to check for matches. Defaults to `"start"`. For multi-line comments, the start is from the first non-decorative character, ignoring whitespace, new lines or characters specified in `decoration`. The other value is match `anywhere` in comments. +* `"decoration"`: optional string that specifies decorative characters that are ignored at the start of a comment, when location is `"start"`. Defaults to `""`. Any sequence of whitespace or the characters from this string are ignored. This option is ignored when location is `"anywhere"`. Example of **incorrect** code for the default `{ "terms": ["todo", "fixme", "xxx"], "location": "start" }` options: @@ -31,6 +32,9 @@ Example of **incorrect** code for the default `{ "terms": ["todo", "fixme", "xxx ```js /*eslint no-warning-comments: "error"*/ +/* +FIXME +*/ function callback(err, results) { if (err) { console.error(err); @@ -73,7 +77,7 @@ Examples of **incorrect** code for the `{ "terms": ["todo", "fixme", "any other // TODO: this // todo: this too // Even this: TODO -/* /* +/* * The same goes for this TODO comment * Or a fixme * as well as any other term @@ -101,6 +105,43 @@ Examples of **correct** code for the `{ "terms": ["todo", "fixme", "any other te ::: +### Decoration Characters + +Examples of **incorrect** code for the `{ "terms": ["todo"], "location": "start", "decoration": "/*" }` options: + +::: incorrect + +```js +/*eslint no-warning-comments: ["error", { "terms": ["todo"], "location": "start", "decoration": "/*" }]*/ + +////// TODO decorative slashes and whitespace are ignored ////// +//***** todo decorative asterisks are also ignored *****// +/** + * TODO new lines are also ignored in block comments. + */ +``` + +::: + +Examples of **correct** code for the `{ "terms": ["todo"], "location": "start", "decoration": "/*" }` options: + +::: correct + +```js +/*eslint no-warning-comments: ["error", { "terms": ["todo"], "location": "start", "decoration": "/*" }]*/ + +// This is to do +// even not any other term +// any other terminal +/** + * The same goes for block comments + * with any other interesting term + * or fix me this + */ +``` + +::: + ## When Not To Use It * If you have a large code base that was not developed with a policy to not use such warning terms, you might get hundreds of warnings / errors which might be counter-productive if you can't fix all of them (e.g. if you don't get the time to do it) as you might overlook other warnings / errors or get used to many of them and don't pay attention on it anymore. From 8a800834a3e39eba0f8737f81b0f6c74111d2d1e Mon Sep 17 00:00:00 2001 From: Lachlan Hunt Date: Sun, 7 Aug 2022 12:47:18 +1000 Subject: [PATCH 04/10] Support array of decoration strings in no-warning-comments --- docs/src/rules/no-warning-comments.md | 12 +++++------- lib/rules/no-warning-comments.js | 7 +++++-- tests/lib/rules/no-warning-comments.js | 15 ++++++++++++++- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/docs/src/rules/no-warning-comments.md b/docs/src/rules/no-warning-comments.md index 9e75bd2ec7b..e034ed9d3b9 100644 --- a/docs/src/rules/no-warning-comments.md +++ b/docs/src/rules/no-warning-comments.md @@ -23,7 +23,7 @@ This rule has an options object literal: * `"terms"`: optional array of terms to match. Defaults to `["todo", "fixme", "xxx"]`. Terms are matched case-insensitively and as whole words: `fix` would match `FIX` but not `fixing`. Terms can consist of multiple words: `really bad idea`. * `"location"`: optional string that configures where in your comments to check for matches. Defaults to `"start"`. For multi-line comments, the start is from the first non-decorative character, ignoring whitespace, new lines or characters specified in `decoration`. The other value is match `anywhere` in comments. -* `"decoration"`: optional string that specifies decorative characters that are ignored at the start of a comment, when location is `"start"`. Defaults to `""`. Any sequence of whitespace or the characters from this string are ignored. This option is ignored when location is `"anywhere"`. +* `"decoration"`: optional string or array of strings that specifies decorative characters that are ignored at the start of a comment, when location is `"start"`. Defaults to `""`. Any sequence of whitespace or the characters from this string are ignored. Note that the string `"/-*"` is equivalent to the array of strings `["/", "-", "*"]`. This option is ignored when location is `"anywhere"`. Example of **incorrect** code for the default `{ "terms": ["todo", "fixme", "xxx"], "location": "start" }` options: @@ -107,12 +107,12 @@ Examples of **correct** code for the `{ "terms": ["todo", "fixme", "any other te ### Decoration Characters -Examples of **incorrect** code for the `{ "terms": ["todo"], "location": "start", "decoration": "/*" }` options: +Examples of **incorrect** code for the `{ "decoration": ["/", "*"] }` options: ::: incorrect ```js -/*eslint no-warning-comments: ["error", { "terms": ["todo"], "location": "start", "decoration": "/*" }]*/ +/*eslint no-warning-comments: ["error", { "decoration": ["/", "*"] }]*/ ////// TODO decorative slashes and whitespace are ignored ////// //***** todo decorative asterisks are also ignored *****// @@ -123,16 +123,14 @@ Examples of **incorrect** code for the `{ "terms": ["todo"], "location": "start" ::: -Examples of **correct** code for the `{ "terms": ["todo"], "location": "start", "decoration": "/*" }` options: +Examples of **correct** code for the `{ "decoration": ["/", "*"] }` options: ::: correct ```js -/*eslint no-warning-comments: ["error", { "terms": ["todo"], "location": "start", "decoration": "/*" }]*/ +/*eslint no-warning-comments: ["error", { "decoration": ["/", "*"] }]*/ // This is to do -// even not any other term -// any other terminal /** * The same goes for block comments * with any other interesting term diff --git a/lib/rules/no-warning-comments.js b/lib/rules/no-warning-comments.js index d38049d56a1..95a8d8bebc1 100644 --- a/lib/rules/no-warning-comments.js +++ b/lib/rules/no-warning-comments.js @@ -39,7 +39,10 @@ module.exports = { enum: ["start", "anywhere"] }, decoration: { - type: "string" + type: ["string", "array"], + items: { + type: "string" + } } }, additionalProperties: false @@ -56,7 +59,7 @@ module.exports = { configuration = context.options[0] || {}, warningTerms = configuration.terms || ["todo", "fixme", "xxx"], location = configuration.location || "start", - decoration = configuration.decoration || "", + decoration = [...configuration.decoration || ""].join(""), selfConfigRegEx = /\bno-warning-comments\b/u; /** diff --git a/tests/lib/rules/no-warning-comments.js b/tests/lib/rules/no-warning-comments.js index 829963df65b..29870a2988b 100644 --- a/tests/lib/rules/no-warning-comments.js +++ b/tests/lib/rules/no-warning-comments.js @@ -429,7 +429,7 @@ ruleTester.run("no-warning-comments", rule, { ] }, { - code: "///// TODO decorated single-line comment (start) \n /////", + code: "///// TODO decorated single-line comment with decoration string \n /////", options: [{ terms: ["todo"], location: "start", decoration: "*/" }], errors: [ { @@ -441,6 +441,19 @@ ruleTester.run("no-warning-comments", rule, { } ] }, + { + code: "///// TODO decorated single-line comment with decoration array \n /////", + options: [{ terms: ["todo"], location: "start", decoration: ["*", "/"] }], + errors: [ + { + messageId: "unexpectedComment", + data: { + matchedTerm: "todo", + comment: "/// TODO decorated single-line comment..." + } + } + ] + }, { code: "///*/*/ TODO decorated single-line comment with multiple decoration characters (start) \n /////", options: [{ terms: ["todo"], location: "start", decoration: "*/" }], From 13f53e7b0f47c01a4e1adaca94b2961f3ac8c23d Mon Sep 17 00:00:00 2001 From: Lachlan Hunt Date: Mon, 8 Aug 2022 09:16:32 +1000 Subject: [PATCH 05/10] Enforce single character decoration strings in no-warning-comments --- docs/src/rules/no-warning-comments.md | 17 ++++++++++++++++- lib/rules/no-warning-comments.js | 6 ++++-- tests/lib/rules/no-warning-comments.js | 15 +-------------- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/docs/src/rules/no-warning-comments.md b/docs/src/rules/no-warning-comments.md index e034ed9d3b9..def95423a81 100644 --- a/docs/src/rules/no-warning-comments.md +++ b/docs/src/rules/no-warning-comments.md @@ -23,7 +23,7 @@ This rule has an options object literal: * `"terms"`: optional array of terms to match. Defaults to `["todo", "fixme", "xxx"]`. Terms are matched case-insensitively and as whole words: `fix` would match `FIX` but not `fixing`. Terms can consist of multiple words: `really bad idea`. * `"location"`: optional string that configures where in your comments to check for matches. Defaults to `"start"`. For multi-line comments, the start is from the first non-decorative character, ignoring whitespace, new lines or characters specified in `decoration`. The other value is match `anywhere` in comments. -* `"decoration"`: optional string or array of strings that specifies decorative characters that are ignored at the start of a comment, when location is `"start"`. Defaults to `""`. Any sequence of whitespace or the characters from this string are ignored. Note that the string `"/-*"` is equivalent to the array of strings `["/", "-", "*"]`. This option is ignored when location is `"anywhere"`. +* `"decoration"`: optional character, or array of characters that specify decorative characters that are ignored at the start of a comment, when location is `"start"`. Defaults to `""`. Any sequence of whitespace or the characters from this property are ignored. This option is ignored when location is `"anywhere"`. Example of **incorrect** code for the default `{ "terms": ["todo", "fixme", "xxx"], "location": "start" }` options: @@ -107,6 +107,21 @@ Examples of **correct** code for the `{ "terms": ["todo", "fixme", "any other te ### Decoration Characters +Examples of **incorrect** code for the `{ "decoration": "*" }` options: + +::: incorrect + +```js +/*eslint no-warning-comments: ["error", { "decoration": "*" }]*/ + +//***** todo decorative asterisks are ignored *****// +/** + * TODO new lines and asterisks are also ignored in block comments. + */ +``` + +::: + Examples of **incorrect** code for the `{ "decoration": ["/", "*"] }` options: ::: incorrect diff --git a/lib/rules/no-warning-comments.js b/lib/rules/no-warning-comments.js index 95a8d8bebc1..6ef9ccba1af 100644 --- a/lib/rules/no-warning-comments.js +++ b/lib/rules/no-warning-comments.js @@ -41,8 +41,10 @@ module.exports = { decoration: { type: ["string", "array"], items: { - type: "string" - } + type: "string", + pattern: "^\\S$" + }, + pattern: "^\\S$" } }, additionalProperties: false diff --git a/tests/lib/rules/no-warning-comments.js b/tests/lib/rules/no-warning-comments.js index 29870a2988b..74779a5b876 100644 --- a/tests/lib/rules/no-warning-comments.js +++ b/tests/lib/rules/no-warning-comments.js @@ -428,19 +428,6 @@ ruleTester.run("no-warning-comments", rule, { } ] }, - { - code: "///// TODO decorated single-line comment with decoration string \n /////", - options: [{ terms: ["todo"], location: "start", decoration: "*/" }], - errors: [ - { - messageId: "unexpectedComment", - data: { - matchedTerm: "todo", - comment: "/// TODO decorated single-line comment..." - } - } - ] - }, { code: "///// TODO decorated single-line comment with decoration array \n /////", options: [{ terms: ["todo"], location: "start", decoration: ["*", "/"] }], @@ -456,7 +443,7 @@ ruleTester.run("no-warning-comments", rule, { }, { code: "///*/*/ TODO decorated single-line comment with multiple decoration characters (start) \n /////", - options: [{ terms: ["todo"], location: "start", decoration: "*/" }], + options: [{ terms: ["todo"], location: "start", decoration: ["*", "/"] }], errors: [ { messageId: "unexpectedComment", From 0eacf7549f7120f495a27664df9d395b645f0c69 Mon Sep 17 00:00:00 2001 From: Lachlan Hunt Date: Mon, 8 Aug 2022 09:18:43 +1000 Subject: [PATCH 06/10] Enforce minimum number of unique decoration characters --- lib/rules/no-warning-comments.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/rules/no-warning-comments.js b/lib/rules/no-warning-comments.js index 6ef9ccba1af..5dfe295765c 100644 --- a/lib/rules/no-warning-comments.js +++ b/lib/rules/no-warning-comments.js @@ -44,7 +44,9 @@ module.exports = { type: "string", pattern: "^\\S$" }, - pattern: "^\\S$" + pattern: "^\\S$", + minItems: 1, + uniqueItems: true } }, additionalProperties: false From 48d8e2fbc5f353e90c1fada090be9d265f27139d Mon Sep 17 00:00:00 2001 From: Lachlan Hunt Date: Thu, 11 Aug 2022 09:32:19 +1000 Subject: [PATCH 07/10] Update docs/src/rules/no-warning-comments.md Co-authored-by: Milos Djermanovic --- docs/src/rules/no-warning-comments.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/rules/no-warning-comments.md b/docs/src/rules/no-warning-comments.md index def95423a81..283a70f951a 100644 --- a/docs/src/rules/no-warning-comments.md +++ b/docs/src/rules/no-warning-comments.md @@ -22,7 +22,7 @@ This rule reports comments that include any of the predefined terms specified in This rule has an options object literal: * `"terms"`: optional array of terms to match. Defaults to `["todo", "fixme", "xxx"]`. Terms are matched case-insensitively and as whole words: `fix` would match `FIX` but not `fixing`. Terms can consist of multiple words: `really bad idea`. -* `"location"`: optional string that configures where in your comments to check for matches. Defaults to `"start"`. For multi-line comments, the start is from the first non-decorative character, ignoring whitespace, new lines or characters specified in `decoration`. The other value is match `anywhere` in comments. +* `"location"`: optional string that configures where in your comments to check for matches. Defaults to `"start"`. The start is from the first non-decorative character, ignoring whitespace, new lines and characters specified in `decoration`. The other value is match `anywhere` in comments. * `"decoration"`: optional character, or array of characters that specify decorative characters that are ignored at the start of a comment, when location is `"start"`. Defaults to `""`. Any sequence of whitespace or the characters from this property are ignored. This option is ignored when location is `"anywhere"`. Example of **incorrect** code for the default `{ "terms": ["todo", "fixme", "xxx"], "location": "start" }` options: From e60eaad64bedbd38d03f0a3a422d0805b5a16cb9 Mon Sep 17 00:00:00 2001 From: Lachlan Hunt Date: Thu, 11 Aug 2022 13:32:47 +1000 Subject: [PATCH 08/10] Change decoration property to array only, update docs for no-warning-comments --- docs/src/rules/no-warning-comments.md | 8 +++---- lib/rules/no-warning-comments.js | 5 ++--- tests/lib/rules/no-warning-comments.js | 29 ++------------------------ 3 files changed, 7 insertions(+), 35 deletions(-) diff --git a/docs/src/rules/no-warning-comments.md b/docs/src/rules/no-warning-comments.md index 283a70f951a..6e610ca4e53 100644 --- a/docs/src/rules/no-warning-comments.md +++ b/docs/src/rules/no-warning-comments.md @@ -23,7 +23,7 @@ This rule has an options object literal: * `"terms"`: optional array of terms to match. Defaults to `["todo", "fixme", "xxx"]`. Terms are matched case-insensitively and as whole words: `fix` would match `FIX` but not `fixing`. Terms can consist of multiple words: `really bad idea`. * `"location"`: optional string that configures where in your comments to check for matches. Defaults to `"start"`. The start is from the first non-decorative character, ignoring whitespace, new lines and characters specified in `decoration`. The other value is match `anywhere` in comments. -* `"decoration"`: optional character, or array of characters that specify decorative characters that are ignored at the start of a comment, when location is `"start"`. Defaults to `""`. Any sequence of whitespace or the characters from this property are ignored. This option is ignored when location is `"anywhere"`. +* `"decoration"`: optional array of characters that are ignored at the start of a comment, when location is `"start"`. Defaults to `[]`. Any sequence of whitespace or the characters from this property are ignored. This option is ignored when location is `"anywhere"`. Example of **incorrect** code for the default `{ "terms": ["todo", "fixme", "xxx"], "location": "start" }` options: @@ -145,11 +145,9 @@ Examples of **correct** code for the `{ "decoration": ["/", "*"] }` options: ```js /*eslint no-warning-comments: ["error", { "decoration": ["/", "*"] }]*/ -// This is to do +//!TODO preceded by non-decoration character /** - * The same goes for block comments - * with any other interesting term - * or fix me this + *!TODO preceded by non-decoration character in a block comment */ ``` diff --git a/lib/rules/no-warning-comments.js b/lib/rules/no-warning-comments.js index 5dfe295765c..d1bfb81d3a0 100644 --- a/lib/rules/no-warning-comments.js +++ b/lib/rules/no-warning-comments.js @@ -39,12 +39,11 @@ module.exports = { enum: ["start", "anywhere"] }, decoration: { - type: ["string", "array"], + type: "array", items: { type: "string", pattern: "^\\S$" }, - pattern: "^\\S$", minItems: 1, uniqueItems: true } @@ -63,7 +62,7 @@ module.exports = { configuration = context.options[0] || {}, warningTerms = configuration.terms || ["todo", "fixme", "xxx"], location = configuration.location || "start", - decoration = [...configuration.decoration || ""].join(""), + decoration = [...configuration.decoration || []].join(""), selfConfigRegEx = /\bno-warning-comments\b/u; /** diff --git a/tests/lib/rules/no-warning-comments.js b/tests/lib/rules/no-warning-comments.js index 74779a5b876..749e5f1ecb9 100644 --- a/tests/lib/rules/no-warning-comments.js +++ b/tests/lib/rules/no-warning-comments.js @@ -38,7 +38,8 @@ ruleTester.run("no-warning-comments", rule, { "/*eslint no-warning-comments: [2, { \"terms\": [\"todo\", \"fixme\", \"any other term\"], \"location\": \"anywhere\" }]*/\n\nvar x = 10;\n", { code: "/*eslint no-warning-comments: [2, { \"terms\": [\"todo\", \"fixme\", \"any other term\"], \"location\": \"anywhere\" }]*/\n\nvar x = 10;\n", options: [{ location: "anywhere" }] }, { code: "// foo", options: [{ terms: ["foo-bar"] }] }, - "/** multi-line block comment with lines starting with\nTODO\nFIXME or\nXXX\n*/" + "/** multi-line block comment with lines starting with\nTODO\nFIXME or\nXXX\n*/", + { code: "//!TODO ", options: [{ decoration: ["*"] }] } ], invalid: [ { @@ -402,32 +403,6 @@ ruleTester.run("no-warning-comments", rule, { } ] }, - { - code: "/** TODO decorated single line block comment (start) */", - options: [{ terms: ["todo"], location: "start", decoration: "*" }], - errors: [ - { - messageId: "unexpectedComment", - data: { - matchedTerm: "todo", - comment: "* TODO decorated single line block..." - } - } - ] - }, - { - code: "/**\n * TODO decorated multi-line block comment (start) \n */", - options: [{ terms: ["todo"], location: "start", decoration: "*" }], - errors: [ - { - messageId: "unexpectedComment", - data: { - matchedTerm: "todo", - comment: "* * TODO decorated multi-line block..." - } - } - ] - }, { code: "///// TODO decorated single-line comment with decoration array \n /////", options: [{ terms: ["todo"], location: "start", decoration: ["*", "/"] }], From 619a6d1b4c943bb94332c4a95da2e36df9984069 Mon Sep 17 00:00:00 2001 From: Lachlan Hunt Date: Sun, 21 Aug 2022 10:16:52 +1000 Subject: [PATCH 09/10] Fix mistakes in README --- docs/src/rules/no-warning-comments.md | 4 ++-- lib/rules/no-warning-comments.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/src/rules/no-warning-comments.md b/docs/src/rules/no-warning-comments.md index 6e610ca4e53..0fde1206799 100644 --- a/docs/src/rules/no-warning-comments.md +++ b/docs/src/rules/no-warning-comments.md @@ -107,12 +107,12 @@ Examples of **correct** code for the `{ "terms": ["todo", "fixme", "any other te ### Decoration Characters -Examples of **incorrect** code for the `{ "decoration": "*" }` options: +Examples of **incorrect** code for the `{ "decoration": ["*"] }` options: ::: incorrect ```js -/*eslint no-warning-comments: ["error", { "decoration": "*" }]*/ +/*eslint no-warning-comments: ["error", { "decoration": ["*"] }]*/ //***** todo decorative asterisks are ignored *****// /** diff --git a/lib/rules/no-warning-comments.js b/lib/rules/no-warning-comments.js index d1bfb81d3a0..9754f50880b 100644 --- a/lib/rules/no-warning-comments.js +++ b/lib/rules/no-warning-comments.js @@ -92,7 +92,7 @@ module.exports = { * For location start: * [\s]* handles optional leading spaces * e.g. terms ["TODO"] matches `// TODO something` - * [\s\*] (where "\*" is the escaped string of decoration) handles + * [\s\*]* (where "\*" is the escaped string of decoration) * handles optional leading spaces or decoration characters (for "start" location only) * e.g. terms ["TODO"] matches `/**** TODO something ... ` */ From 189f0c4d42802eb268933004fcbf908158643b58 Mon Sep 17 00:00:00 2001 From: Lachlan Hunt Date: Sun, 21 Aug 2022 10:17:05 +1000 Subject: [PATCH 10/10] Add test for term starting with decoration character --- tests/lib/rules/no-warning-comments.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/lib/rules/no-warning-comments.js b/tests/lib/rules/no-warning-comments.js index 749e5f1ecb9..74122456121 100644 --- a/tests/lib/rules/no-warning-comments.js +++ b/tests/lib/rules/no-warning-comments.js @@ -428,6 +428,19 @@ ruleTester.run("no-warning-comments", rule, { } } ] + }, + { + code: "//**TODO term starts with a decoration character", + options: [{ terms: ["*todo"], location: "start", decoration: ["*"] }], + errors: [ + { + messageId: "unexpectedComment", + data: { + matchedTerm: "*todo", + comment: "**TODO term starts with a decoration..." + } + } + ] } ] });