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