我遵循打字稿-eslint教程为TypeScript编写自定义TypeScript规则。我可以轻松地运行我用ts-node编写的单元测试(也是用TS编写的),但是我很难将这个规则添加到我的项目的.eslintrc配置文件中。
我尝试使用几个ESLint插件导入本地规则(例如,eslint插件-本地),但这些插件似乎不支持用TS直接编写的规则。我必须先把它转到JS吗?(如果是的话,具体情况如何)是否有办法在不发生转移溢出的情况下添加规则?
发布于 2022-11-17 19:10:34
我最近还在类型记录中构建了一个定制的eslint插件(作为monorepo的一部分),并在集成到monorepo和vscode中的应用程序时遇到了类似的问题。为了解决这个问题,我使用了节点寄存器函数,它负责在没有编译的情况下运行类型记录。
// rules/customRule.ts
import { ESLintUtils } from '@typescript-eslint/utils';
const rule = ESLintUtils.RuleCreator.withoutDocs({
create(context) {
// ...
},
meta: {
// ...
},
});
export default rule;// index.ts
import customRule from './rules/customRule';
const rules = {
"custom-rule": customRule,
};
export default rules;// index.js
/**
* This takes care of running the ts file with commonjs without running a
* compilation process first.
*/
require('ts-node').register()
const pkg = require('./package.json')
const rules = require('./index.ts').default
const pluginName = pkg.name.replace('eslint-plugin-', '')
module.exports = {
rules: rules,
configs: {
recommended: {
plugins: [pluginName],
rules: {
....
}
}
}
}这样,您现在可以在应用程序中使用这个插件,方法是将这个包添加到应用程序和eslintrc文件中。您需要注意的几件事情如下:
// tsconfig.json
{
"extends": "tsconfig/base.json",
"compilerOptions": {
"baseUrl": "./src",
"outDir": "./build",
"module": "esnext",
"noEmit": true,
.....
},
// Added this for typescript file used in custom eslint plugin
"ts-node": {
"compilerOptions": {
"module": "commonjs",
"types": ["node"]
},
"swc": true,
},
"include": ["src/**/*", "cypress/**/*", "./global.d.ts"]
}{
"devDependencies": {
"eslint-plugin-custom": "link:packages/eslint-plugin-custom"
}
}https://stackoverflow.com/questions/72619968
复制相似问题