-
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: username property conversion and removal of unnecessary default…
… props * feat: username property conversion * chore: docs * fix: better targetting of import * fix: output node selection and undefined handling * feat: remove unnecessary default=false props
- Loading branch information
Showing
15 changed files
with
482 additions
and
25 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,5 @@ | ||
# Change filepath flag to file flag (`sf-plugin/no-filepath-flags`) | ||
|
||
🔧 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 --> |
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,5 @@ | ||
# Change number flag to integer (`sf-plugin/no-number-flags`) | ||
|
||
🔧 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 --> |
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,7 @@ | ||
# Arrays of messags should use getMessages instead of getMessage followed by EOL splitting (`sf-plugin/no-split-examples`) | ||
|
||
💼 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 --> |
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,7 @@ | ||
# Convert requiresUsername and supportusername to username flags (`sf-plugin/no-username-properties`) | ||
|
||
💼 This rule is enabled in the ✈️ `migration` config. | ||
|
||
🔧 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 --> |
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,122 @@ | ||
/* | ||
* 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, | ||
getSfCommand, | ||
getSfImportFromProgram, | ||
} from '../../shared/commands'; | ||
import { getFlagsStaticPropertyFromCommandClass } from '../../shared/flags'; | ||
|
||
const propertyMap = new Map< | ||
string, | ||
{ flag: string; message: 'requires' | 'supports' | 'requiresHub'; flagName: string } | ||
>([ | ||
[ | ||
'requiresUsername', | ||
{ | ||
flag: 'requiredOrgFlagWithDeprecations', | ||
flagName: 'target-org', | ||
message: 'requires', | ||
}, | ||
], | ||
['supportsUsername', { flag: 'optionalOrgFlagWithDeprecations', flagName: 'target-org', message: 'supports' }], | ||
[ | ||
'requiresDevhubUsername', | ||
{ | ||
flag: 'requiredHubFlagWithDeprecations', | ||
flagName: 'target-dev-hub', | ||
|
||
message: 'requiresHub', | ||
}, | ||
], | ||
]); | ||
|
||
export const noUsernameProperties = ESLintUtils.RuleCreator.withoutDocs({ | ||
meta: { | ||
docs: { | ||
description: 'Convert requiresUsername and supportusername to username flags', | ||
recommended: 'error', | ||
}, | ||
messages: { | ||
requires: | ||
'Class property requiresUsername is not available on SfCommand and should be removed. Import `requiredOrgFlagWithDeprecations` from `@salesforce/sf-plugins-core` and add it to the flags object.', | ||
requiresHub: | ||
'Class property requiresDevhubUsername is not available on SfCommand and should be removed. Import `requiredHubFlagWithDeprecations` from `@salesforce/sf-plugins-core` and add it to the flags object.', | ||
supports: | ||
'Class property supportUsername is not available on SfCommand and should be removed. Import `optionalOrgFlagWithDeprecations` from `@salesforce/sf-plugins-core` and add it to the flags object.', | ||
}, | ||
type: 'problem', | ||
schema: [], | ||
fixable: 'code', | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return isInCommandDirectory(context) | ||
? { | ||
PropertyDefinition(node): void { | ||
if (ancestorsContainsSfCommand(context.getAncestors())) { | ||
if (node.key.type === AST_NODE_TYPES.Identifier && propertyMap.has(node.key.name)) { | ||
const mappedMetadata = propertyMap.get(node.key.name); | ||
|
||
// ensure the import exists | ||
const ancestors = context.getAncestors(); | ||
const source = context.getSourceCode(); | ||
const importDeclaration = getSfImportFromProgram(ancestors[0]); | ||
if (importDeclaration && !source.getText(importDeclaration).includes(mappedMetadata.flag)) { | ||
const fixedImport = source | ||
.getText(importDeclaration) | ||
.replace(/{(.*)}/g, `{$1, ${mappedMetadata.flag}}`); | ||
context.report({ | ||
node: importDeclaration, | ||
messageId: mappedMetadata.message, | ||
fix: (fixer) => { | ||
return fixer.replaceText(importDeclaration, fixedImport); | ||
}, | ||
}); | ||
} | ||
|
||
// add the flag if not already present | ||
|
||
const outerClass = getSfCommand(ancestors); | ||
const flagsProperty = getFlagsStaticPropertyFromCommandClass(outerClass); | ||
|
||
if (flagsProperty && !source.getText(flagsProperty).includes(mappedMetadata.flag)) { | ||
const addedFlag = `'${mappedMetadata.flagName}': ${mappedMetadata.flag},`; | ||
context.report({ | ||
node, | ||
messageId: mappedMetadata.message, | ||
fix: (fixer) => { | ||
return fixer.insertTextAfterRange( | ||
[flagsProperty.value.range[0] + 1, flagsProperty.value.range[0] + 1], | ||
addedFlag | ||
); | ||
}, | ||
}); | ||
} | ||
|
||
if (source.getText(importDeclaration).includes(mappedMetadata.flag)) { | ||
// remove the property only after the other two fixes have been applied | ||
context.report({ | ||
node, | ||
messageId: mappedMetadata.message, | ||
data: { | ||
property: node.key.name, | ||
}, | ||
fix: (fixer) => { | ||
return 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
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 { extendsSfCommand, isInCommandDirectory } from '../shared/commands'; | ||
|
||
const props = ['requiresProject', 'hidden', 'deprecateAliases']; | ||
|
||
export const noUnnecessaryProperties = ESLintUtils.RuleCreator.withoutDocs({ | ||
meta: { | ||
docs: { | ||
description: 'Boolean properties are false by default, so they should not be set to false', | ||
recommended: 'warn', | ||
}, | ||
messages: { | ||
message: 'The {{prop}} property can be omitted since it is false by default', | ||
}, | ||
type: 'problem', | ||
fixable: 'code', | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return isInCommandDirectory(context) | ||
? { | ||
PropertyDefinition(node): void { | ||
if ( | ||
!node.readonly && | ||
node.static && | ||
node.key.type === AST_NODE_TYPES.Identifier && | ||
props.includes(node.key.name) && | ||
node.parent.type === AST_NODE_TYPES.ClassBody && | ||
node.parent.parent.type === AST_NODE_TYPES.ClassDeclaration && | ||
node.value.type === AST_NODE_TYPES.Literal && | ||
node.value.value === false && | ||
extendsSfCommand(node.parent.parent) | ||
) { | ||
context.report({ | ||
node, | ||
messageId: 'message', | ||
data: { prop: node.key.name }, | ||
fix: (fixer) => 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
Oops, something went wrong.