2 * @fileoverview A rule to ensure consistent quotes used in jsx syntax.
3 * @author Mathias Schreck <https://github.com/lo1tuma>
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils = require("./utils/ast-utils");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
18 const QUOTE_SETTINGS = {
21 description: "singlequote",
23 return str.replace(/'/gu, "\"");
28 description: "doublequote",
30 return str.replace(/"/gu, "'");
35 //------------------------------------------------------------------------------
37 //------------------------------------------------------------------------------
44 description: "enforce the consistent use of either double or single quotes in JSX attributes",
45 category: "Stylistic Issues",
47 url: "https://eslint.org/docs/rules/jsx-quotes"
50 fixable: "whitespace",
54 enum: ["prefer-single", "prefer-double"]
58 unexpected: "Unexpected usage of {{description}}."
63 const quoteOption = context.options[0] || "prefer-double",
64 setting = QUOTE_SETTINGS[quoteOption];
67 * Checks if the given string literal node uses the expected quotes
68 * @param {ASTNode} node A string literal node.
69 * @returns {boolean} Whether or not the string literal used the expected quotes.
72 function usesExpectedQuotes(node) {
73 return node.value.indexOf(setting.quote) !== -1 || astUtils.isSurroundedBy(node.raw, setting.quote);
78 const attributeValue = node.value;
80 if (attributeValue && astUtils.isStringLiteral(attributeValue) && !usesExpectedQuotes(attributeValue)) {
83 messageId: "unexpected",
85 description: setting.description
88 return fixer.replaceText(attributeValue, setting.convert(attributeValue.raw));