Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add no default and depends on flag configuration rule #440

Merged
merged 7 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { noClassesInCommandReturnType } from './rules/no-classes-in-command-retu
import { noExecCmdDoubleQuotes } from './rules/no-execCmd-double-quotes';
import { noMessagesLoad } from './rules/no-messages-load';
import { esmMessageImport } from './rules/esm-message-import';
import { noDefaultDependsOnFlags } from "./rules/no-default-depends-on-flags";

const library = {
plugins: ['sf-plugin'],
Expand Down Expand Up @@ -84,6 +85,7 @@ const recommended = {
'sf-plugin/no-args-parse-without-strict-false': 'error',
'sf-plugin/no-hyphens-aliases': 'error',
'sf-plugin/no-classes-in-command-return-type': 'error',
'sf-plugin/no-default-and-depends-on-flags': 'error',
},
};

Expand All @@ -107,13 +109,15 @@ export = {
'sf-plugin/no-time-flags': 'error',
'sf-plugin/no-id-flags': 'error',
'sf-plugin/no-username-properties': 'error',
'sf-plugin/no-default-and-depends-on-flags': 'error',
'sf-plugin/encourage-alias-deprecation': 'warn',
},
},
},
rules: {
'esm-message-import': esmMessageImport,
'no-h-short-char': dashH,
'no-default-and-depends-on-flags': noDefaultDependsOnFlags,
'no-duplicate-short-characters': noDuplicateShortCharacters,
'run-matches-class-type': runMatchesClassType,
'flag-case': flagCasing,
Expand Down
57 changes: 57 additions & 0 deletions src/rules/no-default-depends-on-flags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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 } from '@typescript-eslint/utils';
import { RuleCreator } from '@typescript-eslint/utils/eslint-utils';
import { ancestorsContainsSfCommand, isInCommandDirectory } from '../shared/commands';
import { flagPropertyIsNamed, isFlag } from '../shared/flags';

export const noDefaultDependsOnFlags = RuleCreator.withoutDocs({
meta: {
docs: {
description: 'Do not allow creation of a flag with default value and dependsOn',
recommended: 'recommended',
},
messages: {
message: 'Cannot create a flag with a default value and dependsOn',
},
type: 'problem',
schema: [],
},
defaultOptions: [],
create(context) {
return isInCommandDirectory(context)
? {
Property(node): void {
// is a flag
if (
isFlag(node) &&
ancestorsContainsSfCommand(context.getAncestors()) &&
node.value?.type === AST_NODE_TYPES.CallExpression &&
node.value.arguments?.[0]?.type === AST_NODE_TYPES.ObjectExpression
) {
const dependsOn = node.value.arguments[0].properties.find(
(property) =>
property.type === AST_NODE_TYPES.Property &&
flagPropertyIsNamed(property, 'dependsOn')
);
const defaultValue = node.value.arguments[0].properties.find(
(property) =>
property.type === AST_NODE_TYPES.Property &&
flagPropertyIsNamed(property, 'default')
);
if (dependsOn && defaultValue) {
cristiand391 marked this conversation as resolved.
Show resolved Hide resolved
context.report({
node: dependsOn,
messageId: 'message',
});
}
}
},
}
: {};
},
});
52 changes: 52 additions & 0 deletions test/rules/no-default-depends-on-flags.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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 { RuleTester } from '@typescript-eslint/rule-tester';

import { noDefaultDependsOnFlags } from "../../src/rules/no-default-depends-on-flags";

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

ruleTester.run('noDefaultDependsOnFlags', noDefaultDependsOnFlags, {
valid: [
{
name: 'does not use default and dependsOn',
filename: path.normalize('src/commands/foo.ts'),
code: `
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
public static flags = {
alias: Flags.string({
summary: 'foo',
char: 'a'
})
}
}

`,
},
],
invalid: [
{
name: 'uses dependsOn and default',
filename: path.normalize('src/commands/foo.ts'),
errors: [{ messageId: 'message' }],
code: `
export default class EnvCreateScratch extends SfCommand<Foo> {
public static flags = {
json: Flags.boolean({
char: 'h',
default: true,
dependsOn: ['myOtherFlag']
})
}
}
`,
},
],
});
Loading