-
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: extract message on command summary/description
- Loading branch information
Showing
5 changed files
with
153 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,45 @@ | ||
/* | ||
* 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 { extendsSfCommand, getClassPropertyIdentifierName, isInCommandDirectory } from '../shared/commands'; | ||
|
||
const propertiesYouShouldntHardCode = ['description', 'summary']; | ||
|
||
export const extractMessageCommand = ESLintUtils.RuleCreator.withoutDocs({ | ||
meta: { | ||
docs: { | ||
description: 'Use loaded messages and separate files for messages', | ||
recommended: 'warn', | ||
}, | ||
messages: { | ||
message: | ||
'Summary/Description property should use messages.getMessage instead of hardcoding the message. See https://github.com/forcedotcom/sfdx-core/blob/v3/MIGRATING_V2-V3.md#messages', | ||
}, | ||
type: 'problem', | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return { | ||
ClassDeclaration(node): void { | ||
// verify it extends SfCommand | ||
if (isInCommandDirectory(context) && extendsSfCommand(node)) { | ||
node.body.body | ||
.filter((prop) => propertiesYouShouldntHardCode.includes(getClassPropertyIdentifierName(prop))) | ||
.forEach((prop) => { | ||
if (prop.type === 'PropertyDefinition' && prop.value.type === 'Literal') { | ||
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,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 { ESLintUtils } from '@typescript-eslint/utils'; | ||
import { extractMessageCommand } from '../../src/rules/extractMessageCommand'; | ||
|
||
const ruleTester = new ESLintUtils.RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
}); | ||
|
||
ruleTester.run('no hardcoded summary/description on command', extractMessageCommand, { | ||
valid: [ | ||
{ | ||
filename: 'src/commands/foo.ts', | ||
code: ` | ||
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> { | ||
public static readonly description = messages.getMessage('description'); | ||
public static readonly summary = messages.getMessage('summary'); | ||
} | ||
`, | ||
}, | ||
// description only | ||
{ | ||
filename: 'src/commands/foo.ts', | ||
code: ` | ||
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> { | ||
public static readonly description = messages.getMessage('description'); | ||
} | ||
`, | ||
}, | ||
// summary only | ||
{ | ||
filename: 'src/commands/foo.ts', | ||
code: ` | ||
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> { | ||
public static readonly summary = messages.getMessage('summary'); | ||
} | ||
`, | ||
}, | ||
// not an sf command | ||
{ | ||
filename: 'src/foo.ts', | ||
code: ` | ||
export default class EnvCreateScratch extends SomethingElse<ScratchCreateResponse> { | ||
public static readonly description = 'foo'; | ||
public static readonly summary = 'bar'; | ||
} | ||
`, | ||
}, | ||
// all sorts of violations but not in the commands directory | ||
{ | ||
filename: 'src/foo.ts', | ||
code: ` | ||
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> { | ||
public static readonly description = 'foo'; | ||
public static readonly summary = 'bar'; | ||
} | ||
`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
filename: 'src/commands/foo.ts', | ||
|
||
errors: [ | ||
{ | ||
messageId: 'message', | ||
}, | ||
], | ||
code: ` | ||
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> { | ||
public static readonly description = 'foo'; | ||
public static readonly summary = messages.getMessage('summary'); | ||
} | ||
`, | ||
}, | ||
{ | ||
errors: [ | ||
{ | ||
messageId: 'message', | ||
}, | ||
{ | ||
messageId: 'message', | ||
}, | ||
], | ||
filename: 'src/commands/foo.ts', | ||
|
||
code: ` | ||
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> { | ||
public static readonly description = 'foo'; | ||
public static readonly summary = 'bar'; | ||
} | ||
`, | ||
}, | ||
], | ||
}); |