Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#174416949] Replace tslint with eslint #752

Merged
merged 13 commits into from Jan 22, 2021

Conversation

BurnedMarshal
Copy link
Contributor

@BurnedMarshal BurnedMarshal commented Jan 8, 2021

List of Changes

  • Replace tslint and italia-tslint-rules with @pagopa/eslint-config
  • Add eslint configuration from @pagopa/eslint-config/strong
  • Autofix all autofixable rules
  • Replace tslint:disable with eslint-disable
  • Change lint script inside package.json

Motivation and Context

TSLint has been deprecated in favour of ESLint. This PR replace TSLint with ESLint and italia-tslint-rules with the new package @pagopa/eslint-config. Some rules from @pagopa/eslint-config/strong has been disabled to minimize code refactoring.

How Has This Been Tested?

  • Local yarn lint and yarn build execution
  • Local yarn test execution

Types of changes

  • Chore
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

Question:

Can we enable eslint even on .test.ts files? So we could have the same code style restrictions on tests.

@pagopa-github-bot
Copy link
Contributor

pagopa-github-bot commented Jan 8, 2021

Affected stories

  • ⚙️ #174416949: Sostituire TSLint con ESLint nei nostri progetti

New dependencies added: @pagopa/eslint-config and eslint-plugin-prettier.

@pagopa/eslint-config

Author: Unknown

Description: This package provide the following ESLint custom rules for Typescript projects.

Homepage: https://github.com/pagopa/eslint-rules#readme

Created14 days ago
Last Updatedabout 1 hour ago
LicenseMIT
Maintainers5
Releases3
Direct Dependencies@typescript-eslint/eslint-plugin, @typescript-eslint/eslint-plugin-tslint, @typescript-eslint/parser, eslint, eslint-config-prettier, eslint-plugin-extra-rules, eslint-plugin-fp, eslint-plugin-functional, eslint-plugin-import, eslint-plugin-jsdoc, eslint-plugin-no-credentials, eslint-plugin-prefer-arrow, eslint-plugin-prettier, eslint-plugin-react, eslint-plugin-sonarjs and prettier
README

PagoPA ESLint config

This package provide the following ESLint custom rules for Typescript projects.

  • recommendend
  • react
  • strong

This repository replace italia-tslint-rules after TSLint deprecation.

The following TSLint rules (included inside italia-tslint-rules) are not supported for eslint at the moment and are missing in this package:

bool-param-default
max-union-size
no-accessor-field-mismatch
no-array-delete // Mitigated by no-delete
no-case-with-or
no-dead-store
no-duplicate-in-composite
no-empty-array
no-extra-semicolon // Mitigated by prettier
no-empty-destructuring // Mititgated by no-empty-pattern
no-gratuitous-expressions
no-hardcoded-credentials
no-ignored-initial-value // Mitigated by no-param-reassign, no-let
no-in-misuse
no-inferred-empty-object-type
no-invalid-await // Mititgated by await-thenable
no-invariant-return
no-misleading-array-reverse // Mitigated by immutable-data
no-misspelled-operator // Mitigated by prettier
no-nested-switch
no-nested-template-literals
no-statements-same-line // Mitigated by prettier
no-try-promise
no-tslint-disable-all
no-unconditional-jump
no-undefined-argument
no-unenclosed-multiline-block // Mitigated by prettier
no-unthrown-error
no-unused-array // Mitigated by no-unused-vars
no-useless-increment
no-useless-intersection
prefer-promise-shorthand
promise-must-complete
use-primitive-type

This list has been produced following these steps:

  1. Follow TSLint to ESLint migration guide on the project io-backend
  2. Running npx tslint-to-eslint-config that produces a list of TSLint rules not available for ESLint (77 rules at the moment)
  3. Manually check each one for an alternative ESLint rules/plugins
  4. Verify each alternative ESLint rule/plugin on a testing file

Usage

Installation and Configuration

To use this package install as devDependecy inside any typescript project with

yarn install -D @pagopa/eslint-config

Create on the project an .eslintrc.js file with the following content

module.exports = {
  "extends": [
    "@pagopa/eslint-config/strong",
  ],
  "rules": {
    // Any project level custom rule
  }
}

Add inside the package.json file a lint and optionally a lint-autofix script as:

"scripts": {
  "lint": "eslint . -c .eslintrc.js --ext .ts,.tsx",
  "lint-autofix": "eslint . -c .eslintrc.js --ext .ts,.tsx --fix",
  ...
}

Migration from TSLint

Remove from the package.json every tslint reference:

  • tslint
  • italia-tslint-rules

Replace all the // tslint:disable-next-line with the proper // eslint-disable-next-line comment. If are present some // tslint:disable replace it with /* eslint-disable */ at the top of the file.

If you need to disable ESLint for some files create .eslintignore file with the list of folders or files that must be excluded from lint process. Copy the exclusion from tslint.json linterOptions.exclude

Delete all tslint related files (es. tslint.json).

Run yarn lint-autofix to refactorize the code automatically with all the auto-fixable ESLint rules.

