From 749c24bc2307ab0e20e5f85babc5c04d5a0a5d8c 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 --- CHANGELOG.md | 1 + .../jest-docblock/src/__tests__/index.test.ts | 21 +++++++++++++++++++ packages/jest-docblock/src/index.ts | 2 +- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b0e4a8b3327..b1c4d99db1f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ ### Fixes - `[jest-worker]` Make `JestWorkerFarm` helper type to include methods of worker module that take more than one argument ([#12839](https://github.com/facebook/jest/pull/12839)) +- `[jest-docblock]` Handle multiline comments in parseWithComments ([#12845](https://github.com/facebook/jest/pull/12845)) ### Chore & Maintenance 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)+/;