Skip to content

Commit

Permalink
Fix: Handle multiline comments in parseWithComments (#12845)
Browse files Browse the repository at this point in the history
Co-authored-by: Sven Sterbling <[email protected]>
  • Loading branch information
ullumullu and ssterb authored May 15, 2022
1 parent 3f78547 commit a12b67d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 21 additions & 0 deletions packages/jest-docblock/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;

Expand Down Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-docblock/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import detectNewline = require('detect-newline');
type Pragmas = Record<string, string | Array<string>>;

const commentEndRe = /\*\/$/;
const commentStartRe = /^\/\*\*/;
const commentStartRe = /^\/\*\*?/;
const docblockRe = /^\s*(\/\*\*?(.|\r?\n)*?\*\/)/;
const lineCommentRe = /(^|\s+)\/\/([^\r\n]*)/g;
const ltrimNewlineRe = /^(\r?\n)+/;
Expand Down

0 comments on commit a12b67d

Please sign in to comment.