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

Check formatting with prettier #239

Closed
wants to merge 3 commits into from
Closed

Check formatting with prettier #239

wants to merge 3 commits into from

Conversation

danvk
Copy link
Owner

@danvk danvk commented Feb 11, 2024

Fixes #238

For my production deadline, checking for the correct number of lines is the only important part.

Still to do:

  • Add number-of-lines mode
  • Read settings from prettier config file
  • Add a verifier:skip-pretttier directive
  • Add an auto-fixer

@danvk
Copy link
Owner Author

danvk commented May 15, 2024

After playing around with this, I didn't wind up using prettier formatting for the second edition.

Here are my notes from February with some examples of why. The gist is that vertical space is at more of a premium in a printed book than it is on your screen.


I’m also not 100% sure I even want my code samples to be prettier-formatted. For example, from #any:

function renderSelector(props: ComponentProps) { /* ... */ }

let selectedId: number = 0;
function handleSelectItem(item: any) {
  selectedId = item.id;
}

renderSelector({onSelectItem: handleSelectItem});

Putting the /* ... */ all on one line feels pretty inoffensive to me. This also feels pretty inoffensive:

function add(a: number, b: number) { return a + b; }
//       ~~~ Duplicate function implementation
function add(a: string, b: string) { return a + b; }
//       ~~~ Duplicate function implementation

So maybe the more useful check is for line length? Here’s another one:

return [['Toni', 'Morrison'], ['Maya', 'Angelou']];

prettier wants to split this across four lines.

I wound up with 237 / 974 samples failed. I think a lot of this is a cascading effect, though, where it’s considering prefixed samples. This is something I can come back to, I don’t think it’s important now.

@danvk danvk closed this May 15, 2024
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.

Enforce prettier formatting of code samples
1 participant