-
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: remove unnecessary command aliases (#135)
* feat: remove unnecessary command aliases * test: single and multiple unnecessary aliases * feat: remove empty aliases
- Loading branch information
Showing
7 changed files
with
251 additions
and
15 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,63 @@ | ||
/* | ||
* 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, getCommandNameParts } from '../shared/commands'; | ||
|
||
export const noUnnecessaryAliases = ESLintUtils.RuleCreator.withoutDocs({ | ||
meta: { | ||
docs: { | ||
description: | ||
'Mark when an alias is unnecessary because its only an order permutation, not really a different name', | ||
recommended: 'error', | ||
}, | ||
messages: { | ||
summary: 'the Salesforce CLI will match the command words in any order, so this alias is unnecessary', | ||
}, | ||
type: 'problem', | ||
schema: [], | ||
fixable: 'code', | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return isInCommandDirectory(context) | ||
? { | ||
Literal(node): void { | ||
if ( | ||
node.parent.type === AST_NODE_TYPES.ArrayExpression && | ||
node.parent.parent.type === AST_NODE_TYPES.PropertyDefinition && | ||
node.parent.parent.key.type === AST_NODE_TYPES.Identifier && | ||
node.parent.parent.key.name === 'aliases' && | ||
ancestorsContainsSfCommand(context.getAncestors()) | ||
) { | ||
const parentLength = node.parent.elements.length; | ||
const cmdParts = getCommandNameParts(context.getPhysicalFilename()); | ||
const aliasParts = typeof node.value === 'string' ? node.value.split(':') : []; | ||
if ( | ||
aliasParts.every((part) => cmdParts.includes(part)) && | ||
cmdParts.every((part) => aliasParts.includes(part)) | ||
) { | ||
context.report({ | ||
node, | ||
messageId: 'summary', | ||
fix: (fixer) => { | ||
const comma = context.getSourceCode().getTokenAfter(node); | ||
if (parentLength === 1) { | ||
return fixer.remove(node.parent.parent); | ||
} | ||
return comma.value === ',' | ||
? fixer.removeRange([node.range[0], node.range[1] + 1]) | ||
: fixer.remove(node); | ||
}, | ||
}); | ||
} | ||
} | ||
}, | ||
} | ||
: {}; | ||
}, | ||
}); |
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
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,99 @@ | ||
/* | ||
* 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 { noUnnecessaryAliases } from '../../src/rules/no-unnecessary-aliases'; | ||
|
||
const ruleTester = new ESLintUtils.RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
}); | ||
|
||
ruleTester.run('no unnecessary aliases', noUnnecessaryAliases, { | ||
valid: [ | ||
{ | ||
name: 'aliases are not a permutation', | ||
filename: path.normalize('src/commands/foo/bar/baz.ts'), | ||
code: ` | ||
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> { | ||
public static readonly aliases = ['bar:baz', 'bar:qux']; | ||
} | ||
`, | ||
}, | ||
|
||
{ | ||
name: 'not in commands dir', | ||
filename: path.normalize('src/shared/foo/bar/baz.ts'), | ||
code: ` | ||
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> { | ||
public static readonly aliases = ['foo:baz:bar']; | ||
} | ||
`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
name: 'one alias is a permutation, but there is another that is not', | ||
errors: [ | ||
{ | ||
messageId: 'summary', | ||
}, | ||
], | ||
filename: path.normalize('src/commands/foo/bar/baz.ts'), | ||
code: ` | ||
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> { | ||
public static readonly aliases = ['bar:baz:foo', 'bar:qux']; | ||
} | ||
`, | ||
output: ` | ||
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> { | ||
public static readonly aliases = [ 'bar:qux']; | ||
} | ||
`, | ||
}, | ||
{ | ||
name: 'only alias is a permutation', | ||
errors: [ | ||
{ | ||
messageId: 'summary', | ||
}, | ||
], | ||
filename: path.normalize('src/commands/foo/bar/baz.ts'), | ||
code: ` | ||
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> { | ||
public static readonly aliases = ['bar:baz:foo']; | ||
} | ||
`, | ||
output: ` | ||
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> { | ||
} | ||
`, | ||
}, | ||
{ | ||
name: 'multiple aliases are a permutation', | ||
errors: [ | ||
{ | ||
messageId: 'summary', | ||
}, | ||
{ | ||
messageId: 'summary', | ||
}, | ||
], | ||
filename: path.normalize('src/commands/foo/bar/baz.ts'), | ||
code: ` | ||
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> { | ||
public static readonly aliases = ['bar:baz:foo', 'baz:bar:foo']; | ||
} | ||
`, | ||
output: ` | ||
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> { | ||
public static readonly aliases = [ ]; | ||
} | ||
`, | ||
}, | ||
], | ||
}); |
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,15 @@ | ||
/* | ||
* 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 { join } from 'path'; | ||
import { getCommandNameParts } from '../../src/shared/commands'; | ||
|
||
describe('getCommandNameParts', () => { | ||
it('should return the command name parts', () => { | ||
expect(getCommandNameParts(join('src', 'commands', 'foo', 'bar.ts'))).toEqual(['foo', 'bar']); | ||
}); | ||
}); |