Skip to content

Commit

Permalink
refactor: deduplicate getting calleeProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Jan 23, 2023
1 parent b19f057 commit 5abaaeb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 45 deletions.
22 changes: 10 additions & 12 deletions src/rules/migration/no-builtin-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils';
import { ancestorsContainsSfCommand, isInCommandDirectory } from '../../shared/commands';
import { isFlag } from '../../shared/flags';
import { getCalleePropertyByName, isFlag } from '../../shared/flags';

const builtInFlagTypes = ['verbose', 'concise', 'quiet'];

Expand All @@ -28,18 +28,16 @@ export const noBuiltinFlags = ESLintUtils.RuleCreator.withoutDocs({
return isInCommandDirectory(context)
? {
Property(node): void {
if (isFlag(node) && ancestorsContainsSfCommand(context.getAncestors())) {
if (
node.key.type === AST_NODE_TYPES.Identifier &&
builtInFlagTypes.includes(node.key.name) &&
node.value?.type === AST_NODE_TYPES.CallExpression &&
node.value.callee?.type === AST_NODE_TYPES.MemberExpression &&
node.value.callee.property?.type === AST_NODE_TYPES.Identifier &&
node.value.callee.property.name === 'builtin'
) {
const toReplace = node.value.callee.property;
if (
isFlag(node) &&
node.key.type === AST_NODE_TYPES.Identifier &&
builtInFlagTypes.includes(node.key.name) &&
ancestorsContainsSfCommand(context.getAncestors())
) {
const toReplace = getCalleePropertyByName(node, 'builtin');
if (toReplace) {
context.report({
node: node.value.callee.property,
node: toReplace,
messageId: 'message',
fix: (fixer) => {
return fixer.replaceText(toReplace, 'boolean');
Expand Down
16 changes: 5 additions & 11 deletions src/rules/migration/no-filepath-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils';
import { ESLintUtils } from '@typescript-eslint/utils';
import { ancestorsContainsSfCommand, isInCommandDirectory } from '../../shared/commands';
import { isFlag } from '../../shared/flags';
import { getCalleePropertyByName, isFlag } from '../../shared/flags';

export const noFilepathFlags = ESLintUtils.RuleCreator.withoutDocs({
meta: {
Expand All @@ -27,16 +27,10 @@ export const noFilepathFlags = ESLintUtils.RuleCreator.withoutDocs({
? {
Property(node): void {
if (isFlag(node) && ancestorsContainsSfCommand(context.getAncestors())) {
if (
(node.key.type === AST_NODE_TYPES.Identifier || node.key.type === AST_NODE_TYPES.Literal) &&
node.value?.type === AST_NODE_TYPES.CallExpression &&
node.value.callee?.type === AST_NODE_TYPES.MemberExpression &&
node.value.callee.property?.type === AST_NODE_TYPES.Identifier &&
node.value.callee.property.name === 'filepath'
) {
const toReplace = node.value.callee.property;
const toReplace = getCalleePropertyByName(node, 'filepath');
if (toReplace) {
context.report({
node: node.value.callee.property,
node: toReplace,
messageId: 'message',
fix: (fixer) => {
return fixer.replaceText(toReplace, 'file');
Expand Down
16 changes: 5 additions & 11 deletions src/rules/migration/no-id-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils';
import { ESLintUtils } from '@typescript-eslint/utils';
import { ancestorsContainsSfCommand, isInCommandDirectory } from '../../shared/commands';
import { isFlag } from '../../shared/flags';
import { getCalleePropertyByName, isFlag } from '../../shared/flags';

export const noIdFlags = ESLintUtils.RuleCreator.withoutDocs({
meta: {
Expand All @@ -27,16 +27,10 @@ export const noIdFlags = ESLintUtils.RuleCreator.withoutDocs({
? {
Property(node): void {
if (isFlag(node) && ancestorsContainsSfCommand(context.getAncestors())) {
if (
(node.key.type === AST_NODE_TYPES.Identifier || node.key.type === AST_NODE_TYPES.Literal) &&
node.value?.type === AST_NODE_TYPES.CallExpression &&
node.value.callee?.type === AST_NODE_TYPES.MemberExpression &&
node.value.callee.property?.type === AST_NODE_TYPES.Identifier &&
node.value.callee.property.name === 'id'
) {
const toReplace = node.value.callee.property;
const toReplace = getCalleePropertyByName(node, 'id');
if (toReplace) {
context.report({
node: node.value.callee.property,
node: toReplace,
messageId: 'message',
fix: (fixer) => {
return fixer.replaceText(toReplace, 'salesforceId');
Expand Down
16 changes: 5 additions & 11 deletions src/rules/migration/no-number-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils';
import { ESLintUtils } from '@typescript-eslint/utils';
import { ancestorsContainsSfCommand, isInCommandDirectory } from '../../shared/commands';
import { isFlag } from '../../shared/flags';
import { getCalleePropertyByName, isFlag } from '../../shared/flags';

export const noNumberFlags = ESLintUtils.RuleCreator.withoutDocs({
meta: {
Expand All @@ -27,16 +27,10 @@ export const noNumberFlags = ESLintUtils.RuleCreator.withoutDocs({
? {
Property(node): void {
if (isFlag(node) && ancestorsContainsSfCommand(context.getAncestors())) {
if (
(node.key.type === AST_NODE_TYPES.Identifier || node.key.type === AST_NODE_TYPES.Literal) &&
node.value?.type === AST_NODE_TYPES.CallExpression &&
node.value.callee?.type === AST_NODE_TYPES.MemberExpression &&
node.value.callee.property?.type === AST_NODE_TYPES.Identifier &&
node.value.callee.property.name === 'number'
) {
const toReplace = node.value.callee.property;
const toReplace = getCalleePropertyByName(node, 'number');
if (toReplace) {
context.report({
node: node.value.callee.property,
node: toReplace,
messageId: 'message',
fix: (fixer) => {
return fixer.replaceText(toReplace, 'integer');
Expand Down
12 changes: 12 additions & 0 deletions src/shared/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,15 @@ export const getFlagsStaticPropertyFromCommandClass = (
return classDeclaration.body.body.find(isFlagsStaticProperty);
}
};

export const getCalleePropertyByName = (
node: TSESTree.Property,
calleePropName: string
): TSESTree.Identifier | undefined =>
(node.key.type === AST_NODE_TYPES.Identifier || node.key.type === AST_NODE_TYPES.Literal) &&
node.value?.type === AST_NODE_TYPES.CallExpression &&
node.value.callee?.type === AST_NODE_TYPES.MemberExpression &&
node.value.callee.property?.type === AST_NODE_TYPES.Identifier &&
node.value.callee.property.name === calleePropName
? node.value.callee.property
: undefined;

0 comments on commit 5abaaeb

Please sign in to comment.