8000 Fix: html-indent about binary expressions (fixes #264) · vuejs/eslint-plugin-vue@c3587b3 · GitHub
[go: up one dir, main page]

Skip to content

Commit c3587b3

Browse files
committed
Fix: html-indent about binary expressions (fixes #264)
1 parent 98c9d1d commit c3587b3

File tree

2 files changed

+80
-14
lines changed

2 files changed

+80
-14
lines changed

lib/rules/html-indent.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,31 @@ function create (context) {
407407
return template.getFirstToken(node)
408408
}
409409

410+
/**
411+
* Check whether a given token is the first token of a statement.
412+
* @param {Token} token The token to check.
413+
* @param {Node} belongingNode The node that the token is belonging to.
414+
* @returns {boolean} `true` if the token is the first token of a statement.
415+
*/
416+
function isFirstOfStatement (token, belongingNode) {
417+
let node = belongingNode
418+
419+
while (node != null) {
420+
const parent = node.parent
421+
const t = parent && parent.type
422+
if (t != null && (t.endsWith('Statement') || t.endsWith('Declaration'))) {
423+
return parent.range[0] === token.range[0]
424+
}
425+
if (t === 'VExpressionContainer') {
426+
return node.range[0] === token.range[0]
427+
}
428+
429+
node = parent
430+
}
431+
432+
return false
433+
}
434+
410435
/**
411436
* Ignore all tokens of the given node.
412437
* @param {Node} node The node to ignore.
@@ -752,8 +777,14 @@ function create (context) {
752777
const leftToken = getChainHeadToken(node)
753778
const opToken = template.getTokenAfter(node.left, isNotRightParen)
754779
const rightToken = template.getTokenAfter(opToken)
780+
const prevToken = template.getTokenBefore(leftToken)
781+
const shouldIndent = (
782+
prevToken == null ||
783+
prevToken.loc.end.line === leftToken.loc.start.line ||
784+
isFirstOfStatement(leftToken, node)
785+
)
755786

756-
setOffset([opToken, rightToken], 1, leftToken)
787+
setOffset([opToken, rightToken], shouldIndent ? 1 : 0, leftToken)
757788
},
758789

759790
'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>
@@ -1347,7 +1347,42 @@ tester.run('html-indent', rule, {
13471347
// Ignore all :D
13481348
ignores: ['*']
13491349
}]
1350-
}
1350+
},
1351+
1352+
// https://github.com/vuejs/eslint-plugin-vue/issues/264
1353+
unIndent`
1354+
<template>
1355+
<div
1356+
:class="{
1357+
foo: (
1358+
a === b &&
1359+
c === d
1360+
)
1361+
}"
1362+
/>
1363+
</template>
1364+
`,
1365+
unIndent`
1366+
<template>
1367+
<div
1368+
:class="{
1369+
foo:
1370+
a === b &&
1371+
c === d
1372+
}"
1373+
/>
1374+
</template>
1375+
`,
1376+
unIndent`
1377+
<template>
1378+
<div
1379+
:class="{
1380+
foo: a === b &&
1381+
c === d
1382+
}"
1383+
/>
1384+
</template>
1385+
`
13511386
],
13521387

13531388
invalid: [
@@ -1819,10 +1854,10 @@ tester.run('html-indent', rule, {
18191854
a
18201855
=
18211856
b
1822-
+
1823-
c
1824-
+
1825-
d
1857+
+
1858+
c
1859+
+
1860+
d
18261861
"
18271862
></div>
18281863
</template>
@@ -1840,10 +1875,10 @@ tester.run('html-indent', rule, {
18401875
{ message: 'Expected indentation of 12 spaces but found 10 spaces.', line: 16 },
18411876
{ message: 'Expected indentation of 16 spaces but found 10 spaces.', line: 17 },
18421877
{ message: 'Expected indentation of 16 spaces but found 10 spaces.', line: 18 },
1843-
{ message: 'Expected indentation of 20 spaces but found 10 spaces.', line: 19 },
1844-
{ message: 'Expected indentation of 20 spaces but found 10 spaces.', line: 20 },
1845-
{ message: 'Expected indentation of 20 spaces but found 10 spaces.', line: 21 },
1846-
{ message: 'Expected indentation of 20 spaces but found 10 spaces.', line: 22 }
1878+
{ message: 'Expected indentation of 16 spaces but found 10 spaces.', line: 19 },
1879+
{ message: 'Expected indentation of 16 spaces but found 10 spaces.', line: 20 },
1880+
{ message: 'Expected indentation of 16 spaces but found 10 spaces.', line: 21 },
1881+
{ message: 'Expected indentation of 16 spaces but found 10 spaces.', line: 22 }
18471882
]
18481883
},
18491884

0 commit comments

Comments
 (0)
0