-
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
4 changed files
with
127 additions
and
1 deletion.
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
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,60 @@ | ||
/* | ||
* 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'; | ||
|
||
export const noSplitExamples = ESLintUtils.RuleCreator.withoutDocs({ | ||
meta: { | ||
docs: { | ||
description: 'Arrays of messags should use getMessages instead of getMessage followed by EOL splitting', | ||
recommended: 'error', | ||
}, | ||
messages: { | ||
message: 'use getMessages', | ||
}, | ||
type: 'problem', | ||
fixable: 'code', | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return isInCommandDirectory(context) | ||
? { | ||
PropertyDefinition(node): void { | ||
if ( | ||
node.static && | ||
node.key.type === AST_NODE_TYPES.Identifier && | ||
node.key.name === 'examples' && | ||
node.parent.type === AST_NODE_TYPES.ClassBody && | ||
node.parent.parent.type === AST_NODE_TYPES.ClassDeclaration && | ||
node.value.type === AST_NODE_TYPES.CallExpression && | ||
node.value.callee.type === AST_NODE_TYPES.MemberExpression && | ||
node.value.callee.object.type === AST_NODE_TYPES.CallExpression && | ||
node.value.callee.object.callee.type === AST_NODE_TYPES.MemberExpression && | ||
node.value.callee.object.callee.property.type === AST_NODE_TYPES.Identifier && | ||
node.value.callee.object.callee.property.name === 'getMessage' && | ||
node.value.callee.property.type === AST_NODE_TYPES.Identifier && | ||
node.value.callee.property.name === 'split' && | ||
extendsSfCommand(node.parent.parent) | ||
) { | ||
const target = node.value; | ||
const fixedText = context | ||
.getSourceCode() | ||
.getText(node.value) | ||
.replace('getMessage', 'getMessages') | ||
.replace(/\.split\(.*\)/, ''); | ||
context.report({ | ||
node: node.value.callee.property, | ||
messageId: 'message', | ||
fix: (fixer) => fixer.replaceText(target, fixedText), | ||
}); | ||
} | ||
}, | ||
} | ||
: {}; | ||
}, | ||
}); |
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,64 @@ | ||
/* | ||
* 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 { noSplitExamples } from '../../src/rules/no-split-examples'; | ||
|
||
const ruleTester = new ESLintUtils.RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
}); | ||
|
||
ruleTester.run('noSplitExamples', noSplitExamples, { | ||
valid: [ | ||
{ | ||
name: 'correct examples with getMessages', | ||
code: ` | ||
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> { | ||
public static readonly examples = messages.getMessages('examples') | ||
} | ||
`, | ||
}, | ||
{ | ||
name: 'not an sf command', | ||
code: ` | ||
export default class EnvCreateScratch extends somethingElse<ScratchCreateResponse> { | ||
public static readonly examples = message.getMessage('examples').split(EOL) | ||
} | ||
`, | ||
}, | ||
{ | ||
name: 'not in the commands folder', | ||
filename: path.normalize('foo.ts'), | ||
code: ` | ||
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> { | ||
public static readonly examples = message.getMessage('examples').split(EOL) | ||
} | ||
`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
name: 'getMessages with split', | ||
filename: path.normalize('src/commands/foo.ts'), | ||
errors: [ | ||
{ | ||
messageId: 'message', | ||
}, | ||
], | ||
code: ` | ||
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> { | ||
public static readonly examples = message.getMessage('examples').split(EOL); | ||
} | ||
`, | ||
output: ` | ||
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> { | ||
public static readonly examples = message.getMessages('examples'); | ||
} | ||
`, | ||
}, | ||
], | ||
}); |