Skip to content

Commit c6d25af

Browse files
mysticateamichalsnik
authored andcommitted
Fix: indent rules false positive (fixes #375) (#395)
1 parent b33ef75 commit c6d25af

8 files changed

+70
-14
lines changed

Diff for: lib/utils/indent-common.js

+30-14
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const KNOWN_NODES = new Set(['ArrayExpression', 'ArrayPattern', 'ArrowFunctionEx
1818
const LT_CHAR = /[\r\n\u2028\u2029]/
1919
const LINES = /[^\r\n\u2028\u2029]+(?:$|\r\n|[\r\n\u2028\u2029])/g
2020
const BLOCK_COMMENT_PREFIX = /^\s*\*/
21+
const TRIVIAL_PUNCTUATOR = /^[(){}[\],;]$/
2122

2223
/**
2324
* Normalize options.
@@ -244,6 +245,21 @@ function isClosingToken (token) {
244245
)
245246
}
246247

248+
/**
249+
* Check whether a given token is trivial or not.
250+
* @param {Token} token The token to check.
251+
* @returns {boolean} `true` if the token is trivial.
252+
*/
253+
function isTrivialToken (token) {
254+
return token != null && (
255+
(token.type === 'Punctuator' && TRIVIAL_PUNCTUATOR.test(token.value)) ||
256+
token.type === 'HTMLTagOpen' ||
257+
token.type === 'HTMLEndTagOpen' ||
258+
token.type === 'HTMLTagClose' ||
259+
token.type === 'HTMLSelfClosingTagClose'
260+
)
261+
}
262+
247263
/**
248264
* Creates AST event handlers for html-indent.
249265
*
@@ -262,6 +278,7 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
262278
* @param {Token|Token[]} token The token to set.
263279
* @param {number} offset The offset of the tokens.
264280
* @param {Token} baseToken The token of the base offset.
281+
* @param {boolean} [trivial=false] The flag for trivial tokens.
265282
* @returns {void}
266283
*/
267284
function setOffset (token, offset, baseToken) {
@@ -352,9 +369,7 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
352369
if (lastToken != null) {
353370
t = lastToken
354371
while ((t = tokenStore.getTokenAfter(t)) != null && t.range[1] <= elementTokens.firstToken.range[0]) {
355-
if (lastToken.loc.end.line !== t.loc.start.line) {
356-
alignTokens.push(t)
357-
}
372+
alignTokens.push(t)
358373
}
359374
}
360375
alignTokens.push(elementTokens.firstToken)
@@ -550,13 +565,15 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
550565
* @returns {number} Correct indentation. If it failed to calculate then `Number.MAX_SAFE_INTEGER`.
551566
*/
552567
function getExpectedIndent (tokens) {
568+
const trivial = isTrivialToken(tokens[0])
553569
let expectedIndent = Number.MAX_SAFE_INTEGER
554570

555571
for (let i = 0; i < tokens.length; ++i) {
556572
const token = tokens[i]
557573
const offsetInfo = offsets.get(token)
558574

559-
if (offsetInfo != null) {
575+
// If the first token is not trivial then ignore trivial following tokens.
576+
if (offsetInfo != null && (trivial || !isTrivialToken(token))) {
560577
if (offsetInfo.expectedIndent != null) {
561578
expectedIndent = Math.min(expectedIndent, offsetInfo.expectedIndent)
562579
} else {
@@ -1429,24 +1446,23 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
14291446
// Process semicolons.
14301447
':statement' (node) {
14311448
const info = offsets.get(tokenStore.getFirstToken(node))
1449+
const lastToken = tokenStore.getLastToken(node)
14321450
if (info == null) {
14331451
return
14341452
}
1453+
if (isSemicolon(lastToken)) {
1454+
offsets.set(lastToken, info)
1455+
}
14351456

14361457
// Set to the semicolon of the previous token for semicolon-free style.
14371458
// E.g.,
14381459
// foo
14391460
// ;[1,2,3].forEach(f)
1440-
const tokens = [
1441-
tokenStore.getTokenBefore(node),
1442-
tokenStore.getLastToken(node)
1443-
].filter(isSemicolon)
1444-
1445-
// Set offsets if the semicolon is at beginning of line.
1446-
for (const token of tokens) {
1447-
const prevToken = tokenStore.getTokenBefore(token)
1448-
if (prevToken == null || token.loc.end.line !== prevToken.loc.start.line) {
1449-
offsets.set(token, info)
1461+
const prevToken = tokenStore.getTokenBefore(node)
1462+
if (isSemicolon(prevToken)) {
1463+
const prevPrevToken = tokenStore.getTokenBefore(prevToken)
1464+
if (prevPrevToken == null || prevToken.loc.end.line !== prevPrevToken.loc.start.line) {
1465+
offsets.set(prevToken, info)
14501466
}
14511467
}
14521468
},

Diff for: tests/fixtures/html-indent/v-start-tag-02.vue

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!--{}-->
2+
<template>
3+
<div
4+
aaa>
5+
</template>

Diff for: tests/fixtures/html-indent/v-start-tag-03.vue

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!--{}-->
2+
<template>
3+
<div
4+
aaa
5+
bbb>
6+
</template>

Diff for: tests/fixtures/html-indent/v-start-tag-04.vue

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!--{}-->
2+
<template>
3+
<div
4+
aaa
5+
bbb
6+
>
7+
</template>

Diff for: tests/fixtures/html-indent/v-start-tag-05.vue

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!--{}-->
2+
<template>
3+
<div
4+
aaa/>
5+
</template>

Diff for: tests/fixtures/script-indent/array-expression-02.vue

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!--{}-->
2+
<script>
3+
var a = [
4+
1]
5+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!--{}-->
2+
<script>
3+
function foo(
4+
a)
5+
{
6+
foo()}
7+
</script>
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!--{}-->
2+
<script>
3+
var obj = {
4+
x: 1}
5+
</script>

0 commit comments

Comments
 (0)