-
-
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
Conversation
I like it. |
Codecov Report
@@ Coverage Diff @@
## master #4517 +/- ##
==========================================
+ Coverage 56.01% 56.14% +0.12%
==========================================
Files 185 185
Lines 6264 6282 +18
Branches 3 3
==========================================
+ Hits 3509 3527 +18
Misses 2754 2754
Partials 1 1
Continue to review full report at Codecov.
|
packages/jest-docblock/README.md
Outdated
|
||
const docblock = extract(code); | ||
console.log(docblock); // "/**\n * Everything is awesome!\n * \n * @everything is:awesome\n * @flow\n */" | ||
|
||
const pragmas = parse(docblock); | ||
console.log(pragmas); // { everything: "is:awesome", flow: "" } | ||
|
||
console.log(print(pragmas), "hi!") // /**\n * hi!\n *\n * @everything is:awesome\n * @flow\n */; |
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.
Should be
console.log(print(pragmas, "hi!"))
@azz, this is awesome and will definitely help with implementing I have one question on it though. Wouldn't the prependPragma use-case also need to be able to extract the non-pragma portion of the docblock? That way if the docblock in a file looks like this:
we'd want to somehow be able to call: docblock.print({ format: '', providesFruit: 'tomatoes' }, "License 2017 (c) Something Inc." ); Which would then output:
The options I can think of are either:
|
packages/jest-docblock/src/index.js
Outdated
@@ -8,6 +8,8 @@ | |||
* @flow | |||
*/ | |||
|
|||
const os = require('os'); |
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.
should use import
packages/jest-docblock/src/index.js
Outdated
exports.extract = extract; | ||
exports.parse = parse; | ||
exports.print = print; |
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.
these can probably be separate export function
s. Not sure why I didn't convert them in my PR for ESM exports...
packages/jest-docblock/src/index.js
Outdated
|
||
const printedComments = | ||
comments | ||
.split(os.EOL) |
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.
can we detect the original EOL
in the comment instead (https://www.npmjs.com/package/detect-newline)? People might use LF even if they're on Windows (e.g. airbnb eslint config requires LF regardless of OS)
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.
Should I do the same for the existing parse
, which has a hard-coded \n
?
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 think that makes sense, yeah 🙂
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.
See @SimenB's comments. I'll let this bake for a little longer until you guys figure out what the right way for this module is :)
I've added a new function, parseWithComments(docblock: string): { comments: string, pragmas: {[key: string]: string} } How does that sound? Still WIP - need to address the EOL comments and add more test cases. |
packages/jest-docblock/src/index.js
Outdated
} | ||
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
This was required for existing code that does import docblock from 'jest-docblock'
to work without changes.
@SimenB should I leave this here or update the existing imports?
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.
Either way works for me. @cpojer thoughts?
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.
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.
The |
Only reason I didn't do that is it would be a breaking change. @cpojer What do you prefer? |
BREAKING CHANGE: Changes jest-docblock to an ES module without a default export, requires `import * as docblock from 'jest-docbloc'`
os.EOL + | ||
' */'; | ||
expect(docblock.parseWithComments(code)).toEqual({ | ||
comments: 'hello' + os.EOL + os.EOL + 'world', |
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:
/**
* hello
* @flow yes
*
* world
*/
I'd expect the result of parseWithComments
to only have a single EOL between the hello and world like:
{
comments: 'hello' + os.EOL + 'world',
pragmas { flow: 'yes' }
}
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:
/**
* My module does special snowflake
* thing x really well.
* @flow
*
* Copyright (c) 1234 Some Guy
*/
print(parseWithComments(text))
to reformat to:
/**
* My module does special snowflake
* thing x really well.
*
* Copyright (c) 1234 Some Guy
*
* @flow
*/
Published jest@test for you guys to use this stuff in prettier. |
* [jest-docblock]: add docblock.print() * Add missing flow type, clean up * Fix docs typo * Fix doc typo * [WIP] add docblock.parseWithComments() * Detect newline character, consolidate API * Fixup types * Remove default export from jest-docblock BREAKING CHANGE: Changes jest-docblock to an ES module without a default export, requires `import * as docblock from 'jest-docbloc'`
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
Prettier is implementing a
--insertPragma
flag to add/** @format */
to docblock, and ideally we'd be able to augment existing docblocks rather than prepending a new one, which will break tools which only look at the first one.Context: prettier/prettier#2865 (comment)
This PR adds a simple
docblock.print()
function.In practice:
Test plan
Unit tests are covering most cases are included.