Skip to content

[Update] Make vue/html-quotes fixable #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions lib/rules/html-end-tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,19 @@ const utils = require('../utils')
* @returns {Object} AST event handlers.
*/
function create (context) {
let hasInvalidEOF = false

return utils.defineTemplateBodyVisitor(context, {
VElement (node) {
if (hasInvalidEOF) {
return
}

const name = node.name
const isVoid = utils.isHtmlVoidElementName(name)
const isSelfClosing = node.startTag.selfClosing
const hasEndTag = node.endTag != null

if (isVoid && hasEndTag) {
context.report({
node: node.endTag,
loc: node.endTag.loc,
message: "'<{{name}}>' should not have end tag.",
data: { name },
fix: (fixer) => fixer.remove(node.endTag)
})
}
if (!isVoid && !hasEndTag && !isSelfClosing) {
context.report({
node: node.startTag,
Expand All @@ -48,6 +45,10 @@ function create (context) {
})
}
}
}, {
Program (node) {
hasInvalidEOF = utils.hasInvalidEOF(node)
}
})
}

Expand Down
22 changes: 20 additions & 2 deletions lib/rules/html-quotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,38 @@ function create (context) {
const double = context.options[0] !== 'single'
const quoteChar = double ? '"' : "'"
const quoteName = double ? 'double quotes' : 'single quotes'
const quotePattern = double ? /"/g : /'/g
const quoteEscaped = double ? '&quot;' : '&apos;'
let hasInvalidEOF

return utils.defineTemplateBodyVisitor(context, {
'VAttribute[value!=null]' (node) {
if (hasInvalidEOF) {
return
}

const text = sourceCode.getText(node.value)
const firstChar = text[0]

if (firstChar !== quoteChar) {
context.report({
node: node.value,
loc: node.value.loc,
message: 'Expected to be enclosed by {{kind}}.',
data: { kind: quoteName }
data: { kind: quoteName },
fix (fixer) {
const contentText = (firstChar === "'" || firstChar === '"') ? text.slice(1, -1) : text
const replacement = quoteChar + contentText.replace(quotePattern, quoteEscaped) + quoteChar

return fixer.replaceText(node.value, replacement)
}
})
}
}
}, {
Program (node) {
hasInvalidEOF = utils.hasInvalidEOF(node)
}
})
}

Expand All @@ -54,7 +72,7 @@ module.exports = {
description: 'enforce quotes style of HTML attributes',
category: 'recommended'
},
fixable: false,
fixable: 'code',
schema: [
{ enum: ['double', 'single'] }
]
Expand Down
9 changes: 9 additions & 0 deletions lib/rules/html-self-closing.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,14 @@ function isEmpty (node, sourceCode) {
function create (context) {
const sourceCode = context.getSourceCode()
const options = parseOptions(context.options[0])
let hasInvalidEOF = false

return utils.defineTemplateBodyVisitor(context, {
'VElement' (node) {
if (hasInvalidEOF) {
return
}

const elementType = getElementType(node)
const mode = options[elementType]

Expand Down Expand Up @@ -131,6 +136,10 @@ function create (context) {
})
}
}
}, {
Program (node) {
hasInvalidEOF = utils.hasInvalidEOF(node)
}
})
}

Expand Down
13 changes: 13 additions & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,5 +601,18 @@ module.exports = {
*/
isSingleLine (node) {
return node.loc.start.line === node.loc.end.line
},

/**
* Check whether the templateBody of the program has invalid EOF or not.
* @param {Program} node The program node to check.
* @returns {boolean} `true` if it has invalid EOF.
*/
hasInvalidEOF (node) {
const body = node.templateBody
if (body == null || body.errors == null) {
return
}
return body.errors.some(error => typeof error.code === 'string' && error.code.startsWith('eof-'))
}
}
30 changes: 12 additions & 18 deletions tests/lib/rules/html-end-tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,21 @@ tester.run('html-end-tags', rule, {
{
filename: 'test.vue',
code: '<template><div><div/></div></template>'
},
{
filename: 'test.vue',
code: '<template><div a="b>test</div></template>'
},
{
filename: 'test.vue',
code: '<template><div><!--</div></template>'
},
{
filename: 'test.vue',
code: '<template><div><svg><![CDATA[test</svg></div></template>'
}
],
invalid: [
// {
// filename: 'test.vue',
// code: '<template><div><hr></hr></div></template>',
// output: '<template><div><hr></template>',
// errors: ["'<hr>' should not have end tag."]
// },
// {
// filename: 'test.vue',
// code: '<template><div><img></img></div></template>',
// output: '<template><div><img></template>',
// errors: ["'<img>' should not have end tag."]
// },
// {
// filename: 'test.vue',
// code: '<template><div><input></input></div></template>',
// output: '<template><div><input></template>',
// errors: ["'<input>' should not have end tag."]
// },
{
filename: 'test.vue',
code: '<template><div><div></div></template>',
Expand Down
25 changes: 25 additions & 0 deletions tests/lib/rules/html-quotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,91 +54,116 @@ tester.run('html-quotes', rule, {
filename: 'test.vue',
code: "<template><div :class='foo'></div></template>",
options: ['single']
},

// Invalid EOF
{
code: '<template><div class="foo></div></template>',
options: ['single']
},
{
code: '<template><div class=\'foo></div></template>',
options: ['double']
}
],
invalid: [
{
filename: 'test.vue',
code: '<template><div class=foo></div></template>',
output: '<template><div class="foo"></div></template>',
errors: ['Expected to be enclosed by double quotes.']
},
{
filename: 'test.vue',
code: "<template><div class='foo'></div></template>",
output: '<template><div class="foo"></div></template>',
errors: ['Expected to be enclosed by double quotes.']
},
{
filename: 'test.vue',
code: '<template><div :class=foo></div></template>',
output: '<template><div :class="foo"></div></template>',
errors: ['Expected to be enclosed by double quotes.']
},
{
filename: 'test.vue',
code: "<template><div :class='foo'></div></template>",
output: '<template><div :class="foo"></div></template>',
errors: ['Expected to be enclosed by double quotes.']
},
{
filename: 'test.vue',
code: '<template><div :class=foo+"bar"></div></template>',
output: '<template><div :class="foo+&quot;bar&quot;"></div></template>',
errors: ['Expected to be enclosed by double quotes.']
},
{
filename: 'test.vue',
code: '<template><div class=foo></div></template>',
output: '<template><div class="foo"></div></template>',
options: ['double'],
errors: ['Expected to be enclosed by double quotes.']
},
{
filename: 'test.vue',
code: "<template><div class='foo'></div></template>",
output: '<template><div class="foo"></div></template>',
options: ['double'],
errors: ['Expected to be enclosed by double quotes.']
},
{
filename: 'test.vue',
code: '<template><div :class=foo></div></template>',
output: '<template><div :class="foo"></div></template>',
options: ['double'],
errors: ['Expected to be enclosed by double quotes.']
},
{
filename: 'test.vue',
code: "<template><div :class='foo'></div></template>",
output: '<template><div :class="foo"></div></template>',
options: ['double'],
errors: ['Expected to be enclosed by double quotes.']
},
{
filename: 'test.vue',
code: '<template><div :class=foo+"bar"></div></template>',
output: '<template><div :class="foo+&quot;bar&quot;"></div></template>',
options: ['double'],
errors: ['Expected to be enclosed by double quotes.']
},
{
filename: 'test.vue',
code: '<template><div class=foo></div></template>',
output: '<template><div class=\'foo\'></div></template>',
options: ['single'],
errors: ['Expected to be enclosed by single quotes.']
},
{
filename: 'test.vue',
code: '<template><div class="foo"></div></template>',
output: '<template><div class=\'foo\'></div></template>',
options: ['single'],
errors: ['Expected to be enclosed by single quotes.']
},
{
filename: 'test.vue',
code: '<template><div :class=foo></div></template>',
output: '<template><div :class=\'foo\'></div></template>',
options: ['single'],
errors: ['Expected to be enclosed by single quotes.']
},
{
filename: 'test.vue',
code: '<template><div :class="foo"></div></template>',
output: '<template><div :class=\'foo\'></div></template>',
options: ['single'],
errors: ['Expected to be enclosed by single quotes.']
},
{
filename: 'test.vue',
code: "<template><div :class=foo+'bar'></div></template>",
output: "<template><div :class='foo+&apos;bar&apos;'></div></template>",
options: ['single'],
errors: ['Expected to be enclosed by single quotes.']
}
Expand Down
6 changes: 5 additions & 1 deletion tests/lib/rules/html-self-closing.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ tester.run('html-self-closing', rule, {
code: '<template><div><!-- comment --></div></template>',
output: null,
options: [{ html: { normal: 'always' }}]
}
},

// Invalid EOF
'<template><div a=">test</div></template>',
'<template><div><!--test</div></template>'

// other cases are in `invalid` tests.
],
Expand Down