简体   繁体   English

Angular项目返回类型的esLint规则

[英]esLint rule for return type of the Angular project

I would like to have an esLint rule to notify about the return type of the method.我想要一个esLint规则来通知方法的返回类型。 But I was not able to do that.但我无法做到这一点。 I have tried this "@angular-eslint/explicit-function-return-type": "on" But no effect.Any clue and also good rules for the new Angular project?我试过这个"@angular-eslint/explicit-function-return-type": "on"但没有效果。新的 Angular 项目有什么线索和好的规则吗?

I would like to have an error (rule) here hence no return type;我想在这里有一个错误(规则),因此没有返回类型;

signUp() 
{
}

I like to adjust it like so after warning or error我喜欢在警告或错误后调整它

signUp() : void
{
}

MRE: MRE:

Please see it here: https://github.com/Sampath-Lokuge/esLint请在此处查看: https://github.com/Sampath-Lokuge/esLint

This is an Ionic/ Angular project.这是一个Ionic/ Angular项目。 You can see this file: https://github.com/Sampath-Lokuge/esLint/blob/main/src/app/folder/folder.page.ts你可以看到这个文件: https://github.com/Sampath-Lokuge/esLint/blob/main/src/app/folder/folder.page.ts

I have default file like so:我有这样的默认文件:

"@angular/core": "~11.2.0",

.eslintrc.json

{
  "root": true,
  "ignorePatterns": [
    "projects/**/*"
  ],
  "overrides": [
    {
      "files": [
        "*.ts"
      ],
      "parserOptions": {
        "project": [
          "tsconfig.json",
          "e2e/tsconfig.json"
        ],
        "createDefaultProgram": true
      },
      "extends": [
        "plugin:@angular-eslint/ng-cli-compat",
        "plugin:@angular-eslint/ng-cli-compat--formatting-add-on",
        "plugin:@angular-eslint/template/process-inline-templates"
      ],
      "rules": {
        "@angular-eslint/component-class-suffix": [
          "error",
          {
            "suffixes": [
              "Page",
              "Component"
            ]
          }
        ],
        "@angular-eslint/component-selector": [
          "error",
          {
            "type": "element",
            "prefix": "app",
            "style": "kebab-case"
          }
        ],
        "@angular-eslint/directive-selector": [
          "error",
          {
            "type": "attribute",
            "prefix": "app",
            "style": "camelCase"
          }
        ]
      }
    },
    {
      "files": [
        "*.html"
      ],
      "extends": [
        "plugin:@angular-eslint/template/recommended"
      ],
      "rules": {}
    }
  ]
}

package.json

{
  "name": "A",
  "version": "0.0.1",
  "author": "A",
  "homepage": "A",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^11.2.13",
    "@angular/common": "~11.2.0",
    "@angular/core": "~11.2.0",
    "@angular/forms": "~11.2.0",
    "@angular/platform-browser": "~11.2.0",
    "@angular/platform-browser-dynamic": "~11.2.0",
    "@angular/router": "~11.2.0",
    "@capacitor/core": "2.4.7",
    "@ionic/angular": "^5.5.2",
    "aws-amplify": "^3.3.27",
    "primeicons": "^4.1.0",
    "primeng": "^11.4.0",
    "rxjs": "~6.6.0",
    "tslib": "^2.0.0",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.1102.4",
    "@angular-eslint/builder": "2.0.2",
    "@angular-eslint/eslint-plugin": "2.0.2",
    "@angular-eslint/eslint-plugin-template": "2.0.2",
    "@angular-eslint/template-parser": "2.0.2",
    "@angular/cli": "~11.2.4",
    "@angular/compiler": "~11.2.0",
    "@angular/compiler-cli": "~11.2.0",
    "@angular/language-service": "~11.2.0",
    "@capacitor/cli": "2.4.7",
    "@ionic/angular-toolkit": "^3.1.1",
    "@types/node": "^12.11.1",
    "@typescript-eslint/eslint-plugin": "4.16.1",
    "@typescript-eslint/parser": "4.16.1",
    "eslint": "^7.6.0",
    "eslint-plugin-import": "2.22.1",
    "eslint-plugin-jsdoc": "30.7.6",
    "eslint-plugin-prefer-arrow": "1.2.2",
    "ts-node": "~8.3.0",
    "typescript": "~4.0.2"
  },
  "description": "A"
}

This is a typescript-eslint rule so in your eslintrc file, you need to add the following rule to the rules section:这是一个 typescript-eslint 规则,因此在您的 eslintrc 文件中,您需要将以下规则添加到规则部分:

"rules": {
  ...
  "@typescript-eslint/explicit-function-return-type": ["error"]
}

Note, to turn this feature on, you provide an array of options including the type of error/warning instead of "on".请注意,要打开此功能,您需要提供一系列选项,包括错误/警告的类型,而不是“打开”。

Do you have prettier installed as well?你也安装了prettier吗? Here is what I use for IntelliJ and it works for the purpose you are requesting.这是我用于 IntelliJ 的内容,它适用于您要求的目的。 Could you also post your package.json perhaps?您也可以发布您的 package.json 吗?

module.exports = {
  parser: '@typescript-eslint/parser',
  extends: ['plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended'],
  parserOptions: {
    ecmaVersion: 2018,
    sourceType: 'module',
  },
  rules: {
    '@typescript-eslint/array-type': ['error', { default: 'array' }],
  },
};

EDIT: After looking at your package.json I would suggest installing prettier.编辑:查看您的 package.json 后,我建议安装更漂亮的。

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

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