-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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] add docblock.print() #4517
Changes from 7 commits
9828730
17d8f0f
130628f
66fdd4a
c0b6a97
854afa6
dc34f5d
a13f950
12a25b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,9 @@ | |
* @flow | ||
*/ | ||
|
||
import detectNewline from 'detect-newline'; | ||
import {EOL} from 'os'; | ||
|
||
const commentEndRe = /\*\/$/; | ||
const commentStartRe = /^\/\*\*/; | ||
const docblockRe = /^\s*(\/\*\*?(.|\r?\n)*?\*\/)/; | ||
|
@@ -16,14 +19,23 @@ const ltrimRe = /^\s*/; | |
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; | ||
|
||
function extract(contents: string): string { | ||
export function extract(contents: string): string { | ||
const match = contents.match(docblockRe); | ||
return match ? match[0].replace(ltrimRe, '') || '' : ''; | ||
} | ||
|
||
function parse(docblock: string): {[key: string]: string} { | ||
export function parse(docblock: string): {[key: string]: string} { | ||
return parseWithComments(docblock).pragmas; | ||
} | ||
|
||
export function parseWithComments( | ||
docblock: string, | ||
): {comments: string, pragmas: {[key: string]: string}} { | ||
const line = detectNewline(docblock) || EOL; | ||
|
||
docblock = docblock | ||
.replace(commentStartRe, '') | ||
.replace(commentEndRe, '') | ||
|
@@ -35,17 +47,64 @@ function parse(docblock: string): {[key: string]: string} { | |
let prev = ''; | ||
while (prev !== docblock) { | ||
prev = docblock; | ||
docblock = docblock.replace(multilineRe, '\n$1 $2\n'); | ||
docblock = docblock.replace(multilineRe, `${line}$1 $2${line}`); | ||
} | ||
docblock = docblock.trim(); | ||
|
||
const result = Object.create(null); | ||
const comments = docblock.replace(propertyRe, '').replace(lineStartRe, line); | ||
let match; | ||
while ((match = propertyRe.exec(docblock))) { | ||
result[match[1]] = match[2]; | ||
} | ||
return result; | ||
return {comments: comments.trim(), pragmas: result}; | ||
} | ||
|
||
export function print({ | ||
comments = '', | ||
pragmas = {}, | ||
}: { | ||
comments?: string, | ||
pragmas?: {[key: string]: string}, | ||
}): string { | ||
const line = detectNewline(comments) || EOL; | ||
const head = '/**'; | ||
const start = ' *'; | ||
const tail = ' */'; | ||
|
||
const keys = Object.keys(pragmas); | ||
|
||
const printedObject = keys | ||
.map(key => start + ' ' + printKeyValue(key, pragmas[key]) + line) | ||
.join(''); | ||
|
||
if (!comments) { | ||
if (keys.length === 0) { | ||
return ''; | ||
} | ||
if (keys.length === 1) { | ||
return `${head} ${printKeyValue(keys[0], pragmas[keys[0]])}${tail}`; | ||
} | ||
} | ||
|
||
const printedComments = | ||
comments | ||
.split(line) | ||
.map(textLine => `${start} ${textLine}`) | ||
.join(line) + line; | ||
|
||
return ( | ||
head + | ||
line + | ||
(comments ? printedComments : '') + | ||
(comments && keys.length ? start + line : '') + | ||
printedObject + | ||
tail | ||
); | ||
} | ||
|
||
function printKeyValue(key, value) { | ||
return `@${key} ${value}`.trim(); | ||
} | ||
|
||
exports.extract = extract; | ||
exports.parse = parse; | ||
export default {extract, parse, parseWithComments, print}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was required for existing code that does @SimenB should I leave this here or update the existing imports? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Either way works for me. @cpojer thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fine to make a breaking change here, I'll publish 21.2 tomorrow or so, and for minor releases we can break the boundaries between Jest's modules. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With respect to this case of:
I'd expect the result of
parseWithComments
to only have a single EOL between the hello and world like:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I modified the test case here to show what I mean: https://github.com/azz/jest/pull/1/files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was actually intended. Because we'll be reprinting comments above pragmas, it made sense to me that:
print(parseWithComments(text))
to reformat to: