|
| 1 | +use biome_analyze::{ |
| 2 | + Ast, Rule, RuleDiagnostic, RuleSource, context::RuleContext, declare_lint_rule, |
| 3 | +}; |
| 4 | +use biome_console::markup; |
| 5 | +use biome_js_syntax::{ |
| 6 | + AnyJsExpression, AnyJsLiteralExpression, JsFileSource, JsStringLiteralExpression, JsxAttribute, |
| 7 | + JsxExpressionAttributeValue, JsxString, JsxText, inner_string_text, |
| 8 | +}; |
| 9 | +use biome_rowan::{AstNode, AstNodeList, TextRange, declare_node_union}; |
| 10 | +use biome_rule_options::no_jsx_literals::NoJsxLiteralsOptions; |
| 11 | + |
| 12 | +declare_lint_rule! { |
| 13 | + /// Disallow string literals inside JSX elements. |
| 14 | + /// |
| 15 | + /// This rule discourages the use of |
| 16 | + /// string literals directly within JSX elements. String literals in JSX can make code harder |
| 17 | + /// to maintain, especially in applications that require internationalization or dynamic content. |
| 18 | + /// |
| 19 | + /// ## Examples |
| 20 | + /// |
| 21 | + /// ### Invalid |
| 22 | + /// |
| 23 | + /// ```jsx,expect_diagnostic |
| 24 | + /// <div>Hello World</div> |
| 25 | + /// ``` |
| 26 | + /// |
| 27 | + /// ```jsx,expect_diagnostic |
| 28 | + /// <>Welcome to our site</> |
| 29 | + /// ``` |
| 30 | + /// |
| 31 | + /// ```jsx,expect_diagnostic |
| 32 | + /// <span> |
| 33 | + /// Please enter your name |
| 34 | + /// </span> |
| 35 | + /// ``` |
| 36 | + /// |
| 37 | + /// ### Valid |
| 38 | + /// |
| 39 | + /// ```jsx |
| 40 | + /// <div>{'Hello World'}</div> |
| 41 | + /// ``` |
| 42 | + /// |
| 43 | + /// ```jsx |
| 44 | + /// <>{'Welcome to our site'}</> |
| 45 | + /// ``` |
| 46 | + /// |
| 47 | + /// ```jsx |
| 48 | + /// <span> |
| 49 | + /// {'Please enter your name'} |
| 50 | + /// </span> |
| 51 | + /// ``` |
| 52 | + /// |
| 53 | + /// ```jsx |
| 54 | + /// <div>{`Hello ${name}`}</div> |
| 55 | + /// ``` |
| 56 | + /// |
| 57 | + /// ## Options |
| 58 | + /// |
| 59 | + /// ### `noStrings` |
| 60 | + /// |
| 61 | + /// When enabled, the rule will also flag string literals inside JSX expressions and attributes. |
| 62 | + /// |
| 63 | + /// > **Default:** `false` |
| 64 | + /// |
| 65 | + /// ```json,options |
| 66 | + /// { |
| 67 | + /// "options": { |
| 68 | + /// "noStrings": true |
| 69 | + /// } |
| 70 | + /// } |
| 71 | + /// ``` |
| 72 | + /// |
| 73 | + /// ```jsx,expect_diagnostic,use_options |
| 74 | + /// <span> |
| 75 | + /// {'Please enter your name'} |
| 76 | + /// </span> |
| 77 | + /// ``` |
| 78 | + /// ```jsx,expect_diagnostic,use_options |
| 79 | + /// <Component title="Hello!" /> |
| 80 | + /// ``` |
| 81 | + /// |
| 82 | + /// |
| 83 | + /// |
| 84 | + /// ### `allowedStrings` |
| 85 | + /// |
| 86 | + /// An array of strings that are allowed as literals. This can be useful for common words |
| 87 | + /// or characters that don't need to be wrapped in expressions. |
| 88 | + /// |
| 89 | + /// ```json,options |
| 90 | + /// { |
| 91 | + /// "options": { |
| 92 | + /// "allowedStrings": ["Hello", " ", "·"] |
| 93 | + /// } |
| 94 | + /// } |
| 95 | + /// ``` |
| 96 | + /// |
| 97 | + /// ```jsx,use_options |
| 98 | + /// <> |
| 99 | + /// <div>Hello</div> |
| 100 | + /// <div> </div> |
| 101 | + /// <div>·</div> |
| 102 | + /// </> |
| 103 | + /// ``` |
| 104 | + /// |
| 105 | + /// ### `ignoreProps` |
| 106 | + /// |
| 107 | + /// When enabled, the rule will ignore string literals used as prop values. |
| 108 | + /// |
| 109 | + /// > **Default:** `false` |
| 110 | + /// |
| 111 | + /// ```json,options |
| 112 | + /// { |
| 113 | + /// "options": { |
| 114 | + /// "ignoreProps": true |
| 115 | + /// } |
| 116 | + /// } |
| 117 | + /// ``` |
| 118 | + /// |
| 119 | + /// ```jsx,use_options |
| 120 | + /// <> |
| 121 | + /// <Component title="Welcome" /> |
| 122 | + /// <input placeholder="Enter name" /> |
| 123 | + /// </> |
| 124 | + /// ``` |
| 125 | + /// |
| 126 | + pub NoJsxLiterals { |
| 127 | + version: "next", |
| 128 | + name: "noJsxLiterals", |
| 129 | + language: "jsx", |
| 130 | + recommended: false, |
| 131 | + sources: &[RuleSource::EslintReact("jsx-no-literals").same()], |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +impl Rule for NoJsxLiterals { |
| 136 | + type Query = Ast<AnyJsxText>; |
| 137 | + type State = TextRange; |
| 138 | + type Signals = Option<Self::State>; |
| 139 | + type Options = NoJsxLiteralsOptions; |
| 140 | + |
| 141 | + fn run(ctx: &RuleContext<Self>) -> Self::Signals { |
| 142 | + let file_source = ctx.source_type::<JsFileSource>(); |
| 143 | + if !file_source.is_jsx() { |
| 144 | + return None; |
| 145 | + } |
| 146 | + |
| 147 | + let node = ctx.query(); |
| 148 | + let options = ctx.options(); |
| 149 | + |
| 150 | + if options.ignore_props |
| 151 | + && node |
| 152 | + .syntax() |
| 153 | + .ancestors() |
| 154 | + .skip(1) |
| 155 | + .any(|n| JsxAttribute::can_cast(n.kind())) |
| 156 | + { |
| 157 | + return None; |
| 158 | + } |
| 159 | + |
| 160 | + let value_token = match node { |
| 161 | + AnyJsxText::JsxText(text) => text.value_token().ok()?, |
| 162 | + AnyJsxText::JsStringLiteralExpression(expression) => { |
| 163 | + if !options.no_strings { |
| 164 | + return None; |
| 165 | + } |
| 166 | + expression.value_token().ok()? |
| 167 | + } |
| 168 | + AnyJsxText::JsxString(string) => { |
| 169 | + if !options.no_strings { |
| 170 | + return None; |
| 171 | + } |
| 172 | + string.value_token().ok()? |
| 173 | + } |
| 174 | + AnyJsxText::JsxExpressionAttributeValue(expression) => { |
| 175 | + if !options.no_strings { |
| 176 | + return None; |
| 177 | + } |
| 178 | + let expression = expression.expression().ok()?; |
| 179 | + match expression { |
| 180 | + AnyJsExpression::AnyJsLiteralExpression( |
| 181 | + AnyJsLiteralExpression::JsStringLiteralExpression(string_literal), |
| 182 | + ) => string_literal.value_token().ok()?, |
| 183 | + AnyJsExpression::JsTemplateExpression(expression) => { |
| 184 | + return if expression.elements().len() <= 1 { |
| 185 | + Some(expression.range()) |
| 186 | + } else { |
| 187 | + None |
| 188 | + }; |
| 189 | + } |
| 190 | + |
| 191 | + _ => return None, |
| 192 | + } |
| 193 | + } |
| 194 | + }; |
| 195 | + |
| 196 | + for allowed_string in &options.allowed_strings { |
| 197 | + if inner_string_text(&value_token) == allowed_string.as_ref() { |
| 198 | + return None; |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + if inner_string_text(&value_token).trim().is_empty() { |
| 203 | + return None; |
| 204 | + } |
| 205 | + |
| 206 | + Some(value_token.text_trimmed_range()) |
| 207 | + } |
| 208 | + |
| 209 | + fn diagnostic(_ctx: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> { |
| 210 | + Some( |
| 211 | + RuleDiagnostic::new( |
| 212 | + rule_category!(), |
| 213 | + state, |
| 214 | + markup! { |
| 215 | + "Incorrect use of string literal detected." |
| 216 | + }, |
| 217 | + ) |
| 218 | + .note(markup! { |
| 219 | + "String literals in JSX can make code harder to maintain and internationalize." |
| 220 | + }) |
| 221 | + .note(markup! { |
| 222 | + "Consider avoiding hardcoded strings entirely." |
| 223 | + }), |
| 224 | + ) |
| 225 | + } |
| 226 | +} |
| 227 | + |
| 228 | +declare_node_union! { |
| 229 | + pub AnyJsxText = JsxText |
| 230 | + | JsStringLiteralExpression |
| 231 | + | JsxString |
| 232 | + | JsxExpressionAttributeValue |
| 233 | +} |
0 commit comments