Skip to content

Fix: indent rules false positive (fixes #375) #395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions lib/utils/indent-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const KNOWN_NODES = new Set(['ArrayExpression', 'ArrayPattern', 'ArrowFunctionEx
const LT_CHAR = /[\r\n\u2028\u2029]/
const LINES = /[^\r\n\u2028\u2029]+(?:$|\r\n|[\r\n\u2028\u2029])/g
const BLOCK_COMMENT_PREFIX = /^\s*\*/
const TRIVIAL_PUNCTUATOR = /^[(){}[\],;]$/

/**
* Normalize options.
Expand Down Expand Up @@ -244,6 +245,21 @@ function isClosingToken (token) {
)
}

/**
* Check whether a given token is trivial or not.
* @param {Token} token The token to check.
* @returns {boolean} `true` if the token is trivial.
*/
function isTrivialToken (token) {
return token != null && (
(token.type === 'Punctuator' && TRIVIAL_PUNCTUATOR.test(token.value)) ||
token.type === 'HTMLTagOpen' ||
token.type === 'HTMLEndTagOpen' ||
token.type === 'HTMLTagClose' ||
token.type === 'HTMLSelfClosingTagClose'
)
}

/**
* Creates AST event handlers for html-indent.
*
Expand All @@ -262,6 +278,7 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
* @param {Token|Token[]} token The token to set.
* @param {number} offset The offset of the tokens.
* @param {Token} baseToken The token of the base offset.
* @param {boolean} [trivial=false] The flag for trivial tokens.
* @returns {void}
*/
function setOffset (token, offset, baseToken) {
Expand Down Expand Up @@ -352,9 +369,7 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
if (lastToken != null) {
t = lastToken
while ((t = tokenStore.getTokenAfter(t)) != null && t.range[1] <= elementTokens.firstToken.range[0]) {
if (lastToken.loc.end.line !== t.loc.start.line) {
alignTokens.push(t)
}
alignTokens.push(t)
}
}
alignTokens.push(elementTokens.firstToken)
Expand Down Expand Up @@ -550,13 +565,15 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
* @returns {number} Correct indentation. If it failed to calculate then `Number.MAX_SAFE_INTEGER`.
*/
function getExpectedIndent (tokens) {
const trivial = isTrivialToken(tokens[0])
let expectedIndent = Number.MAX_SAFE_INTEGER

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

if (offsetInfo != null) {
// If the first token is not trivial then ignore trivial following tokens.
if (offsetInfo != null && (trivial || !isTrivialToken(token))) {
if (offsetInfo.expectedIndent != null) {
expectedIndent = Math.min(expectedIndent, offsetInfo.expectedIndent)
} else {
Expand Down Expand Up @@ -1429,24 +1446,23 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
// Process semicolons.
':statement' (node) {
const info = offsets.get(tokenStore.getFirstToken(node))
const lastToken = tokenStore.getLastToken(node)
if (info == null) {
return
}
if (isSemicolon(lastToken)) {
offsets.set(lastToken, info)
}

// Set to the semicolon of the previous token for semicolon-free style.
// E.g.,
// foo
// ;[1,2,3].forEach(f)
const tokens = [
tokenStore.getTokenBefore(node),
tokenStore.getLastToken(node)
].filter(isSemicolon)

// Set offsets if the semicolon is at beginning of line.
for (const token of tokens) {
const prevToken = tokenStore.getTokenBefore(token)
if (prevToken == null || token.loc.end.line !== prevToken.loc.start.line) {
offsets.set(token, info)
const prevToken = tokenStore.getTokenBefore(node)
if (isSemicolon(prevToken)) {
const prevPrevToken = tokenStore.getTokenBefore(prevToken)
if (prevPrevToken == null || prevToken.loc.end.line !== prevPrevToken.loc.start.line) {
offsets.set(prevToken, info)
}
}
},
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/html-indent/v-start-tag-02.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!--{}-->
<template>
<div
aaa>
</template>
6 changes: 6 additions & 0 deletions tests/fixtures/html-indent/v-start-tag-03.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!--{}-->
<template>
<div
aaa
bbb>
</template>
7 changes: 7 additions & 0 deletions tests/fixtures/html-indent/v-start-tag-04.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!--{}-->
<template>
<div
aaa
bbb
>
</template>
5 changes: 5 additions & 0 deletions tests/fixtures/html-indent/v-start-tag-05.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!--{}-->
<template>
<div
aaa/>
</template>
5 changes: 5 additions & 0 deletions tests/fixtures/script-indent/array-expression-02.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!--{}-->
<script>
var a = [
1]
</script>
7 changes: 7 additions & 0 deletions tests/fixtures/script-indent/function-declaration-04.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!--{}-->
<script>
function foo(
a)
{
foo()}
</script>
5 changes: 5 additions & 0 deletions tests/fixtures/script-indent/object-expression-03.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!--{}-->
<script>
var obj = {
x: 1}
</script>