JS-2011 [DO NOT MERGE] Add context-aware JS rules CSV generator#7475
JS-2011 [DO NOT MERGE] Add context-aware JS rules CSV generator#7475nathsou wants to merge 3 commits into
Conversation
| const requiredDependency = | ||
| (await readLocalRequiredDependencyOverride(sonarKey)) ?? | ||
| metadata.extra?.requiredDependency ?? | ||
| []; |
There was a problem hiding this comment.
💡 Edge Case: Empty local requiredDependency array shadows rspec fallback
In listContextAwareJsRules, requiredDependency is computed as (await readLocalRequiredDependencyOverride(sonarKey)) ?? metadata.extra?.requiredDependency ?? []. readLocalRequiredDependencyOverride returns undefined only when the requiredDependency declaration is absent from meta.ts. If a meta.ts contains export const requiredDependency = [] as const; (declaration present but no string literals), the function returns an empty array []. Because [] ?? x yields [] (nullish coalescing only falls back on null/undefined), the rspec metadata.extra.requiredDependency fallback is silently skipped, and such a rule would then be dropped from the CSV if it has no ecma/module requirement. If empty declarations are never expected this is harmless, but the intent is ambiguous. Consider returning undefined when the parsed dependency list is empty so the rspec value can still take effect.
Fall back to rspec metadata when the local declaration parses to an empty list.:
const dependencies: string[] = [];
for (const literal of match.groups.values.matchAll(stringLiteralPattern)) {
dependencies.push(literal[1] ?? literal[2]);
}
return dependencies.length > 0 ? dependencies : undefined;
- Apply fix
Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎
| const requiredDependencyPattern = | ||
| /export const requiredDependency = \[(?<values>[\s\S]*?)\](?:\s+as const)?;/; | ||
| const stringLiteralPattern = /'([^']+)'|"([^"]+)"/g; |
There was a problem hiding this comment.
💡 Quality: Regex-based parsing of meta.ts is brittle and fails silently
readLocalRequiredDependencyOverride parses requiredDependency out of TypeScript source with requiredDependencyPattern/stringLiteralPattern regexes rather than importing the module. This works for the current uniform style (export const requiredDependency = ['x'] as const;), but it will silently return undefined/wrong values if the declaration is ever written differently (e.g. split across differently-formatted lines, uses template literals, references imported constants, or has a trailing comment inside the array). Since a miss just omits a dependency rather than erroring, drift between the tool and the actual meta.ts files would go unnoticed. Consider dynamically importing the meta.ts module (via tsx, which is already used to run the tool) and reading the exported requiredDependency value directly, which is exact and robust to formatting.
Was this helpful? React with 👍 / 👎
| export async function listContextAwareJsRules(): Promise<ContextAwareJsRule[]> { | ||
| const sonarKeys = await listSonarRuleKeys(); | ||
| const rules = await Promise.all( | ||
| sonarKeys.map(async sonarKey => { | ||
| const metadata = await readRspecRuleMetadata(sonarKey); | ||
| if (!metadata.compatibleLanguages.includes('js')) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const requiredDependency = | ||
| (await readLocalRequiredDependencyOverride(sonarKey)) ?? | ||
| metadata.extra?.requiredDependency ?? | ||
| []; | ||
| const requiredEcmaVersion = getRequiredEcmaVersion(metadata.tags); | ||
| const requiredModuleType = getRequiredModuleType(metadata.tags); |
There was a problem hiding this comment.
💡 Performance: Unbounded concurrent file reads across all rule directories
listContextAwareJsRules maps every rule key through Promise.all with no concurrency cap. With ~540 rule directories, up to ~540 metadata reads (plus ~540 meta.ts reads for js-compatible rules) are issued simultaneously. On systems with a low open-file-descriptor limit this risks EMFILE: too many open files and makes failures non-deterministic. This is a dev-only tool so impact is low, but batching the reads (e.g. via a small concurrency limiter or processing in chunks) would make the tool more robust.
Was this helpful? React with 👍 / 👎
Ruling ReportCode no longer flagged (2 issues)S2699http/test/ConnectionMock.spec.js:256 254 |
255 |
> 256 | it('should require code to be a number', function() {
257 | });
258 | http/test/ConnectionMock.spec.js:260 258 |
259 |
> 260 | it('should require body to be a string', function() {
261 |
262 | }); |
CI failed: The build is failing due to a malformed XML test report generated by the new rules script and a tooling error where Knip flags the same new script as an unused file.OverviewTwo distinct failures are blocking the CI pipeline, both directly related to the newly introduced FailuresMalformed Generic Test Execution Report (confidence: high)
Knip Unused File Detection (confidence: high)
Summary
Code Review 👍 Approved with suggestions 0 resolved / 3 findingsAdds a TypeScript tool to generate a CSV of context-aware JS rules. Ensure the implementation handles potential failures by replacing brittle regex parsing with module imports and addressing unbounded concurrency in file reads. 💡 Edge Case: Empty local requiredDependency array shadows rspec fallback📄 tools/list-context-aware-js-rules.ts:74-77 📄 tools/list-context-aware-js-rules.ts:144-158 In Fall back to rspec metadata when the local declaration parses to an empty list.💡 Quality: Regex-based parsing of meta.ts is brittle and fails silently📄 tools/list-context-aware-js-rules.ts:61-63 📄 tools/list-context-aware-js-rules.ts:144-158
💡 Performance: Unbounded concurrent file reads across all rule directories📄 tools/list-context-aware-js-rules.ts:65-79
🤖 Prompt for agentsTip Comment OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |
|





Summary
Test plan
tmp_csv=$(mktemp) && npx tsx tools/list-context-aware-js-rules.ts > "$tmp_csv" && diff -u "$tmp_csv" tools/list-context-aware-js-rules.csvnpx -p prettier prettier --check --no-config --parser typescript --single-quote --trailing-comma all --arrow-parens avoid --print-width 100 --end-of-line lf tools/list-context-aware-js-rules.ts