Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

🐛Fix: incorrect indentation in vue/script-indent rule #503

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
michalsnik merged 4 commits into vuejs:master from ota-meshi:fix-script-indent
Sep 28, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
50 changes: 21 additions & 29 deletions lib/utils/indent-common.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ 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 @@ -245,21 +244,6 @@ 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 Down Expand Up @@ -562,33 +546,38 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
/**
* Calculate correct indentation of the line of the given tokens.
* @param {Token[]} tokens Tokens which are on the same line.
* @returns {number} Correct indentation. If it failed to calculate then `Number.MAX_SAFE_INTEGER`.
* @returns {object|null} Correct indentation. If it failed to calculate then `null`.
*/
function getExpectedIndent (tokens) {
const trivial = isTrivialToken(tokens[0])
let expectedIndent = Number.MAX_SAFE_INTEGER
function getExpectedIndents (tokens) {
const expectedIndents = []

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

// If the first token is not trivial then ignore trivial following tokens.
Copy link
Member

@michalsnik michalsnik Sep 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this comment is not relevant anymore

Copy link

@euclid1990 euclid1990 Sep 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@michalsnik I have a question, If this PR is merged, how can I install latest eslint-plugin-vue with fixed code ?

if (offsetInfo != null && (trivial || !isTrivialToken(token))) {
if (offsetInfo != null) {
if (offsetInfo.expectedIndent != null) {
expectedIndent = Math.min(expectedIndent, offsetInfo.expectedIndent)
expectedIndents.push(offsetInfo.expectedIndent)
} else {
const baseOffsetInfo = offsets.get(offsetInfo.baseToken)
if (baseOffsetInfo != null && baseOffsetInfo.expectedIndent != null && (i === 0 || !baseOffsetInfo.baseline)) {
expectedIndent = Math.min(expectedIndent, baseOffsetInfo.expectedIndent + offsetInfo.offset * options.indentSize)
expectedIndents.push(baseOffsetInfo.expectedIndent + offsetInfo.offset * options.indentSize)
if (baseOffsetInfo.baseline) {
break
}
}
}
}
}
if (!expectedIndents.length) {
return null
}

return expectedIndent
return {
expectedIndent: expectedIndents[0],
expectedBaseIndent: expectedIndents.reduce((a, b) => Math.min(a, b))
}
}

/**
Expand Down Expand Up @@ -744,11 +733,14 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
// Calculate and save expected indentation.
const firstToken = tokens[0]
const actualIndent = firstToken.loc.start.column
const expectedIndent = getExpectedIndent(tokens)
if (expectedIndent === Number.MAX_SAFE_INTEGER) {
const expectedIndents = getExpectedIndents(tokens)
if (!expectedIndents) {
return
}

const expectedBaseIndent = expectedIndents.expectedBaseIndent
const expectedIndent = expectedIndents.expectedIndent

// Debug log
// console.log('line', firstToken.loc.start.line, '=', { actualIndent, expectedIndent }, 'from:')
// for (const token of tokens) {
Expand All @@ -771,11 +763,11 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
if (offsetInfo.baseline) {
// This is a baseline token, so the expected indent is the column of this token.
if (options.indentChar === ' ') {
offsetInfo.expectedIndent = Math.max(0, token.loc.start.column + expectedIndent - actualIndent)
offsetInfo.expectedIndent = Math.max(0, token.loc.start.column + expectedBaseIndent - actualIndent)
} else {
// In hard-tabs mode, it cannot align tokens strictly, so use one additional offset.
// But the additional offset isn't needed if it's at the beginning of the line.
offsetInfo.expectedIndent = expectedIndent + (token === tokens[0] ? 0 : 1)
offsetInfo.expectedIndent = expectedBaseIndent + (token === tokens[0] ? 0 : 1)
}
baseline.add(token)
} else if (baseline.has(offsetInfo.baseToken)) {
Expand All @@ -784,7 +776,7 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
baseline.add(token)
} else {
// Otherwise, set the expected indent of this line.
offsetInfo.expectedIndent = expectedIndent
offsetInfo.expectedIndent = expectedBaseIndent
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/script-indent/array-expression-03.vue
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!--{}-->
<script>
var v = [() =>
1
,b]
</script>
6 changes: 6 additions & 0 deletions tests/fixtures/script-indent/array-expression-04.vue
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!--{}-->
<script>
var v = [() =>
1
,]
</script>
Loading

AltStyle によって変換されたページ (->オリジナル) /