no-json-superset.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * @author Toru Nagashima <https://github.com/mysticatea>
  3. * See LICENSE file in root directory for full license.
  4. */
  5. "use strict"
  6. const { definePatternSearchGenerator } = require("../utils")
  7. const iterateTargetChars = definePatternSearchGenerator(/[\u2028\u2029]/gu)
  8. module.exports = {
  9. meta: {
  10. docs: {
  11. description: "disallow `\\u2028` and `\\u2029` in string literals.",
  12. category: "ES2019",
  13. recommended: false,
  14. url:
  15. "http://mysticatea.github.io/eslint-plugin-es/rules/no-json-superset.html",
  16. },
  17. fixable: "code",
  18. schema: [],
  19. messages: {
  20. forbidden: "ES2019 '\\u{{code}}' in string literals is forbidden.",
  21. },
  22. },
  23. create(context) {
  24. const sourceCode = context.getSourceCode()
  25. return {
  26. Literal(node) {
  27. if (typeof node.value !== "string") {
  28. return
  29. }
  30. const offset = node.range[0]
  31. for (const { index } of iterateTargetChars(node.raw)) {
  32. const code = node.raw.codePointAt(index).toString(16)
  33. const loc = sourceCode.getLocFromIndex(offset + index)
  34. context.report({
  35. node,
  36. loc,
  37. messageId: "forbidden",
  38. data: { code },
  39. fix(fixer) {
  40. return fixer.replaceTextRange(
  41. [offset + index, offset + index + 1],
  42. `\\u${code}`
  43. )
  44. },
  45. })
  46. }
  47. },
  48. }
  49. },
  50. }