eslint-plugin-prettier

Author: Teddy Katz

Description: Runs prettier as an eslint rule

Homepage: https://github.com/prettier/eslint-plugin-prettier#readme

Createdalmost 4 years ago
Last Updated18 days ago
LicenseMIT
Maintainers3
Releases25
Direct Dependenciesprettier-linter-helpers
Keywordseslint, eslintplugin, eslint-plugin and prettier
README

eslint-plugin-prettier Build Status

Runs Prettier as an ESLint rule and reports differences as individual ESLint issues.

If your desired formatting does not match Prettier’s output, you should use a different tool such as prettier-eslint instead.

Sample

error: Insert `,` (prettier/prettier) at pkg/commons-atom/ActiveEditorRegistry.js:22:25:
  20 | import {
  21 |   observeActiveEditorsDebounced,
> 22 |   editorChangesDebounced
     |                         ^
  23 | } from './debounced';;
  24 |
  25 | import {observableFromSubscribeFunction} from '../commons-node/event';


error: Delete `;` (prettier/prettier) at pkg/commons-atom/ActiveEditorRegistry.js:23:21:
  21 |   observeActiveEditorsDebounced,
  22 |   editorChangesDebounced
> 23 | } from './debounced';;
     |                     ^
  24 |
  25 | import {observableFromSubscribeFunction} from '../commons-node/event';
  26 | import {cacheWhileSubscribed} from '../commons-node/observable';


2 errors found.

./node_modules/.bin/eslint --format codeframe pkg/commons-atom/ActiveEditorRegistry.js (code from nuclide).

Installation

npm install --save-dev eslint-plugin-prettier
npm install --save-dev --save-exact prettier

eslint-plugin-prettier does not install Prettier or ESLint for you. You must install these yourself.

Then, in your .eslintrc.json:

{
  "plugins": ["prettier"],
  "rules": {
    "prettier/prettier": "error"
  }
}

Recommended Configuration

This plugin works best if you disable all other ESLint rules relating to code formatting, and only enable rules that detect potential bugs. (If another active ESLint rule disagrees with prettier about how code should be formatted, it will be impossible to avoid lint errors.) You can use eslint-config-prettier to disable all formatting-related ESLint rules.

This plugin ships with a plugin:prettier/recommended config that sets up both the plugin and eslint-config-prettier in one go.

  1. In addition to the above installation instructions, install eslint-config-prettier:

    npm install --save-dev eslint-config-prettier
  2. Then you need to add plugin:prettier/recommended as the last extension in your .eslintrc.json:

    {
      "extends": ["plugin:prettier/recommended"]
    }

    You can then set Prettier's own options inside a .prettierrc file.

  3. Some ESLint plugins (such as eslint-plugin-react) also contain rules that conflict with Prettier. Add extra exclusions for the plugins you use like so:

    {
      "extends": [
        "plugin:prettier/recommended",
        "prettier/flowtype",
        "prettier/react"
      ]
    }

    For the list of every available exclusion rule set, please see the readme of eslint-config-prettier.

Exactly what does plugin:prettier/recommended do? Well, this is what it expands to:

{
  "extends": ["prettier"],
  "plugins": ["prettier"],
  "rules": {
    "prettier/prettier": "error",
    "arrow-body-style": "off",
    "prefer-arrow-callback": "off"
  }
}
  • "extends": ["prettier"] enables the main config from eslint-config-prettier, which turns off some ESLint core rules that conflict with Prettier.
  • "plugins": ["prettier"] registers this plugin.
  • "prettier/prettier": "error" turns on the rule provided by this plugin, which runs Prettier from within ESLint.
  • "arrow-body-style": "off" and "prefer-arrow-callback": "off" turns off two ESLint core rules that unfortunately are problematic with this plugin – see the next section.

arrow-body-style and prefer-arrow-callback issue

If you use arrow-body-style or prefer-arrow-callback together with the prettier/prettier rule from this plugin, you can in some cases end up with invalid code due to a bug in ESLint’s autofix – see issue #65.

For this reason, it’s recommended to turn off these rules. The plugin:prettier/recommended config does that for you.

You can still use these rules together with this plugin if you want, because the bug does not occur all the time. But if you do, you need to keep in mind that you might end up with invalid code, where you manually have to insert a missing closing parenthesis to get going again.

If you’re fixing large of amounts of previously unformatted code, consider temporarily disabling the prettier/prettier rule and running eslint --fix and prettier --write separately.

Options

