-
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.
- Loading branch information
Showing
12 changed files
with
463 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"typescript.tsdk": "node_modules/typescript/lib" | ||
"typescript.tsdk": "node_modules/typescript/lib", | ||
"cSpell.words": ["TSES"] | ||
} |
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,46 @@ | ||
/* | ||
* 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 { AST_NODE_TYPES } from '@typescript-eslint/utils'; | ||
import { ancestorsContainsSfCommand, isInCommandDirectory } from '../shared/commands'; | ||
|
||
export const getConnectionWithVersion = ESLintUtils.RuleCreator.withoutDocs({ | ||
meta: { | ||
docs: { | ||
description: 'Calls to getConnection should pass in a version', | ||
recommended: 'warn', | ||
}, | ||
messages: { | ||
addVersion: `getConnection should pass in a version, typically from the api-version flag, | ||
even if that value may be undefined. | ||
Otherwise, the org will default to its maximum version`, | ||
}, | ||
type: 'problem', | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return { | ||
CallExpression(node): void { | ||
if ( | ||
isInCommandDirectory(context) && | ||
node.type === AST_NODE_TYPES.CallExpression && | ||
node.arguments.length === 0 && | ||
node.callee?.type === AST_NODE_TYPES.MemberExpression && | ||
node.callee.property.type === AST_NODE_TYPES.Identifier && | ||
node.callee.property?.name === 'getConnection' && | ||
ancestorsContainsSfCommand(context.getAncestors()) | ||
) { | ||
context.report({ | ||
node: node.callee.property, | ||
messageId: 'addVersion', | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
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 { ESLintUtils } from '@typescript-eslint/utils'; | ||
import { isInCommandDirectory, ancestorsContainsSfCommand } from '../../shared/commands'; | ||
export const noDeprecatedProperties = ESLintUtils.RuleCreator.withoutDocs({ | ||
meta: { | ||
docs: { | ||
description: 'Removes non-existent properties left over from SfdxCommand', | ||
recommended: 'error', | ||
}, | ||
messages: { | ||
property: 'Class property {{property}} is not available on SfCommand and should be removed', | ||
}, | ||
type: 'problem', | ||
schema: [], | ||
fixable: 'code', | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return { | ||
PropertyDefinition(node): void { | ||
if (isInCommandDirectory(context) && ancestorsContainsSfCommand(context.getAncestors())) { | ||
if ( | ||
node.key.type === 'Identifier' && | ||
[ | ||
'requiresUsername', | ||
'supportUsername', | ||
'supportsDevhubUsername', | ||
'requiresDevhubUsername', | ||
'varargs', | ||
].includes(node.key.name) | ||
) { | ||
context.report({ | ||
node, | ||
messageId: 'property', | ||
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,101 @@ | ||
/* | ||
* 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 { AST_NODE_TYPES } from '@typescript-eslint/utils'; | ||
import { ancestorsContainsSfCommand, getRunMethod, getSfCommand, isInCommandDirectory } from '../../shared/commands'; | ||
|
||
export const noThisFlags = ESLintUtils.RuleCreator.withoutDocs({ | ||
meta: { | ||
docs: { | ||
description: 'Fix references to this.org (property on SfdxCommand)', | ||
recommended: 'error', | ||
}, | ||
messages: { | ||
noThisFlags: 'SfCommand does not have a this.flags property. Make sure you parse the flag.', | ||
useFlags: 'change this.flags to flags', | ||
instanceProp: 'create a this.flags property on SfCommand', | ||
setThisFlags: 'flags is defined on the class, but never set. Set it equal to the parsed flags property.', | ||
}, | ||
hasSuggestions: true, | ||
type: 'suggestion', | ||
schema: [], | ||
fixable: 'code', | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return { | ||
MemberExpression(node): void { | ||
if ( | ||
isInCommandDirectory(context) && | ||
node.type === AST_NODE_TYPES.MemberExpression && | ||
node.object?.type === AST_NODE_TYPES.ThisExpression && | ||
node.property?.type === AST_NODE_TYPES.Identifier && | ||
node.property?.name === 'flags' && | ||
ancestorsContainsSfCommand(context.getAncestors()) | ||
) { | ||
// it's ok if there's a this.org on the class... | ||
const classAbove = getSfCommand(context.getAncestors()); | ||
const runMethod = getRunMethod(classAbove); | ||
|
||
if ( | ||
classAbove && | ||
classAbove.body.body.find( | ||
(b) => | ||
b.type === 'PropertyDefinition' && | ||
b.key.type === 'Identifier' && | ||
b.key.name === 'flags' && | ||
b.static === false | ||
) | ||
) { | ||
// ...as long as it's been set in the run method | ||
const flagsParse = | ||
runMethod.type === 'MethodDefinition' | ||
? runMethod.value.body.body.find( | ||
(b) => b.type === 'VariableDeclaration' && context.getSourceCode().getText(b).includes('this.parse') | ||
) | ||
: undefined; | ||
const source = context.getSourceCode().getText(); | ||
if (!source.includes('this.flags = ')) { | ||
context.report({ | ||
node, | ||
messageId: 'instanceProp', | ||
fix: (fixer) => { | ||
return fixer.insertTextAfter(flagsParse, 'this.flags = flags;'); | ||
}, | ||
}); | ||
} | ||
} else { | ||
// we have no this.flags. Make one, or use flags | ||
context.report({ | ||
node, | ||
messageId: 'noThisFlags', | ||
suggest: [ | ||
{ | ||
messageId: 'useFlags', | ||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type | ||
fix: (fixer) => { | ||
return fixer.replaceText(node, 'flags'); | ||
}, | ||
}, | ||
{ | ||
messageId: 'instanceProp', | ||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type | ||
fix: (fixer) => { | ||
return fixer.insertTextBefore( | ||
runMethod, | ||
`private flags: Interfaces.InferredFlags<typeof ${classAbove.id.name}.flags>;` | ||
); | ||
}, | ||
}, | ||
], | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
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,94 @@ | ||
/* | ||
* 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 { AST_NODE_TYPES } from '@typescript-eslint/utils'; | ||
import { ancestorsContainsSfCommand, getRunMethod, getSfCommand, isInCommandDirectory } from '../../shared/commands'; | ||
|
||
export const noThisOrg = ESLintUtils.RuleCreator.withoutDocs({ | ||
meta: { | ||
docs: { | ||
description: 'Fix references to this.org (property on SfdxCommand)', | ||
recommended: 'error', | ||
}, | ||
messages: { | ||
noThisOrg: 'SfCommand does not have a this.org property. Make sure you parse the org flag.', | ||
useFlags: "change this.org to flags['target-org']", | ||
instanceProp: 'create a this.org property on SfCommand', | ||
setThisOrg: 'this org is defined on the class, but never set. Set it equal to the org flag.', | ||
}, | ||
hasSuggestions: true, | ||
type: 'suggestion', | ||
schema: [], | ||
fixable: 'code', | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return { | ||
MemberExpression(node): void { | ||
if ( | ||
isInCommandDirectory(context) && | ||
node.type === AST_NODE_TYPES.MemberExpression && | ||
node.object?.type === AST_NODE_TYPES.ThisExpression && | ||
node.property?.type === AST_NODE_TYPES.Identifier && | ||
node.property?.name === 'org' && | ||
ancestorsContainsSfCommand(context.getAncestors()) | ||
) { | ||
// it's ok if there's a this.org on the class... | ||
const classAbove = getSfCommand(context.getAncestors()); | ||
const runMethod = getRunMethod(classAbove); | ||
|
||
if ( | ||
classAbove && | ||
classAbove.body.body.find( | ||
(b) => b.type === 'PropertyDefinition' && b.key.type === 'Identifier' && b.key.name === 'org' | ||
) | ||
) { | ||
// ...as long as it's been set in the run method | ||
const flagsParse = | ||
runMethod.type === 'MethodDefinition' | ||
? runMethod.value.body.body.find( | ||
(b) => b.type === 'VariableDeclaration' && context.getSourceCode().getText(b).includes('this.parse') | ||
) | ||
: undefined; | ||
const source = context.getSourceCode().getText(); | ||
if (!source.includes('this.org = ')) { | ||
context.report({ | ||
node, | ||
messageId: 'instanceProp', | ||
fix: (fixer) => { | ||
return fixer.insertTextAfter(flagsParse, "this.org = flags['target-org'];"); | ||
}, | ||
}); | ||
} | ||
} else { | ||
// we have no this.flags. Make one, or use the parsed flags | ||
context.report({ | ||
node, | ||
messageId: 'noThisOrg', | ||
suggest: [ | ||
{ | ||
messageId: 'useFlags', | ||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type | ||
fix: (fixer) => { | ||
return fixer.replaceText(node, "flags['target-org']"); | ||
}, | ||
}, | ||
{ | ||
messageId: 'instanceProp', | ||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type | ||
fix: (fixer) => { | ||
return fixer.insertTextBefore(runMethod, 'private org: Org;\r'); | ||
}, | ||
}, | ||
], | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
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.