Skip to content

Format Code with Prettier#47

Open
kevinastone wants to merge 2 commits into
masterfrom
prettier
Open

Format Code with Prettier#47
kevinastone wants to merge 2 commits into
masterfrom
prettier

Conversation

@kevinastone
Copy link
Copy Markdown
Collaborator

Prettier automatically formats your code so you can focus on coding rather than style and format.

@frezik
Copy link
Copy Markdown
Contributor

frezik commented Sep 3, 2020

I'm going to strongly disagree with this one. I have specific reasons for some of the stylistic choices, and prettifiers tend not to follow those guidelines.

@frezik
Copy link
Copy Markdown
Contributor

frezik commented Sep 3, 2020

Actually, looking over it more closely, it's mostly good changes, but I have quibbles that I hope can be addressed by config changes to the prettier. I'll add comments to the commit directly.

Comment thread src/db-fixture-pg.ts
Comment on lines -18 to +12
public addToDB(
db_frontend: pg_db.PG
,success_callback: () => void
): void
{
public addToDB(db_frontend: pg_db.PG, success_callback: () => void): void {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Breaking out parameters line by line, as in the original, is preferred. Commas can go to the end of the line.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Prettier collapses to a single-line when it can. That's the basic formatting algorithm: collapse-all and then progressively expand to fit under the line-length. There's not much control if you prefer always expanded to multi-line for function arguments such as this case. 😐

Comment thread src/db-fixture-pg.ts
Comment on lines -50 to +39
this.addArea( db, area_callback );
this.addArea(db, area_callback);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It shouldn't collapse whitespace like this. Whitespace is good.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I didn't see an option for this in prettier.

Comment thread get_google_oauth.js
Comment on lines -6 to +11
'https://www.googleapis.com/auth/admin.directory.group.member'
,'https://www.googleapis.com/auth/admin.directory.group'
,'https://mail.google.com/'
,'https://www.googleapis.com/auth/gmail.modify'
,'https://www.googleapis.com/auth/gmail.compose'
,'https://www.googleapis.com/auth/gmail.send'
'https://www.googleapis.com/auth/admin.directory.group.member',
'https://www.googleapis.com/auth/admin.directory.group',
'https://mail.google.com/',
'https://www.googleapis.com/auth/gmail.modify',
'https://www.googleapis.com/auth/gmail.compose',
'https://www.googleapis.com/auth/gmail.send',
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This change is fine, but FYI, my original reason for leading commas like this is because adding items to the end of a list is more common than the beginning. In languages that don't allow a final comma at the end, this leading comma style helps prevent syntax errors when adding new elements. I do think the style is ugly, but it helps prevent problems.

Then I found out JavaScript added final commas to everything between ES5 and ES2017, so this ugly style isn't necessary.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, trailing commas alleviates the benefits of some styles that focus on diff-cleanliness.

Comment thread get_google_oauth.js
Comment on lines -31 to +33
client_id, client_secret, redirect_uris[0] );
client_id,
client_secret,
redirect_uris[0],
);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This sort of thing is good. I vastly prefer breaking out parameters line by line this way. Vertical space doesn't need to be conserved.

Comment thread src/db-fixture-pg.ts
Comment on lines +69 to +71
'INSERT INTO members (',
'full_name',
') VALUES ($1) RETURNING id',
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This might be tough for this prettifier to handle. The 'full_name' parameter here was given an extra layer of indent, because it makes sense for the SQL. The prettifier isn't likely to grok embedded SQL like that, though. I'm guessing there's some way to say "PRETTIFIER LEAVE THIS BLOCK ALONE", but that would be tedious to add to all the embedded SQL in here.

I don't like it, but can live with it.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, you can use inline comments to ignore specific lines (or "nodes"): https://prettier.io/docs/en/ignore.html#javascript

Comment thread src/context.ts
Comment on lines -5 to +4
export interface Logger
{
export interface Logger {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My personal style is that curly brackets is K&R Style. Curlies should go on their own line for important structural things (interfaces, classes, non-anonymous functions), and on the same line for internal things (conditionals, loops, etc.)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Didn't see any option for alternative bracketing styles unfortunately.

Comment thread src/db-fixture-pg.ts
Comment on lines -34 to +25
this.addTool( db, area_id, member_id,
(tool2_id, tool2_name) => tool2_callback(
tool2_id, tool2_name, area_id, member_id )
this.addTool(db, area_id, member_id, (tool2_id, tool2_name) =>
tool2_callback(tool2_id, tool2_name, area_id, member_id),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Here, I don't like the original or the new version. Parameters should break out line by line.

@kevinastone
Copy link
Copy Markdown
Collaborator Author

kevinastone commented Sep 4, 2020

I'm less concerned about adopting any given style myself, but I do think it's a significant obstacle for newcomers and others looking to learn or contribute. So, if you're looking to grow contributors, I'd consider adopting prettier. For a beginner, being able to run npm format and their code should be format will be acceptable is an accelerator. (For more veterans, it's also easier since you can also just run the formatter on-save like I do and stop thinking about nits around spacing).

https://prettier.io/docs/en/why-prettier.html covers the general philosophy and the general adoption and trend of opinionated automatic code formatters across the industry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants