Skip to content

Commit 84df89c

Browse files
CHANGE: @W-17386421@: Hide rule_languages fields from pmd and cpd eng... (#151)
1 parent e12463b commit 84df89c

File tree

9 files changed

+43
-37
lines changed

9 files changed

+43
-37
lines changed

packages/code-analyzer-pmd-engine/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@salesforce/code-analyzer-pmd-engine",
33
"description": "Plugin package that adds 'pmd' and 'cpd' as engines into Salesforce Code Analyzer",
4-
"version": "0.15.0",
4+
"version": "0.15.1-SNAPSHOT",
55
"author": "The Salesforce Code Analyzer Team",
66
"license": "BSD-3-Clause",
77
"homepage": "https://developer.salesforce.com/docs/platform/salesforce-code-analyzer/overview",

packages/code-analyzer-pmd-engine/src/config.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export type PmdEngineConfig = {
1919
// If not defined, or equal to null, then an attempt will be made to automatically discover a 'java' command from your environment.
2020
java_command: string
2121

22+
// !! NOTE !! - HIDDEN UNTIL A USER REQUESTS THIS - ALL LANGUAGES ARE ENABLED BY DEFAULT SO THERE MAY NOT BE A USE CASE FOR THIS YET
2223
// List of languages associated with the PMD rules to be made available for 'pmd' engine rule selection.
2324
// The languages that you may choose from are: 'apex', 'html', 'javascript' (or 'ecmascript'), 'visualforce', 'xml'
2425
// See https://pmd.github.io/pmd/tag_rule_references.html to learn about the PMD rules available for each language.
@@ -42,7 +43,7 @@ export type PmdEngineConfig = {
4243

4344
export const DEFAULT_PMD_ENGINE_CONFIG: PmdEngineConfig = {
4445
java_command: DEFAULT_JAVA_COMMAND,
45-
rule_languages: ['apex', 'visualforce'],
46+
rule_languages: PMD_AVAILABLE_LANGUAGES, // hidden
4647
java_classpath_entries: [],
4748
custom_rulesets: []
4849
}
@@ -55,11 +56,10 @@ export const PMD_ENGINE_CONFIG_DESCRIPTION: ConfigDescription = {
5556
valueType: "string",
5657
defaultValue: null // Using null for doc and since it indicates that the value is calculated based on the environment
5758
},
58-
rule_languages: {
59-
descriptionText: getMessage('PmdConfigFieldDescription_rule_languages', toAvailableLanguagesText(PMD_AVAILABLE_LANGUAGES)),
60-
valueType: "array",
61-
defaultValue: DEFAULT_PMD_ENGINE_CONFIG.rule_languages,
62-
},
59+
60+
// rule_languages - is excluded here so that it can remain hidden
61+
// rule_languages WILL REMAIN HIDDEN UNTIL A USER REQUESTS THIS - ALL LANGUAGES ARE ENABLED BY DEFAULT SO THERE MAY NOT BE A USE CASE FOR THIS YET
62+
6363
java_classpath_entries: {
6464
descriptionText: getMessage('PmdConfigFieldDescription_java_classpath_entries'),
6565
valueType: "array",
@@ -81,6 +81,7 @@ export type CpdEngineConfig = {
8181
// If not defined, or equal to null, then an attempt will be made to automatically discover a 'java' command from your environment.
8282
java_command: string
8383

84+
// !! NOTE !! - HIDDEN UNTIL A USER REQUESTS THIS - ALL LANGUAGES ARE ENABLED BY DEFAULT SO THERE MAY NOT BE A USE CASE FOR THIS YET
8485
// List of languages associated with CPD to be made available for 'cpd' engine rule selection.
8586
// The languages that you may choose from are: 'apex', 'html', 'javascript' (or 'ecmascript'), 'typescript', 'visualforce', 'xml'
8687
rule_languages: string[]
@@ -97,7 +98,7 @@ export type CpdEngineConfig = {
9798

9899
export const DEFAULT_CPD_ENGINE_CONFIG: CpdEngineConfig = {
99100
java_command: DEFAULT_JAVA_COMMAND,
100-
rule_languages: ['apex', 'html', 'javascript', 'typescript', 'visualforce', 'xml'],
101+
rule_languages: CPD_AVAILABLE_LANGUAGES, // hidden
101102
minimum_tokens: 100,
102103
skip_duplicate_files: false
103104
}
@@ -110,11 +111,10 @@ export const CPD_ENGINE_CONFIG_DESCRIPTION: ConfigDescription = {
110111
valueType: "string",
111112
defaultValue: null // Using null for doc and since it indicates that the value is calculated based on the environment
112113
},
113-
rule_languages: {
114-
descriptionText: getMessage('CpdConfigFieldDescription_rule_languages', toAvailableLanguagesText(CPD_AVAILABLE_LANGUAGES)),
115-
valueType: "array",
116-
defaultValue: DEFAULT_CPD_ENGINE_CONFIG.rule_languages
117-
},
114+
115+
// rule_languages - is excluded here so that it can remain hidden
116+
// rule_languages WILL REMAIN HIDDEN UNTIL A USER REQUESTS THIS - ALL LANGUAGES ARE ENABLED BY DEFAULT SO THERE MAY NOT BE A USE CASE FOR THIS YET
117+
118118
minimum_tokens: {
119119
descriptionText: getMessage('CpdConfigFieldDescription_minimum_tokens'),
120120
valueType: "number",

packages/code-analyzer-pmd-engine/src/cpd-engine.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,18 @@ export class CpdEngine extends Engine {
126126

127127
function createRuleForLanguage(languageId: LanguageId): RuleDescription {
128128
const languageTag: string = languageId.charAt(0).toUpperCase() + languageId.slice(1);
129+
130+
// We agreed that html and xml can be noisy and are less important for users to be made aware of duplicate code
131+
// so we will be just adding Recommended tag to programming languages: apex, javascript, typescript, and visualforce
132+
const recommendedLanguages: Set<LanguageId> = new Set([LanguageId.APEX, LanguageId.JAVASCRIPT, LanguageId.TYPESCRIPT, LanguageId.VISUALFORCE]);
133+
129134
return {
130135
name: getRuleNameFromLanguage(languageId),
131136
severityLevel: SeverityLevel.Info,
132-
tags: [COMMON_TAGS.RECOMMENDED, COMMON_TAGS.CATEGORIES.DESIGN, languageTag],
137+
tags: [
138+
... (recommendedLanguages.has(languageId) ? [COMMON_TAGS.RECOMMENDED] : []),
139+
COMMON_TAGS.CATEGORIES.DESIGN,
140+
languageTag],
133141
description: getMessage('DetectCopyPasteForLanguageRuleDescription', languageId),
134142
resourceUrls: ['https://docs.pmd-code.org/latest/pmd_userdocs_cpd.html#refactoring-duplicates']
135143
}

packages/code-analyzer-pmd-engine/src/messages.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ const MESSAGE_CATALOG : { [key: string]: string } = {
1010
`May be provided as the name of a command that exists on the path, or an absolute file path location.\n` +
1111
`If unspecified, or specified as null, then an attempt will be made to automatically discover a 'java' command from your environment.`,
1212

13-
PmdConfigFieldDescription_rule_languages:
14-
`List of languages associated with the PMD rules to be made available for 'pmd' engine rule selection.\n` +
15-
`The languages that you may choose from are: %s.\n` +
16-
`See https://pmd.github.io/pmd/tag_rule_references.html to learn about the PMD rules available for each language.`,
17-
1813
PmdConfigFieldDescription_java_classpath_entries:
1914
`List of jar files and/or folders to add the Java classpath when running PMD.\n` +
2015
`Each entry may be given as an absolute path or a relative path to 'config_root'.\n` +
@@ -34,10 +29,6 @@ const MESSAGE_CATALOG : { [key: string]: string } = {
3429
`CPD ENGINE CONFIGURATION\n` +
3530
`To learn more about this configuration, visit: __LINK_COMING_SOON__`,
3631

37-
CpdConfigFieldDescription_rule_languages:
38-
`List of languages associated with CPD to be made available for 'cpd' engine rule selection.\n` +
39-
`The languages that you may choose from are: %s.`,
40-
4132
CpdConfigFieldDescription_minimum_tokens:
4233
`The minimum number of tokens required to be in a duplicate block of code in order to be reported as a violation.\n` +
4334
`The concept of a token may be defined differently per language, but in general it a distinct basic element of source code.\n` +

packages/code-analyzer-pmd-engine/src/pmd-engine.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ export class PmdEngine extends Engine {
7979
.map(ruleName => fetchRuleInfoByRuleName(ruleInfoList, ruleName))
8080
.filter(ruleInfo => ruleInfo !== null);
8181

82+
if (selectedRuleInfoList.length === 0) {
83+
this.emitRunRulesProgressEvent(100);
84+
return {violations: []};
85+
}
86+
8287
const pmdResults: PmdResults = await this.pmdWrapperInvoker.invokeRunCommand(selectedRuleInfoList, filesToScan,
8388
(innerPerc: number) => this.emitRunRulesProgressEvent(10 + 88*(innerPerc/100))); // 10 to 98%
8489

packages/code-analyzer-pmd-engine/test/plugin.test.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ describe('Tests for the PmdCpdEnginesPlugin', () => {
3838

3939
it(`When describeEngineConfig is passed 'pmd' then the correct config description is returned`, async () => {
4040
expect(plugin.describeEngineConfig('pmd')).toEqual(PMD_ENGINE_CONFIG_DESCRIPTION);
41-
42-
// Sanity check that we list the correct available languages:
43-
expect(PMD_ENGINE_CONFIG_DESCRIPTION.fieldDescriptions!['rule_languages'].descriptionText).toEqual(
44-
getMessage('PmdConfigFieldDescription_rule_languages',
45-
`'apex', 'html', 'javascript' (or 'ecmascript'), 'visualforce', 'xml'`));
4641
});
4742

4843
it('When describeEngineConfig is passed an unsupported engine name, then an error is thrown', async () => {
@@ -71,7 +66,7 @@ describe('Tests for the PmdCpdEnginesPlugin', () => {
7166
expect(resolvedConfig.java_command.endsWith('java')).toEqual(true);
7267
expect(resolvedConfig).toEqual({
7368
java_command: resolvedConfig.java_command, // Already checked that it ends with 'java'
74-
rule_languages: ['apex', 'visualforce'],
69+
rule_languages: ['apex', 'html', 'javascript', 'visualforce', 'xml'],
7570
java_classpath_entries: [],
7671
custom_rulesets: []
7772
});

packages/code-analyzer-pmd-engine/test/pmd-engine.test.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ describe('Tests for the getName method of PmdEngine', () => {
3333

3434

3535
describe('Tests for the describeRules method of PmdEngine', () => {
36-
it('When using defaults without workspace, then apex and visualforce rules are returned', async () => {
36+
it('When using defaults without workspace, then all language rules are returned', async () => {
3737
const engine: PmdEngine = new PmdEngine(DEFAULT_PMD_ENGINE_CONFIG);
3838
const logEvents: LogEvent[] = [];
3939
engine.onEvent(EventType.LogEvent, (e: LogEvent) => logEvents.push(e));
4040
const progressEvents: DescribeRulesProgressEvent[] = [];
4141
engine.onEvent(EventType.DescribeRulesProgressEvent, (e: DescribeRulesProgressEvent) => progressEvents.push(e));
4242

4343
const ruleDescriptions: RuleDescription[] = await engine.describeRules({});
44-
await expectRulesToMatchGoldFile(ruleDescriptions, 'rules_apexAndVisualforce.goldfile.json');
44+
await expectRulesToMatchGoldFile(ruleDescriptions, 'rules_allLanguages.goldfile.json');
4545

4646
// Also check that we have fine logs with the argument list and the duration in milliseconds
4747
const fineLogEvents: LogEvent[] = logEvents.filter(e => e.logLevel === LogLevel.Fine);
@@ -66,11 +66,21 @@ describe('Tests for the describeRules method of PmdEngine', () => {
6666
await expectRulesToMatchGoldFile(ruleDescriptions, 'rules_apexOnly.goldfile.json');
6767
});
6868

69-
it('When using defaults with workspace containing only apex and xml code, then only apex rules are returned', async () => {
69+
it('When using defaults with workspace containing only apex and visualforce code, then only apex and visualforce rules are returned', async () => {
7070
const engine: PmdEngine = new PmdEngine(DEFAULT_PMD_ENGINE_CONFIG);
7171
const workspace: Workspace = new Workspace([
7272
path.join(TEST_DATA_FOLDER, 'samplePmdWorkspace', 'dummy.trigger'),
73-
path.join(TEST_DATA_FOLDER, 'samplePmdWorkspace', 'dummy.xml')
73+
path.join(TEST_DATA_FOLDER, 'samplePmdWorkspace', 'dummy.page')
74+
]);
75+
const ruleDescriptions: RuleDescription[] = await engine.describeRules({workspace: workspace});
76+
await expectRulesToMatchGoldFile(ruleDescriptions, 'rules_apexAndVisualforce.goldfile.json');
77+
});
78+
79+
it('When using defaults with workspace containing only apex and text files, then only apex rules are returned', async () => {
80+
const engine: PmdEngine = new PmdEngine(DEFAULT_PMD_ENGINE_CONFIG);
81+
const workspace: Workspace = new Workspace([
82+
path.join(TEST_DATA_FOLDER, 'samplePmdWorkspace', 'dummy.trigger'),
83+
path.join(TEST_DATA_FOLDER, 'samplePmdWorkspace', 'dummy.txt')
7484
]);
7585
const ruleDescriptions: RuleDescription[] = await engine.describeRules({workspace: workspace});
7686
await expectRulesToMatchGoldFile(ruleDescriptions, 'rules_apexOnly.goldfile.json');
@@ -393,7 +403,7 @@ describe('Tests for the runRules method of PmdEngine', () => {
393403
const progressEvents: RunRulesProgressEvent[] = [];
394404
engine.onEvent(EventType.RunRulesProgressEvent, (e: RunRulesProgressEvent) => progressEvents.push(e));
395405

396-
const workspace: Workspace = new Workspace([path.join(TEST_DATA_FOLDER, 'samplePmdWorkspace', 'dummy.xml')]);
406+
const workspace: Workspace = new Workspace([path.join(TEST_DATA_FOLDER, 'samplePmdWorkspace', 'dummy.txt')]);
397407
const ruleNames: string[] = ['OperationWithLimitsInLoop', 'VfUnescapeEl'];
398408
const results: EngineRunResults = await engine.runRules(ruleNames, {workspace: workspace});
399409

packages/code-analyzer-pmd-engine/test/test-data/cpdGoldfiles/rules_allDefaultLanguages.goldfile.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"name": "DetectCopyPasteForHtml",
1717
"severityLevel": 5,
1818
"tags": [
19-
"Recommended",
2019
"Design",
2120
"Html"
2221
],
@@ -68,7 +67,6 @@
6867
"name": "DetectCopyPasteForXml",
6968
"severityLevel": 5,
7069
"tags": [
71-
"Recommended",
7270
"Design",
7371
"Xml"
7472
],

packages/code-analyzer-pmd-engine/test/test-data/cpdGoldfiles/rules_apexAndHtmlOnly.goldfile.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"name": "DetectCopyPasteForHtml",
1717
"severityLevel": 5,
1818
"tags": [
19-
"Recommended",
2019
"Design",
2120
"Html"
2221
],

0 commit comments

Comments
 (0)