From 0f4d8e0af8d17872ed0919077572b9fa4a6f4ec2 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Sat, 9 Oct 2021 10:40:47 +0800 Subject: [PATCH] fix(`check-indentation`): ensure decorators in fenced code blocks do not terminate ignoring of indentation in code blocks; fixes #789 Prevent decorators from being interpreted as tags which may force end code block masking --- README.md | 14 ++++++++++++++ src/rules/checkIndentation.js | 2 +- test/rules/assertions/checkIndentation.js | 22 ++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 286997599..5f98799be 100644 --- a/README.md +++ b/README.md @@ -1932,6 +1932,20 @@ function quux () { * ``` */ // "jsdoc/check-indentation": ["error"|"warn", {"excludeTags":[]}] + +/** + * @example + * ``` + * @MyDecorator({ + * myOptions: 42 + * }) + * export class MyClass {} + * ``` + */ +function MyDecorator(options: { myOptions: number }) { + return (Base: Function) => {}; +} +// "jsdoc/check-indentation": ["error"|"warn", {"excludeTags":["example","MyDecorator"]}] ```` diff --git a/src/rules/checkIndentation.js b/src/rules/checkIndentation.js index 94d37914a..43d2ef926 100644 --- a/src/rules/checkIndentation.js +++ b/src/rules/checkIndentation.js @@ -9,7 +9,7 @@ const maskExcludedContent = (str, excludeTags) => { }; const maskCodeBlocks = (str) => { - const regContent = /([ \t]+\*)[ \t]```[^\n]*?([\w|\W]*?\n)(?=[ \t]*\*(?:[ \t]*(?:```|@)|\/))/gu; + const regContent = /([ \t]+\*)[ \t]```[^\n]*?([\w|\W]*?\n)(?=[ \t]*\*(?:[ \t]*(?:```|@\w+\s)|\/))/gu; return str.replace(regContent, (_match, margin, code) => { return new Array(code.match(/\n/gu).length + 1).join(margin + '\n'); diff --git a/test/rules/assertions/checkIndentation.js b/test/rules/assertions/checkIndentation.js index 688278cac..9269c818f 100644 --- a/test/rules/assertions/checkIndentation.js +++ b/test/rules/assertions/checkIndentation.js @@ -289,5 +289,27 @@ export default { excludeTags: [], }], }, + { + code: ` + /** + * @example + * \`\`\` + * @MyDecorator({ + * myOptions: 42 + * }) + * export class MyClass {} + * \`\`\` + */ + function MyDecorator(options: { myOptions: number }) { + return (Base: Function) => {}; + } + `, + options: [ + { + excludeTags: ['example', 'MyDecorator'], + }, + ], + parser: require.resolve('@typescript-eslint/parser'), + }, ], };