Skip to content

Commit

Permalink
[jest-docblock] Preserve leading whitespace in docblock comments (jes…
Browse files Browse the repository at this point in the history
  • Loading branch information
azz authored and cpojer committed Oct 1, 2017
1 parent c89ee8c commit 6e5d149
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
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

0 comments on commit 6e5d149

Please sign in to comment.