简体   繁体   English

为什么在尝试使用eslint nodejs api时未应用eslint插件?

[英]Why eslint plugins are not applied when I am trying to use eslint nodejs api?

I want to use eslint api in my node js script. 我想在我的节点js脚本中使用eslint api。 I found this: https://eslint.org/docs/developer-guide/nodejs-api . 我发现了这一点: https : //eslint.org/docs/developer-guide/nodejs-api So I am trying to create CLIEngine, load config and lint something using API. 所以我正在尝试创建CLIEngine,加载配置并使用API​​进行处理。

When I am trying to lint some code using API I am getting error: Definition for rule <ruleName> was not found. 当我尝试使用API Definition for rule <ruleName> was not found.某些代码时,出现错误: Definition for rule <ruleName> was not found. .

You can watch code in this repo: https://github.com/sharikovvladislav/eslint-try-api-with-plugins 您可以在此仓库中查看代码: https : //github.com/sharikovvladislav/eslint-try-api-with-plugins

Here is whole my code: 这是我的全部代码:

const CLIEngine = require('eslint').CLIEngine;
const Linter = require('eslint').Linter;
const path = require('path');

const cli = new CLIEngine({
    configPath: path.resolve(process.cwd(), '.eslintrc.json')
});

const linter = new Linter();

const result = linter.verifyAndFix('var foo', cli.getConfigForFile("script.js"));

console.log(result);

In result I get something like this: 结果我得到这样的东西:

 ~/tmp/test-eslint-with-plugins  node script.js
{ fixed: false,
  messages:
   [ { ruleId: 'node/no-extraneous-require',
       message:
        'Definition for rule \'node/no-extraneous-require\' was not found.',
       line: 1,
       column: 1,
       endLine: 1,
       endColumn: 2,
       severity: 2,
       nodeType: null }
  // ....
]
}

I have this in my package dependency: 我的包依赖项中包含以下内容:

    "eslint": "^6.1.0",
    "eslint-plugin-node": "^9.1.0"

This is my eslintrc: 这是我的eslintrc:

{
    "extends": [
        "eslint:recommended",
        "plugin:node/recommended"
    ],
    "rules": {
        "node/exports-style": ["error", "module.exports"],
        "node/file-extension-in-import": ["error", "always"],
        "node/prefer-global/buffer": ["error", "always"],
        "node/prefer-global/console": ["error", "always"],
        "node/prefer-global/process": ["error", "always"],
        "node/prefer-global/url-search-params": ["error", "always"],
        "node/prefer-global/url": ["error", "always"],
        "node/prefer-promises/dns": "error",
        "node/prefer-promises/fs": "error"
    }
}

So I specified some plugins and rules. 所以我指定了一些插件和规则。 Looks like everything is correct. 看起来一切正确。

I tried to debug a bit and I realised that CLI is created (looks like) normally. 我尝试调试一下,然后意识到CLI是正常创建的(看起来像)。 Also, config file parsed correctly. 此外,配置文件已正确解析。 I can see this in config properties: 我可以在配置属性中看到这一点:

cli.getConfigForFile("script.js").plugins
>> ["node"]
Object.keys(cli.getConfigForFile("script.js").rules).filter(key => key.startsWith('node'))
>> ["node/exports-style", "node/file-extension-in-import", "node/prefer-global/buffer", "node/prefer-global/console", "node/prefer-global/process", "node/prefer-global/url-search-params", "node/prefer-global/url", "node/prefer-promises/dns", "node/prefer-promises/fs", "node/no-deprecated-api", "node/no-extraneous-require", "node/no-missing-require", "node/no-unpublished-bin", "node/no-unpublished-require", "node/no-unsupported-features/es-builtins", "node/no-unsupported-features/es-syntax", "node/no-unsupported-features/node-builtins", "node/process-exit-as-throw", "node/shebang", "node/no-extraneous-import", "node/no-missing-import", "node/no-unpublished-import"]

Why do I get Definition for rule \\'node/no-extraneous-require\\' was not found. 为什么Definition for rule \\'node/no-extraneous-require\\' was not found. error (and other errors like this)? 错误(以及其他类似错误)? What I am missing? 我缺少什么?

Main point is not using Linter/linter at all. 要点是根本不使用棉绒/棉绒。

The answer is here: https://github.com/eslint/eslint/issues/12030#issuecomment-516214604 答案在这里: https : //github.com/eslint/eslint/issues/12030#issuecomment-516214604

Linter class is designed to work in browsers, so it doesn't depend on any file system. Linter类旨在在浏览器中工作,因此它不依赖于任何文件系统。 This means that the Linter doesn't find/load any plugins, parsers, and shareable configs. 这意味着Linter不会找到/加载任何插件,解析器和可共享的配置。 You have to prepare those manually by linter.defineRule method and something like. 您必须通过linter.defineRule方法之类手动进行准备。

Here is full code: 这是完整的代码:

const cli = new CLIEngine({
    configPath: path.resolve(process.cwd(), '.eslintrc.json'),
    // eslint --fix *
    fix: true
});

// run lint & get output for fix
const report = cli.executeOnFiles([path.resolve(process.cwd(), 'test.js')]);

// log about report
console.log('>> report', report);

// write fixed files to fs
CLIEngine.outputFixes(report);

Here is full code about how to fix it in my case (github): https://github.com/sharikovvladislav/eslint-try-api-with-plugins/pull/1 这是有关如何在我的情况下解决它的完整代码(github): https : //github.com/sharikovvladislav/eslint-try-api-with-plugins/pull/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM