Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Handle multiline comments in parseWithComments #12845

Merged
merged 1 commit into from May 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
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
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
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