Skip to content

Commit a3f8c2e

Browse files
erkamyamanKaap10
andauthored
feat(mcp): turn single lint rules off with lint.rules (#440)
A lint.rules option turns single lint rules off. A rule set to false is left out of the panel, the Audit tab, the agent tools (scan-templates, scan-pipe-usage, explain-pipe, lint-routes, lint-forms, analog-lint, audit) and pangular lint, including the SARIF rule list. pangular lint now reads pangular.config.json. Co-authored-by: Vardhman Gupta <[email protected]>
1 parent 0c52a6b commit a3f8c2e

28 files changed

Lines changed: 475 additions & 129 deletions

‎apps/docs/src/content/getting-started/cli.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ The file holds the same options as `createPangular()`, as JSON. Save this as `pa
4545
{
4646
"inspectors": {"analog": false},
4747
"agent": {"readOnly": true},
48-
"redaction": {"secretNames": ["passport"]}
48+
"redaction": {"secretNames": ["passport"]},
49+
"lint": {"rules": {"img-missing-alt": false}}
4950
}
5051
```
5152

‎apps/docs/src/content/getting-started/configuration.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ interface PangularConfig {
131131
};
132132
popup?: {hotkey?: string | false};
133133
plugins?: Record<string, boolean | {enabled?: boolean; tools?: boolean; actions?: boolean}>;
134+
lint?: {rules?: Record<string, boolean>};
134135
}
135136
```
136137

@@ -264,6 +265,25 @@ The server plugins themselves go in `serverPlugins`, an option of the Express hu
264265

265266
It is off by default because it starts processes on your machine. A value other than `true` or `false` prints a warning and keeps it off.
266267

268+
### Lint rules
269+
270+
| Option | Default | What it does |
271+
| ------------------- | ------- | --------------------------------------------------------------------------------------------------------------------------- |
272+
| `lint.rules.<rule>` | `true` | `false` turns off an individual lint rule across linters (template lint, pipe usage, routes, forms and Analog diagnostics). |
273+
274+
```json
275+
{
276+
"lint": {
277+
"rules": {
278+
"img-missing-alt": false,
279+
"track-by-index": false
280+
}
281+
}
282+
}
283+
```
284+
285+
A rule set to `false` is left out of the panel, the **Audit** tab, the agent tools (`scan-templates`, `scan-pipe-usage`, `explain-pipe`, `lint-routes`, `lint-forms`, `analog-lint` and `audit`) and the CI export (`pangular lint`). Use the rule name a finding shows. The template rules are listed under [scan-templates](../agents/tools.md#scan-templates).
286+
267287
## Check the active configuration
268288

269289
The server checks the options when it starts. An unknown key, a value of the wrong type or a limit outside its bounds prints one `[pangular]` warning in the terminal that lists each problem and the value used instead. An unknown key names the closest known key:
@@ -290,6 +310,7 @@ The [Dashboard](../inspectors/dashboard.md#configuration-block) has a **Configur
290310
| **Popup shortcut** | `popup.hotkey`, or **Off** when it is `false`. |
291311
| **Plugins off** | Plugins set to `false` in `plugins`. |
292312
| **Plugin tools or actions off** | Plugins with `tools` or `actions` set to `false`. |
313+
| **Lint rules off** | Rules set to `false` in `lint.rules`. |
293314

294315
## Where to next
295316

extension/ui/assets/browser-agent-rpc-BXhoSh1z-CA8CCnXn.js renamed to extension/ui/assets/browser-agent-rpc-BXhoSh1z-CO6sIa2s.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 73 additions & 73 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎extension/ui/index.html‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/devtools/src/__tests__/audit-tools.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,4 +412,23 @@ describe('the audit RPC functions for the panel', () => {
412412
ok: false,
413413
});
414414
});
415+
416+
it('excludes findings for rules turned off in config.lint.rules', async () => {
417+
const { call } = await boot({
418+
lint: { rules: { 'wildcard-not-last': false } },
419+
inspectors: { pipes: false },
420+
});
421+
await call('push-component-tree', tree('tab1'));
422+
await call('push-router', {
423+
pageId: 'tab1',
424+
reportedAt: Date.now(),
425+
config: [
426+
{ path: '**', fullPath: '**' },
427+
{ path: 'home', fullPath: 'home' },
428+
],
429+
setup: { features: {}, guards: {} },
430+
});
431+
const result = await call('get-audit', { pageId: 'tab1' });
432+
expect(result.report.findings.filter((f: { area: string }) => f.area === 'router')).toEqual([]);
433+
});
415434
});

‎packages/devtools/src/__tests__/config.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
agentAllowed,
1313
agentSafety,
1414
connectionConfig,
15+
isLintRuleEnabled,
1516
pangularConfigProblems,
1617
pickPangularConfig,
1718
resolvePangularConfig,
@@ -76,6 +77,27 @@ describe('resolvePangularConfig', () => {
7677
tanstackQuery: true,
7778
plugins: true,
7879
});
80+
expect(config.lint).toEqual({ rules: {} });
81+
});
82+
83+
it('resolves lint rules and tests whether a rule is enabled', () => {
84+
const config = resolvePangularConfig({
85+
lint: {
86+
rules: {
87+
'img-missing-alt': false,
88+
'track-by-index': true,
89+
invalid: 'no' as unknown as boolean,
90+
},
91+
},
92+
});
93+
expect(config.lint.rules).toEqual({
94+
'img-missing-alt': false,
95+
'track-by-index': true,
96+
});
97+
expect(isLintRuleEnabled('img-missing-alt', config)).toBe(false);
98+
expect(isLintRuleEnabled('track-by-index', config)).toBe(true);
99+
expect(isLintRuleEnabled('other-rule', config)).toBe(true);
100+
expect(isLintRuleEnabled('img-missing-alt', undefined)).toBe(true);
79101
});
80102

81103
it('carries a disabled inspector into its agent tools and panel actions', () => {
@@ -226,6 +248,7 @@ describe('summarizePangularConfig', () => {
226248
actions: { forms: false, http: false, ngrx: false },
227249
redaction: { secretNames: ['passport'], unmask: ['pin'] },
228250
limits: { refreshMs: 1000, httpCalls: 50 },
251+
lint: { rules: { 'img-missing-alt': false, 'track-by-index': true } },
229252
});
230253
expect(summarizePangularConfig(config)).toEqual([
231254
{ label: 'Inspectors off', value: 'NgRx, Pipes' },
@@ -235,6 +258,7 @@ describe('summarizePangularConfig', () => {
235258
{ label: 'Extra secret names', value: 'passport' },
236259
{ label: 'Unmasked names', value: 'pin' },
237260
{ label: 'Limits', value: 'poll every 1000 ms, 50 HTTP calls' },
261+
{ label: 'Lint rules off', value: 'img-missing-alt' },
238262
]);
239263
});
240264
});
@@ -631,6 +655,7 @@ describe('pangularConfigProblems', () => {
631655
actions: { ngrx: false },
632656
redaction: { secretNames: ['passport'], unmask: ['pin'] },
633657
limits: { refreshMs: 1000 },
658+
lint: { rules: { 'img-missing-alt': false } },
634659
};
635660
expect(pangularConfigProblems(valid)).toEqual([]);
636661
expect(pangularConfigProblems(resolvePangularConfig(valid))).toEqual([]);
@@ -661,6 +686,20 @@ describe('pangularConfigProblems', () => {
661686
expect(pangularConfigProblems({ agent: { readOnly: 'true' } })).toEqual([
662687
'`agent.readOnly` should be true or false, got the string "true". It was ignored, so it is off.',
663688
]);
689+
expect(pangularConfigProblems({ lint: 'false' as never })).toEqual([
690+
'`lint` should be an object, got the string "false". It was ignored.',
691+
]);
692+
expect(pangularConfigProblems({ lint: { rules: 'none' as never } })).toEqual([
693+
'`lint.rules` should be an object, got the string "none". It was ignored.',
694+
]);
695+
expect(
696+
pangularConfigProblems({ lint: { rules: { 'img-missing-alt': 'no' as never } } }),
697+
).toEqual([
698+
'`lint.rules.img-missing-alt` should be true or false, got the string "no". It was ignored, so it is on.',
699+
]);
700+
expect(pangularConfigProblems({ lint: { ruls: {} } as never })).toEqual([
701+
'Unknown option `lint.ruls` was ignored. Did you mean `rules`?',
702+
]);
664703
});
665704

666705
it('prints one warning from createPangular and none for a clean config', () => {

‎packages/devtools/src/__tests__/lint-export.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,33 @@ describe('runSourceLints', () => {
8585
expect(failsAt(result, 'warning')).toBe(true);
8686
expect(failsAt(result, 'info')).toBe(true);
8787
});
88+
89+
it('omits findings for rules disabled via options.rules or options.config', async () => {
90+
const dir = project();
91+
const withRules = await runSourceLints(dir, {
92+
compiler,
93+
rules: { 'img-missing-alt': false },
94+
});
95+
expect(withRules.findings.map((f) => f.rule)).toEqual(['impure-pipe-in-for']);
96+
97+
const withConfig = await runSourceLints(dir, {
98+
compiler,
99+
config: { lint: { rules: { 'impure-pipe-in-for': false } } },
100+
});
101+
expect(withConfig.findings.map((f) => f.rule)).toEqual(['img-missing-alt']);
102+
});
88103
});
89104

90105
describe('toSarif', () => {
106+
it('leaves template rules turned off out of the rule list', async () => {
107+
const sarif = toSarif(
108+
await runSourceLints(project(), { compiler, rules: { 'img-missing-alt': false } }),
109+
);
110+
const ids = sarif.runs[0]!.tool.driver.rules.map((rule) => rule.id);
111+
expect(ids).not.toContain('templates/img-missing-alt');
112+
expect(ids).toContain('templates/track-by-index');
113+
});
114+
91115
it('writes a SARIF 2.1.0 log with rules, levels and physical locations', async () => {
92116
const sarif = toSarif(await runSourceLints(project(), { compiler }));
93117
expect(sarif.version).toBe('2.1.0');
@@ -186,4 +210,15 @@ describe('pangular lint', () => {
186210
const unknown = await run(['lint', '--root', dir, '--only', 'forms']);
187211
expect(unknown.notes.join('\n')).toContain('Unknown lint "forms"');
188212
});
213+
214+
it('respects rules turned off in pangular.config.json', async () => {
215+
const dir = project();
216+
writeFileSync(
217+
join(dir, 'pangular.config.json'),
218+
JSON.stringify({ lint: { rules: { 'impure-pipe-in-for': false } } }),
219+
);
220+
const { out } = await run(['lint', '--root', dir, '--only', 'pipes']);
221+
const json = JSON.parse(out);
222+
expect(json.findings).toEqual([]);
223+
});
189224
});

‎packages/devtools/src/cli.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,11 @@ export function createPangularCli(options: PangularCliOptions = {}) {
346346
cli.option('--config <file>', `Devtools config as JSON (default: ${PANGULAR_CONFIG_FILE})`);
347347
cli.option('--read-only', 'Drop the agent tools that act on the page or the server');
348348
registerSkillsCommand(cli, { cwd: (flags) => resolveCliRoot(flags, env), log });
349-
registerLintCommand(cli, { cwd: (flags) => resolveCliRoot(flags, env), log });
349+
registerLintCommand(cli, {
350+
cwd: (flags) => resolveCliRoot(flags, env),
351+
config: (flags, root) => loadCliConfig(flags, root, env),
352+
log,
353+
});
350354
const defaultCommand = cli.commands.find((command) => command.isDefaultCommand);
351355
if (defaultCommand) {
352356
const run = async (

‎packages/devtools/src/config.ts‎

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,11 @@ export interface PangularConfig {
229229
* `inspectors.plugins: false` turns every plugin off.
230230
*/
231231
plugins?: Record<string, boolean | PluginSwitchInput>;
232+
/** Turn individual lint rules off: `{ 'img-missing-alt': false }`. */
233+
lint?: {
234+
/** Rule status by rule name. `false` turns the rule off; `true` or omitted keeps it on. */
235+
rules?: Record<string, boolean>;
236+
};
232237
}
233238

234239
/** What a `plugins` entry can turn off; each is on when left out. */
@@ -263,6 +268,9 @@ export interface ResolvedPangularConfig {
263268
popup: { hotkey: string | false };
264269
/** The `plugins` entries that were given, by plugin id. */
265270
plugins: Record<string, PluginSwitches>;
271+
lint: {
272+
rules: Record<string, boolean>;
273+
};
266274
}
267275

268276
/** The switches of one plugin under `config`, whether or not it has a `plugins` entry. */
@@ -648,9 +656,37 @@ export function resolvePangularConfig(config?: unknown): ResolvedPangularConfig
648656
) as Record<PangularLimit, number>,
649657
popup: { hotkey: popupHotkey(record(input['popup'])['hotkey']) },
650658
plugins: pluginEntries(input['plugins']),
659+
lint: { rules: lintRules(input['lint']) },
651660
};
652661
}
653662

663+
const MAX_LINT_RULE_ENTRIES = 200;
664+
665+
function lintRules(value: unknown): Record<string, boolean> {
666+
const rules = record(record(value)['rules']);
667+
const resolved: Record<string, boolean> = {};
668+
for (const [key, val] of Object.entries(rules).slice(0, MAX_LINT_RULE_ENTRIES)) {
669+
if (typeof val === 'boolean') {
670+
resolved[key] = val;
671+
}
672+
}
673+
return resolved;
674+
}
675+
676+
/**
677+
* Whether a lint rule is enabled by the devtools config: on unless the config
678+
* explicitly sets `lint.rules[rule]` to `false`.
679+
*/
680+
export function isLintRuleEnabled(
681+
rule: string,
682+
config?: ResolvedPangularConfig | PangularConfig,
683+
): boolean {
684+
if (!config) return true;
685+
const rules = 'lint' in config && config.lint ? config.lint.rules : undefined;
686+
if (!rules || typeof rules !== 'object') return true;
687+
return rules[rule] !== false;
688+
}
689+
654690
const MAX_PLUGIN_ENTRIES = 100;
655691

656692
function pluginEntries(value: unknown): Record<string, PluginSwitches> {
@@ -713,6 +749,7 @@ const CONFIG_KEYS = [
713749
'limits',
714750
'popup',
715751
'plugins',
752+
'lint',
716753
] as const;
717754

718755
/**
@@ -834,6 +871,21 @@ export function pangularConfigProblems(config: unknown): string[] {
834871
booleans(`plugins.${id}`, entry, ['enabled', 'tools', 'actions'], 'on');
835872
}
836873
}
874+
const lint = object('lint', input['lint'], ['rules']);
875+
const rules = lint['rules'];
876+
if (rules !== undefined) {
877+
if (!isRecord(rules)) {
878+
problems.push(`\`lint.rules\` should be an object, got ${describe(rules)}. It was ignored.`);
879+
} else {
880+
for (const [key, value] of Object.entries(rules)) {
881+
if (typeof value !== 'boolean') {
882+
problems.push(
883+
`\`lint.rules.${key}\` should be true or false, got ${describe(value)}. It was ignored, so it is on.`,
884+
);
885+
}
886+
}
887+
}
888+
}
837889
return problems;
838890
}
839891

