Skip to content

Commit

Permalink
Fix: ignore preformatted tokens in html-indent (fixes #653)(#659)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi authored and mysticatea committed Nov 24, 2018
1 parent 0e51839 commit 78bd936
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 2 deletions.
31 changes: 31 additions & 0 deletions lib/utils/indent-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
},
Expand Down
122 changes: 120 additions & 2 deletions tests/lib/rules/html-indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,83 @@ tester.run('html-indent', rule, loadPatterns(
</pre>
</template>
`
},
{
filename: 'test.vue',
code: unIndent`
<template>
<pre>
<span>aaa</span>
<span>bbb</span>
<span>ccc</span>
</pre>
</template>
`
},
{
filename: 'test.vue',
code: unIndent`
<template>
<pre>aaa
bbb ccc
ddd
fff</pre>
</template>
`
},
{
filename: 'test.vue',
code: unIndent`
<template>
<pre><span>aaa</span>
<span>bbb</span> <span>ccc</span>
<span>ddd</span>
<span>fff</span></pre>
</template>
`
},
{
filename: 'test.vue',
code: unIndent`
<template>
<div><pre>aaa
bbb ccc
ddd
fff</pre></div>
</template>
`
},
{
filename: 'test.vue',
code: unIndent`
<template>
<pre>
<!-- comment -->
<!-- comment --> <!-- comment -->
<!-- comment --></pre>
</template>
`
},
{
filename: 'test.vue',
code: unIndent`
<template>
<pre>
<!-- comment --> text <span />
<span /> <!-- comment --> text
text <span /> <!-- comment -->
</pre>
<div>
<input>
</div>
<pre>
<!-- comment --> text <span /></pre>
<pre>
<span /> <!-- comment --> text</pre>
<pre>
text <span /> <!-- comment --></pre>
</template>
`
}
],

Expand Down Expand Up @@ -564,13 +641,54 @@ tester.run('html-indent', rule, loadPatterns(
aaa
bbb
ccc
</pre>
</pre>
</template>
`,
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`
<template>
<pre
:class="[
'a',
'b',
'c'
]"
>
aaa
bbb
ccc
</pre>
</template>
`,
output: unIndent`
<template>
<pre
:class="[
'a',
'b',
'c'
]"
>
aaa
bbb
ccc
</pre>
</template>
`,
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 }
]
}
Expand Down

0 comments on commit 78bd936

Please sign in to comment.