-
Notifications
You must be signed in to change notification settings - Fork 0
/
no-double-spaces-in-picklist-fields.ts
49 lines (45 loc) · 2.14 KB
/
no-double-spaces-in-picklist-fields.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import * as fs from 'node:fs';
import type { CustomField } from '@salesforce/types/metadata';
import type { Location } from '../common/types.js';
import { RuleClass, SingleRuleResult } from '../common/types.js';
import { parseMetadataXml, getLineAndColNumber } from '../common/util.js';
export default class NoDoubleSpacesInPicklistFields extends RuleClass {
public ruleId: string = 'no-double-spaces-in-picklist-fields';
public shortDescriptionText = "Picklist labels and values should not contain double spaces eg. 'Offer Made'.";
public fullDescriptionText =
"Picklist labels and values should not contain double spaces eg. 'Offer Made'. Remove the double space from the label and/or value.";
public startLine = 1;
public endLine = 1;
public execute(): void {
const customFields = this.files.filter(
(file) => (file.includes('__c') || file.includes('__e')) && file.endsWith('.field-meta.xml')
);
customFields.forEach((file) => {
const fileText = fs.readFileSync(file, 'utf-8');
const customField = parseMetadataXml<CustomField>(fileText, 'CustomField');
if (customField.type && ['Picklist', 'MultiselectPicklist'].includes(customField.type)) {
// Sometimes picklists contain a single value(!) - so check if array and cast if not.
const values = Array.isArray(customField.valueSet?.valueSetDefinition?.value)
? customField.valueSet?.valueSetDefinition?.value
: [customField.valueSet?.valueSetDefinition?.value];
values.forEach((value) => {
checkForDoubleSpace.call(this, file, fileText, value?.label);
checkForDoubleSpace.call(this, file, fileText, value?.fullName);
});
}
});
function checkForDoubleSpace(
this: NoDoubleSpacesInPicklistFields,
file: string,
fileText: string,
value?: string
): void {
if (value?.toString().includes(' ')) {
const location: Location = getLineAndColNumber(this.ruleId, file, fileText, value);
this.results.push(
new SingleRuleResult(file, location.startLine, location.endLine, location.startColumn, location.endColumn)
);
}
}
}
}