Skip to content

Commit 02970ac

Browse files
committed
Fix: html-indent about binary expressions (fixes #264)
1 parent 1ac12f5 commit 02970ac

File tree

2 files changed

+81
-15
lines changed

2 files changed

+81
-15
lines changed

lib/rules/html-indent.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,31 @@ function create (context) {
387387
return template.getFirstToken(node)
388388
}
389389

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+
390415
/**
391416
* Ignore all tokens of the given node.
392417
* @param {Node} node The node to ignore.
@@ -722,8 +747,14 @@ function create (context) {
722747
const leftToken = getChainHeadToken(node)
723748
const opToken = template.getTokenAfter(node.left, isNotRightParen)
724749
const rightToken = template.getTokenAfter(opToken)
725-
726-
setOffset([opToken, rightToken], 1, leftToken)
750+
const prevToken = template.getTokenBefore(leftToken)
751+
const shouldIndent = (
752+
prevToken == null ||
753+
prevToken.loc.end.line === leftToken.loc.start.line ||
754+
isFirstOfStatement(leftToken, node)
755+
)
756+
757+
setOffset([opToken, rightToken], shouldIndent ? 1 : 0, leftToken)
727758
},
728759

729760
'AwaitExpression, RestElement, SpreadElement, UnaryExpression' (node) {

tests/lib/rules/html-indent.js

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,10 @@ tester.run('html-indent', rule, {
255255
a
256256
=
257257
b
258-
+
259-
c
260-
+
261-
d
258+
+
259+
c
260+
+
261+
d
262262
"
263263
></div>
264264
</template>
@@ -1307,7 +1307,42 @@ tester.run('html-indent', rule, {
13071307
// Ignore all :D
13081308
ignores: ['*']
13091309
}]
1310-
}
1310+
},
1311+
1312+
// https://github.com/vuejs/eslint-plugin-vue/issues/264
1313+
unIndent`
1314+
<template>
1315+
<div
1316+
:class="{
1317+
foo: (
1318+
a === b &&
1319+
c === d
1320+
)
1321+
}"
1322+
/>
1323+
</template>
1324+
`,
1325+
unIndent`
1326+
<template>
1327+
<div
1328+
:class="{
1329+
foo:
1330+
a === b &&
1331+
c === d
1332+
}"
1333+
/>
1334+
</template>
1335+
`,
1336+
unIndent`
1337+
<template>
1338+
<div
1339+
:class="{
1340+
foo: a === b &&
1341+
c === d
1342+
}"
1343+
/>
1344+
</template>
1345+
`
13111346
],
13121347

13131348
invalid: [
@@ -1779,10 +1814,10 @@ tester.run('html-indent', rule, {
17791814
a
17801815
=
17811816
b
1782-
+
1783-
c
1784-
+
1785-
d
1817+
+
1818+
c
1819+
+
1820+
d
17861821
"
17871822
></div>
17881823
</template>
@@ -1800,10 +1835,10 @@ tester.run('html-indent', rule, {
18001835
{ message: 'Expected indentation of 12 spaces but found 10 spaces.', line: 16 },
18011836
{ message: 'Expected indentation of 16 spaces but found 10 spaces.', line: 17 },
18021837
{ message: 'Expected indentation of 16 spaces but found 10 spaces.', line: 18 },
1803-
{ message: 'Expected indentation of 20 spaces but found 10 spaces.', line: 19 },
1804-
{ message: 'Expected indentation of 20 spaces but found 10 spaces.', line: 20 },
1805-
{ message: 'Expected indentation of 20 spaces but found 10 spaces.', line: 21 },
1806-
{ message: 'Expected indentation of 20 spaces but found 10 spaces.', line: 22 }
1838+
{ message: 'Expected indentation of 16 spaces but found 10 spaces.', line: 19 },
1839+
{ message: 'Expected indentation of 16 spaces but found 10 spaces.', line: 20 },
1840+
{ message: 'Expected indentation of 16 spaces but found 10 spaces.', line: 21 },
1841+
{ message: 'Expected indentation of 16 spaces but found 10 spaces.', line: 22 }
18071842
]
18081843
},
18091844

0 commit comments

Comments
 (0)