Skip to content

Commit

Permalink
NEW(eslint): @W-17378695@: Add in rule mappings to lock in severities…
Browse files Browse the repository at this point in the history
… and tags for eslint (#179)
  • Loading branch information
stephen-carter-at-sf authored Dec 20, 2024
1 parent 14a7933 commit 9b81fbc
Show file tree
Hide file tree
Showing 18 changed files with 8,749 additions and 2,879 deletions.
2 changes: 1 addition & 1 deletion packages/code-analyzer-eslint-engine/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@salesforce/code-analyzer-eslint-engine",
"description": "Plugin package that adds 'eslint' as an engine into Salesforce Code Analyzer",
"version": "0.16.1",
"version": "0.17.0-SNAPSHOT",
"author": "The Salesforce Code Analyzer Team",
"license": "BSD-3-Clause",
"homepage": "https://developer.salesforce.com/docs/platform/salesforce-code-analyzer/overview",
Expand Down
20 changes: 16 additions & 4 deletions packages/code-analyzer-eslint-engine/src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {MissingESLintWorkspace, PresentESLintWorkspace} from "./workspace";
import {EmitLogEventFcn, ESLintStrategy, LegacyESLintStrategy} from "./strategy";
import {ESLintEngineConfig} from "./config";
import {getMessage} from "./messages";
import {RULE_MAPPINGS} from "./rule-mappings";

export class ESLintEngine extends Engine {
static readonly NAME = "eslint";
Expand Down Expand Up @@ -125,11 +126,22 @@ export class ESLintEngine extends Engine {
}
}

function toRuleDescription(name: string, metadata: Rule.RuleMetaData, status: ESLintRuleStatus | undefined): RuleDescription {
function toRuleDescription(ruleName: string, metadata: Rule.RuleMetaData, status: ESLintRuleStatus | undefined): RuleDescription {
let severityLevel: SeverityLevel;
let tags: string[];

if (ruleName in RULE_MAPPINGS) {
severityLevel = RULE_MAPPINGS[ruleName].severity;
tags = RULE_MAPPINGS[ruleName].tags;
} else { // Any rule we don't know about from our RULE_MAPPINGS must be a custom rule. Unit tests prevent otherwise.
severityLevel = toSeverityLevel(metadata, status);
tags = [... toTags(metadata, status), COMMON_TAGS.CUSTOM];
}

return {
name: name,
severityLevel: toSeverityLevel(metadata, status),
tags: toTags(metadata, status),
name: ruleName,
severityLevel: severityLevel,
tags: tags,
description: metadata.docs?.description || '',
resourceUrls: metadata.docs?.url ? [metadata.docs.url] : []
}
Expand Down
1,344 changes: 1,344 additions & 0 deletions packages/code-analyzer-eslint-engine/src/rule-mappings.ts

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions packages/code-analyzer-eslint-engine/test/rule-mappings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {RuleDescription} from "@salesforce/code-analyzer-engine-api";
import {RULE_MAPPINGS} from "../src/rule-mappings";
import {ESLintEngine} from "../src/engine";
import {DEFAULT_CONFIG} from "../src/config";

describe('Tests for the rule-mappings', () => {
it('Test that the list of all bundled rules matches our RULE_MAPPINGS list', async () => {
const engine: ESLintEngine = new ESLintEngine(DEFAULT_CONFIG);
const ruleDescriptions: RuleDescription[] = await engine.describeRules({});
const actualRuleNames: Set<string> = new Set(ruleDescriptions.map(rd => rd.name));
const ruleNamesInRuleMappings: Set<string> = new Set(Object.keys(RULE_MAPPINGS));

const unusedRuleNamesFromRuleMappings: Set<string> = setDiff(ruleNamesInRuleMappings, actualRuleNames);
if (unusedRuleNamesFromRuleMappings.size > 0) {
throw new Error("The following rule names are found in RULE_MAPPINGS but do not associated to any bundled eslint rules that come with our base configs:\n " +
[...unusedRuleNamesFromRuleMappings].join(', '));

}

const missingRuleNamesFromRuleMappings: Set<string> = setDiff(actualRuleNames, ruleNamesInRuleMappings);
if (missingRuleNamesFromRuleMappings.size > 0) {
throw new Error("The following bundled rules are missing from the RULE_MAPPINGS list (Note: when adding them, make sure to add in the appropriate language tags):\n " +
[...missingRuleNamesFromRuleMappings].join(', ') + "\n\n" +
"Calculated Rule Descriptions for the missing rules: \n" +
JSON.stringify(ruleDescriptions.filter(rd => missingRuleNamesFromRuleMappings.has(rd.name)), null, 2)
);
}
});
});

function setDiff(setA: Set<string>, setB: Set<string>): Set<string> {
return new Set(
[...setA].filter(value => !setB.has(value))
);
}
Loading

0 comments on commit 9b81fbc

Please sign in to comment.