【问题标题】:Did Firebase Cloud Functions ESLint change recently?Firebase Cloud Functions ESLint 最近是否发生了变化?
【发布时间】:2021-04-12 20:33:28
【问题描述】:

几个月前我用firebase创建了一个云功能项目,并使用了linting。

我最近使用 linting 创建了一个新的云函数项目,现在 linter 抱怨我从未设置的随机规则。我不记得它在几个月前执行了几乎数量的样式规则。

类似的东西:

This line has a length of 95. Maximum allowed is 80
Missing JSDoc comment
Missing Trailing comma
expected indentation of 2 spaces but found 4
Strings must use singlequote

它也不让我使用 async/await。

我发现我可以在我的 .eslintrc.js 文件中单独设置这些规则,但这很烦人,我不想这样做。默认情况下,为什么不禁用这些规则?我只想要确保我的代码在运行时不会失败的基本规则,而不是像单引号/双引号和最大行长这样的随机样式偏好。

有什么方法可以将基本的 linting 功能与 firebase 函数一起使用?

【问题讨论】:

  • 它们在 9.1.1 中确实发生了变化,但您可以随意设置 rules - 请务必阅读该文档。您必须自己决定“基本 linting 功能”的实际含义。但是没有 linter 会告诉你你的代码是否绝对不会失败。您必须运行它才能找到答案。

标签: firebase google-cloud-functions eslint


【解决方案1】:

您可以去掉 google extends 值,但我建议保留它并关闭最困扰您的规则,对我来说是缩进和最大长度(行数):

module.exports = {
  root: true,
  env: {
    es6: true,
    node: true,
  },
  extends: [
    "eslint:recommended",
    "google",
  ],
  rules: {
    "quotes": ["error", "double"],
    "indent": ["off"],
    "max-len": ["off"],
  },
};

对于对此感到困惑的任何人,您可以编辑 Cloud Functions 文件夹中的 lint 配置文件。在这个答案中,该文件被命名为.eslintrc.js

【讨论】:

    【解决方案2】:

    我遇到了和你一样的问题。新的、更严格的 linting 规则似乎来自 Firebase 函数现在默认使用“google” eslint 基本配置插件这一事实。阅读更多关于配置 ESLint 插件的信息in the docs。我的旧 Firebase 函数使用 tslint 没有问题。

    当我从 eslint 收到样式错误时,我的 .eslintrc.js 文件如下所示:

    module.exports = {
        env: {
            es6: true,
            node: true,
        },
        extends: [
            'eslint:recommended',
            'plugin:import/errors',
            'plugin:import/warnings',
            'plugin:import/typescript',
            'google',
        ],
        parser: '@typescript-eslint/parser',
        parserOptions: {
            project: ['tsconfig.json', 'tsconfig.dev.json'],
            sourceType: 'module',
        },
        ignorePatterns: [
            '/lib/**/*', // Ignore built files.
        ],
        plugins: ['@typescript-eslint', 'import'],
        rules: {
            quotes: ['error', 'double'],
        },
    };
    

    我从 extends 属性中删除了“google”,这似乎解决了几乎所有的样式 linting 问题。

    现在看起来像这样:

    module.exports = {
        env: {
            es6: true,
            node: true,
        },
        extends: [
            'eslint:recommended',
            'plugin:import/errors',
            'plugin:import/warnings',
            'plugin:import/typescript',
        ],
        parser: '@typescript-eslint/parser',
        parserOptions: {
            project: ['tsconfig.json', 'tsconfig.dev.json'],
            sourceType: 'module',
        },
        ignorePatterns: [
            '/lib/**/*', // Ignore built files.
        ],
        plugins: ['@typescript-eslint', 'import'],
        rules: {
            quotes: ['error', 'double'],
        },
    };
    

    【讨论】:

    • 我还必须摆脱规则对象。我很困惑为什么(就像谷歌为什么这样做)一方面你可以打开 tslint 强制执行,如果使用双引号会给出 linting 错误,但是如果使用单引号,这个新的默认值会给出错误...... .
    猜你喜欢
    • 2012-12-18
    • 1970-01-01
    • 2022-09-23
    • 2018-09-22
    • 2014-05-13
    • 2019-03-06
    • 2012-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多