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

[jest-docblock] Preserve leading whitespace in docblock comments #4576

Merged
merged 1 commit into from
Oct 1, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/jest-docblock/src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,15 @@ describe('docblock', () => {
});
});

it('preserves leading whitespace in multiline comments from docblock', () => {
const code =
'/**' + os.EOL + ' * hello' + os.EOL + ' * world' + os.EOL + ' */';

expect(docblock.parseWithComments(code).comments).toEqual(
' hello' + os.EOL + ' world',
);
});

it('extracts comments from beginning and end of docblock', () => {
const code =
'/**' +
Expand Down
14 changes: 7 additions & 7 deletions packages/jest-docblock/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ const commentStartRe = /^\/\*\*/;
const docblockRe = /^\s*(\/\*\*?(.|\r?\n)*?\*\/)/;
const lineCommentRe = /\/\/([^\r\n]*)/g;
const ltrimRe = /^\s*/;
const rtrimRe = /\s*$/;
const ltrimNewlineRe = /^(\r?\n)+/;
const multilineRe = /(?:^|\r?\n) *(@[^\r\n]*?) *\r?\n *([^@\r\n\s][^@\r\n]+?) *\r?\n/g;
const propertyRe = /(?:^|\r?\n) *@(\S+) *([^\r\n]*)/g;
const stringStartRe = /(\r?\n|^) *\*/g;
const lineStartRe = /(\r?\n|^) */g;
const wsRe = /[\t ]+/g;
const stringStartRe = /(\r?\n|^) *\* ?/g;

export function extract(contents: string): string {
const match = contents.match(docblockRe);
Expand All @@ -43,7 +43,6 @@ export function parseWithComments(
docblock = docblock
.replace(commentStartRe, '')
.replace(commentEndRe, '')
.replace(wsRe, ' ')
.replace(lineCommentRe, '')
.replace(stringStartRe, '$1');

Expand All @@ -53,15 +52,16 @@ export function parseWithComments(
prev = docblock;
docblock = docblock.replace(multilineRe, `${line}$1 $2${line}`);
}
docblock = docblock.trim();
docblock = docblock.replace(ltrimNewlineRe, '').replace(rtrimRe, '');

const result = Object.create(null);
const comments = docblock.replace(propertyRe, '').replace(lineStartRe, line);
const comments = docblock.replace(propertyRe, '');

let match;
while ((match = propertyRe.exec(docblock))) {
result[match[1]] = match[2];
}
return {comments: comments.trim(), pragmas: result};
return {comments, pragmas: result};
}

export function print({
Expand Down