-
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: readonly properties for static class (ex: summary)
- Loading branch information
Showing
3 changed files
with
126 additions
and
0 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,50 @@ | ||
/* | ||
* 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 = ['summary', 'description', 'examples', 'flags', 'requiresProject', 'hidden']; | ||
|
||
export const readOnlyProperties = ESLintUtils.RuleCreator.withoutDocs({ | ||
meta: { | ||
docs: { | ||
description: 'Class-level static properties, like flags or descriptions, should be marked read-only', | ||
recommended: 'error', | ||
}, | ||
messages: { | ||
message: 'The {{prop}} property should be read-only', | ||
}, | ||
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 && | ||
extendsSfCommand(node.parent.parent) | ||
) { | ||
context.report({ | ||
node, | ||
messageId: 'message', | ||
data: { prop: node.key.name }, | ||
fix: (fixer) => fixer.insertTextBefore(node.key, 'readonly '), | ||
}); | ||
} | ||
}, | ||
} | ||
: {}; | ||
}, | ||
}); |
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,73 @@ | ||
/* | ||
* 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 { readOnlyProperties } from '../../src/rules/readOnlyProperties'; | ||
|
||
const ruleTester = new ESLintUtils.RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
}); | ||
|
||
ruleTester.run('readOnlyProperties', readOnlyProperties, { | ||
valid: [ | ||
{ | ||
name: 'correct example for a command', | ||
code: ` | ||
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> { | ||
public static readonly summary = 'foo' | ||
public static readonly examples = 'baz' | ||
} | ||
`, | ||
}, | ||
{ | ||
name: 'not an sf command', | ||
code: ` | ||
export default class EnvCreateScratch extends somethingElse<ScratchCreateResponse> { | ||
public static description = 'bar' | ||
} | ||
`, | ||
}, | ||
{ | ||
name: 'not in the commands folder', | ||
filename: path.normalize('foo.ts'), | ||
code: ` | ||
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> { | ||
public static description = 'bar' | ||
public static summary = 'baz' | ||
} | ||
`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
name: 'does not have readonly', | ||
filename: path.normalize('src/commands/foo.ts'), | ||
errors: [ | ||
{ | ||
messageId: 'message', | ||
data: { prop: 'description' }, | ||
}, | ||
{ | ||
messageId: 'message', | ||
data: { prop: 'summary' }, | ||
}, | ||
], | ||
code: ` | ||
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> { | ||
public static description = 'bar' | ||
public static summary = 'baz' | ||
} | ||
`, | ||
output: ` | ||
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> { | ||
public static readonly description = 'bar' | ||
public static readonly summary = 'baz' | ||
} | ||
`, | ||
}, | ||
], | ||
}); |