Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4e7d0d8

Browse files
committedDec 7, 2017
update logic
1 parent 02970ac commit 4e7d0d8

File tree

2 files changed

+100
-26
lines changed

2 files changed

+100
-26
lines changed
 

‎lib/rules/html-indent.js

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,47 @@ function isBeginningOfLine (node, index, nodes) {
198198
return false
199199
}
200200

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+
201242
/**
202243
* Creates AST event handlers for html-indent.
203244
*
@@ -387,31 +428,6 @@ function create (context) {
387428
return template.getFirstToken(node)
388429
}
389430

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-
415431
/**
416432
* Ignore all tokens of the given node.
417433
* @param {Node} node The node to ignore.
@@ -751,7 +767,7 @@ function create (context) {
751767
const shouldIndent = (
752768
prevToken == null ||
753769
prevToken.loc.end.line === leftToken.loc.start.line ||
754-
isFirstOfStatement(leftToken, node)
770+
isBeginningOfElement(leftToken, node)
755771
)
756772

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

‎tests/lib/rules/html-indent.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,64 @@ 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>
13451403
`
13461404
],
13471405

0 commit comments

Comments
 (0)
Please sign in to comment.