Add custom ESLint rule for pattern-based import restrictions
Adds material-ui/no-restricted-imports rule to replace complex no-restricted-imports configurations with robust glob pattern matching on import source strings.
Implementation
-
Rule:
packages/code-infra/src/eslint/material-ui/rules/no-restricted-imports.mjs- Restricts imports by source string patterns (e.g.,
'@mui/material/Box') rather than resolved file paths - Uses minimatch for glob patterns:
@mui/material/*,@mui/*/internal/**,./**/*.css - Supports custom error messages per pattern
- Handles both ESM (
import) and CommonJS (require())
- Restricts imports by source string patterns (e.g.,
-
Tests: 17 test cases covering glob patterns, wildcards, named/namespace imports, CommonJS, custom messages
-
Export: Added to
material-uiplugin inindex.mjs -
Documentation: Added ESLint plugin section to README with usage examples
Usage
// eslint.config.mjs
{
rules: {
'material-ui/no-restricted-imports': [
'error',
[
{
pattern: '@mui/material/*',
message: 'Use the default import from @mui/material instead.'
},
{
pattern: '@mui/*/internal/**',
message: 'Do not import from internal modules.'
}
]
]
}
}
Comparison with existing rule
no-restricted-resolved-imports matches resolved file paths (after module resolution). This new rule matches import source strings directly, making it more suitable for restricting module/package imports by pattern.
[!WARNING]
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
iojs.org
- Triggering command:
curl --fail --compressed -q -L -s REDACTED -o -(dns block)If you need me to access, download, or install something from one of these locations, you can either:
- Configure Actions setup steps to set up my environment, which run before the firewall is enabled
- Add the appropriate URLs or hosts to the custom allowlist in this repository's Copilot coding agent settings (admins only)
Original prompt
This section details on the original issue you should resolve
<issue_title>[code-infra] Create a custom eslint rule for restricted imports</issue_title> <issue_description># Feature request
Summary
We've been using
no-restricted-importswith complex configuration to restrict certain import paths. Makes sense to have a more robust rule specific to the use case.More context here - https://github.com/mui/base-ui/pull/2054#discussion_r2171392158
Examples in other libraries
Motivation
</issue_description>
Comments on the Issue (you are @copilot in this section)
- Fixes mui/mui-public#390
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.