Skip to content

Commit 52afc17

Browse files
committed
Fixed an issue where the rules vue/script-indent unexpected error report whileforce using semicolon.
1 parent 3f88f28 commit 52afc17

File tree

7 files changed

+619
-29
lines changed

7 files changed

+619
-29
lines changed

lib/utils/indent-common.js

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ 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 = /^[(){}[\],;]$/
2221

2322
/**
2423
* Normalize options.
@@ -245,21 +244,6 @@ function isClosingToken (token) {
245244
)
246245
}
247246

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-
263247
/**
264248
* Creates AST event handlers for html-indent.
265249
*
@@ -562,33 +546,38 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
562546
/**
563547
* Calculate correct indentation of the line of the given tokens.
564548
* @param {Token[]} tokens Tokens which are on the same line.
565-
* @returns {number} Correct indentation. If it failed to calculate then `Number.MAX_SAFE_INTEGER`.
549+
* @returns {object|null} Correct indentation. If it failed to calculate then `null`.
566550
*/
567-
function getExpectedIndent (tokens) {
568-
const trivial = isTrivialToken(tokens[0])
569-
let expectedIndent = Number.MAX_SAFE_INTEGER
551+
function getExpectedIndents (tokens) {
552+
const expectedIndents = []
570553

571554
for (let i = 0; i < tokens.length; ++i) {
572555
const token = tokens[i]
573556
const offsetInfo = offsets.get(token)
574557

575558
// If the first token is not trivial then ignore trivial following tokens.
576-
if (offsetInfo != null && (trivial || !isTrivialToken(token))) {
559+
if (offsetInfo != null) {
577560
if (offsetInfo.expectedIndent != null) {
578-
expectedIndent = Math.min(expectedIndent, offsetInfo.expectedIndent)
561+
expectedIndents.push(offsetInfo.expectedIndent)
579562
} else {
580563
const baseOffsetInfo = offsets.get(offsetInfo.baseToken)
581564
if (baseOffsetInfo != null && baseOffsetInfo.expectedIndent != null && (i === 0 || !baseOffsetInfo.baseline)) {
582-
expectedIndent = Math.min(expectedIndent, baseOffsetInfo.expectedIndent + offsetInfo.offset * options.indentSize)
565+
expectedIndents.push(baseOffsetInfo.expectedIndent + offsetInfo.offset * options.indentSize)
583566
if (baseOffsetInfo.baseline) {
584567
break
585568
}
586569
}
587570
}
588571
}
589572
}
573+
if (!expectedIndents.length) {
574+
return null
575+
}
590576

591-
return expectedIndent
577+
return {
578+
expectedIndent: expectedIndents[0],
579+
expectedBaseIndent: expectedIndents.reduce((a, b) => Math.min(a, b))
580+
}
592581
}
593582

594583
/**
@@ -744,11 +733,14 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
744733
// Calculate and save expected indentation.
745734
const firstToken = tokens[0]
746735
const actualIndent = firstToken.loc.start.column
747-
const expectedIndent = getExpectedIndent(tokens)
748-
if (expectedIndent === Number.MAX_SAFE_INTEGER) {
736+
const expectedIndents = getExpectedIndents(tokens)
737+
if (!expectedIndents) {
749738
return
750739
}
751740

741+
const expectedBaseIndent = expectedIndents.expectedBaseIndent
742+
const expectedIndent = expectedIndents.expectedIndent
743+
752744
// Debug log
753745
// console.log('line', firstToken.loc.start.line, '=', { actualIndent, expectedIndent }, 'from:')
754746
// for (const token of tokens) {
@@ -771,11 +763,11 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
771763
if (offsetInfo.baseline) {
772764
// This is a baseline token, so the expected indent is the column of this token.
773765
if (options.indentChar === ' ') {
774-
offsetInfo.expectedIndent = Math.max(0, token.loc.start.column + expectedIndent - actualIndent)
766+
offsetInfo.expectedIndent = Math.max(0, token.loc.start.column + expectedBaseIndent - actualIndent)
775767
} else {
776768
// In hard-tabs mode, it cannot align tokens strictly, so use one additional offset.
777769
// But the additional offset isn't needed if it's at the beginning of the line.
778-
offsetInfo.expectedIndent = expectedIndent + (token === tokens[0] ? 0 : 1)
770+
offsetInfo.expectedIndent = expectedBaseIndent + (token === tokens[0] ? 0 : 1)
779771
}
780772
baseline.add(token)
781773
} else if (baseline.has(offsetInfo.baseToken)) {
@@ -784,7 +776,7 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
784776
baseline.add(token)
785777
} else {
786778
// Otherwise, set the expected indent of this line.
787-
offsetInfo.expectedIndent = expectedIndent
779+
offsetInfo.expectedIndent = expectedBaseIndent
788780
}
789781
}
790782
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!--{}-->
2+
<script>
3+
var v = [() =>
4+
1
5+
,b]
6+
</script>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!--{}-->
2+
<script>
3+
var v = [() =>
4+
1
5+
,]
6+
</script>

0 commit comments

Comments
 (0)