Skip to content

Airbnb guidelines for auto-closing tag sequence alignment break indentation #149

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

Open
tymarats opened this issue Nov 15, 2017 · 2 comments

Comments

@tymarats
Copy link

Airbnb guidelines for alignment of React auto-closing tags (https://github.com/airbnb/javascript/tree/master/react#alignment) breaks indentation, as in the following example

export default <cx>
    <MyComponent
        that={1}
        has={2}
        closing={3}
        tag={4}
        in={5}
        new={6}
        line={7}
    />

<OtherComponent
    should={1}
    be={2}
    indented={3}
/>
    </cx>

while this one works fine:

/* Ok indentation */
export default <cx>
    <MyComponent
        that={1}
        has={2}
        closing={3}
        tag={4}
        in={5}
        same={6}
        line={7} />

    <OtherComponent
        indents={1}
        ok={2} />
</cx>

This seems to happen only in cases when there is an empty line between the auto-closing tag sequence and the next tag, so this seems to work fine:

export default <cx>
    <MyComponent
        that={1}
        has={2}
        closing={3}
        tag={4}
        in={5}
        new={6}
        line={7}
    />
    <OtherComponent
        indents={1}
        ok={2}
    />
</cx>

@tymarats
Copy link
Author

I don't really know vim script, but something along these lines might make things better in this scenario:

    " Align '/>' and '>' with '<' for multiline tags.
    if getline(v:lnum) =~? s:endtag
      let ind = ind - &sw
    endif

    " Then correct the indentation of any JSX following '/>' or '>'.
    " Skip empty lines
    let i = 1
    while ((v:lnum - i) != a:firstline && getline(v:lnum - i) =~? '^\s*$')
       let i = i + 1
    endwhile

    if getline(v:lnum - i) =~? s:endtag
      let ind = ind + &sw
    endif

Obviously, one needs to be aware of {/* ... */} multi-line blocks as well, but I have no idea how to handle that.

@JESii
Copy link

JESii commented Mar 20, 2019

I have exactly the same issue; when can we get the pull request processed? I'm forced to manually indent many lines to pass the validations so that I can push my code.
I've read the README -- thanks for a great plugin!
I'm using pangloss/vim-javascript and mxw/vim-jsx.

When I define a component as follows:

    return (
      <div className="App-intro">
        <div className="page-container">
          <PageHeader title={`${view} Template Selection`} request={currentRequest} />
          <ErrorMsg errorMsg='Waiting for update to template request' />
          <FooterNavButtonGroup backButton={null} nextButton={this.pageValidation} />
          <TextField id="search-query-field" label="Filter by" value={searchQuery} onChange={this.handleSearchQueryChange} style={{ marginBottom: '1em' }} >
          </TextField>
          <ErrorMsg errorMsg={pageErrorMessage} />
          <div style={{ marginBottom: '1em', fontWeight: 'bold', fontStyle: 'italic' }}>
            {pageInfoMessage}
          </div>
          <TemplatesTable templatesObject={allTemplates} handleChange={this.handleChange} handleDelete={this.handleDelete} />
        </div>
        <FooterNavButtonGroup backButton={null} nextButton={this.pageValidation} />
      </div>
    );

image

everything works OK.
But the airbnb eslint rules require that I code it like this:

    return (
      <div className="App-intro">
        <div className="page-container">
          <PageHeader title={`${view} Template Selection`} request={currentRequest} />
          <ErrorMsg errorMsg='Waiting for update to template request' />
          <FooterNavButtonGroup backButton={null} nextButton={this.pageValidation} />
          <TextField
            id="search-query-field"
            label="Filter by"
            value={searchQuery}
            onChange={this.handleSearchQueryChange}
            style={{ marginBottom: '1em' }}
          />
          </TextField>
          <ErrorMsg errorMsg={pageErrorMessage} />
          <div style={{ marginBottom: '1em', fontWeight: 'bold', fontStyle: 'italic' }}>
            {pageInfoMessage}
          </div>
          <TemplatesTable templatesObject={allTemplates} handleChange={this.handleChange} handleDelete={this.handleDelete} />
        </div>
        <FooterNavButtonGroup backButton={null} nextButton={this.pageValidation} />
      </div>
    );

which winds up being formatted like this:
image

The problem seems to hit with I have a /> or > on a line by themselves to close the tag.

I'm working on a minimal .vimrc, but figured I'd send this your way and see if it made sense.

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

No branches or pull requests

2 participants