Skip to content

Conversation

moznion
Copy link
Contributor

@moznion moznion commented Mar 30, 2020

PR Checklist

Overview

When it gives the following TSLint configuration:

{
  "rules": {
    "ban-types": [true,
      ["Object", "Use {} instead."],
    ]
  }
}

as-is:

  "rules": {
      "@typescript-eslint/ban-types": "error"
  }

maybe this doesn't make sense.

to-be:

  "rules": {
      "@typescript-eslint/ban-types": [
          "error",
          {
              "types": {
                  "Object": {
                      "message": "Use {} instead."
                  }
              }
          }
      ]
  }

This pull-request enhances a converter for ban-types to make the behaviour to be expected: https://github.com/typescript-eslint/typescript-eslint/blob/f3160b471f8247e157555b6cf5b40a1f6ccdc233/packages/eslint-plugin/docs/rules/ban-types.md

@JoshuaKGoldberg JoshuaKGoldberg added the status: waiting for reviewer Waiting for a maintainer to review label Mar 30, 2020
In the past, this tool didn't respect the TSLint configuration of `ban-types`,
it means even if TSLint specified `ban-types` with the argument, generated
ESLint configuration doesn't contain the detailed configuration of that.

For example, when the TSLint configuration is like the following:

```
{
  "rules": {
    "ban-types": [true,
      ["Object", "Use {} instead."],
    ]
  }
}
```

then, older tool generates the following ESLint configuration:

```
  "rules": {
      "@typescript-eslint/ban-types": "error"
  }
```

According to the official documentation, probably this generated result
doesn't make sense because each rule requires the type name (and the message).

https://github.com/typescript-eslint/typescript-eslint/blob/f3160b471f8247e157555b6cf5b40a1f6ccdc233/packages/eslint-plugin/docs/rules/ban-types.md

So this commit makes the generated ESLint configuration to be suitable to the certainly
working configuration, like so:

```
  "rules": {
      "@typescript-eslint/ban-types": [
          "error",
          {
              "types": {
                  "Object": {
                      "message": "Use {} instead."
                  }
              }
          }
      ]
  }
```

See also: https://palantir.github.io/tslint/rules/ban-types/
Related ticket: typescript-eslint#352
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

This looks great so far, thanks @moznion! I left a few (nit)picks in the source, but if you disagree with them I don't think they should block this PR from being merged.

One thing that is blocking it is the lack of test coverage for two cases:

  • Line 12: testing what happens if the input argument isn't an array
  • Line 17: testing what happens if the input array doesn't have a type

message: string;
};

const bannedTypesObj: { [key: string]: ConvertBanTypeArgument | null } = {};
Copy link
Member

Choose a reason for hiding this comment

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

Small nit, Record is now the built-in type that can handle these kinds of types:

Suggested change
const bannedTypesObj: { [key: string]: ConvertBanTypeArgument | null } = {};
const bannedTypesObj: Record<string, ConvertBanTypeArgument | null> = {};

message: string;
};

const bannedTypesObj: { [key: string]: ConvertBanTypeArgument | null } = {};
Copy link
Member

Choose a reason for hiding this comment

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

A few naming nits for this file:

  • "Obj" is unnecessary in bannedTypesObj; could you rename to bannedTypes?
  • typ isn't very clear and I was confused at first as to what it means; could you rename to bannedType or similar?

break;
}

const typ = rule[0];
Copy link
Member

Choose a reason for hiding this comment

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

Nit: if you want to be really slick ✨ 😉 you could do something like:

const [bannedType, message] = rule;

...so you can turn the object assigned to bannedTypesObj[typ] into { message }.

@JoshuaKGoldberg
Copy link
Member

Oh, and feel free to push new commits instead of force pushing. Helps keep the review comments findable 👍

@JoshuaKGoldberg JoshuaKGoldberg added status: waiting for author The PR author should address requested changes and removed status: waiting for reviewer Waiting for a maintainer to review labels Mar 30, 2020
@moznion
Copy link
Contributor Author

moznion commented Mar 30, 2020

Thanks for your lightning-fast reviews! I'll tweak them!
(and I'm sorry to do force-push, the motivation was keeping clean this pull request...)

@moznion
Copy link
Contributor Author

moznion commented Mar 30, 2020

@JoshuaKGoldberg I've just pushed tweaked commits. Could you please review them again?

@moznion
Copy link
Contributor Author

moznion commented Mar 30, 2020

Oops, somehow I removed logic that checks a rule is array object or not. I added that at 3d8ded3.

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

Michael Fassbender saying "perfection" seriously

@JoshuaKGoldberg JoshuaKGoldberg added status: waiting for reviewer Waiting for a maintainer to review and removed status: waiting for author The PR author should address requested changes labels Mar 30, 2020
@JoshuaKGoldberg JoshuaKGoldberg merged commit 255d807 into typescript-eslint:master Mar 30, 2020
@JoshuaKGoldberg
Copy link
Member

Thanks @moznion!

@moznion
Copy link
Contributor Author

moznion commented Mar 30, 2020

Thank you for your support, @JoshuaKGoldberg!

@moznion moznion deleted the fix_ban_types branch March 30, 2020 16:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting for reviewer Waiting for a maintainer to review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ban-types converter should respect the original TSLint configuration for each type descriptor

2 participants