UNPKG

eslint-plugin-react-snob

Version:
57 lines (56 loc) 2.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.requireJsxStringBraces = void 0; const utils_1 = require("../utils"); exports.requireJsxStringBraces = (0, utils_1.createRule)({ create(context) { return { JSXAttribute(node) { if (node.value && node.value.type === 'Literal' && typeof node.value.value === 'string') { const attributeName = node.name.type === 'JSXIdentifier' ? node.name.name : node.name.type === 'JSXNamespacedName' ? `${node.name.namespace.name}:${node.name.name.name}` : 'attribute'; context.report({ data: { attribute: attributeName, value: String(node.value.value), }, fix(fixer) { if (node.value && node.value.type === 'Literal' && typeof node.value.raw === 'string') { const quote = node.value.raw.charAt(0); const innerValue = node.value.raw.slice(1, -1); // check if the string contains line breaks if (innerValue.includes('\n')) { // convert to template literal for multi-line strings return fixer.replaceText(node.value, `{\`${innerValue.replace(/`/g, '\\`')}\`}`); } else { // keep as regular string literal for single-line strings return fixer.replaceText(node.value, `{${quote}${innerValue}${quote}}`); } } return null; }, messageId: 'requireBraces', node: node.value, }); } }, }; }, defaultOptions: [], meta: { docs: { description: 'Require curly braces around string literals in JSX attributes', }, fixable: 'code', messages: { requireBraces: 'String literals in JSX attributes should be wrapped in curly braces: {{attribute}}={"{{value}}"}', }, schema: [], type: 'suggestion', }, name: 'require-jsx-string-braces', });