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

Skip assertDoc calls in production #3268

Merged
merged 4 commits into from Nov 16, 2017
Merged

Conversation

duailibe
Copy link
Member

@duailibe duailibe commented Nov 14, 2017

Context: #3263 (comment)

When printDocToString calls fill, it's not necessary to call assertDoc for all of the remaining elements since they already checked when the doc was originally created (somewhere in printAstToDoc).

@suchipi
Copy link
Member

suchipi commented Nov 14, 2017

If we never assertDoc in fill, are we still covered? Or are there situations where we call fill without asserting doc in printAstToDoc?

We might want to offload this kind of check to a type checker like flow or etc instead of taking the runtime hit (static type checking instead). But of course, adding flow could make things more overwhelming for new contributors. Not sure how often new contributors make changes to the doc printer though (I suspect rarely).

@duailibe
Copy link
Member Author

If we never assertDoc in fill, are we still covered? Or are there situations where we call fill without asserting doc in printAstToDoc?

I'm not sure I understand this correctly. But if I did, I think we're not covered, since the case for fill is to receive values (not other docs: group or concat): fill(["text", separator, "line", separator, ...]) so those would be "unchecked" docs.

I was wondering what would be the downside of skipping the assertDoc check entirely in production. Our own code is covered by tests and the only problem that could happen would be a parser misbehaving (like returning something not string when it should be a string?).

@suchipi
Copy link
Member

suchipi commented Nov 14, 2017

Sorry that I wasn't clear, but yes, you understood correctly.

I like the idea of skipping assertDoc in production. Let's do that instead of special-casing fill.

@duailibe duailibe changed the title Skip redundant assertDoc calls in fill Skip assertDoc calls in production Nov 14, 2017
throw new Error(
"Value " + JSON.stringify(val) + " is not a valid document"
);
if (process.env.NODE_ENV === "production") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be !==

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙄 sorry about that

@duailibe
Copy link
Member Author

duailibe commented Nov 15, 2017

@suchipi Looks like just calling the function is expensive enough :( (or the forEach call)

@duailibe
Copy link
Member Author

duailibe commented Nov 15, 2017

I had to wrap each call with the if and add the replace in rollup build script to remove them from the dist file (optional but skips an if check, which is a good thing). I thought it would not get removed without minification but it was, to my surprise.

It's a bit ugly that bunch of ifs lying around but I think it's worth the benefit. We could change them with a __DEV__ call witch is a bit fancier, and we just have to change the rollup replace plugin to achieve that. Another option would be automatically removing them in rollup but I thought it was too much magic.

Let me know what you think!

@@ -2,7 +2,7 @@

function assertDoc(val) {
/* istanbul ignore if */
if (process.env.NODE_ENV === "production") {
if (process.env.NODE_ENV !== "production") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this is the right way of doing it, a lot of people will run prettier in "development" env.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@azz I updated the PR, can you check again?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify, when building the bundle, we will replace that check with "production" to remove that entirely

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right. Cool.

Copy link
Member

@suchipi suchipi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind the ifs; I think people are pretty used to seeing process.env.NODE_ENV in code these days, compared to something more "magic" like __DEV__.

@duailibe duailibe merged commit 66d9b26 into prettier:master Nov 16, 2017
@duailibe duailibe deleted the fill-assert-doc branch November 16, 2017 11:55
@suchipi suchipi added this to the 1.9 milestone Nov 28, 2017
@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jan 19, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jan 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants