Skip to content

Commit b67c107

Browse files
committed
update logic
1 parent 02970ac commit b67c107

File tree

2 files changed

+113
-4
lines changed

2 files changed

+113
-4
lines changed

lib/rules/html-indent.js

+28-4
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,19 @@ function create (context) {
388388
}
389389

390390
/**
391-
* Check whether a given token is the first token of a statement.
391+
* Check whether a given token is the first token of:
392+
*
393+
* - ExpressionStatement
394+
* - VExpressionContainer
395+
* - A parameter of CallExpression/NewExpression
396+
* - An element of ArrayExpression
397+
* - An expression of SequenceExpression
398+
*
392399
* @param {Token} token The token to check.
393400
* @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.
401+
* @returns {boolean} `true` if the token is the first token of an element.
395402
*/
396-
function isFirstOfStatement (token, belongingNode) {
403+
function isBeginningOfElement (token, belongingNode) {
397404
let node = belongingNode
398405

399406
while (node != null) {
@@ -405,6 +412,23 @@ function create (context) {
405412
if (t === 'VExpressionContainer') {
406413
return node.range[0] === token.range[0]
407414
}
415+
if (t === 'CallExpression' || t === 'NewExpression') {
416+
const openParen = template.getTokenAfter(parent.callee, isNotRightParen)
417+
return parent.arguments.some(param =>
418+
getFirstAndLastTokens(param, openParen.range[1]).firstToken.range[0] === token.range[0]
419+
)
420+
}
421+
if (t === 'ArrayExpression') {
422+
return parent.elements.some(element =>
423+
element != null &&
424+
getFirstAndLastTokens(element).firstToken.range[0] === token.range[0]
425+
)
426+
}
427+
if (t === 'SequenceExpression') {
428+
return parent.expressions.some(expr =>
429+
getFirstAndLastTokens(expr).firstToken.range[0] === token.range[0]
430+
)
431+
}
408432

409433
node = parent
410434
}
@@ -751,7 +775,7 @@ function create (context) {
751775
const shouldIndent = (
752776
prevToken == null ||
753777
prevToken.loc.end.line === leftToken.loc.start.line ||
754-
isFirstOfStatement(leftToken, node)
778+
isBeginningOfElement(leftToken, node)
755779
)
756780

757781
setOffset([opToken, rightToken], shouldIndent ? 1 : 0, leftToken)

tests/lib/rules/html-indent.js

+85
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,91 @@ tester.run('html-indent', rule, {
13421342
}"
13431343
/>
13441344
</template>
1345+
`,
1346+
unIndent`
1347+
<template>
1348+
<div
1349+
:class="
1350+
[
1351+
a
1352+
+
1353+
b,
1354+
c
1355+
+
1356+
d
1357+
]
1358+
"
1359+
/>
1360+
</template>
1361+
`,
1362+
unIndent`
1363+
<template>
1364+
<div
1365+
:class="
1366+
foo(
1367+
a
1368+
+
1369+
b,
1370+
c
1371+
+
1372+
d
1373+
)
1374+
"
1375+
/>
1376+
<div
1377+
:class="
1378+
new Foo(
1379+
a
1380+
+
1381+
b,
1382+
c
1383+
+
1384+
d
1385+
)
1386+
"
1387+
/>
1388+
</template>
1389+
`,
1390+
unIndent`
1391+
<template>
1392+
<div
1393+
:class="
1394+
a
1395+
+
1396+
b,
1397+
c
1398+
+
1399+
d
1400+
"
1401+
/>
1402+
</template>
1403+
`,
1404+
unIndent`
1405+
<template>
1406+
<div
1407+
:class="
1408+
foo(
1409+
a
1410+
+
1411+
b
1412+
)
1413+
"
1414+
/>
1415+
<div
1416+
:class="
1417+
foo(
1418+
(
1419+
a +
1420+
b
1421+
),
1422+
(
1423+
c +
1424+
d
1425+
)
1426+
)
1427+
"
1428+
/>
1429+
</template>
13451430
`
13461431
],
13471432

0 commit comments

Comments
 (0)