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

Prettify code blocks in JavaScript docs #9565

Closed
wants to merge 1 commit into from

Conversation

wbamberg
Copy link
Collaborator

@wbamberg wbamberg commented Oct 4, 2021

This is the result of running:

npx prettier --write ./files/en-us/web/javascript

...which has the effect of prettifying the JS code blocks in the JS documentation.

Just an experiment to see if we might want to do this.

@github-actions github-actions bot added the Content:JS JavaScript docs label Oct 4, 2021
toString: function () {
return "I'm an object!";
},
};
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

One thing is that it destroys nicely aligned comments, which is a bit sad.

slice(start, end)
slice();
slice(start);
slice(start, end);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It also adds semicolons to Syntax section, which I'm not sure if we like. This section is odd though because although we are marking it up as JS, it isn't really.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We don't like - i.e. omission is deliberate decision.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Again if we want to run Prettier over JS, we should not mark up "syntax" as JS, because it isn't.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Slightly amusingly to me, I just tagged you on a discussion on prettier here: https://github.com/mdn/content/discussions/9901

If we adopt prettier as a "default" then I'd rather mark up as JS and include the semicolon. The syntax highlighting makes this more readable.

type: 'Invertebrates', // Default value of properties
displayType: function() { // Method which will display type of Animal
type: "Invertebrates", // Default value of properties
displayType: function () {
Copy link
Member

@estelle estelle Oct 4, 2021

Choose a reason for hiding this comment

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

spot checking, and this is the only 'concern' ... but it's ok here, though may not always be - to have the 'this comment explains this function' that was on the end of the line coming after the thing it explains instead of before it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also FWIW I much prefer function() { over function () {. If we did that there should be a fair bit of churn because it contradicts the style guide: https://developer.mozilla.org/en-US/docs/MDN/Guidelines/Code_guidelines/JavaScript#use_expanded_syntax

total.increase(); // #count now = 8
total.increase(5); // #count now = 13
console.log(total.current); // expected output: 13
total.reset(); // #count now = 7
Copy link
Member

Choose a reason for hiding this comment

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

the spacing of having all the output lined up was intentional and has been removed. Again, this is ok

@@ -1,12 +1,13 @@
---
title: 'SyntaxError: missing } after property list'
Copy link
Member

Choose a reason for hiding this comment

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

All the 'don't do this' errors weren't corrected in this series. Not sure how the autoprefix missed it, but it's good it did.

Copy link
Member

Choose a reason for hiding this comment

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

never mind. found a case where it corrected a 'don't do this'

function* f() {};
var obj = new f;
function* f() {}
var obj = new f();
Copy link
Member

Choose a reason for hiding this comment

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

this should not have been changed.

Copy link
Collaborator Author

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can't it be trained to ignore js with the trailing example-bad - i.e. js example-bad is not treated as JS.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Can't it be trained to ignore js with the trailing example-bad - i.e. js example-bad is not treated as JS.

No. I think our only options would be to use the // prettier-ignore comment, or not to use js in the info string. I had hoped that we could use the <!-- prettier-ignore --> before the code block, but as you can see from the huge link above that doesn't work.

We could train the syntax highlighter to recognize bad-js as something to be highlighted (or even rewrite to as brush: js in the markdown-to-html conversion).

Copy link
Collaborator

Choose a reason for hiding this comment

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

Or perhaps consider a more configurable alternative to prettier.

@hamishwillee
Copy link
Collaborator

Honestly, lot of churn for little real value from what I'm seeing.

@hamishwillee
Copy link
Collaborator

hamishwillee commented Oct 18, 2021

I think we absolutely do want to do this, if we can configure it in a way we can all agree on - ideally aligned with other big projects. The starting point for those rules might be https://developer.mozilla.org/en-US/docs/MDN/Guidelines/Code_guidelines/JavaScript#use_expanded_syntax

@wbamberg
Copy link
Collaborator Author

I think we absolutely do want to do this, if we can configure it in a way we can all agree on - ideally aligned with other big projects. The starting point for those rules might be https://developer.mozilla.org/en-US/docs/MDN/Guidelines/Code_guidelines/JavaScript#use_expanded_syntax

Prettier, by design, doesn't give you many configuration options. The idea is that instead of spending time arguing about how you want things to be formatted, you just use Prettier: https://prettier.io/docs/en/why-prettier.html.

I'm very much in favour of this. Although, like everyone else, I have opinions about how code should be formatted, I'd much rather just adopt a consistent style, have tooling to make sure it gets applied, and never have to think about it again.

@wbamberg
Copy link
Collaborator Author

I'm closing this, because although we still definitely want to do it, I don't expect this PR to be merged in anything like this form.

@wbamberg wbamberg closed this Mar 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:JS JavaScript docs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants