Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jsx-quotes.js 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * @fileoverview A rule to ensure consistent quotes used in jsx syntax.
  3. * @author Mathias Schreck <https://github.com/lo1tuma>
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../util/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Constants
  12. //------------------------------------------------------------------------------
  13. const QUOTE_SETTINGS = {
  14. "prefer-double": {
  15. quote: "\"",
  16. description: "singlequote",
  17. convert(str) {
  18. return str.replace(/'/g, "\"");
  19. }
  20. },
  21. "prefer-single": {
  22. quote: "'",
  23. description: "doublequote",
  24. convert(str) {
  25. return str.replace(/"/g, "'");
  26. }
  27. }
  28. };
  29. //------------------------------------------------------------------------------
  30. // Rule Definition
  31. //------------------------------------------------------------------------------
  32. module.exports = {
  33. meta: {
  34. type: "layout",
  35. docs: {
  36. description: "enforce the consistent use of either double or single quotes in JSX attributes",
  37. category: "Stylistic Issues",
  38. recommended: false,
  39. url: "https://eslint.org/docs/rules/jsx-quotes"
  40. },
  41. fixable: "whitespace",
  42. schema: [
  43. {
  44. enum: ["prefer-single", "prefer-double"]
  45. }
  46. ]
  47. },
  48. create(context) {
  49. const quoteOption = context.options[0] || "prefer-double",
  50. setting = QUOTE_SETTINGS[quoteOption];
  51. /**
  52. * Checks if the given string literal node uses the expected quotes
  53. * @param {ASTNode} node - A string literal node.
  54. * @returns {boolean} Whether or not the string literal used the expected quotes.
  55. * @public
  56. */
  57. function usesExpectedQuotes(node) {
  58. return node.value.indexOf(setting.quote) !== -1 || astUtils.isSurroundedBy(node.raw, setting.quote);
  59. }
  60. return {
  61. JSXAttribute(node) {
  62. const attributeValue = node.value;
  63. if (attributeValue && astUtils.isStringLiteral(attributeValue) && !usesExpectedQuotes(attributeValue)) {
  64. context.report({
  65. node: attributeValue,
  66. message: "Unexpected usage of {{description}}.",
  67. data: {
  68. description: setting.description
  69. },
  70. fix(fixer) {
  71. return fixer.replaceText(attributeValue, setting.convert(attributeValue.raw));
  72. }
  73. });
  74. }
  75. }
  76. };
  77. }
  78. };