在 eslint 规则中为 JSX.Element 类型的变量指定 Pascal 大小写命名约定
Specify PascalCase Naming Convetion for Variables of JSX.Element Type in eslint Rules
我想在我的 React+Typescript 项目中对 JSX.Element
类型的变量强制使用 PascalCase。我经常使用以下模式来创建功能组件,我喜欢通过给它一个 PascalCase 名称来区分我的同名导出:
//MyComponent.tsx
//PascalCase for MyComponent
export const MyComponent= (): JSX.Element =>{
return (
<div>
My Component
</div>
)
}
export default MyComponent
根据我当前的 linter 设置,我收到了 Variable name `MyComponent` must match one of the following formats: camelCase, UPPER_CASE
的警告。我如何在我的 linter 设置中添加规则,以对 JSX.Element 类型的变量强制执行 PascalCase?
这是我目前的.eslintrc.json
:
{
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": ["@typescript-eslint", "react-hooks"],
"extends": [
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended"
],
"rules": {
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn",
"react/prop-types": "off",
"@typescript-eslint/naming-convention": "warn",
"@typescript-eslint/explicit-function-return-type": [
"warn",
{
"allowExpressions": true
}
]
},
"settings": {
"react": {
"pragma": "React",
"version": "detect"
}
}
}
看起来目前在声明时是不可能的(参见下面的 link)。但是,不正确的大小写不能扩展到声明它的文件之外,因为小写名称是为 div
、span
等内部类型保留的
// my-component.tsx
export const myComponent: FC<{}> = () => (
<div>My Component</div>
);
export default myComponent;
// app.tsx
import { myComponent } from './my-component';
// Fails to compile with: Property 'myComponent' does not
// exist on type 'JSX.IntrinsicElements'. TS2339
const App = (): JSX.Element => <myComponent />;
在默认导出的情况下,声明文件中使用的名称被有效地删除,因为它只是分配给导入文件中选择的任何名称:
// app.tsx
import SomeOtherName from './my-component';
const App = (): JSX.Element => <SomeOtherName />;
关于没有特定于 React 组件的规则的讨论在这里:typescript-eslint/issues/2607
Additionally - there's no guarantee that a function that returns a
JSX.Element is in fact a react component. Utility functions that
return JSX.Element, but aren't react components are a relatively
common practice, which would cause false-positives.
编辑:以下内容不正确。
这不是组件的正确类型。
您的组件函数应该是类型“React.FunctionalComponent”(或其更短的别名“FC”)。这样一个组件 **returns** 一个 `ReactElement`。
import { FC } from 'react';
const MyComponent: FC<{}> = () => {
return (
<div>
My Component
</div>
)
}
我认为 eslint 没有针对这种情况的设置,但您可以创建自定义规则并应用它。请检查以下 link 是否相同。
我想在我的 React+Typescript 项目中对 JSX.Element
类型的变量强制使用 PascalCase。我经常使用以下模式来创建功能组件,我喜欢通过给它一个 PascalCase 名称来区分我的同名导出:
//MyComponent.tsx
//PascalCase for MyComponent
export const MyComponent= (): JSX.Element =>{
return (
<div>
My Component
</div>
)
}
export default MyComponent
根据我当前的 linter 设置,我收到了 Variable name `MyComponent` must match one of the following formats: camelCase, UPPER_CASE
的警告。我如何在我的 linter 设置中添加规则,以对 JSX.Element 类型的变量强制执行 PascalCase?
这是我目前的.eslintrc.json
:
{
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": ["@typescript-eslint", "react-hooks"],
"extends": [
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended"
],
"rules": {
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn",
"react/prop-types": "off",
"@typescript-eslint/naming-convention": "warn",
"@typescript-eslint/explicit-function-return-type": [
"warn",
{
"allowExpressions": true
}
]
},
"settings": {
"react": {
"pragma": "React",
"version": "detect"
}
}
}
看起来目前在声明时是不可能的(参见下面的 link)。但是,不正确的大小写不能扩展到声明它的文件之外,因为小写名称是为 div
、span
等内部类型保留的
// my-component.tsx
export const myComponent: FC<{}> = () => (
<div>My Component</div>
);
export default myComponent;
// app.tsx
import { myComponent } from './my-component';
// Fails to compile with: Property 'myComponent' does not
// exist on type 'JSX.IntrinsicElements'. TS2339
const App = (): JSX.Element => <myComponent />;
在默认导出的情况下,声明文件中使用的名称被有效地删除,因为它只是分配给导入文件中选择的任何名称:
// app.tsx
import SomeOtherName from './my-component';
const App = (): JSX.Element => <SomeOtherName />;
关于没有特定于 React 组件的规则的讨论在这里:typescript-eslint/issues/2607
Additionally - there's no guarantee that a function that returns a JSX.Element is in fact a react component. Utility functions that return JSX.Element, but aren't react components are a relatively common practice, which would cause false-positives.
编辑:以下内容不正确。
import { FC } from 'react';
const MyComponent: FC<{}> = () => {
return (
<div>
My Component
</div>
)
}
我认为 eslint 没有针对这种情况的设置,但您可以创建自定义规则并应用它。请检查以下 link 是否相同。