From 2c65c339c3da9cc2704c2c399dab0dfa43e7ea30 Mon Sep 17 00:00:00 2001 From: Sven Sterbling Date: Fri, 13 May 2022 21:30:49 +0200 Subject: [PATCH] Fix: Handle multiline comments in parseWithComments This problem was found while using prettier with pragmas enabled and I backtracked it to the `parseWithComments` function from jest-docblock. In this context it is especially problematic in cases were the top level comment is not formatted as a dock lock but a "multi-line comment" to disable an es-lint rule. /* eslint-disable @typescript-eslint/unbound-method */ Especially given the fact that the `extract` function seems to handle this case correctly. Therefore this solution alters the commentStartRe to keep the second * as optional. This should ensure that all existing use cases work as expected with proper docblocks but also ensures that an "invalid" multiline comment works as well. Links: https://github.com/ullumullu/jest/blob/main/packages/jest-docblock/src/__tests__/index.test.ts#L29 --- .../jest-docblock/src/__tests__/index.test.ts | 21 +++++++++++++++++++ packages/jest-docblock/src/index.ts | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/jest-docblock/src/__tests__/index.test.ts b/packages/jest-docblock/src/__tests__/index.test.ts index b5e783756171..366ed1181404 100644 --- a/packages/jest-docblock/src/__tests__/index.test.ts +++ b/packages/jest-docblock/src/__tests__/index.test.ts @@ -31,6 +31,11 @@ describe('docblock', () => { expect(docblock.extract(code)).toBe(`/*${EOL} * @team foo${EOL}*/`); }); + it('extracts from invalid docblock singleline', () => { + const code = `/* some comment @team foo */${EOL}const x = foo;`; + expect(docblock.extract(code)).toBe('/* some comment @team foo */'); + }); + it('returns extract and parsedocblock', () => { const code = `/** @provides module-name */${EOL}${EOL}.dummy {}${EOL}`; @@ -204,6 +209,22 @@ describe('docblock', () => { }); }); + it('extract from invalid docblock', () => { + const code = `/* @format: everything${EOL}// keep me */`; + expect(docblock.parseWithComments(code)).toEqual({ + comments: '// keep me', + pragmas: {'format:': 'everything'}, + }); + }); + + it('extract from invalid docblock singleline', () => { + const code = '/* some test */'; + expect(docblock.parseWithComments(code)).toEqual({ + comments: ' some test', + pragmas: {}, + }); + }); + it('extracts docblock comments as CRLF when docblock contains CRLF', () => { const code = '/**\r\n * foo\r\n * bar\r\n*/'; expect(docblock.parseWithComments(code)).toEqual({ diff --git a/packages/jest-docblock/src/index.ts b/packages/jest-docblock/src/index.ts index fd96b01d795d..f682fe90b1c3 100644 --- a/packages/jest-docblock/src/index.ts +++ b/packages/jest-docblock/src/index.ts @@ -11,7 +11,7 @@ import detectNewline = require('detect-newline'); type Pragmas = Record>; const commentEndRe = /\*\/$/; -const commentStartRe = /^\/\*\*/; +const commentStartRe = /^\/\*\*?/; const docblockRe = /^\s*(\/\*\*?(.|\r?\n)*?\*\/)/; const lineCommentRe = /(^|\s+)\/\/([^\r\n]*)/g; const ltrimNewlineRe = /^(\r?\n)+/;