From 52afc1734681c4452b2c01431306fa5d24056622 Mon Sep 17 00:00:00 2001 From: ota-meshi Date: Fri, 22 Jun 2018 21:56:19 +0900 Subject: [PATCH 1/3] Fixed an issue where the rules `vue/script-indent` unexpected error report whileforce using semicolon. --- lib/utils/indent-common.js | 50 +- .../script-indent/array-expression-03.vue | 6 + .../script-indent/array-expression-04.vue | 6 + .../script-indent/indent-valid-fixture-01.vue | 541 ++++++++++++++++++ tests/fixtures/script-indent/issue441.vue | 10 + tests/fixtures/script-indent/issue443.vue | 27 + .../script-indent/sequence-expression-04.vue | 8 + 7 files changed, 619 insertions(+), 29 deletions(-) create mode 100644 tests/fixtures/script-indent/array-expression-03.vue create mode 100644 tests/fixtures/script-indent/array-expression-04.vue create mode 100644 tests/fixtures/script-indent/indent-valid-fixture-01.vue create mode 100644 tests/fixtures/script-indent/issue441.vue create mode 100644 tests/fixtures/script-indent/issue443.vue create mode 100644 tests/fixtures/script-indent/sequence-expression-04.vue diff --git a/lib/utils/indent-common.js b/lib/utils/indent-common.js index f93379daa..e11c4d87a 100644 --- a/lib/utils/indent-common.js +++ b/lib/utils/indent-common.js @@ -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,24 +546,23 @@ 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 } @@ -587,8 +570,14 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti } } } + 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 } } } diff --git a/tests/fixtures/script-indent/array-expression-03.vue b/tests/fixtures/script-indent/array-expression-03.vue new file mode 100644 index 000000000..0e15d8aad --- /dev/null +++ b/tests/fixtures/script-indent/array-expression-03.vue @@ -0,0 +1,6 @@ + + \ No newline at end of file diff --git a/tests/fixtures/script-indent/array-expression-04.vue b/tests/fixtures/script-indent/array-expression-04.vue new file mode 100644 index 000000000..1530c24b5 --- /dev/null +++ b/tests/fixtures/script-indent/array-expression-04.vue @@ -0,0 +1,6 @@ + + \ No newline at end of file diff --git a/tests/fixtures/script-indent/indent-valid-fixture-01.vue b/tests/fixtures/script-indent/indent-valid-fixture-01.vue new file mode 100644 index 000000000..112c4bde2 --- /dev/null +++ b/tests/fixtures/script-indent/indent-valid-fixture-01.vue @@ -0,0 +1,541 @@ + + + \ No newline at end of file diff --git a/tests/fixtures/script-indent/issue441.vue b/tests/fixtures/script-indent/issue441.vue new file mode 100644 index 000000000..a6738f2a3 --- /dev/null +++ b/tests/fixtures/script-indent/issue441.vue @@ -0,0 +1,10 @@ + + \ No newline at end of file diff --git a/tests/fixtures/script-indent/issue443.vue b/tests/fixtures/script-indent/issue443.vue new file mode 100644 index 000000000..4a8c356e7 --- /dev/null +++ b/tests/fixtures/script-indent/issue443.vue @@ -0,0 +1,27 @@ + + \ No newline at end of file diff --git a/tests/fixtures/script-indent/sequence-expression-04.vue b/tests/fixtures/script-indent/sequence-expression-04.vue new file mode 100644 index 000000000..f263bf523 --- /dev/null +++ b/tests/fixtures/script-indent/sequence-expression-04.vue @@ -0,0 +1,8 @@ + + From 4451a4aeb04883b23f676b242e9f608bea6510ed Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Sat, 15 Sep 2018 00:11:44 +0900 Subject: [PATCH 2/3] remove unrelated comment --- lib/utils/indent-common.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/utils/indent-common.js b/lib/utils/indent-common.js index e11c4d87a..4f0156cb2 100644 --- a/lib/utils/indent-common.js +++ b/lib/utils/indent-common.js @@ -555,7 +555,6 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti const token = tokens[i] const offsetInfo = offsets.get(token) - // If the first token is not trivial then ignore trivial following tokens. if (offsetInfo != null) { if (offsetInfo.expectedIndent != null) { expectedIndents.push(offsetInfo.expectedIndent) From 1e5a12c9274b02a3d9ba693c2bb0efe9b00eebf1 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Sat, 15 Sep 2018 00:20:30 +0900 Subject: [PATCH 3/3] fixed parse error --- .../script-indent/indent-valid-fixture-01.vue | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/fixtures/script-indent/indent-valid-fixture-01.vue b/tests/fixtures/script-indent/indent-valid-fixture-01.vue index 112c4bde2..c1423bf9b 100644 --- a/tests/fixtures/script-indent/indent-valid-fixture-01.vue +++ b/tests/fixtures/script-indent/indent-valid-fixture-01.vue @@ -187,7 +187,7 @@ var a = function () { f = g; // <- }; -function c(a, b) { +function c1(a, b) { if (a || (a && b)) { return d; @@ -463,14 +463,14 @@ if ( c++; // <- } -function c(d) { +function c2(d) { return { e: function(f, g) { } }; } -function a(b) { +function a1(b) { switch(x) { case 1: if (foo) { @@ -479,14 +479,14 @@ function a(b) { } } -function a(b) { +function a2(b) { switch(x) { case 1: c; } } -function a(b) { +function a3(b) { switch(x) { case 1: c; } @@ -503,7 +503,7 @@ function test() { a(); } -function a(b) { +function a4(b) { switch(x) { case 1: { // <- @@ -523,7 +523,7 @@ switch (a) { c(); } -function test(x) { +function test1(x) { switch (x) { case 1: return function() {