-
Notifications
You must be signed in to change notification settings - Fork 3.9k
lint(app): enforce the centralized-frontend-config rule, and fix its one violation #5583
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
Open
ntdatt812
wants to merge
4
commits into
tinyhumansai:main
Choose a base branch
from
ntdatt812:fix/enforce-centralized-frontend-config
base: main
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bf59ef3
lint(app): enforce the centralized-frontend-config rule, and fix its …
ntdatt812 8cbce2b
lint(app): pin the selector to import.meta and cover computed access
ntdatt812 cc4b6f0
style(app): run prettier on the new lint-rule test
ntdatt812 aeddef9
lint(app): route the second violation main grew through config
ntdatt812 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
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
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
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,70 @@ | ||
| import tsparser from '@typescript-eslint/parser'; | ||
| import { Linter } from 'eslint'; | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| // eslint.config.js is plain JS and ships no declarations; importing the real | ||
| // file is the point of this test, so the shape is asserted below at runtime. | ||
| // @ts-expect-error -- untyped JS module | ||
| import eslintConfig from '../../eslint.config.js'; | ||
|
|
||
| /** | ||
| * Guards the `no-restricted-syntax` selector that keeps frontend config reads | ||
| * funnelled through `src/utils/config.ts`. The selector is easy to get subtly | ||
| * wrong in both directions -- `new.target` is a MetaProperty just like | ||
| * `import.meta`, and computed access stores the key on `property.value` rather | ||
| * than `property.name` -- so the exact boundary is pinned here. | ||
| * | ||
| * The selector and the ignore list are read out of the real `eslint.config.js` | ||
| * rather than restated, so these assertions cannot drift away from what ships. | ||
| * Linting runs through a bare `Linter` because the repo config is type-aware | ||
| * and `parserOptions.project` rejects the synthetic file used for the probes. | ||
| */ | ||
|
|
||
| const configBlock = (eslintConfig as Linter.Config[]).find( | ||
| block => block.rules?.['no-restricted-syntax'] | ||
| ); | ||
|
|
||
| const [, restriction] = (configBlock?.rules?.['no-restricted-syntax'] ?? []) as [ | ||
| string, | ||
| { selector: string; message: string }, | ||
| ]; | ||
|
|
||
| const linter = new Linter(); | ||
|
|
||
| function lint(code: string) { | ||
| const messages = linter.verify(code, { | ||
| languageOptions: { parser: tsparser, ecmaVersion: 'latest', sourceType: 'module' }, | ||
| rules: { 'no-restricted-syntax': ['error', restriction] }, | ||
| }); | ||
| // A parse failure would otherwise read as "the rule did not fire". | ||
| expect(messages.filter(message => message.fatal)).toEqual([]); | ||
| return messages; | ||
| } | ||
|
|
||
| describe('centralized-frontend-config lint rule', () => { | ||
| it('is wired into the shipped config', () => { | ||
| expect(restriction?.selector).toBeTypeOf('string'); | ||
| expect(configBlock?.ignores).toContain('src/utils/config.ts'); | ||
| expect(configBlock?.ignores).toContain('src/test/**'); | ||
| }); | ||
|
|
||
| it('rejects dotted import.meta.env access', () => { | ||
| expect(lint('export const a = import.meta.env.DEV;')).toHaveLength(1); | ||
| }); | ||
|
|
||
| it('rejects computed import.meta["env"] access', () => { | ||
| expect(lint("export const a = import.meta['env'].DEV;")).toHaveLength(1); | ||
| }); | ||
|
|
||
| it('allows other import.meta properties', () => { | ||
| expect(lint('export const a = import.meta.url;')).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('allows new.target.env, which is a different meta-property', () => { | ||
| expect(lint('export function G() { return new.target.env; }')).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('allows a plain object property named env', () => { | ||
| expect(lint('const o = { env: 1 }; export const a = o.env;')).toHaveLength(0); | ||
| }); | ||
| }); |
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
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
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.