Skip to content

Commit

Permalink
feat: min/max warns if no default
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Jul 15, 2022
1 parent f7aa198 commit 49d58f6
Show file tree
Hide file tree
Showing 3 changed files with 158 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 @@ -5,6 +5,7 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { noDuplicateShortCharacters } from './rules/noDuplicateShortCharacters';
import { flagMinMaxDefault } from './rules/flagMinMaxDefault';
import { flagSummary } from './rules/flagSummary';
import { flagCasing } from './rules/flagCasing';
import { extractMessageFlags } from './rules/extractMessageFlags';
Expand All @@ -28,6 +29,7 @@ export = {
'sf-plugin/no-hardcoded-messages-commands': 'warn',
'sf-plugin/flag-cross-references': 'error',
'sf-plugin/json-flag': 'error',
'sf-plugin/flag-min-max-default': 'warn',
},
},
},
Expand All @@ -41,5 +43,6 @@ export = {
'command-summary': commandSummary,
'command-example': commandExamples,
'json-flag': jsonFlag,
'flag-min-max-default': flagMinMaxDefault,
},
};
50 changes: 50 additions & 0 deletions src/rules/flagMinMaxDefault.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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 { ESLintUtils } from '@typescript-eslint/utils';
import { ancestorsContainsSfCommand, isInCommandDirectory } from '../shared/commands';
import { flagPropertyIsNamed, isFlag } from '../shared/flags';

export const flagMinMaxDefault = ESLintUtils.RuleCreator.withoutDocs({
meta: {
docs: {
description: 'Enforce that flags with min/max values have a default value',
recommended: 'warn',
},
messages: {
message: 'If your flag has a min or max value, it should have a default value. Otherwise, it will be undefined',
},
type: 'problem',
schema: [],
},
defaultOptions: [],
create(context) {
return {
Property(node): void {
if (isInCommandDirectory(context) && isFlag(node) && ancestorsContainsSfCommand(context.getAncestors())) {
if (
node.value?.type === 'CallExpression' &&
node.value.arguments?.[0]?.type === 'ObjectExpression' &&
// has min/max
node.value.arguments[0].properties.some(
(property) =>
property.type === 'Property' &&
(flagPropertyIsNamed(property, 'min') || flagPropertyIsNamed(property, 'max'))
) &&
!node.value.arguments[0].properties.some(
(property) => property.type === 'Property' && flagPropertyIsNamed(property, 'default')
)
) {
context.report({
node,
messageId: 'message',
});
}
}
},
};
},
});
105 changes: 105 additions & 0 deletions test/rules/flagMinMaxDefault.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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 { flagMinMaxDefault } from '../../src/rules/flagMinMaxDefault';

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

ruleTester.run('flagMinMaxDefault', flagMinMaxDefault, {
valid: [
{
filename: path.normalize('src/commands/foo.ts'),
code: `
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
public static flags = {
alias: Flags.integer({
min: 1,
max: 5,
default: 2
})
}
}
`,
},

{
filename: path.normalize('foo.ts'),
code: `
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
public static flags = {
alias: Flags.integer({
min: 1,
max: 5
}),
}
}
`,
},
],
invalid: [
{
filename: path.normalize('src/commands/foo.ts'),
errors: [
{
messageId: 'message',
data: { flagName: 'Alias' },
},
],
code: `
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
public static flags = {
foo: Flags.integer({
min: 5
}),
}
}
`,
},
{
errors: [
{
messageId: 'message',
},
],
filename: path.normalize('src/commands/foo.ts'),

code: `
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
public static flags = {
foo: Flags.integer({
max: 5
}),
}
}
`,
},
{
errors: [
{
messageId: 'message',
},
],
filename: path.normalize('src/commands/foo.ts'),

code: `
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
public static flags = {
foo: Flags.integer({
min: 3,
max: 5
}),
}
}
`,
},
],
});

0 comments on commit 49d58f6

Please sign in to comment.