Skip to content

Commit

Permalink
fix: add rule for salesforceId len both
Browse files Browse the repository at this point in the history
@W-12346910@
Add rule to support new id length option ‘both’
  • Loading branch information
peternhale committed Jan 10, 2023
1 parent d5da517 commit 61bdebf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/rules/id-flag-suggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const idFlagSuggestions = ESLintUtils.RuleCreator.withoutDocs({
message: 'Suggestion: salesforceId flags have additional properties to validate the Id. Consider using them.',
lengthSuggestion15: 'require the ID to be 15 characters',
lengthSuggestion18: 'require the ID to be 18 characters',
lengthSuggestionBoth: 'require the ID to be 15 or 18 characters',
typeSuggestion: 'require the ID to start with a 3-character prefix',
},
type: 'suggestion',
Expand Down Expand Up @@ -51,6 +52,7 @@ export const idFlagSuggestions = ESLintUtils.RuleCreator.withoutDocs({
const fixedStartsWith = existing.replace('salesforceId({', "salesforceId({startsWith: '000',");
const fixer15 = existing.replace('salesforceId({', 'salesforceId({length: 15,');
const fixer18 = existing.replace('salesforceId({', 'salesforceId({length: 18,');
const fixerBoth = existing.replace('salesforceId({', "salesforceId({length: 'both',");

context.report({
node: node.key,
Expand All @@ -69,6 +71,12 @@ export const idFlagSuggestions = ESLintUtils.RuleCreator.withoutDocs({
).concat(
!hasLength
? [
{
messageId: 'lengthSuggestionBoth',
fix: (fixer: RuleFixer): RuleFix => {
return fixer.replaceText(node, fixerBoth);
},
},
{
messageId: 'lengthSuggestion15',
fix: (fixer: RuleFixer): RuleFix => {
Expand Down
23 changes: 23 additions & 0 deletions test/rules/id-flag-suggestions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ export default class EnvCreateScratch extends SfCommand<Foo> {
}),
}
}
`,
},
{
messageId: 'lengthSuggestionBoth',
output: `
export default class EnvCreateScratch extends SfCommand<Foo> {
public static flags = {
id: Flags.salesforceId({length: 'both',
}),
}
}
`,
},
{
Expand Down Expand Up @@ -122,6 +133,18 @@ export default class EnvCreateScratch extends SfCommand<Foo> {
{
messageId: 'message',
suggestions: [
{
messageId: 'lengthSuggestionBoth',
output: `
export default class EnvCreateScratch extends SfCommand<Foo> {
public static flags = {
foo: Flags.salesforceId({length: 'both',
startsWith: '000',
}),
}
}
`,
},
{
messageId: 'lengthSuggestion15',
output: `
Expand Down

0 comments on commit 61bdebf

Please sign in to comment.