mirror of
https://github.com/forcedotcom/afv-library.git
synced 2026-07-30 03:09:50 +08:00
2.6 KiB
2.6 KiB
ESLint Tier 2: Configurable Rules
← Back to ESLint Rules Discovery
Tier 2: Configurable Rules
When no built-in rule exists but the pattern is generic (ban a function, ban a property, ban a syntax construct):
Option A: no-restricted-globals
Ban specific global functions or variables.
Example: Ban setTimeout/setInterval
// eslint.config.js
module.exports = [
{
files: ["**/lwc/**/*.js"],
rules: {
"no-restricted-globals": ["error",
{
name: "setTimeout",
message: "Use LWC lifecycle hooks instead of setTimeout"
},
{
name: "setInterval",
message: "Use LWC lifecycle hooks instead of setInterval"
}
]
}
}
];
Example: Ban window.localStorage
rules: {
"no-restricted-globals": ["error",
{
name: "localStorage",
message: "Use Salesforce state management instead of localStorage"
}
]
}
Option B: no-restricted-syntax
Ban specific AST patterns using ESLint selectors.
Example: Ban eval() calls
rules: {
"no-restricted-syntax": ["error",
{
selector: "CallExpression[callee.name='eval']",
message: "eval() is forbidden for security reasons"
}
]
}
Example: Ban innerHTML property access
rules: {
"no-restricted-syntax": ["error",
{
selector: "MemberExpression[property.name='innerHTML']",
message: "innerHTML is forbidden - use textContent or render() for XSS prevention"
}
]
}
Example: Ban for-in loops
rules: {
"no-restricted-syntax": ["error",
{
selector: "ForInStatement",
message: "for-in loops are forbidden - use for-of or Object.keys()"
}
]
}
ESLint Selector Syntax Cheat Sheet
| Pattern | Selector | Example |
|---|---|---|
| Function call | CallExpression[callee.name='functionName'] |
eval(), alert() |
| Property access | MemberExpression[property.name='propName'] |
obj.innerHTML |
| Statement type | ForInStatement, WithStatement |
for-in, with statements |
| Operator | BinaryExpression[operator='=='] |
== instead of === |
| Literal value | Literal[value='string'] |
Specific string literals |
Full selector docs: https://eslint.org/docs/latest/extend/selectors
Option C: no-restricted-properties
Ban specific object properties.
Example: Ban Object.prototype methods
rules: {
"no-restricted-properties": ["error",
{
object: "Object",
property: "setPrototypeOf",
message: "setPrototypeOf is forbidden for performance reasons"
}
]
}