From 8645eae18e3c1cf2e971fd276ce67b8310015dc2 Mon Sep 17 00:00:00 2001 From: ota Date: Tue, 13 Nov 2018 18:10:06 +0900 Subject: [PATCH 1/4] Fix: ignore preformatted tokens in `html-indent` --- lib/utils/indent-common.js | 31 +++++ tests/lib/rules/html-indent.js | 243 ++++++++++++++++++++++++++++++++- 2 files changed, 272 insertions(+), 2 deletions(-) diff --git a/lib/utils/indent-common.js b/lib/utils/indent-common.js index c8c867250..ab4981edb 100644 --- a/lib/utils/indent-common.js +++ b/lib/utils/indent-common.js @@ -258,6 +258,7 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti const options = parseOptions(context.options[0], context.options[1] || {}, defaultOptions) const sourceCode = context.getSourceCode() const offsets = new Map() + const preformattedTokens = new Set() /** * Set offset to the given tokens. @@ -301,6 +302,29 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti } } + /** + * Sets preformatted tokens to the given element node. + * @param {Node} node The node to set. + * @returns {void} + */ + function setPreformattedTokens (node) { + const endToken = node.endTag && tokenStore.getFirstToken(node.endTag) || tokenStore.getTokenAfter(node) + + const option = { + includeComments: true, + filter: token => token != null && ( + token.type === 'HTMLText' || + token.type === 'HTMLTagOpen' || + token.type === 'HTMLEndTagOpen' || + token.type === 'HTMLComment' + ) + } + for (const token of tokenStore.getTokensBetween(node.startTag, endToken, option)) { + preformattedTokens.add(token) + } + preformattedTokens.add(endToken) + } + /** * Get the first and last tokens of the given node. * If the node is parenthesized, this gets the outermost parentheses. @@ -782,6 +806,11 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti } } + // It does not validate preformatted tokens. + if (preformattedTokens.has(firstToken)) { + return + } + // Calculate the expected indents for comments. // It allows the same indent level with the previous line. const lastOffsetInfo = offsets.get(lastToken) @@ -821,6 +850,8 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti if (node.name !== 'pre') { const childTokens = node.children.map(n => tokenStore.getFirstToken(n)) setOffset(childTokens, 1, startTagToken) + } else { + setPreformattedTokens(node) } setOffset(endTagToken, 0, startTagToken) }, diff --git a/tests/lib/rules/html-indent.js b/tests/lib/rules/html-indent.js index a0613009a..d9d793274 100644 --- a/tests/lib/rules/html-indent.js +++ b/tests/lib/rules/html-indent.js @@ -281,6 +281,204 @@ tester.run('html-indent', rule, loadPatterns( ` + }, + { + filename: 'test.vue', + code: unIndent` + + ` + }, + { + filename: 'test.vue', + code: unIndent` + + ` + }, + { + filename: 'test.vue', + code: unIndent` + + ` + }, + { + filename: 'test.vue', + code: unIndent` + + ` + }, + { + filename: 'test.vue', + code: unIndent` + + ` + }, + { + filename: 'test.vue', + code: unIndent` + + ` + }, + { + filename: 'test.vue', + code: unIndent` + + ` + }, + { + filename: 'test.vue', + code: unIndent` + + ` + }, + { + filename: 'test.vue', + code: unIndent` + + ` + }, + { + filename: 'test.vue', + code: unIndent` + + ` + }, + { + filename: 'test.vue', + code: unIndent` + + ` + }, + { + filename: 'test.vue', + code: unIndent` + + ` + }, + { + filename: 'test.vue', + code: unIndent` + + ` + }, + { + filename: 'test.vue', + code: unIndent` + + ` + }, + { + filename: 'test.vue', + code: unIndent` + + ` } ], @@ -564,13 +762,54 @@ tester.run('html-indent', rule, loadPatterns( aaa bbb ccc - + `, errors: [ { message: 'Expected indentation of 2 spaces but found 0 spaces.', line: 2 }, { message: 'Expected indentation of 4 spaces but found 0 spaces.', line: 3 }, - { message: 'Expected indentation of 2 spaces but found 0 spaces.', line: 4 }, + { message: 'Expected indentation of 2 spaces but found 0 spaces.', line: 4 } + ] + }, + { + filename: 'test.vue', + code: unIndent` + + `, + output: unIndent` + + `, + errors: [ + { message: 'Expected indentation of 2 spaces but found 0 spaces.', line: 2 }, + { message: 'Expected indentation of 4 spaces but found 0 spaces.', line: 3 }, + { message: 'Expected indentation of 6 spaces but found 0 spaces.', line: 4 }, + { message: 'Expected indentation of 6 spaces but found 0 spaces.', line: 5 }, + { message: 'Expected indentation of 6 spaces but found 0 spaces.', line: 6 }, + { message: 'Expected indentation of 4 spaces but found 0 spaces.', line: 7 }, { message: 'Expected indentation of 2 spaces but found 0 spaces.', line: 8 } ] } From 797b0b4630c6a774509f03a3f34cc268ab152e74 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Tue, 13 Nov 2018 22:40:41 +0900 Subject: [PATCH 2/4] deleted duplicate test cases. --- tests/lib/rules/html-indent.js | 132 --------------------------------- 1 file changed, 132 deletions(-) diff --git a/tests/lib/rules/html-indent.js b/tests/lib/rules/html-indent.js index d9d793274..3b4561c5f 100644 --- a/tests/lib/rules/html-indent.js +++ b/tests/lib/rules/html-indent.js @@ -327,138 +327,6 @@ tester.run('html-indent', rule, loadPatterns( ` }, - { - filename: 'test.vue', - code: unIndent` - - ` - }, - { - filename: 'test.vue', - code: unIndent` - - ` - }, - { - filename: 'test.vue', - code: unIndent` - - ` - }, - { - filename: 'test.vue', - code: unIndent` - - ` - }, - { - filename: 'test.vue', - code: unIndent` - - ` - }, - { - filename: 'test.vue', - code: unIndent` - - ` - }, - { - filename: 'test.vue', - code: unIndent` - - ` - }, - { - filename: 'test.vue', - code: unIndent` - - ` - }, - { - filename: 'test.vue', - code: unIndent` - - ` - }, - { - filename: 'test.vue', - code: unIndent` - - ` - }, { filename: 'test.vue', code: unIndent` From b4dbf18b3a9482eb03ba829e640fdedc0d3ecfc9 Mon Sep 17 00:00:00 2001 From: ota Date: Thu, 22 Nov 2018 10:40:33 +0900 Subject: [PATCH 3/4] Fixed mixed-operators --- lib/utils/indent-common.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils/indent-common.js b/lib/utils/indent-common.js index 21fa03853..1d83f9c52 100644 --- a/lib/utils/indent-common.js +++ b/lib/utils/indent-common.js @@ -308,7 +308,7 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti * @returns {void} */ function setPreformattedTokens (node) { - const endToken = node.endTag && tokenStore.getFirstToken(node.endTag) || tokenStore.getTokenAfter(node) + const endToken = (node.endTag && tokenStore.getFirstToken(node.endTag)) || tokenStore.getTokenAfter(node) const option = { includeComments: true, From c9206397de9e9ac3091735460747cf76ebdd95a6 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Sat, 24 Nov 2018 01:21:04 +0900 Subject: [PATCH 4/4] Add test case --- tests/lib/rules/html-indent.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/lib/rules/html-indent.js b/tests/lib/rules/html-indent.js index 3b4561c5f..e8a229699 100644 --- a/tests/lib/rules/html-indent.js +++ b/tests/lib/rules/html-indent.js @@ -316,6 +316,17 @@ tester.run('html-indent', rule, loadPatterns( ` }, + { + filename: 'test.vue', + code: unIndent` + + ` + }, { filename: 'test.vue', code: unIndent`