noJsxLiterals
¥Summary
-
规则生效日期:
v2.2.4¥Rule available since:
v2.2.4 -
诊断类别:
lint/nursery/noJsxLiterals¥Diagnostic Category:
lint/nursery/noJsxLiterals -
此规则没有修复方案。
¥This rule doesn’t have a fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
-
¥Same as
react/jsx-no-literals
-
¥How to configure
{ "linter": { "rules": { "nursery": { "noJsxLiterals": "error" } } }}¥Description
禁止在 JSX 元素中使用字符串字面量。
¥Disallow string literals inside JSX elements.
此规则不鼓励在 JSX 元素中直接使用字符串字面量。JSX 中的字符串字面量会使代码更难维护,尤其是在需要国际化或动态内容的应用中。
¥This rule discourages the use of string literals directly within JSX elements. String literals in JSX can make code harder to maintain, especially in applications that require internationalization or dynamic content.
¥Examples
¥Invalid
<div>Hello World</div>code-block.jsx:1:6 lint/nursery/noJsxLiterals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Incorrect use of string literal detected.
> 1 │ <div>Hello World</div>
│ ^^^^^^^^^^^
2 │
ℹ String literals in JSX can make code harder to maintain and internationalize.
ℹ Consider avoiding hardcoded strings entirely.
<>Welcome to our site</>code-block.jsx:1:3 lint/nursery/noJsxLiterals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Incorrect use of string literal detected.
> 1 │ <>Welcome to our site</>
│ ^^^^^^^^^^^^^^^^^^^
2 │
ℹ String literals in JSX can make code harder to maintain and internationalize.
ℹ Consider avoiding hardcoded strings entirely.
<span> Please enter your name</span>code-block.jsx:1:7 lint/nursery/noJsxLiterals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Incorrect use of string literal detected.
> 1 │ <span>
│
> 2 │ Please enter your name
> 3 │ </span>
│
4 │
ℹ String literals in JSX can make code harder to maintain and internationalize.
ℹ Consider avoiding hardcoded strings entirely.
¥Valid
<div>{'Hello World'}</div><>{'Welcome to our site'}</><span> {'Please enter your name'}</span><div>{`Hello ${name}`}</div>¥Options
noStrings
Section titled “noStrings”启用后,该规则还会标记 JSX 表达式和属性中的字符串字面量。
¥When enabled, the rule will also flag string literals inside JSX expressions and attributes.
默认:
false¥Default:
false
{ "linter": { "rules": { "nursery": { "noJsxLiterals": { "options": { "noStrings": true } } } } }}<span> {'Please enter your name'}</span>code-block.jsx:2:4 lint/nursery/noJsxLiterals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Incorrect use of string literal detected.
1 │ <span>
> 2 │ {‘Please enter your name’}
│ ^^^^^^^^^^^^^^^^^^^^^^^^
3 │ </span>
4 │
ℹ String literals in JSX can make code harder to maintain and internationalize.
ℹ Consider avoiding hardcoded strings entirely.
<Component title="Hello!" />code-block.jsx:1:18 lint/nursery/noJsxLiterals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Incorrect use of string literal detected.
> 1 │ <Component title=“Hello!” />
│ ^^^^^^^^
2 │
ℹ String literals in JSX can make code harder to maintain and internationalize.
ℹ Consider avoiding hardcoded strings entirely.
allowedStrings
Section titled “allowedStrings”允许作为字面量的字符串数组。对于不需要用表达式封装的常用单词或字符,这可能很有用。
¥An array of strings that are allowed as literals. This can be useful for common words or characters that don’t need to be wrapped in expressions.
{ "linter": { "rules": { "nursery": { "noJsxLiterals": { "options": { "allowedStrings": [ "Hello", " ", "·" ] } } } } }}<> <div>Hello</div> <div> </div> <div>·</div></>ignoreProps
Section titled “ignoreProps”启用后,该规则将忽略用作 prop 值的字符串字面量。
¥When enabled, the rule will ignore string literals used as prop values.
默认:
false¥Default:
false
{ "linter": { "rules": { "nursery": { "noJsxLiterals": { "options": { "ignoreProps": true } } } } }}<> <Component title="Welcome" /> <input placeholder="Enter name" /></>¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号