Skip to content

Commit

Permalink
Fixed: "ignores" option of html-indent does not work (#1016)
Browse files Browse the repository at this point in the history
* Fixed: "ignores" option of "html-indent" does not work

* Add testcase that mixed indent
  • Loading branch information
ota-meshi committed Dec 27, 2019
1 parent c8cdd77 commit 386cec4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/utils/indent-common.js
Expand Up @@ -278,7 +278,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()
const ignoreTokens = new Set()

/**
* Set offset to the given tokens.
Expand Down Expand Up @@ -341,9 +341,9 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
)
}
for (const token of tokenStore.getTokensBetween(node.startTag, endToken, option)) {
preformattedTokens.add(token)
ignoreTokens.add(token)
}
preformattedTokens.add(endToken)
ignoreTokens.add(endToken)
}

/**
Expand Down Expand Up @@ -600,6 +600,7 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
function ignore (node) {
for (const token of tokenStore.getTokens(node)) {
offsets.delete(token)
ignoreTokens.add(token)
}
}

Expand Down Expand Up @@ -861,8 +862,8 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
}
}

// It does not validate preformatted tokens.
if (preformattedTokens.has(firstToken)) {
// It does not validate ignore tokens.
if (ignoreTokens.has(firstToken)) {
return
}

Expand Down
19 changes: 19 additions & 0 deletions tests/fixtures/html-indent/opts-ignores-vattribute-01.vue
@@ -0,0 +1,19 @@
<!--{"options":[2,{"ignores": ["VAttribute"]}]}-->
<template>
<div>
<div
attr1="a">
</div>
<div
attr2>
</div>
<div
:attr3="a">
</div>
<div
:attr4="
a
">
</div>
</div>
</template>
19 changes: 19 additions & 0 deletions tests/lib/rules/html-indent.js
Expand Up @@ -756,6 +756,25 @@ tester.run('html-indent', rule, loadPatterns(
{ message: 'Expected indentation of 4 spaces but found 0 spaces.', line: 7 },
{ message: 'Expected indentation of 2 spaces but found 0 spaces.', line: 8 }
]
},
{
filename: 'test.vue',
code: unIndent`
<template>
\t <div attr1
\t\t attr2/>
</template>
`,
output: unIndent`
<template>
\t<div attr1
\t\t attr2/>
</template>
`,
options: ['tab', { 'ignores': ['VAttribute'] }],
errors: [
{ message: 'Expected "\\t" character, but found " " character.', line: 2 }
]
}
]
))

0 comments on commit 386cec4

Please sign in to comment.