html-quotes.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. category: '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. create (context) {
  37. const sourceCode = context.getSourceCode()
  38. const double = context.options[0] !== 'single'
  39. const avoidEscape = context.options[1] && context.options[1].avoidEscape === true
  40. const quoteChar = double ? '"' : "'"
  41. const quoteName = double ? 'double quotes' : 'single quotes'
  42. let hasInvalidEOF
  43. return utils.defineTemplateBodyVisitor(context, {
  44. 'VAttribute[value!=null]' (node) {
  45. if (hasInvalidEOF) {
  46. return
  47. }
  48. const text = sourceCode.getText(node.value)
  49. const firstChar = text[0]
  50. if (firstChar !== quoteChar) {
  51. const quoted = (firstChar === "'" || firstChar === '"')
  52. if (avoidEscape && quoted) {
  53. const contentText = text.slice(1, -1)
  54. if (contentText.includes(quoteChar)) {
  55. return
  56. }
  57. }
  58. context.report({
  59. node: node.value,
  60. loc: node.value.loc,
  61. message: 'Expected to be enclosed by {{kind}}.',
  62. data: { kind: quoteName },
  63. fix (fixer) {
  64. const contentText = quoted ? text.slice(1, -1) : text
  65. const fixToDouble = avoidEscape && !quoted && contentText.includes(quoteChar)
  66. ? (
  67. double
  68. ? contentText.includes("'")
  69. : !contentText.includes('"')
  70. )
  71. : double
  72. const quotePattern = fixToDouble ? /"/g : /'/g
  73. const quoteEscaped = fixToDouble ? '"' : '''
  74. const fixQuoteChar = fixToDouble ? '"' : "'"
  75. const replacement = fixQuoteChar + contentText.replace(quotePattern, quoteEscaped) + fixQuoteChar
  76. return fixer.replaceText(node.value, replacement)
  77. }
  78. })
  79. }
  80. }
  81. }, {
  82. Program (node) {
  83. hasInvalidEOF = utils.hasInvalidEOF(node)
  84. }
  85. })
  86. }
  87. }