microsoft / tslint-microsoft-contrib

A set of TSLint rules used on some Microsoft projects.
MIT License
702 stars 198 forks source link

import-name rule should support regex pattern matching #853

Closed jeffryang24 closed 4 years ago

jeffryang24 commented 5 years ago

Rule Suggestion

Is your rule for a general problem or is it specific to your development style?

I think it is only for my development style, but it could be useful for the general problem too.

What does your suggested rule do?

Currently, import-name only supports static name matching, such as: underscore to _, etc. But, I actually found that the import-name rule also supports regex matching while the documentation (README.md) doesn't tell it so.

List several examples where your rule could be used

I have many container components with (\w+)Container.tsx pattern, such as RebookModalButtonContainer.tsx, etc. I thought that import-name rule could accept regex pattern, so I add the rule below.

path/to/someFile.tsx

import RebookModalButton from 'path/to/RebookModalButtonContainer'; // triggers warning from tslint.

tslint.json

{
  "rules": {
    "import-name": [true, {
      "(\\w+)Container": "$1", // this rule doesn't work
      "*Container": "$1",  // this rule works
      "*Container": "", // this rule also works
    }]
  }
}

And the warning for import script above is gone away. But, this is my question for this issue.

Additional context

VSCode: 1.32.3 (Snap package)
TSLint extension: https://marketplace.visualstudio.com/items?itemName=ms-vscode.vscode-typescript-tslint-plugin
TSLint version: ^5.14.0
tslint-microsoft-contrib: ~5.2.1

Currently, I only want to know if import-name rule supports regex pattern matching, which is very helpful for handling the same import-name pattern instead of manually register it one by one. If it doesn't support it, maybe I can help to fix this issue if possible. :smile:

JoshuaKGoldberg commented 5 years ago

Currently, I only want to know if import-name rule supports regex pattern matching

Alas, right now it does not. That would be a feature request.

maybe I can help to fix this issue if possible

Yes please! That'd be great!

However, we should note that there might be backwards compatibility issues: if an existing rule configuration is meant to explicitly refer to a single path, and that path happens to include characters that mean something in a regular expression, adding in regex support would change the rule's behavior. How can the rule support regular expressions in a non-breaking way?

Related: #452

JoshuaKGoldberg commented 4 years ago

☠️ It's time! ☠️

Per #876, this repository is no longer accepting feature pull requests. TSLint is being deprecated and we recommend you switch to https://typescript-eslint.io.

Thanks for open sourcing with us, everyone!