reactjs - ESLint 导入插件的正则表达式规则始终将类型导入保持在底部
问题描述
我正在尝试将eslint-plugin-simple-import-sort
插件配置为在运行时自动对我的导入进行排序eslint --fix
。
我想始终将所有type
导入保持在底部。例如,对于给定的导入——</p>
import Link from 'next/link';
import type { NextPage } from 'next';
import type { NextRouter } from 'next/router';
import {
Box
} from '@chakra-ui/react';
import { MyCustomIcon } from '../../components/icons/custom';
我希望它们像这样重新排序——</p>
import Link from 'next/link';
import {
Box
} from '@chakra-ui/react';
import { MyCustomIcon } from '../../components/icons/custom';
import type { NextPage } from 'next';
import type { NextRouter } from 'next/router';
到目前为止,这是我在 ESLint 配置中设法编写的内容——</p>
{
"rules": {
"simple-import-sort/imports": [
"error",
{
"groups": [
[
// Node.js internals before everything.
"^(path)$",
// React comes first (except for type imports).
"^([react](?!\\u0000$))+$",
"^([react\\-dom](?!\\u0000$))+$",
// Next.js comes second (except for type imports).
"^(next/.*(?!\\u0000$))+$",
"^(next-.*(?!\\u0000$))+$",
// Important third-party stuff comes third.
"^(react-hook-form|fuse.js|(?!\\u0000$))+$",
// @emotion comes fourth.
"^@emotion/react$",
// @chakra-ui comes fifth.
"^@chakra-ui/react$",
// react-* packages come sixth.
"^([react-\\w](?!\\u0000$))+$",
// Every other third-party imports come next (except for type imports).
// "^@?\\w./\\-@"
"^([\\w./\\-@)](?!\\u0000$))+$"
],
[
// Parent imports. Put `..` last.
"^\\.\\.(?!/?$)",
"^\\.\\./?$",
// Other relative imports. Put same-folder imports and `.` last.
"^\\./(?=.*/)(?!/?$)",
"\\.(?!/?$)",
"^\\./?$",
// Style imports.
"^.+\\.css$",
// Image & other static asset imports.
"^.+\\.(svg|json)$"
],
[
// Type imports (`u0000$`) have to always be last.
"^@?(react).*\\u0000$",
"^@?(next).*\\u0000$",
"^@?(react-.*).*\\u0000$",
"^@?\\w.*\\u0000$",
"^[^.].*\\u0000$",
"^\\..*\\u0000$"
]
]
}
]
}
}
有了这个设置,我希望这条线import type { NextRouter } from 'next/router';
被推到底部,但事实并非如此。我正在努力使用正则表达式,因为我可以看到问题在于这种模式 - https://regexr.com/5qaon
^(next/.*(?!\\u0000$))+$
这似乎匹配所有内容,包括类型导入。我认为这是我的最终结果需要修改的模式;只要不以. .*
_\u0000
解决方案
推荐阅读
- graphql - 主键与 Amplify Schema 文件的关系
- javascript - React Material-UI Autocomplete:如何在更改另一个字段中的值时清除在自动完成字段中选择的多个值(mui 芯片)?
- javascript - Discord bot 不播放音频文件 (node.js)
- javascript - 尝试将表单结果导出到单个字段
- json - 编组为 JSON 时带有反斜杠的额外双引号
- java - 迭代器可以用于哪些数据结构?
- microsoft-teams - 将文件附加到 Microsoft Teams 上的频道
- vue.js - 如何在 vue3 中实现 debounce
- linux - “源”命令的参数有什么作用?
- c - 为什么“for”循环不起作用并且程序停止?