|
| 1 | +/** |
| 2 | + * @fileoverview Enforce curly braces or disallow unnecessary curly brace in JSX |
| 3 | + * @author Jacky Ho |
| 4 | + */ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Constants |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +const OPTION_ALWAYS = 'always'; |
| 12 | +const OPTION_NEVER = 'never'; |
| 13 | +const OPTION_IGNORE = 'ignore'; |
| 14 | + |
| 15 | +const OPTION_VALUES = [ |
| 16 | + OPTION_ALWAYS, |
| 17 | + OPTION_NEVER, |
| 18 | + OPTION_IGNORE |
| 19 | +]; |
| 20 | +const DEFAULT_CONFIG = {props: OPTION_NEVER, children: OPTION_NEVER}; |
| 21 | + |
| 22 | +// ------------------------------------------------------------------------------ |
| 23 | +// Rule Definition |
| 24 | +// ------------------------------------------------------------------------------ |
| 25 | + |
| 26 | +module.exports = { |
| 27 | + meta: { |
| 28 | + docs: { |
| 29 | + description: |
| 30 | + 'Disallow unnecessary JSX expressions when literals alone are sufficient ' + |
| 31 | + 'or enfore JSX expressions on literals in JSX children or attributes', |
| 32 | + category: 'Stylistic Issues', |
| 33 | + recommended: false |
| 34 | + }, |
| 35 | + fixable: 'code', |
| 36 | + |
| 37 | + schema: [ |
| 38 | + { |
| 39 | + oneOf: [ |
| 40 | + { |
| 41 | + type: 'object', |
| 42 | + properties: { |
| 43 | + props: {enum: OPTION_VALUES, default: OPTION_NEVER}, |
| 44 | + children: {enum: OPTION_VALUES, default: OPTION_NEVER} |
| 45 | + }, |
| 46 | + additionalProperties: false |
| 47 | + }, |
| 48 | + { |
| 49 | + enum: OPTION_VALUES |
| 50 | + } |
| 51 | + ] |
| 52 | + } |
| 53 | + ] |
| 54 | + }, |
| 55 | + |
| 56 | + create: function(context) { |
| 57 | + const ruleOptions = context.options[0]; |
| 58 | + const userConfig = typeof ruleOptions === 'string' ? |
| 59 | + {props: ruleOptions, children: ruleOptions} : |
| 60 | + Object.assign({}, DEFAULT_CONFIG, ruleOptions); |
| 61 | + |
| 62 | + function containsBackslashForEscaping(rawStringValue) { |
| 63 | + return rawStringValue.includes('\\'); |
| 64 | + } |
| 65 | + |
| 66 | + function escapeDoubleQuotes(rawStringValue) { |
| 67 | + return rawStringValue.replace(/\\"/g, '"').replace(/"/g, '\\"'); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Report and fix an unnecessary curly brace violation on a node |
| 72 | + * @param {ASTNode} node - The AST node with an unnecessary JSX expression |
| 73 | + * @param {String} text - The text to replace the unnecessary JSX expression |
| 74 | + */ |
| 75 | + function reportUnnecessaryCurly(JSXExpressionNode) { |
| 76 | + context.report({ |
| 77 | + node: JSXExpressionNode, |
| 78 | + message: 'Curly braces are unnecessary here.', |
| 79 | + fix: function(fixer) { |
| 80 | + const expression = JSXExpressionNode.expression; |
| 81 | + const expressionType = expression.type; |
| 82 | + const parentType = JSXExpressionNode.parent.type; |
| 83 | + |
| 84 | + let textToReplace; |
| 85 | + if (parentType === 'JSXAttribute') { |
| 86 | + textToReplace = `"${escapeDoubleQuotes( |
| 87 | + expressionType === 'TemplateLiteral' ? |
| 88 | + expression.quasis[0].value.raw : |
| 89 | + expression.raw.substring(1, expression.raw.length - 1) |
| 90 | + )}"`; |
| 91 | + } else { |
| 92 | + textToReplace = expressionType === 'TemplateLiteral' ? |
| 93 | + expression.quasis[0].value.cooked : expression.value; |
| 94 | + } |
| 95 | + |
| 96 | + return fixer.replaceText(JSXExpressionNode, textToReplace); |
| 97 | + } |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + function reportMissingCurly(literalNode) { |
| 102 | + context.report({ |
| 103 | + node: literalNode, |
| 104 | + message: 'Need to wrap this literal in a JSX expression.', |
| 105 | + fix: function(fixer) { |
| 106 | + const expression = literalNode.parent.type === 'JSXAttribute' ? |
| 107 | + `{"${escapeDoubleQuotes( |
| 108 | + literalNode.raw.substring(1, literalNode.raw.length - 1) |
| 109 | + )}"}` : |
| 110 | + `{${JSON.stringify(literalNode.value)}}`; |
| 111 | + |
| 112 | + return fixer.replaceText(literalNode, expression); |
| 113 | + } |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + function lintUnnecessaryCurly(JSXExpressionNode) { |
| 118 | + const expression = JSXExpressionNode.expression; |
| 119 | + const expressionType = expression.type; |
| 120 | + const parentType = JSXExpressionNode.parent.type; |
| 121 | + |
| 122 | + if ( |
| 123 | + expressionType === 'Literal' && |
| 124 | + typeof expression.value === 'string' && ( |
| 125 | + parentType === 'JSXAttribute' || |
| 126 | + !containsBackslashForEscaping(expression.raw)) |
| 127 | + ) { |
| 128 | + reportUnnecessaryCurly(JSXExpressionNode); |
| 129 | + } else if ( |
| 130 | + expressionType === 'TemplateLiteral' && |
| 131 | + expression.expressions.length === 0 && ( |
| 132 | + parentType === 'JSXAttribute' || |
| 133 | + !containsBackslashForEscaping(expression.quasis[0].value.raw)) |
| 134 | + ) { |
| 135 | + reportUnnecessaryCurly(JSXExpressionNode); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + function areRuleConditionsSatisfied(parentType, config, ruleCondition) { |
| 140 | + return ( |
| 141 | + parentType === 'JSXAttribute' && |
| 142 | + typeof config.props === 'string' && |
| 143 | + config.props === ruleCondition |
| 144 | + ) || ( |
| 145 | + parentType === 'JSXElement' && |
| 146 | + typeof config.children === 'string' && |
| 147 | + config.children === ruleCondition |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + function shouldCheckForUnnecessaryCurly(parent, config) { |
| 152 | + const parentType = parent.type; |
| 153 | + |
| 154 | + // If there are more than one JSX child, there is no need to check for |
| 155 | + // unnecessary curly braces. |
| 156 | + if (parentType === 'JSXElement' && parent.children.length !== 1) { |
| 157 | + return false; |
| 158 | + } |
| 159 | + |
| 160 | + return areRuleConditionsSatisfied(parentType, config, OPTION_NEVER); |
| 161 | + } |
| 162 | + |
| 163 | + function shouldCheckForMissingCurly(parentType, config) { |
| 164 | + return areRuleConditionsSatisfied(parentType, config, OPTION_ALWAYS); |
| 165 | + } |
| 166 | + |
| 167 | + // -------------------------------------------------------------------------- |
| 168 | + // Public |
| 169 | + // -------------------------------------------------------------------------- |
| 170 | + |
| 171 | + return { |
| 172 | + JSXExpressionContainer: node => { |
| 173 | + const parent = node.parent; |
| 174 | + |
| 175 | + if (shouldCheckForUnnecessaryCurly(parent, userConfig)) { |
| 176 | + lintUnnecessaryCurly(node); |
| 177 | + } |
| 178 | + }, |
| 179 | + |
| 180 | + Literal: node => { |
| 181 | + const parentType = node.parent.type; |
| 182 | + |
| 183 | + if (shouldCheckForMissingCurly(parentType, userConfig)) { |
| 184 | + reportMissingCurly(node); |
| 185 | + } |
| 186 | + } |
| 187 | + }; |
| 188 | + } |
| 189 | +}; |
0 commit comments