no-restricted-syntax
Added in v0.5.2Configuration
Rule Details
Disallows specified syntax. This is useful when a project wants to restrict language features such as try-catch, classes, or the in operator.
Restrictions use ESTree node names, such as FunctionDeclaration and WithStatement, or more precise AST selectors. Any node matching a configured selector is reported.
Examples of incorrect code for this rule with ["error", "FunctionExpression", "WithStatement", "BinaryExpression[operator='in']"], parsed as a script:
Examples of correct code for the same configuration:
Options
The rule accepts a list of selector strings:
Entries can also be objects with a required selector and an optional message:
The custom message replaces the default Using '<selector>' is not allowed. diagnostic. String and object entries can be mixed in one configuration.
Selectors can also target supported TypeScript syntax. For example, TSEnumDeclaration[const=true] selects const enums, while TSEnumDeclaration selects all enums. [const] only checks whether the attribute exists, so it matches ordinary enums as well.
When Not To Use It
Leave this rule disabled if your project has no syntax restrictions.
Differences from upstream
- Malformed selectors are ignored instead of rejecting the rule configuration. For example,
["error", "[", "WithStatement"]still restrictsWithStatementin rslint; ESLint rejects the malformed[selector. - TypeScript selectors are limited to the supported AST node mappings. Not every type annotation or type-parameter wrapper is exposed, so broad selectors on type-only syntax can differ from typescript-eslint. Receiver attribute paths also look through TypeScript assertions, such as
(console as any).log(). - The bare
*selector follows the underlying TypeScript AST traversal, starting at the program's children, and does not report additional ESTree wrappers. Named selectors such asClassBodyandChainExpression, and other broad selectors, can match supported wrappers. UseProgramorProgram:exitto select the program itself.