Skip to content

Commit

Permalink
feat: handle longDescription
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Dec 8, 2022
1 parent d0b39ed commit b2fca2d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
41 changes: 37 additions & 4 deletions src/rules/flagSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import { flagPropertyIsNamed, isFlag } from '../shared/flags';
export const flagSummary = ESLintUtils.RuleCreator.withoutDocs({
meta: {
docs: {
description: 'Enforce that flags have a summary property',
description: 'Enforce that flags have a summary property and that longDescription is renamed to description',
recommended: 'error',
},
messages: {
message: 'Flags should have a summary property',
longDescription: 'use "description" instead of "longDescription"',
},
type: 'problem',
schema: [],
Expand All @@ -26,10 +27,13 @@ export const flagSummary = ESLintUtils.RuleCreator.withoutDocs({
return isInCommandDirectory(context)
? {
Property(node): void {
if (isFlag(node) && ancestorsContainsSfCommand(context.getAncestors())) {
if (
isFlag(node) &&
ancestorsContainsSfCommand(context.getAncestors()) &&
node.value?.type === AST_NODE_TYPES.CallExpression &&
node.value.arguments?.[0]?.type === AST_NODE_TYPES.ObjectExpression
) {
if (
node.value?.type === AST_NODE_TYPES.CallExpression &&
node.value.arguments?.[0]?.type === AST_NODE_TYPES.ObjectExpression &&
!node.value.arguments[0].properties.some(
(property) => property.type === AST_NODE_TYPES.Property && flagPropertyIsNamed(property, 'summary')
)
Expand All @@ -53,6 +57,35 @@ export const flagSummary = ESLintUtils.RuleCreator.withoutDocs({
: {}),
});
}
if (
!node.value.arguments[0].properties.some(
(property) =>
property.type === AST_NODE_TYPES.Property && flagPropertyIsNamed(property, 'description')
)
) {
// if there is no description, but there is a longDescription, turn that into the description
const longDescriptionProp = node.value.arguments[0].properties.find(
(property) =>
property.type === AST_NODE_TYPES.Property && flagPropertyIsNamed(property, 'longDescription')
);
if (!longDescriptionProp) {
return;
}
const range =
longDescriptionProp && 'key' in longDescriptionProp ? longDescriptionProp?.key.range : undefined;
return context.report({
node: longDescriptionProp,
messageId: 'longDescription',
...(range
? {
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
fix: (fixer) => {
return fixer.replaceTextRange(range, 'description');
},
}
: {}),
});
}
}
},
}
Expand Down
30 changes: 30 additions & 0 deletions test/rules/flagSummary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,36 @@ export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
}),
}
}
`,
},
{
name: 'summary, but longDescription should be description',
filename: path.normalize('src/commands/foo.ts'),
errors: [
{
messageId: 'longDescription',
data: { flagName: 'Alias' },
},
],
output: `
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
public static flags = {
alias: Flags.string({
summary: 'foo',
description: 'bar'
}),
}
}
`,
code: `
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
public static flags = {
alias: Flags.string({
summary: 'foo',
longDescription: 'bar'
}),
}
}
`,
},
{
Expand Down

0 comments on commit b2fca2d

Please sign in to comment.