Skip to content

docs: added more no-restricted-syntax FAQ examples #9872

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

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 91 additions & 7 deletions docs/troubleshooting/faqs/General.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -74,35 +74,119 @@ This generic rule allows you to specify a [selector](https://eslint.org/docs/dev

You can use an AST visualization tool such as [typescript-eslint playground](/play#showAST=es) > _Options_ > _AST Explorer_ on its left sidebar by selecting _ESTree_ to help in figuring out the structure of the AST that you want to ban.

For example, you can ban enums (or some variation of) using one of the following configs:
<details>
<summary>Banning confusing property uses</summary>

```jsonc
{
"rules": {
"no-restricted-syntax": [
"error",
// ban all enums

// Ban accessing `constructor.name`:
{
"selector": "MemberExpression[object.property.name='constructor'][property.name='name']",
"message": "'constructor.name' is not reliable after code minifier usage.",
},

// Ban get and set accessors:
{
"selector": "Property:matches([kind = \"get\"], [kind = \"set\"]), MethodDefinition:matches([kind = \"get\"], [kind = \"set\"])",
"message": "Don't use get and set accessors.",
},
],
},
}
```

</details>

<details>
<summary>Banning specific uses of class properties</summary>

```jsonc
{
"rules": {
"no-restricted-syntax": [
"error",

// Ban `private` members:
{
"selector": ":matches(PropertyDefinition, MethodDefinition)[accessibility=\"private\"]",
"message": "Use `#private` members instead.",
},

// Ban `#private` members:
{
"selector": ":matches(PropertyDefinition, MethodDefinition) > PrivateIdentifier.key",
"message": "Use the `private` modifier instead.",
},

// Ban static `this`:
{
"selector": "MethodDefinition[static = true] ThisExpression",
"message": "Prefer using the class's name directly.",
},
],
},
}
```

</details>

<details>
<summary>Banning specific uses of TypeScript enums</summary>

```jsonc
{
"rules": {
"no-restricted-syntax": [
"error",

// Ban all enums:
{
"selector": "TSEnumDeclaration",
"message": "My reason for not using any enums at all",
"message": "My reason for not using any enums at all.",
},

// ban just const enums
// Ban just `const` enums:
{
"selector": "TSEnumDeclaration[const=true]",
"message": "My reason for not using const enums",
"message": "My reason for not using const enums.",
},

// ban just non-const enums
// Ban just non-`const` enums:
{
"selector": "TSEnumDeclaration:not([const=true])",
"message": "My reason for not using non-const enums",
"message": "My reason for not using non-const enums.",
},
],
},
}
```

</details>

<details>
<summary>Banning specific uses of TypeScript tuples</summary>

```jsonc
{
"rules": {
"no-restricted-syntax": [
"error",
// enforce tuple members have labels
{
"selector": "TSTupleType > :not(TSNamedTupleMember)",
"message": "All tuples should have labels.",
},
],
},
}
```

</details>

## How do I check to see what versions are installed?

If you have multiple versions of our tooling, it can cause various bugs for you.
Expand Down
Loading