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