Skip to content

Commit

Permalink
fix: add filepath -> file conversion (#117)
Browse files Browse the repository at this point in the history
* fix: add filepath -> file conversion

* feat: add number -> integer conversion
  • Loading branch information
WillieRuemmele authored Dec 26, 2022
1 parent d769360 commit 0de381c
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import { readOnlyProperties } from './rules/read-only-properties';
import { noTimeFlags } from './rules/migration/no-time-flags';
import { idFlagSuggestions } from './rules/id-flag-suggestions';
import { noIdFlags } from './rules/migration/no-id-flags';
import { noFilepathFlags } from './rules/migration/no-filepath-flags';
import { noNumberFlags } from './rules/migration/no-number-flags';

const recommended = {
plugins: ['sf-plugin'],
Expand Down Expand Up @@ -105,5 +107,7 @@ export = {
'no-time-flags': noTimeFlags,
'id-flag-suggestions': idFlagSuggestions,
'no-id-flags': noIdFlags,
'no-filepath-flags': noFilepathFlags,
'no-number-flags': noNumberFlags,
},
};
51 changes: 51 additions & 0 deletions src/rules/migration/no-filepath-flags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* 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 { ancestorsContainsSfCommand, isInCommandDirectory } from '../../shared/commands';
import { isFlag } from '../../shared/flags';

export const noFilepathFlags = ESLintUtils.RuleCreator.withoutDocs({
meta: {
docs: {
description: 'Change filepath flag to file flag',
recommended: 'error',
},
messages: {
message: 'filepath flags are not available on sfCommand. Use file instead',
},
type: 'problem',
schema: [],
fixable: 'code',
},
defaultOptions: [],
create(context) {
return isInCommandDirectory(context)
? {
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;
context.report({
node: node.value.callee.property,
messageId: 'message',
fix: (fixer) => {
return fixer.replaceText(toReplace, 'file');
},
});
}
}
},
}
: {};
},
});
51 changes: 51 additions & 0 deletions src/rules/migration/no-number-flags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* 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 { ancestorsContainsSfCommand, isInCommandDirectory } from '../../shared/commands';
import { isFlag } from '../../shared/flags';

export const noNumberFlags = ESLintUtils.RuleCreator.withoutDocs({
meta: {
docs: {
description: 'Change number flag to integer',
recommended: 'error',
},
messages: {
message: 'number flags are not available on sfCommand. Use integer instead',
},
type: 'problem',
schema: [],
fixable: 'code',
},
defaultOptions: [],
create(context) {
return isInCommandDirectory(context)
? {
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;
context.report({
node: node.value.callee.property,
messageId: 'message',
fix: (fixer) => {
return fixer.replaceText(toReplace, 'integer');
},
});
}
}
},
}
: {};
},
});
69 changes: 69 additions & 0 deletions test/rules/migration/no-filepath-flags.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* 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 path from 'path';
import { ESLintUtils } from '@typescript-eslint/utils';
import { noFilepathFlags } from '../../../src/rules/migration/no-filepath-flags';

const ruleTester = new ESLintUtils.RuleTester({
parser: '@typescript-eslint/parser',
});

ruleTester.run('noFilepathFlags', noFilepathFlags, {
valid: [
{
name: 'filepath flag',
filename: path.normalize('src/commands/foo.ts'),
code: `
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
public static flags = {
verbose: Flags.file({
summary: 'foo'
}),
}
}
`,
},
{
name: 'not in command directory',
filename: path.normalize('foo.ts'),
code: `
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
public static flags = {
verbose: Flags.filepath({}),
}
}
`,
},
],
invalid: [
{
name: 'filepath flag',
filename: path.normalize('src/commands/foo.ts'),
errors: [{ messageId: 'message' }],
code: `
export default class EnvCreateScratch extends SfCommand<Foo> {
public static flags = {
verbose: Flags.filepath({
summary: 'foo'
}),
}
}
`,
output: `
export default class EnvCreateScratch extends SfCommand<Foo> {
public static flags = {
verbose: Flags.file({
summary: 'foo'
}),
}
}
`,
},
],
});
2 changes: 1 addition & 1 deletion test/rules/migration/no-id-flags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const ruleTester = new ESLintUtils.RuleTester({
parser: '@typescript-eslint/parser',
});

ruleTester.run('noBuiltinFlags', noIdFlags, {
ruleTester.run('noIdFlags', noIdFlags, {
valid: [
{
name: 'salesforceId flag',
Expand Down
69 changes: 69 additions & 0 deletions test/rules/migration/no-number-flags.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* 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 path from 'path';
import { ESLintUtils } from '@typescript-eslint/utils';
import { noNumberFlags } from '../../../src/rules/migration/no-number-flags';

const ruleTester = new ESLintUtils.RuleTester({
parser: '@typescript-eslint/parser',
});

ruleTester.run('noNumberFlags', noNumberFlags, {
valid: [
{
name: 'integer flag',
filename: path.normalize('src/commands/foo.ts'),
code: `
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
public static flags = {
verbose: Flags.integer({
summary: 'foo'
}),
}
}
`,
},
{
name: 'not in command directory',
filename: path.normalize('foo.ts'),
code: `
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
public static flags = {
verbose: Flags.integer({}),
}
}
`,
},
],
invalid: [
{
name: 'id flag',
filename: path.normalize('src/commands/foo.ts'),
errors: [{ messageId: 'message' }],
code: `
export default class EnvCreateScratch extends SfCommand<Foo> {
public static flags = {
verbose: Flags.number({
summary: 'foo'
}),
}
}
`,
output: `
export default class EnvCreateScratch extends SfCommand<Foo> {
public static flags = {
verbose: Flags.integer({
summary: 'foo'
}),
}
}
`,
},
],
});

0 comments on commit 0de381c

Please sign in to comment.