Note: While it is possible to pass options to Prettier via your ESLint configuration file, it is not recommended because editor extensions such as prettier-atom and prettier-vscode will read .prettierrc, but won't read settings from ESLint, which can lead to an inconsistent experience.

  • The first option:

    • An object representing options that will be passed into prettier. Example:

      "prettier/prettier": ["error", {"singleQuote": true, "parser": "flow"}]

      NB: This option will merge and override any config set with .prettierrc files

  • The second option:

    • An object with the following options

      • usePrettierrc: Enables loading of the Prettier configuration file, (default: true). May be useful if you are using multiple tools that conflict with each other, or do not wish to mix your ESLint settings with your Prettier configuration.

        "prettier/prettier": ["error", {}, {
          "usePrettierrc": false
        }]
      • fileInfoOptions: Options that are passed to prettier.getFileInfo to decide whether a file needs to be formatted. Can be used for example to opt-out from ignoring files located in node_modules directories.

        "prettier/prettier": ["error", {}, {
          "fileInfoOptions": {
            "withNodeModules": true
          }
        }]
  • The rule is autofixable -- if you run eslint with the --fix flag, your code will be formatted according to prettier style.


Contributing

See CONTRIBUTING.md

Generated by 🚫 dangerJS

@codecov
Copy link

codecov bot commented Jan 8, 2021

Codecov Report

Merging #752 (f8e5a19) into master (f523227) will decrease coverage by 0.30%.
The diff coverage is 79.48%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #752      +/-   ##
==========================================
- Coverage   80.16%   79.85%   -0.31%     
==========================================
  Files          70       71       +1     
  Lines        2319     2328       +9     
  Branches      369      372       +3     
==========================================
  Hits         1859     1859              
- Misses        447      456       +9     
  Partials       13       13              
Impacted Files Coverage Δ
src/clients/api.ts 71.42% <0.00%> (ø)
src/controllers/bonusController.ts 100.00% <ø> (ø)
src/controllers/messagesController.ts 100.00% <ø> (ø)
src/controllers/profileController.ts 100.00% <ø> (ø)
src/controllers/servicesController.ts 83.33% <ø> (ø)
src/controllers/userDataProcessingController.ts 83.33% <ø> (ø)
src/server.ts 0.00% <0.00%> (ø)
src/services/apiClientFactory.ts 71.42% <ø> (ø)
src/services/redisUserMetadataStorage.ts 85.41% <ø> (ø)
src/services/userDataProcessingService.ts 67.39% <ø> (ø)
... and 49 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6367908...f8e5a19. Read the comment docs.

@gunzip
Copy link
Contributor

gunzip commented Jan 11, 2021

lgtm two things:

  1. can you write upgrading notes in the readme here: https://github.com/pagopa/eslint-rules/ ?
  2. if there are "scriptable" things (ie. renaming comments to disable tslint -> eslint), can you add the script into eslint-rules repo?

@BurnedMarshal
Copy link
Contributor Author

lgtm two things:

  1. can you write upgrading notes in the readme here: https://github.com/pagopa/eslint-rules/ ?
  2. if there are "scriptable" things (ie. renaming comments to disable tslint -> eslint), can you add the script into eslint-rules repo?

@gunzip About 1 a more detailed REAME was provided pagopa/eslint-rules/pull/3

@BurnedMarshal
Copy link
Contributor Author

lgtm two things:

  1. can you write upgrading notes in the readme here: https://github.com/pagopa/eslint-rules/ ?
  2. if there are "scriptable" things (ie. renaming comments to disable tslint -> eslint), can you add the script into eslint-rules repo?

@gunzip About 2 maybe could be used this package (link) for comment conversion. If you like it we could add reference to this package inside the README of @pagopa/eslint-rules

@gunzip
Copy link
Contributor

gunzip commented Jan 11, 2021

@gunzip About 2 maybe could be used this package (link) for comment conversion. If you like it we could add reference to this package inside the README of @pagopa/eslint-rules

yup

Comment on lines +7 to +8
**/__tests__/*
**/__mocks__/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't use eslint on those files as well? I mean: I see the point of not having such files to block a build, but also I'd like to enforce code conventions on test code (and, to use prettier)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea. But enable the linter on these files generate 330 problems because tslint was previously disabled on mocks and tests.

Comment on lines +125 to +133
readonly allowNotifyIPSourceRange: ReadonlyArray<CIDR>;
readonly allowPagoPAIPSourceRange: ReadonlyArray<CIDR>;
readonly allowMyPortalIPSourceRange: ReadonlyArray<CIDR>;
readonly allowBPDIPSourceRange: ReadonlyArray<CIDR>;
readonly allowSessionHandleIPSourceRange: ReadonlyArray<CIDR>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't that redundant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the readonly keyword makes the prop like a const so it can't be reassigned (es. myVar.allowNotifyIPSourceRange = myValue is forbidden). ReadonlyArray instead make the array immutable. They work on different layers.

@gunzip
Copy link
Contributor

gunzip commented Jan 21, 2021

why draft? can this be merged?

@BurnedMarshal
Copy link
Contributor Author

BurnedMarshal commented Jan 21, 2021

why draft? can this be merged?

I remove the draft. Maybe a rebase is needed before we can merge.

@BurnedMarshal BurnedMarshal marked this pull request as ready for review January 21, 2021 13:38
Copy link
Contributor

@AleDore AleDore left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@BurnedMarshal BurnedMarshal merged commit 925e59f into master Jan 22, 2021
@BurnedMarshal BurnedMarshal deleted the 174416949-migrate-to-eslint branch January 22, 2021 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
5 participants