diff --git a/docs/troubleshooting/faqs/General.mdx b/docs/troubleshooting/faqs/General.mdx index 3878f72a69f1..2aa8c4b1700a 100644 --- a/docs/troubleshooting/faqs/General.mdx +++ b/docs/troubleshooting/faqs/General.mdx @@ -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: +
+Banning confusing property uses ```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.", + }, + ], + }, +} +``` + +
+ +
+Banning specific uses of class properties + +```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.", + }, + ], + }, +} +``` + +
+ +
+Banning specific uses of TypeScript enums + +```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.", }, ], }, } ``` +
+ +
+Banning specific uses of TypeScript tuples + +```jsonc +{ + "rules": { + "no-restricted-syntax": [ + "error", + // enforce tuple members have labels + { + "selector": "TSTupleType > :not(TSNamedTupleMember)", + "message": "All tuples should have labels.", + }, + ], + }, +} +``` + +
+ ## 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.