8000 Fix: ignore preformatted tokens in `html-indent` · Demivan/eslint-plugin-vue@0f27d01 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0f27d01

Browse files
committed
Fix: ignore preformatted tokens in html-indent
1 parent f98123c commit 0f27d01

File tree

2 files changed

+164
-2
lines changed

2 files changed

+164
-2
lines changed

lib/utils/indent-common.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
258258
const options = parseOptions(context.options[0], context.options[1] || {}, defaultOptions)
259259
const sourceCode = context.getSourceCode()
260260
const offsets = new Map()
261+
const preformattedTokens = new Set()
261262

262263
/**
263264
* Set offset to the given tokens.
@@ -301,6 +302,29 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
301302
}
302303
}
303304

305+
/**
306+
* Sets preformatted tokens to the given element node.
307+
* @param {Node} node The node to set.
308+
* @returns {void}
309+
*/
310+
function setPreformattedTokens (node) {
311+
const endToken = node.endTag && tokenStore.getFirstToken(node.endTag) || tokenStore.getTokenAfter(node)
312+
313+
const option = {
314+
includeComments: true,
315+
filter: token => token != null && (
316+
token.type === 'HTMLText' ||
317+
token.type === 'HTMLTagOpen' ||
318+
token.type === 'HTMLEndTagOpen' ||
319+
token.type === 'HTMLComment'
320+
)
321+
}
322+
for (const token of tokenStore.getTokensBetween(node.startTag, endToken, option)) {
323+
preformattedTokens.add(token)
324+
}
325+
preformattedTokens.add(endToken)
326+
}
327+
304328
/**
305329
* Get the first and last tokens of the given node.
306330
* If the node is parenthesized, this gets the outermost parentheses.
@@ -782,6 +806,11 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
782806
}
783807
}
784808

809+
// It does not validate preformatted tokens.
810+
if (preformattedTokens.has(firstToken)) {
811+
return
812+
}
813+
785814
// Calculate the expected indents for comments.
786815
// It allows the same indent level with the previous line.
787816
const lastOffsetInfo = offsets.get(lastToken)
@@ -821,6 +850,8 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
821850
if (node.name !== 'pre') {
822851
const childTokens = node.children.map(n => tokenStore.getFirstToken(n))
823852
setOffset(childTokens, 1, startTagToken)
853+
} else {
854+
setPreformattedTokens(node)
824855
}
825856
setOffset(endTagToken, 0, startTagToken)
826857
},

tests/lib/rules/html-indent.js

Lines changed: 133 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,72 @@ tester.run('html-indent', rule, loadPatterns(
281281
</pre>
282282
</template>
283283
`
284+
},
285+
{
286+
filename: 'test.vue',
287+
code: unIndent`
288+
<template>
289+
<pre>
290+
<span>aaa</span>
291+
<span>bbb</span>
292+
<span>ccc</span>
293+
</pre>
294+
</template>
295+
`
296+
},
297+
{
298+
filename: 'test.vue',
299+
code: unIndent`
300+
<template>
301+
<pre>aaa
302+
bbb ccc
303+
ddd
304+
fff</pre>
305+
</template>
306+
`
307+
},
308+
{
309+
filename: 'test.vue',
310+
code: unIndent`
311+
<template>
312+
<pre><span>aaa</span>
313+
<span>bbb</span> <span>ccc</span>
314+
<span>ddd</span>
315+
<span>fff</span></pre>
316+
</template>
317+
`
318+
},
319+
{
320+
filename: 'test.vue',
321+
code: unIndent`
322+
<template>
323+
<pre>
324+
<!-- comment -->
325+
<!-- comment --> <!-- comment -->
326+
<!-- comment --></pre>
327+
</template>
328+
`
329+
},
330+
{
331+
filename: 'test.vue',
332+
code: unIndent`
333+
<template>
334+
<pre>
335+
<!-- comment --> text <span />
336+
<span /> <!-- comment --> text
337+
text <span /> <!-- comment -->
338+
</pre>
339+
<div>
340+
<input>
341+
</div>
342+
<pre>
343+
<!-- comment --> text <span /></pre>
344+
<pre>
345+
<span /> <!-- comment --> text</pre>
346+
<pre>
347+
text <span /> <!-- comment --></pre>
348+
</template>
349+
`
284350
}
285351
],
286352

@@ -564,13 +630,78 @@ tester.run('html-indent', rule, loadPatterns(
564630
aaa
565631
bbb
566632
ccc
567-
</pre>
633+
</pre>
634+
</template>
635+
`,
636+
errors: [
637+
{ message: 'Expected indentation of 2 spaces but found 0 spaces.', line: 2 },
638+
{ message: 'Expected indentation of 4 spaces but found 0 spaces.', line: 3 },
639+
{ message: 'Expected indentation of 2 spaces but found 0 spaces.', line: 4 }
640+
]
641+
},
642+
{
643+
filename: 'test.vue',
644+
code: unIndent`
645+
<template>
646+
<pre
647+
style=""
648+
>
649+
no end tag
650+
</template>
651+
`,
652+
output: unIndent`
653+
<template>
654+
<pre
655+
style=""
656+
>
657+
no end tag
658+
</template>
659+
`,
660+
errors: [
661+
{ message: 'Expected indentation of 2 spaces but found 0 spaces.', line: 2 },
662+
{ message: 'Expected indentation of 4 spaces but found 0 spaces.', line: 3 },
663+
{ message: 'Expected indentation of 2 spaces but found 0 spaces.', line: 4 }
664+
]
665+
},
666+
{
667+
filename: 'test.vue',
668+
code: unIndent`
669+
<template>
670+
<pre
671+
:class="[
672+
'a',
673+
'b',
674+
'c'
675+
]"
676+
>
677+
aaa
678+
bbb
679+
ccc
680+
</pre>
568681
</template>
569682
`,
683+
output: unIndent`
684+
<template>
685+
<pre
686+
:class="[
687+
'a',
688+
'b',
689+
'c'
690+
]"
691+
>
692+
aaa
693+
bbb
694+
ccc
695+
</pre>
696+
</template>
697+
`,
570698
errors: [
571699
{ message: 'Expected indentation of 2 spaces but found 0 spaces.', line: 2 },
572700
{ message: 'Expected indentation of 4 spaces but found 0 spaces.', line: 3 },
573-
{ message: 'Expected indentation of 2 spaces but found 0 spaces.', line: 4 },
701+
{ message: 'Expected indentation of 6 spaces but found 0 spaces.', line: 4 },
702+
{ message: 'Expected indentation of 6 spaces but found 0 spaces.', line: 5 },
703+
{ message: 'Expected indentation of 6 spaces but found 0 spaces.', line: 6 },
704+
{ message: 'Expected indentation of 4 spaces but found 0 spaces.', line: 7 },
574705
{ message: 'Expected indentation of 2 spaces but found 0 spaces.', line: 8 }
575706
]
576707
}

0 commit comments

Comments
 (0)
0