Skip to content

Commit 39de540

Browse files
mysticateamichalsnik
authored andcommitted
Fix: indent bug about semicolons
1 parent fb40b9a commit 39de540

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

lib/utils/indent-common.js

+18-7
Original file line numberDiff line numberDiff line change
@@ -1419,14 +1419,25 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
14191419
// Process semicolons.
14201420
':statement' (node) {
14211421
const info = offsets.get(tokenStore.getFirstToken(node))
1422-
const prevToken = tokenStore.getTokenBefore(node)
1423-
const lastToken = tokenStore.getLastToken(node)
1424-
1425-
if (info != null && isSemicolon(prevToken)) {
1426-
offsets.set(prevToken, info)
1422+
if (info == null) {
1423+
return
14271424
}
1428-
if (info != null && isSemicolon(lastToken)) {
1429-
offsets.set(lastToken, info)
1425+
1426+
// Set to the semicolon of the previous token for semicolon-free style.
1427+
// E.g.,
1428+
// foo
1429+
// ;[1,2,3].forEach(f)
1430+
const tokens = [
1431+
tokenStore.getTokenBefore(node),
1432+
tokenStore.getLastToken(node)
1433+
].filter(isSemicolon)
1434+
1435+
// Set offsets if the semicolon is at beginning of line.
1436+
for (const token of tokens) {
1437+
const prevToken = tokenStore.getTokenBefore(token)
1438+
if (prevToken == null || token.loc.end.line !== prevToken.loc.start.line) {
1439+
offsets.set(token, info)
1440+
}
14301441
}
14311442
},
14321443

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*{}*/
2+
if (a)
3+
b;
4+
c
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*{}*/
2+
function wrap() {
3+
if (x)
4+
this.doSomething();
5+
foo
6+
}

0 commit comments

Comments
 (0)