-
Notifications
You must be signed in to change notification settings - Fork 28
[code-infra] Add ESLint rules to enforce tree-shakeable production guards for dev-only functions #794
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
Draft
Copilot
wants to merge
15
commits into
master
Choose a base branch
from
copilot/fix-19649be1-758d-483e-a6b9-e2b49dcc0646
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
[code-infra] Add ESLint rules to enforce tree-shakeable production guards for dev-only functions #794
Changes from 6 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1a47091
Initial plan
Copilot 39925a9
Add require-dev-wrapper ESLint rule for production guards
Copilot cd90e72
Add documentation and examples to require-dev-wrapper rule
Copilot 911cf1b
Update require-dev-wrapper rule to accept both === and !== comparisons
Copilot a98e23a
Split require-dev-wrapper into two separate rules
Copilot 3650dff
Merge branch 'master' into copilot/fix-19649be1-758d-483e-a6b9-e2b49d…
Janpot c14b74f
Refactor rules: DRY up code and fix else block handling
Copilot 1942d5a
Simplify branch checking logic as suggested
Copilot 651aa6a
Remove recursive containsProcessEnvNodeEnv check
Copilot 7937026
Refactor isNodeEnvComparison to accept binary expression directly
Copilot 7816981
Move isNodeEnvComparison to require-dev-wrapper and simplify branch d…
Copilot ba214aa
Fix require-dev-wrapper to only accept tree-shakeable patterns
Copilot ebe83ed
fix manually
Janpot a476694
Add test case for nested if statement in else block
Copilot 25f9303
Merge branch 'master' into copilot/fix-19649be1-758d-483e-a6b9-e2b49d…
Janpot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
packages/code-infra/src/eslint/material-ui/rules/consistent-production-guard.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| /** | ||
| * ESLint rule that enforces consistent patterns for production guard checks. | ||
| * | ||
| * @example | ||
| * // Valid - comparing with 'production' | ||
| * if (process.env.NODE_ENV !== 'production') {} | ||
| * | ||
| * @example | ||
| * // Valid - comparing with 'production' | ||
| * if (process.env.NODE_ENV === 'production') {} | ||
| * | ||
| * @example | ||
| * // Invalid - comparing with 'development' | ||
| * if (process.env.NODE_ENV === 'development') {} | ||
| * | ||
| * @example | ||
| * // Invalid - comparing with 'test' | ||
| * if (process.env.NODE_ENV !== 'test') {} | ||
| * | ||
| * @example | ||
| * // Invalid - non-static construct | ||
| * const env = 'production'; | ||
| * if (process.env.NODE_ENV !== env) {} | ||
| * | ||
| * @example | ||
| * // Usage in ESLint config | ||
| * { | ||
| * rules: { | ||
| * 'material-ui/consistent-production-guard': 'error' | ||
| * } | ||
| * } | ||
| * | ||
| * @type {import('eslint').Rule.RuleModule} | ||
| */ | ||
| const rule = { | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| description: | ||
| 'Enforce consistent patterns for production guard checks using process.env.NODE_ENV', | ||
| }, | ||
| messages: { | ||
| invalidComparison: | ||
| "Only compare process.env.NODE_ENV with 'production'. Use `process.env.NODE_ENV !== 'production'` or `process.env.NODE_ENV === 'production'` instead of comparing with '{{ comparedValue }}'.", | ||
| nonStaticComparison: | ||
| "Production guard must use a statically analyzable pattern. Use `process.env.NODE_ENV === 'production'` or `process.env.NODE_ENV !== 'production'` with a string literal.", | ||
| invalidUsage: | ||
| "process.env.NODE_ENV must be used in a binary comparison with === or !==. Use `process.env.NODE_ENV !== 'production'` or `process.env.NODE_ENV === 'production'`.", | ||
| }, | ||
| schema: [], | ||
| }, | ||
| create(context) { | ||
| /** | ||
| * Checks if a node is process.env.NODE_ENV | ||
| * @param {import('estree').Node} node | ||
| * @returns {boolean} | ||
| */ | ||
| function isProcessEnvNodeEnv(node) { | ||
| return ( | ||
| node.type === 'MemberExpression' && | ||
| node.object.type === 'MemberExpression' && | ||
| node.object.object.type === 'Identifier' && | ||
| node.object.object.name === 'process' && | ||
| node.object.property.type === 'Identifier' && | ||
| node.object.property.name === 'env' && | ||
| node.property.type === 'Identifier' && | ||
| node.property.name === 'NODE_ENV' | ||
| ); | ||
| } | ||
|
|
||
| return { | ||
| BinaryExpression(node) { | ||
| // Check if this is a comparison with === or !== | ||
| if (node.operator === '===' || node.operator === '!==') { | ||
| // Check if left side is process.env.NODE_ENV | ||
| if (isProcessEnvNodeEnv(node.left)) { | ||
| // Right side must be a literal | ||
| if (node.right.type !== 'Literal') { | ||
| context.report({ | ||
| node, | ||
| messageId: 'nonStaticComparison', | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| // Right side must be the string 'production' | ||
| if (node.right.value !== 'production') { | ||
| context.report({ | ||
| node, | ||
| messageId: 'invalidComparison', | ||
| data: { | ||
| comparedValue: String(node.right.value), | ||
| }, | ||
| }); | ||
| } | ||
| } | ||
| // Check if right side is process.env.NODE_ENV (reversed comparison) | ||
| else if (isProcessEnvNodeEnv(node.right)) { | ||
| // Left side must be a literal | ||
| if (node.left.type !== 'Literal') { | ||
| context.report({ | ||
| node, | ||
| messageId: 'nonStaticComparison', | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| // Left side must be the string 'production' | ||
| if (node.left.value !== 'production') { | ||
| context.report({ | ||
| node, | ||
| messageId: 'invalidComparison', | ||
| data: { | ||
| comparedValue: String(node.left.value), | ||
| }, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| // Catch any other usage of process.env.NODE_ENV (not in a valid binary expression) | ||
| MemberExpression(node) { | ||
| if (isProcessEnvNodeEnv(node)) { | ||
| // Check if it's part of a valid binary expression | ||
| const parent = node.parent; | ||
| if ( | ||
| parent && | ||
| parent.type === 'BinaryExpression' && | ||
| (parent.operator === '===' || parent.operator === '!==') | ||
| ) { | ||
| // This is handled by BinaryExpression visitor | ||
| return; | ||
| } | ||
|
|
||
| // Invalid usage | ||
| context.report({ | ||
| node, | ||
| messageId: 'invalidUsage', | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
|
|
||
| export default rule; | ||
160 changes: 160 additions & 0 deletions
160
packages/code-infra/src/eslint/material-ui/rules/consistent-production-guard.test.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| import eslint from 'eslint'; | ||
| import parser from '@typescript-eslint/parser'; | ||
| import rule from './consistent-production-guard.mjs'; | ||
|
|
||
| const ruleTester = new eslint.RuleTester({ | ||
| languageOptions: { | ||
| parser, | ||
| }, | ||
| }); | ||
|
|
||
| ruleTester.run('consistent-production-guard', rule, { | ||
| valid: [ | ||
| // Should pass: Valid !== comparison with 'production' | ||
| { | ||
| code: ` | ||
| if (process.env.NODE_ENV !== 'production') { | ||
| console.log('dev'); | ||
| } | ||
| `, | ||
| }, | ||
| // Should pass: Valid === comparison with 'production' | ||
| { | ||
| code: ` | ||
| if (process.env.NODE_ENV === 'production') { | ||
| console.log('prod'); | ||
| } | ||
| `, | ||
| }, | ||
| // Should pass: Reversed comparison (literal on left) | ||
| { | ||
| code: ` | ||
| if ('production' !== process.env.NODE_ENV) { | ||
| console.log('dev'); | ||
| } | ||
| `, | ||
| }, | ||
| // Should pass: Reversed comparison with === | ||
| { | ||
| code: ` | ||
| if ('production' === process.env.NODE_ENV) { | ||
| console.log('prod'); | ||
| } | ||
| `, | ||
| }, | ||
| // Should pass: Code without process.env.NODE_ENV | ||
| { | ||
| code: ` | ||
| const foo = 'bar'; | ||
| if (foo === 'baz') { | ||
| console.log('test'); | ||
| } | ||
| `, | ||
| }, | ||
| ], | ||
| invalid: [ | ||
| // Should fail: Comparing with 'development' | ||
| { | ||
| code: ` | ||
| if (process.env.NODE_ENV === 'development') { | ||
| console.log('dev'); | ||
| } | ||
| `, | ||
| errors: [ | ||
| { | ||
| messageId: 'invalidComparison', | ||
| data: { comparedValue: 'development' }, | ||
| }, | ||
| ], | ||
| }, | ||
| // Should fail: Comparing with 'test' | ||
| { | ||
| code: ` | ||
| if (process.env.NODE_ENV !== 'test') { | ||
| console.log('not test'); | ||
| } | ||
| `, | ||
| errors: [ | ||
| { | ||
| messageId: 'invalidComparison', | ||
| data: { comparedValue: 'test' }, | ||
| }, | ||
| ], | ||
| }, | ||
| // Should fail: Reversed comparison with 'development' | ||
| { | ||
| code: ` | ||
| if ('development' === process.env.NODE_ENV) { | ||
| console.log('dev'); | ||
| } | ||
| `, | ||
| errors: [ | ||
| { | ||
| messageId: 'invalidComparison', | ||
| data: { comparedValue: 'development' }, | ||
| }, | ||
| ], | ||
| }, | ||
| // Should fail: Non-static comparison (variable) | ||
| { | ||
| code: ` | ||
| const env = 'production'; | ||
| if (process.env.NODE_ENV !== env) { | ||
| console.log('check'); | ||
| } | ||
| `, | ||
| errors: [ | ||
| { | ||
| messageId: 'nonStaticComparison', | ||
| }, | ||
| ], | ||
| }, | ||
| // Should fail: Non-static comparison (reversed) | ||
| { | ||
| code: ` | ||
| const env = 'production'; | ||
| if (env === process.env.NODE_ENV) { | ||
| console.log('check'); | ||
| } | ||
| `, | ||
| errors: [ | ||
| { | ||
| messageId: 'nonStaticComparison', | ||
| }, | ||
| ], | ||
| }, | ||
| // Should fail: Invalid usage (function call) | ||
| { | ||
| code: ` | ||
| foo(process.env.NODE_ENV); | ||
| `, | ||
| errors: [ | ||
| { | ||
| messageId: 'invalidUsage', | ||
| }, | ||
| ], | ||
| }, | ||
| // Should fail: Invalid usage (assignment) | ||
| { | ||
| code: ` | ||
| const env = process.env.NODE_ENV; | ||
| `, | ||
| errors: [ | ||
| { | ||
| messageId: 'invalidUsage', | ||
| }, | ||
| ], | ||
| }, | ||
| // Should fail: Invalid usage (template literal) | ||
| { | ||
| code: ` | ||
| const message = \`Environment: \${process.env.NODE_ENV}\`; | ||
| `, | ||
| errors: [ | ||
| { | ||
| messageId: 'invalidUsage', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.