Skip to content

Commit

Permalink
feat: autofix Messages.load (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc authored Mar 13, 2023
1 parent 7488aa7 commit 633654c
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ module.exports = {
| [no-h-short-char](docs/rules/no-h-short-char.md) | Do not allow creation of a flag with short char -h | ✈️ ✅ | | | |
| [no-hardcoded-messages-commands](docs/rules/no-hardcoded-messages-commands.md) | Use loaded messages and separate files for messages | | ✈️ ✅ | | |
| [no-hardcoded-messages-flags](docs/rules/no-hardcoded-messages-flags.md) | Use loaded messages and separate files for messages | | ✈️ ✅ | | |
| [no-hyphens-aliases](docs/rules/no-hyphens-aliases.md) | Mark when an alias starts with a hyphen, like -f or --foo | ✈️ ✅ | | 🔧 | |
| [no-id-flags](docs/rules/no-id-flags.md) | Change Id flag to salesforceId | ✈️ | | 🔧 | |
| [no-json-flag](docs/rules/no-json-flag.md) | Do not allow creation of json flag | ✈️ ✅ | | | |
| [no-missing-messages](docs/rules/no-missing-messages.md) | Checks core Messages usage for correct usage of named messages and message tokens | 📚 ✈️ ✅ | | | |
Expand All @@ -88,7 +89,7 @@ module.exports = {
| [no-unnecessary-aliases](docs/rules/no-unnecessary-aliases.md) | Mark when an alias is unnecessary because its only an order permutation, not really a different name | ✈️ ✅ | | 🔧 | |
| [no-unnecessary-properties](docs/rules/no-unnecessary-properties.md) | Boolean properties are false by default, so they should not be set to false | | ✈️ ✅ | 🔧 | |
| [no-username-properties](docs/rules/no-username-properties.md) | Convert requiresUsername and supportusername to username flags | ✈️ | | 🔧 | |
| [read-only-properties](docs/rules/read-only-properties.md) | Class-level static properties, like flags or descriptions, should be marked read-only | | ✈️ ✅ | 🔧 | |
| [read-only-properties](docs/rules/read-only-properties.md) | Class-level static properties, like flags or descriptions, should be marked public and read-only | | ✈️ ✅ | 🔧 | |
| [run-matches-class-type](docs/rules/run-matches-class-type.md) | The return type of the run method should match the Type passed to sfCommand | ✈️ ✅ | | 🔧 | |
| [sfdx-flags-property](docs/rules/sfdx-flags-property.md) | Change flag definitions to SfCommand version | ✈️ | | 🔧 | |
| [should-parse-flags](docs/rules/should-parse-flags.md) | The run method should call this.parse when there are flags | ✈️ | | 🔧 | |
Expand Down
7 changes: 7 additions & 0 deletions docs/rules/no-hyphens-aliases.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Mark when an alias starts with a hyphen, like -f or --foo (`sf-plugin/no-hyphens-aliases`)

💼 This rule is enabled in the following configs: ✈️ `migration`, ✅ `recommended`.

🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).

<!-- end auto-generated rule header -->
2 changes: 1 addition & 1 deletion docs/rules/read-only-properties.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Class-level static properties, like flags or descriptions, should be marked read-only (`sf-plugin/read-only-properties`)
# Class-level static properties, like flags or descriptions, should be marked public and read-only (`sf-plugin/read-only-properties`)

⚠️ This rule _warns_ in the following configs: ✈️ `migration`, ✅ `recommended`.

Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ import { noArgsParseWithoutStrictFalse } from './rules/no-args-parse-without-str
import { noHyphenAliases } from './rules/no-hyphens-aliases';
import { noClassesInCommandReturnType } from './rules/no-classes-in-command-return-type';
import { noExecCmdDoubleQuotes } from './rules/no-execCmd-double-quotes';
import { noMessagesLoad } from './rules/no-messages-load';

const library = {
plugins: ['sf-plugin'],
rules: {
'sf-plugin/no-missing-messages': 'error',
'sf-plugin/no-messages-load': 'error',
'sf-plugin/no-execcmd-double-quotes': 'error',
},
};
Expand Down Expand Up @@ -148,5 +150,6 @@ export = {
'no-hyphens-aliases': noHyphenAliases,
'no-classes-in-command-return-type': noClassesInCommandReturnType,
'no-execcmd-double-quotes': noExecCmdDoubleQuotes,
'no-messages-load': noMessagesLoad,
},
};
47 changes: 47 additions & 0 deletions src/rules/no-messages-load.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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';

export const noMessagesLoad = ESLintUtils.RuleCreator.withoutDocs({
meta: {
docs: {
description: 'Use Messages.loadMessages() instead of Messages.load()',
recommended: 'error',
},
messages: {
message: 'Use Messages.loadMessages() instead of Messages.load()',
},
type: 'problem',
schema: [],
fixable: 'code',
},
defaultOptions: [],
create(context) {
return {
CallExpression(node): void {
if (
node.callee.type === AST_NODE_TYPES.MemberExpression &&
node.callee.object.type === AST_NODE_TYPES.Identifier &&
node.callee.object.name === 'Messages' &&
node.callee.property.type === AST_NODE_TYPES.Identifier &&
node.callee.property.name === 'load' &&
node.arguments[0].type === AST_NODE_TYPES.Literal &&
node.arguments[1].type === AST_NODE_TYPES.Literal
) {
const replacementText = `Messages.loadMessages(${node.arguments[0].raw}, ${node.arguments[1].raw})`;
context.report({
node: node.callee.property,
messageId: 'message',
fix: (fixer) => {
return fixer.replaceText(node, replacementText);
},
});
}
},
};
},
});
33 changes: 33 additions & 0 deletions test/rules/no-messages-load.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 { noMessagesLoad } from '../../src/rules/no-messages-load';

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

ruleTester.run('noMessagesLoad', noMessagesLoad, {
valid: [
{
name: 'messages.loadMessages',
code: "const messages = Messages.loadMessages('foo', 'bar')",
},
],
invalid: [
{
name: 'messages.load',
errors: [
{
messageId: 'message',
},
],
code: "const messages = Messages.load('foo', 'bar', ['a', 'b'])",
output: "const messages = Messages.loadMessages('foo', 'bar')",
},
],
});

0 comments on commit 633654c

Please sign in to comment.