Skip to content

Commit 6bb6f69

Browse files
mysticateamichalsnik
authored andcommitted
Fix: wrong indentation (fixes #407) (#413)
1 parent 12054ea commit 6bb6f69

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

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

+10-12
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,9 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
10241024
ExportAllDeclaration (node) {
10251025
const tokens = tokenStore.getTokens(node)
10261026
const firstToken = tokens.shift()
1027+
if (isSemicolon(last(tokens))) {
1028+
tokens.pop()
1029+
}
10271030
setOffset(tokens, 1, firstToken)
10281031
},
10291032

@@ -1445,25 +1448,20 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
14451448

14461449
// Process semicolons.
14471450
':statement' (node) {
1448-
const info = offsets.get(tokenStore.getFirstToken(node))
1451+
const firstToken = tokenStore.getFirstToken(node)
14491452
const lastToken = tokenStore.getLastToken(node)
1450-
if (info == null) {
1451-
return
1452-
}
1453-
if (isSemicolon(lastToken)) {
1454-
offsets.set(lastToken, info)
1453+
if (isSemicolon(lastToken) && firstToken !== lastToken) {
1454+
setOffset(lastToken, 0, firstToken)
14551455
}
14561456

14571457
// Set to the semicolon of the previous token for semicolon-free style.
14581458
// E.g.,
14591459
// foo
14601460
// ;[1,2,3].forEach(f)
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)
1466-
}
1461+
const info = offsets.get(firstToken)
1462+
const prevToken = tokenStore.getTokenBefore(firstToken)
1463+
if (info != null && isSemicolon(prevToken) && prevToken.loc.end.line === firstToken.loc.start.line) {
1464+
offsets.set(prevToken, info)
14671465
}
14681466
},
14691467

Diff for: tests/fixtures/script-indent/export-named-declaration-04.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<script>
33
export
44
const a = 0
5-
;
5+
;
66
export
77
const
88
b

Diff for: tests/fixtures/script-indent/issue407.vue

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!--{"options":[4,{"baseIndent":1,"switchCase":2}]}-->
2+
<script>
3+
function test() {
4+
const variable = 0;
5+
6+
if (variable === 0) {
7+
console.info('wrong indentation reported');
8+
}
9+
}
10+
11+
test();
12+
</script>

0 commit comments

Comments
 (0)