-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: prevent use of h as short char
- Loading branch information
Showing
5 changed files
with
116 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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, AST_NODE_TYPES } from '@typescript-eslint/utils'; | ||
import { ancestorsContainsSfCommand, isInCommandDirectory } from '../shared/commands'; | ||
import { flagPropertyIsNamed, isFlag } from '../shared/flags'; | ||
|
||
export const dashH = ESLintUtils.RuleCreator.withoutDocs({ | ||
meta: { | ||
docs: { | ||
description: 'do not allow creation of a flag with short char -h', | ||
recommended: 'error', | ||
}, | ||
messages: { | ||
message: '-h is reserved for help. Choose a different short character', | ||
}, | ||
type: 'problem', | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return { | ||
Property(node): void { | ||
// is a flag | ||
if (isInCommandDirectory(context) && isFlag(node) && ancestorsContainsSfCommand(context.getAncestors())) { | ||
if ( | ||
node.value?.type === 'CallExpression' && | ||
node.value.arguments?.[0]?.type === 'ObjectExpression' && | ||
node.value.arguments[0].properties.find( | ||
(property) => | ||
property.type === 'Property' && | ||
flagPropertyIsNamed(property, 'char') && | ||
property.value.type === AST_NODE_TYPES.Literal && | ||
property.value.value === 'h' | ||
) | ||
) { | ||
context.report({ | ||
node, | ||
messageId: 'message', | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* 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 { dashH } from '../../src/rules/dash-h'; | ||
|
||
const ruleTester = new ESLintUtils.RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
}); | ||
|
||
ruleTester.run('dashH', dashH, { | ||
valid: [ | ||
{ | ||
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' | ||
}), | ||
} | ||
} | ||
`, | ||
}, | ||
{ | ||
filename: path.normalize('foo.ts'), | ||
code: ` | ||
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> { | ||
public static flags = { | ||
json: Flags.boolean({ | ||
char: 'h' | ||
}), | ||
} | ||
} | ||
`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
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' | ||
}), | ||
} | ||
} | ||
`, | ||
}, | ||
], | ||
}); |