Skip to content

Commit

Permalink
feat: prevent hyphens in aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Feb 23, 2023
1 parent e051772 commit b8d3fe4
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/rules/no-hyphens-aliases.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 { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils';
import { isInCommandDirectory, ancestorsContainsSfCommand } from '../shared/commands';
import { isFlag } from '../shared/flags';

export const noHyphenAliases = ESLintUtils.RuleCreator.withoutDocs({
meta: {
docs: {
description: 'Mark when an alias starts with a hyphen, like -f or --foo',
recommended: 'error',
},
messages: {
summary: 'aliases should not start with hyphens',
},
type: 'problem',
schema: [],
fixable: 'code',
},
defaultOptions: [],
create(context) {
return isInCommandDirectory(context)
? {
Literal(node): void {
if (
typeof node.value === 'string' &&
node.value.startsWith('-') &&
node.parent.type === AST_NODE_TYPES.ArrayExpression &&
node.parent.parent.type === AST_NODE_TYPES.Property &&
node.parent.parent.key.type === AST_NODE_TYPES.Identifier &&
node.parent.parent.key.name === 'aliases' &&
isFlag(node.parent.parent.parent?.parent?.parent) &&
ancestorsContainsSfCommand(context.getAncestors())
) {
context.report({
node,
messageId: 'summary',
fix: (fixer) => {
return fixer.replaceText(node, `'${node.value.replace(/^-+/, '')}'`);
},
});
}
},
}
: {};
},
});
125 changes: 125 additions & 0 deletions test/rules/no-hyphen-aliases.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* 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 { noHyphenAliases } from '../../src/rules/no-hyphens-aliases';

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

ruleTester.run('no hyphen aliases', noHyphenAliases, {
valid: [
{
name: 'aliases without hyphens',
filename: path.normalize('src/commands/foo/bar/baz.ts'),
code: `
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
public static flags = {
alias: Flags.string({
summary: 'foo',
aliases: ['f', 'fooflag']
}),
}
}
`,
},
],
invalid: [
{
name: 'hyphen short char',
errors: [
{
messageId: 'summary',
},
],
filename: path.normalize('src/commands/foo/bar/baz.ts'),
code: `
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
public static flags = {
alias: Flags.string({
summary: 'foo',
aliases: ['-f']
}),
}
}
`,
output: `
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
public static flags = {
alias: Flags.string({
summary: 'foo',
aliases: ['f']
}),
}
}
`,
},
{
name: 'double hyphen word alias',
errors: [
{
messageId: 'summary',
},
],
filename: path.normalize('src/commands/foo/bar/baz.ts'),
code: `
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
public static flags = {
alias: Flags.string({
summary: 'foo',
aliases: ['--fooflag']
}),
}
}
`,
output: `
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
public static flags = {
alias: Flags.string({
summary: 'foo',
aliases: ['fooflag']
}),
}
}
`,
},

{
name: 'both, together',
errors: [
{
messageId: 'summary',
},
{
messageId: 'summary',
},
],
filename: path.normalize('src/commands/foo/bar/baz.ts'),
code: `
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
public static flags = {
alias: Flags.string({
summary: 'foo',
aliases: ['-f', '--fooflag']
}),
}
}
`,
output: `
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
public static flags = {
alias: Flags.string({
summary: 'foo',
aliases: ['f', 'fooflag']
}),
}
}
`,
},
],
});

0 comments on commit b8d3fe4

Please sign in to comment.