Skip to content

Commit

Permalink
fix: handle flag that are 'literals' (surrounded by single quotes) in…
Browse files Browse the repository at this point in the history
…stead of a basic identifier
  • Loading branch information
mshanemc committed Jul 26, 2023
1 parent d2c5c68 commit ab5689b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
10 changes: 4 additions & 6 deletions src/rules/extract-message-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { ESLintUtils, AST_NODE_TYPES } from '@typescript-eslint/utils';
import { isFlag } from '../shared/flags';
import { isNodeOfType } from '@typescript-eslint/utils/dist/ast-utils';
import { isFlag, resolveFlagName } from '../shared/flags';
import { ancestorsContainsSfCommand, isInCommandDirectory } from '../shared/commands';

export const extractMessageFlags = ESLintUtils.RuleCreator.withoutDocs({
Expand Down Expand Up @@ -44,11 +45,8 @@ export const extractMessageFlags = ESLintUtils.RuleCreator.withoutDocs({
messageId: 'message',
});
}
const flag = ancestors.find((a) => isFlag(a));
const flagName =
flag?.type === AST_NODE_TYPES.Property &&
flag.key.type === AST_NODE_TYPES.Identifier &&
flag?.key?.name;
const flag = ancestors.filter(isNodeOfType(AST_NODE_TYPES.Property)).find((a) => isFlag(a));
const flagName = flag ? resolveFlagName(flag) : undefined;
if (
flagName &&
node.value.type === AST_NODE_TYPES.CallExpression &&
Expand Down
6 changes: 3 additions & 3 deletions src/rules/flag-casing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
import { ESLintUtils } from '@typescript-eslint/utils';
import { ancestorsContainsSfCommand, isInCommandDirectory } from '../shared/commands';
import { getFlagName, isFlag } from '../shared/flags';
import { isFlag, resolveFlagName } from '../shared/flags';

const toLowerKebabCase = (str: string): string =>
str
Expand All @@ -33,8 +33,8 @@ export const flagCasing = ESLintUtils.RuleCreator.withoutDocs({
? {
Property(node): void {
if (isFlag(node) && ancestorsContainsSfCommand(context.getAncestors())) {
const flagName = getFlagName(node);
if (toLowerKebabCase(flagName) !== flagName) {
const flagName = resolveFlagName(node);
if (flagName && toLowerKebabCase(flagName) !== flagName) {
context.report({
node,
messageId: 'message',
Expand Down
11 changes: 0 additions & 11 deletions src/shared/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@

import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';

export const getFlagName = (node: TSESTree.Property): string => {
switch (node.key.type) {
case 'Identifier':
return node.key.name;
case 'Literal':
return node.key.value as string;
default:
throw new Error(`Unknown flag type ${node.key.type}`);
}
};

/** Current node is 'foo' : Flags.x({}) */
export const isFlag = (node: TSESTree.Node): boolean =>
node.type === AST_NODE_TYPES.Property &&
Expand Down
41 changes: 41 additions & 0 deletions test/rules/extract-message-flags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
}),
}
}
`,
},
{
name: 'flag with a hyphen',
filename: path.normalize('src/commands/foo.ts'),
code: `
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
public static flags = {
'hyphen-flag': Flags.string({
description: messages.getMessage('flags.hyphen-flag.description')
}),
}
}
`,
},
{
Expand Down Expand Up @@ -201,6 +214,34 @@ export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
}),
}
}
`,
},
{
name: 'description flag naming with a hyphen',
filename: path.normalize('src/commands/foo.ts'),
errors: [
{
messageId: 'descriptionFormat',
data: { name: 'hyphen-flag' },
},
],
output: `
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
public static flags = {
'hyphen-flag': Flags.string({
description: messages.getMessage('flags.hyphen-flag.description')
}),
}
}
`,
code: `
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
public static flags = {
'hyphen-flag': Flags.string({
description: messages.getMessage('flags.hyphenFlag.desc')
}),
}
}
`,
},
],
Expand Down

0 comments on commit ab5689b

Please sign in to comment.