Skip to content

Commit

Permalink
feat: convert protected props to public
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Feb 23, 2023
1 parent a8bf8ba commit 4c3ba46
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
28 changes: 19 additions & 9 deletions src/rules/read-only-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ const props = ['summary', 'description', 'examples', 'flags', 'requiresProject',
export const readOnlyProperties = ESLintUtils.RuleCreator.withoutDocs({
meta: {
docs: {
description: 'Class-level static properties, like flags or descriptions, should be marked read-only',
description: 'Class-level static properties, like flags or descriptions, should be marked public and read-only',
recommended: 'error',
},
messages: {
message: 'The {{prop}} property should be read-only',
readonly: 'The {{prop}} property should be read-only',
public: 'The {{prop}} property should be public',
},
type: 'problem',
fixable: 'code',
Expand All @@ -28,20 +29,29 @@ export const readOnlyProperties = ESLintUtils.RuleCreator.withoutDocs({
? {
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 '),
});
if (!node.readonly) {
context.report({
node,
messageId: 'readonly',
data: { prop: node.key.name },
fix: (fixer) => fixer.insertTextBefore(node.key, 'readonly '),
});
} else if (node.accessibility !== 'public') {
const replacementText = context.getSourceCode().getText(node).replace(node.accessibility, 'public');
context.report({
node,
messageId: 'public',
data: { prop: node.key.name },
fix: (fixer) => fixer.replaceText(node, replacementText),
});
}
}
},
}
Expand Down
31 changes: 29 additions & 2 deletions test/rules/read-only-properties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
filename: path.normalize('src/commands/foo.ts'),
errors: [
{
messageId: 'message',
messageId: 'readonly',
data: { prop: 'description' },
},
{
messageId: 'message',
messageId: 'readonly',
data: { prop: 'summary' },
},
],
Expand All @@ -67,6 +67,33 @@ export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
public static readonly description = 'bar'
public static readonly summary = 'baz'
}
`,
},
{
name: 'does not have public or readonly',
filename: path.normalize('src/commands/foo.ts'),
errors: [
{
messageId: 'public',
data: { prop: 'description' },
},

{
messageId: 'public',
data: { prop: 'summary' },
},
],
code: `
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
protected static readonly description = 'bar'
protected static readonly summary = 'baz'
}
`,
output: `
export default class EnvCreateScratch extends SfCommand<ScratchCreateResponse> {
public static readonly description = 'bar'
public static readonly summary = 'baz'
}
`,
},
],
Expand Down

0 comments on commit 4c3ba46

Please sign in to comment.