no-parsing-error.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // Helpers
  9. // ------------------------------------------------------------------------------
  10. // https://html.spec.whatwg.org/multipage/parsing.html#parse-errors
  11. const DEFAULT_OPTIONS = Object.freeze(Object.assign(Object.create(null), {
  12. 'abrupt-closing-of-empty-comment': true,
  13. 'absence-of-digits-in-numeric-character-reference': true,
  14. 'cdata-in-html-content': true,
  15. 'character-reference-outside-unicode-range': true,
  16. 'control-character-in-input-stream': true,
  17. 'control-character-reference': true,
  18. 'eof-before-tag-name': true,
  19. 'eof-in-cdata': true,
  20. 'eof-in-comment': true,
  21. 'eof-in-tag': true,
  22. 'incorrectly-closed-comment': true,
  23. 'incorrectly-opened-comment': true,
  24. 'invalid-first-character-of-tag-name': true,
  25. 'missing-attribute-value': true,
  26. 'missing-end-tag-name': true,
  27. 'missing-semicolon-after-character-reference': true,
  28. 'missing-whitespace-between-attributes': true,
  29. 'nested-comment': true,
  30. 'noncharacter-character-reference': true,
  31. 'noncharacter-in-input-stream': true,
  32. 'null-character-reference': true,
  33. 'surrogate-character-reference': true,
  34. 'surrogate-in-input-stream': true,
  35. 'unexpected-character-in-attribute-name': true,
  36. 'unexpected-character-in-unquoted-attribute-value': true,
  37. 'unexpected-equals-sign-before-attribute-name': true,
  38. 'unexpected-null-character': true,
  39. 'unexpected-question-mark-instead-of-tag-name': true,
  40. 'unexpected-solidus-in-tag': true,
  41. 'unknown-named-character-reference': true,
  42. 'end-tag-with-attributes': true,
  43. 'duplicate-attribute': true,
  44. 'end-tag-with-trailing-solidus': true,
  45. 'non-void-html-element-start-tag-with-trailing-solidus': false,
  46. 'x-invalid-end-tag': true,
  47. 'x-invalid-namespace': true
  48. }))
  49. // ------------------------------------------------------------------------------
  50. // Rule Definition
  51. // ------------------------------------------------------------------------------
  52. module.exports = {
  53. meta: {
  54. docs: {
  55. description: 'disallow parsing errors in `<template>`',
  56. category: 'essential',
  57. url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v4.7.1/docs/rules/no-parsing-error.md'
  58. },
  59. fixable: null,
  60. schema: [
  61. {
  62. type: 'object',
  63. properties: Object.keys(DEFAULT_OPTIONS).reduce((ret, code) => {
  64. ret[code] = { type: 'boolean' }
  65. return ret
  66. }, {}),
  67. additionalProperties: false
  68. }
  69. ]
  70. },
  71. create (context) {
  72. const options = Object.assign({}, DEFAULT_OPTIONS, context.options[0] || {})
  73. return {
  74. Program (program) {
  75. const node = program.templateBody
  76. if (node == null || node.errors == null) {
  77. return
  78. }
  79. for (const error of node.errors) {
  80. if (error.code && !options[error.code]) {
  81. continue
  82. }
  83. context.report({
  84. node,
  85. loc: { line: error.lineNumber, column: error.column },
  86. message: 'Parsing error: {{message}}.',
  87. data: {
  88. message: error.message.endsWith('.')
  89. ? error.message.slice(0, -1)
  90. : error.message
  91. }
  92. })
  93. }
  94. }
  95. }
  96. }
  97. }