@@ -198,6 +198,47 @@ function isBeginningOfLine (node, index, nodes) {
198
198
return false
199
199
}
200
200
201
+ /**
202
+ * Check whether a given token is the first token of:
203
+ *
204
+ * - ExpressionStatement
205
+ * - VExpressionContainer
206
+ * - A parameter of CallExpression/NewExpression
207
+ * - An element of ArrayExpression
208
+ * - An expression of SequenceExpression
209
+ *
210
+ * @param {Token } token The token to check.
211
+ * @param {Node } belongingNode The node that the token is belonging to.
212
+ * @returns {boolean } `true` if the token is the first token of an element.
213
+ */
214
+ function isBeginningOfElement ( token , belongingNode ) {
215
+ let node = belongingNode
216
+
217
+ while ( node != null ) {
218
+ const parent = node . parent
219
+ const t = parent && parent . type
220
+ if ( t != null && ( t . endsWith ( 'Statement' ) || t . endsWith ( 'Declaration' ) ) ) {
221
+ return parent . range [ 0 ] === token . range [ 0 ]
222
+ }
223
+ if ( t === 'VExpressionContainer' ) {
224
+ return node . range [ 0 ] === token . range [ 0 ]
225
+ }
226
+ if ( t === 'CallExpression' || t === 'NewExpression' ) {
227
+ return parent . arguments . some ( param => param . range [ 0 ] === token . range [ 0 ] )
228
+ }
229
+ if ( t === 'ArrayExpression' ) {
230
+ return parent . elements . some ( element => element != null && element . range [ 0 ] === token . range [ 0 ] )
231
+ }
232
+ if ( t === 'SequenceExpression' ) {
233
+ return parent . expressions . some ( expr => expr . range [ 0 ] === token . range [ 0 ] )
234
+ }
235
+
236
+ node = parent
237
+ }
238
+
239
+ return false
240
+ }
241
+
201
242
/**
202
243
* Creates AST event handlers for html-indent.
203
244
*
@@ -387,31 +428,6 @@ function create (context) {
387
428
return template . getFirstToken ( node )
388
429
}
389
430
390
- /**
391
- * Check whether a given token is the first token of a statement.
392
- * @param {Token } token The token to check.
393
- * @param {Node } belongingNode The node that the token is belonging to.
394
- * @returns {boolean } `true` if the token is the first token of a statement.
395
- */
396
- function isFirstOfStatement ( token , belongingNode ) {
397
- let node = belongingNode
398
-
399
- while ( node != null ) {
400
- const parent = node . parent
401
- const t = parent && parent . type
402
- if ( t != null && ( t . endsWith ( 'Statement' ) || t . endsWith ( 'Declaration' ) ) ) {
403
- return parent . range [ 0 ] === token . range [ 0 ]
404
- }
405
- if ( t === 'VExpressionContainer' ) {
406
- return node . range [ 0 ] === token . range [ 0 ]
407
- }
408
-
409
- node = parent
410
- }
411
-
412
- return false
413
- }
414
-
415
431
/**
416
432
* Ignore all tokens of the given node.
417
433
* @param {Node } node The node to ignore.
@@ -751,7 +767,7 @@ function create (context) {
751
767
const shouldIndent = (
752
768
prevToken == null ||
753
769
prevToken . loc . end . line === leftToken . loc . start . line ||
754
- isFirstOfStatement ( leftToken , node )
770
+ isBeginningOfElement ( leftToken , node )
755
771
)
756
772
757
773
setOffset ( [ opToken , rightToken ] , shouldIndent ? 1 : 0 , leftToken )
0 commit comments