summaryrefslogtreecommitdiff
path: root/test/manual/indent/jsx.jsx
blob: c200979df8cc3fdb545b7cef0aa77e0c6bb8bad5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
var foo = <div></div>;

return (
  <div>
  </div>
  <div>
    <div></div>
    <div>
      <div></div>
    </div>
  </div>
);

React.render(
  <div>
    <div></div>
  </div>,
  {
    a: 1
  },
  <div>
    <div></div>
  </div>
);

return (
  // Sneaky!
  <div></div>
);

return (
  <div></div>
  // Sneaky!
);

React.render(
  <input
  />,
  {
    a: 1
  }
);

return (
  <div>
    {array.map(function () {
      return {
        a: 1
      };
    })}
  </div>
);

return (
  <div attribute={array.map(function () {
         return {
           a: 1
         };

         return {
           a: 1
         };

         return {
           a: 1
         };
       })}>
  </div>
);

return (
  <div attribute={{
         a: 1, // Indent relative to “attribute” column.
         b: 2
       } && {  // Dedent to “attribute” column.
         a: 1,
         b: 2
       }} />   // Also dedent.
);

return (
  <div attribute=
       {   // Indent properly on another line, too.
         {
           a: 1,
           b: 2,
         } && (
           // Indent other forms, too.
           a ? b :
             c ? d :
             e
         )
       } />
)

// JSXMemberExpression names are parsed/indented:
<Foo.Bar>
  <div>
    <Foo.Bar>
      Hello World!
    </Foo.Bar>
    <Foo.Bar>
      <div>
      </div>
    </Foo.Bar>
  </div>
</Foo.Bar>

// JSXOpeningFragment and JSXClosingFragment are parsed/indented:
<>
  <div>
    <>
      Hello World!
    </>
    <>
      <div>
      </div>
    </>
  </div>
</>

// Indent void expressions (no need for contextual parens / commas)
// (https://github.com/mooz/js2-mode/issues/140#issuecomment-166250016).
<div className="class-name">
  <h2>Title</h2>
  {array.map(() => {
    return <Element />;
  })}
  {message}
</div>
// Another example of above issue
// (https://github.com/mooz/js2-mode/issues/490).
<App>
  <div>
    {variable1}
    <Component/>
  </div>
</App>

// Comments and arrows can break indentation (Bug#24896 /
// https://github.com/mooz/js2-mode/issues/389).
const Component = props => (
  <FatArrow a={e => c}
            b={123}>
  </FatArrow>
);
const Component = props => (
  <NoFatArrow a={123}
              b={123}>
  </NoFatArrow>
);
const Component = props => ( // Parse this comment, please.
  <FatArrow a={e => c}
            b={123}>
  </FatArrow>
);
const Component = props => ( // Parse this comment, please.
  <NoFatArrow a={123}
              b={123}>
  </NoFatArrow>
);
// Another example of above issue (Bug#30225).
class {
  render() {
    return (
      <select style={{paddingRight: "10px"}}
              onChange={e => this.setState({value: e.target.value})}
              value={this.state.value}>
        <option>Hi</option>
      </select>
    );
  }
}

// JSX attributes of an arrow function’s expression body’s JSX
// expression should be indented with respect to the JSX opening
// element (Bug#26001 /
// https://github.com/mooz/js2-mode/issues/389#issuecomment-271869380).
class {
  render() {
    const messages = this.state.messages.map(
      message => <Message key={message.id}
                          text={message.text}
                          mine={message.mine} />
    );    return messages;
  }
  render() {
    const messages = this.state.messages.map(message =>
      <Message key={message.timestamp}
               text={message.text}
               mine={message.mine} />
    );    return messages;
  }
}

// Users expect tag closers to align with the tag’s start; this is the
// style used in the React docs, so it should be the default.
// - https://github.com/mooz/js2-mode/issues/389#issuecomment-390766873
// - https://github.com/mooz/js2-mode/issues/482
// - Bug#32158
const foo = (props) => (
  <div>
    <input
      cat={i => i}
    />
    <button
      className="square"
    >
      {this.state.value}
    </button>
  </div>
);

// Embedded JSX in parens breaks indentation
// (https://github.com/mooz/js2-mode/issues/411).
let a = (
  <div>
    {condition && <Component/>}
    {condition && <Component/>}
    <div/>
  </div>
)
let b = (
  <div>
    {condition && (<Component/>)}
    <div/>
  </div>
)
let c = (
  <div>
    {condition && (<Component/>)}
    {condition && "something"}
  </div>
)
let d = (
  <div>
    {(<Component/>)}
    {condition && "something"}
  </div>
)
// Another example of the above issue (Bug#27000).
function testA() {
  return (
    <div>
      <div> { ( <div/> ) } </div>
    </div>
  );
}
function testB() {
  return (
    <div>
      <div> { <div/> } </div>
    </div>
  );
}
// Another example of the above issue
// (https://github.com/mooz/js2-mode/issues/451).
class Classy extends React.Component {
  render () {
    return (
      <div>
        <ul className="tocListRoot">
          { this.state.list.map((item) => {
            return (<div />)
          })}
        </ul>
      </div>
    )
  }
}

// Self-closing tags should be indented properly
// (https://github.com/mooz/js2-mode/issues/459).
export default ({ stars }) => (
  <div className='overlay__container'>
    <div className='overlay__header overlay--text'>
      Congratulations!
    </div>
    <div className='overlay__reward'>
      <Icon {...createIconProps(stars > 0)} size='large' />
      <div className='overlay__reward__bottom'>
        <Icon {...createIconProps(stars > 1)} size='small' />
        <Icon {...createIconProps(stars > 2)} size='small' />
      </div>
    </div>
    <div className='overlay__description overlay--text'>
      You have created <large>1</large> reminder
    </div>
  </div>
)

// JS expressions should not break indentation
// (https://github.com/mooz/js2-mode/issues/462).
//
// In the referenced issue, the user actually wanted indentation which
// was simply different than Emacs’ SGML attribute indentation.
// Nevertheless, his issue highlighted our inability to properly
// indent code with JSX inside JSXExpressionContainers inside JSX.
return (
  <Router>
    <Bar>
      <Route exact path="/foo"
             render={() => (
               <div>nothing</div>
             )} />
      <Route exact path="/bar" />
    </Bar>
  </Router>
)

// Local Variables:
// indent-tabs-mode: nil
// js-indent-level: 2
// End: