html-quotes.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @author Toru Nagashima
  3. * @copyright 2017 Toru Nagashima. All rights reserved.
  4. * See LICENSE file in root directory for full license.
  5. */
  6. 'use strict'
  7. const utils = require('../utils')
  8. module.exports = {
  9. meta: {
  10. type: 'layout',
  11. docs: {
  12. description: 'enforce quotes style of HTML attributes',
  13. categories: ['vue3-strongly-recommended', 'vue2-strongly-recommended'],
  14. url: 'https://eslint.vuejs.org/rules/html-quotes.html'
  15. },
  16. fixable: 'code',
  17. schema: [
  18. { enum: ['double', 'single'] },
  19. {
  20. type: 'object',
  21. properties: {
  22. avoidEscape: {
  23. type: 'boolean'
  24. }
  25. },
  26. additionalProperties: false
  27. }
  28. ],
  29. messages: {
  30. expected: 'Expected to be enclosed by {{kind}}.'
  31. }
  32. },
  33. /** @param {RuleContext} context */
  34. create(context) {
  35. const sourceCode = context.getSourceCode()
  36. const double = context.options[0] !== 'single'
  37. const avoidEscape =
  38. context.options[1] && context.options[1].avoidEscape === true
  39. const quoteChar = double ? '"' : "'"
  40. const quoteName = double ? 'double quotes' : 'single quotes'
  41. /** @type {boolean} */
  42. let hasInvalidEOF
  43. return utils.defineTemplateBodyVisitor(
  44. context,
  45. {
  46. 'VAttribute[value!=null]'(node) {
  47. if (hasInvalidEOF) {
  48. return
  49. }
  50. if (utils.isVBindSameNameShorthand(node)) {
  51. // v-bind same-name shorthand (Vue 3.4+)
  52. return
  53. }
  54. const text = sourceCode.getText(node.value)
  55. const firstChar = text[0]
  56. if (firstChar !== quoteChar) {
  57. const quoted = firstChar === "'" || firstChar === '"'
  58. if (avoidEscape && quoted) {
  59. const contentText = text.slice(1, -1)
  60. if (contentText.includes(quoteChar)) {
  61. return
  62. }
  63. }
  64. context.report({
  65. node: node.value,
  66. loc: node.value.loc,
  67. messageId: 'expected',
  68. data: { kind: quoteName },
  69. fix(fixer) {
  70. const contentText = quoted ? text.slice(1, -1) : text
  71. let fixToDouble = double
  72. if (avoidEscape && !quoted && contentText.includes(quoteChar)) {
  73. fixToDouble = double
  74. ? contentText.includes("'")
  75. : !contentText.includes('"')
  76. }
  77. const quotePattern = fixToDouble ? /"/g : /'/g
  78. const quoteEscaped = fixToDouble ? '"' : '''
  79. const fixQuoteChar = fixToDouble ? '"' : "'"
  80. const replacement =
  81. fixQuoteChar +
  82. contentText.replace(quotePattern, quoteEscaped) +
  83. fixQuoteChar
  84. return fixer.replaceText(node.value, replacement)
  85. }
  86. })
  87. }
  88. }
  89. },
  90. {
  91. Program(node) {
  92. hasInvalidEOF = utils.hasInvalidEOF(node)
  93. }
  94. }
  95. )
  96. }
  97. }