@@ -868,8 +920,8 @@ export function configFromConnection(
868920
export function pickPangularConfig<T extends PangularConfig>(
869921
options: T,
870922
): { config: PangularConfig; rest: Omit<T, keyof PangularConfig> } {
871-
const { inspectors, agent, actions, redaction, limits, popup, plugins, ...rest } = options;
872-
return { config: { inspectors, agent, actions, redaction, limits, popup, plugins }, rest };
923+
const { inspectors, agent, actions, redaction, limits, popup, plugins, lint, ...rest } = options;
924+
return { config: { inspectors, agent, actions, redaction, limits, popup, plugins, lint }, rest };
873925
}
874926

875927
/** Form actions that work without a `formId`: they act on the page, not on one form. */
@@ -1780,6 +1832,12 @@ export function summarizePangularConfig(
17801832
`${id} (${[!entry.tools && 'tools', !entry.actions && 'actions'].filter(Boolean).join(', ')})`,
17811833
),
17821834
);
1835+
add(
1836+
'Lint rules off',
1837+
Object.entries(config.lint?.rules ?? {})
1838+
.filter(([, enabled]) => !enabled)
1839+
.map(([rule]) => rule),
1840+
);
17831841
return items;
17841842
}
17851843

0 commit comments

Comments
 (0)