- 
 
 - 
  Notifications
 
You must be signed in to change notification settings  - Fork 695
 
 🐛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
 
 
 
 
  Merged
 Changes from 1 commit
 Commits
 
 
 Show all changes
 
 
 4 commits
 
 
 Select commit
 Hold shift + click to select a range
 
 
 File filter
Filter by extension
Conversations
 Failed to load comments. 
 
 
 
  Loading
 
 Jump to
 
 Jump to file
 
 
 
 Failed to load files. 
 
 
 
  Loading
 
 Diff view
Diff view
There are no files selected for viewing
 
 
 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
 Learn more about bidirectional Unicode characters
 
 
 
 
 | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
 
 
 
  | 
 @@ -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. | ||
| 
 
 
 
  | 
 @@ -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. | ||
| * | ||
| 
 
 
 
  | 
 @@ -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. | ||
  
  | 
||
| 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)) | ||
| } | ||
| } | ||
| 
  | 
||
| /** | ||
| 
 
 
 
  | 
 @@ -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) { | ||
| 
 
 
 
  | 
 @@ -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)) { | ||
| 
 
 
 
  | 
 @@ -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 | ||
| } | ||
| } | ||
| } | ||
| 
 
 
 
  | 
 
 ||
 
 
 
 6 changes: 6 additions & 0 deletions
 
 
 
 tests/fixtures/script-indent/array-expression-03.vue
 
 
 
 
  
 
 
 
 
 
 
 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
 Learn more about bidirectional Unicode characters
 
 
 
 
 | 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
 
 
 
 
  
 
 
 
 
 
 
 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
 Learn more about bidirectional Unicode characters
 
 
 
 
 | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <!--{}--> | ||
| <script> | ||
| var v = [() => | ||
| 1 | ||
| ,] | ||
| </script> | 
 
 Oops, something went wrong.
 
 
 
 Add this suggestion to a batch that can be applied as a single commit.
 This suggestion is invalid because no changes were made to the code.
 Suggestions cannot be applied while the pull request is closed.
 Suggestions cannot be applied while viewing a subset of changes.
 Only one suggestion per line can be applied in a batch.
 Add this suggestion to a batch that can be applied as a single commit.
 Applying suggestions on deleted lines is not supported.
 You must change the existing code in this line in order to create a valid suggestion.
 Outdated suggestions cannot be applied.
 This suggestion has been applied or marked resolved.
 Suggestions cannot be applied from pending reviews.
 Suggestions cannot be applied on multi-line comments.
 Suggestions cannot be applied while the pull request is queued to merge.
 Suggestion cannot be applied right now. Please check back later.