question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Configuring pre-commit for jsx, Typescript, tsx

See original GitHub issue

There’s not a lot of documentation on how to use the pre-commit method (maybe that’s by design?) but this is what I’ve done.

pip install pre-commit
touch .pre-commit-config.yaml

.pre-commit-config.yaml

    -   repo: https://github.com/prettier/prettier
        sha: 'a29b407'  # Use the sha or tag you want to point at
        hooks:
        -   id: prettier
pre-commit install
pre-commit autoupdate

.App.jsx

    export default class extends Component {
        handleChange = (e) => {
            this.setState({
                input: e.target.value,
            });
            console.log('aye', 'foo', 'blah', 'blah', 'aye', 'foo', 'blah', 'blah', 'aye', 'foo', 'blah', 'blah');
            console.log('aye', 'foo', 'blah', 'blah', 'aye', 'foo', 'blah', 'blah', 'aye', 'foo', 'blah', 'blah');
            console.log('aye', 'foo', 'blah', 'blah', 'aye', 'foo', 'blah', 'blah', 'aye', 'foo', 'blah', 'blah');
            console.log('aye', 'foo', 'blah', 'blah', 'aye', 'foo', 'blah', 'blah', 'aye', 'foo', 'blah', 'blah');
    
            [
                        'lorem',
                        'ipsum',
                        'dolor',
                        sit('amet'),
                        consectetur['adipiscing'] + 'elit',
                    ].reduce((first, second) => first + second, '');
    
        }
        // ...
        // ....
git add --all
git commit

Then I see this:

screen shot 2017-09-03 at 8 05 39 pm

This is working on regular js files if I attempt to commit them, but not jsx or tsx (I didn’t try regular ts).

As to why I’m using pre-commit, it’s in the Readme as being acceptable to use, and I’d also like to keep the ability to stage hunks without staging the entire file.

I also have a followup question: does the pre-commit stuff use the .prettierrc config file, or do you need to pass in your own styles? If so, how do you do this as well?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
asottilecommented, Sep 5, 2017

@reywright the latest configuration (just merged) should support ts / tsx as well – thanks for the issue!

To get the latest version (before a tagged release is made in prettier) you can use pre-commit autoupdate --bleeding-edge (this will get the latest revision of master instead of the latest tag). Or use 4d68faf or newer.

@chriskuehl thanks again for pinging 😄 @vjeux if there’s additional issues about pre-commit integration now or in the future, feel free to ping me 😃

0reactions
asottilecommented, Sep 5, 2017

Sweet, here’s my proposal in PR form: https://github.com/prettier/prettier/pull/2759

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring Prettier and TypeScript Compiler as a Pre-commit ...
1. Install prettier, husky and lint-staged · 2. Configure prettier · 3. Create a lint-staged config file: .lintstagedrc: · 4. Create a husky...
Read more >
Pre-Commit Hooks for Pretter and ESLint in TypeScript Projects
Learn how to configure your TypeScript project to automatically prettify and lint your code before committing to git using husky.
Read more >
How to Add ESLint, Prettier, and Husky (Git Hooks) to Your ...
A complete guide on adding Eslint with Prettier to a TypeScript React project and running pre-commit command on each Git commit command.
Read more >
Build a robust React app with Husky pre-commit hooks and ...
Building robust React projects may seem complex at first, but this guide will explain how you can do so with Husky hooks and...
Read more >
Husky + Lint-Staged on a React TypeScript Project
Setting up to commit only if all Unit Tests passed. To trigger some action before committing, we will use the Git Hook called...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found