Skip to content

Commit

Permalink
feat: no builtin boolean flags
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Dec 8, 2022
1 parent 9a4dd96 commit f3aadc8
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { shouldParseFlags } from './rules/migration/shouldParseFlags';
import { noThisFlags } from './rules/migration/noThisFlags';
import { getConnectionWithVersion } from './rules/getConnectionsWithVersion';
import { noOclifFlagsCommandImport } from './rules/noOclifFlagsCommandImport';
import { noBuiltinFlags } from './rules/migration/noBuiltinFlags';

const recommended = {
plugins: ['sf-plugin'],
Expand Down Expand Up @@ -61,6 +62,7 @@ export = {
'sf-plugin/should-parse-flags': 'error',
'sf-plugin/no-this-org': 'error',
'sf-plugin/no-this-flags': 'error',
'sf-plugin/no-builtin-flags': 'error',
},
},
},
Expand All @@ -87,5 +89,6 @@ export = {
'no-this-flags': noThisFlags,
'get-connection-with-version': getConnectionWithVersion,
'no-oclif-flags-command-import': noOclifFlagsCommandImport,
'no-builtin-flags': noBuiltinFlags,
},
};
54 changes: 54 additions & 0 deletions src/rules/migration/noBuiltinFlags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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';

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

export const noBuiltinFlags = ESLintUtils.RuleCreator.withoutDocs({
meta: {
docs: {
description: "Handling for sfdxCommand's flags.builtin",
recommended: 'error',
},
messages: {
message: 'Built-in flags are not available on sfCommand. Use a boolean and add your own summary message',
},
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 &&
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;
context.report({
node: node.value.callee.property,
messageId: 'message',
fix: (fixer) => {
return fixer.replaceText(toReplace, 'boolean');
},
});
}
}
},
}
: {};
},
});
69 changes: 69 additions & 0 deletions test/rules/migration/noBuiltInFlags.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 { noBuiltinFlags } from '../../../src/rules/migration/noBuiltinFlags';

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

ruleTester.run('noBuiltinFlags', noBuiltinFlags, {
valid: [
{
name: 'flags without json',
filename: path.normalize('src/commands/foo.ts'),
code: `
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
public static flags = {
verbose: Flags.boolean({
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.builtin({}),
}
}
`,
},
],
invalid: [
{
name: 'builtin flag',
filename: path.normalize('src/commands/foo.ts'),
errors: [{ messageId: 'message' }],
code: `
export default class EnvCreateScratch extends SfCommand<Foo> {
public static flags = {
verbose: Flags.builtin({
summary: 'foo'
}),
}
}
`,
output: `
export default class EnvCreateScratch extends SfCommand<Foo> {
public static flags = {
verbose: Flags.boolean({
summary: 'foo'
}),
}
}
`,
},
],
});

0 comments on commit f3aadc8

Please sign in to comment.