-
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: add rule construct props must readonly (#34)
* feat: add rule * feat: add documents * feat: update rule docs
- Loading branch information
1 parent
9475f3c
commit edf7b49
Showing
27 changed files
with
231 additions
and
22 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
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
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
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,26 @@ | ||
--- | ||
title: eslint-plugin-cdk - no-mutable-props-interface | ||
titleTemplate: ":title" | ||
--- | ||
|
||
# no-mutable-props-interface | ||
|
||
このルールは、コンストラクトまたはスタックの、`Props`(インターフェース)のパブリック変数を変更可能にすることを禁止します。 | ||
|
||
Props で変更可能なパブリック変数を指定すると、意図しない副作用を引き起こす可能性があるため推奨されません。 | ||
|
||
#### ✅ 正しい例 | ||
|
||
```ts | ||
interface MyConstructProps { | ||
readonly bucket: IBucket; | ||
} | ||
``` | ||
|
||
#### ❌ 誤った例 | ||
|
||
```ts | ||
interface MyConstructProps { | ||
bucket: IBucket; | ||
} | ||
``` |
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
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
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,26 @@ | ||
--- | ||
title: eslint-plugin-cdk - no-mutable-props-interface | ||
titleTemplate: ":title" | ||
--- | ||
|
||
# no-mutable-props-interface | ||
|
||
This rule disallow making public properties of constructs or stack `props` (interfaces) mutable. | ||
|
||
It is not a good to specify mutable public properties in props, as this can lead to unintended side effects. | ||
|
||
#### ✅ Correct Example | ||
|
||
```ts | ||
interface MyConstructProps { | ||
readonly bucket: IBucket; | ||
} | ||
``` | ||
|
||
#### ❌ Incorrect Example | ||
|
||
```ts | ||
interface MyConstructProps { | ||
bucket: IBucket; | ||
} | ||
``` |
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
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 @@ | ||
import { RuleTester } from "@typescript-eslint/rule-tester"; | ||
|
||
import { noMutablePropsInterface } from "../no-mutable-props-interface.mjs"; | ||
|
||
const ruleTester = new RuleTester({ | ||
languageOptions: { | ||
parserOptions: { | ||
projectService: { | ||
allowDefaultProject: ["*.ts*"], | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
ruleTester.run("no-mutable-props-interface", noMutablePropsInterface, { | ||
valid: [ | ||
// WHEN: All properties are readonly | ||
{ | ||
code: ` | ||
interface TestProps { | ||
readonly name: string; | ||
readonly age: number; | ||
} | ||
`, | ||
}, | ||
// WHEN: Interface name does not end with "Props" | ||
{ | ||
code: ` | ||
interface Test { | ||
name: string; | ||
age: number; | ||
} | ||
`, | ||
}, | ||
// WHEN: Optional properties are readonly | ||
{ | ||
code: ` | ||
interface UserProps { | ||
readonly name: string; | ||
readonly age?: number; | ||
} | ||
`, | ||
}, | ||
], | ||
invalid: [ | ||
// WHEN: readonly is not set | ||
{ | ||
code: ` | ||
interface TestProps { | ||
name: string; | ||
age: number; | ||
} | ||
`, | ||
output: ` | ||
interface TestProps { | ||
readonly name: string; | ||
readonly age: number; | ||
} | ||
`, | ||
errors: [ | ||
{ messageId: "noMutablePropsInterface" }, | ||
{ messageId: "noMutablePropsInterface" }, | ||
], | ||
}, | ||
// WHEN: Some properties do not have readonly | ||
{ | ||
code: ` | ||
interface UserProps { | ||
readonly name: string; | ||
age: number; | ||
} | ||
`, | ||
output: ` | ||
interface UserProps { | ||
readonly name: string; | ||
readonly age: number; | ||
} | ||
`, | ||
errors: [{ messageId: "noMutablePropsInterface" }], | ||
}, | ||
// WHEN: Optional properties do not have readonly | ||
{ | ||
code: ` | ||
interface ConfigProps { | ||
name: string; | ||
age?: number; | ||
} | ||
`, | ||
output: ` | ||
interface ConfigProps { | ||
readonly name: string; | ||
readonly age?: number; | ||
} | ||
`, | ||
errors: [ | ||
{ messageId: "noMutablePropsInterface" }, | ||
{ messageId: "noMutablePropsInterface" }, | ||
], | ||
}, | ||
], | ||
}); |
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,51 @@ | ||
import { ESLintUtils } from "@typescript-eslint/utils"; | ||
|
||
export const noMutablePropsInterface = ESLintUtils.RuleCreator.withoutDocs({ | ||
meta: { | ||
type: "problem", | ||
docs: { | ||
description: "Disallow mutable properties in Props interfaces", | ||
}, | ||
fixable: "code", | ||
messages: { | ||
noMutablePropsInterface: | ||
"Property '{{ propertyName }}' in Props interface should be readonly.", | ||
}, | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
const sourceCode = context.sourceCode; | ||
|
||
return { | ||
TSInterfaceDeclaration(node) { | ||
// NOTE: Interface name check for "Props" | ||
if (!node.id.name.endsWith("Props")) return; | ||
|
||
for (const property of node.body.body) { | ||
if ( | ||
property.type !== "TSPropertySignature" || | ||
property.key.type !== "Identifier" | ||
) { | ||
continue; | ||
} | ||
|
||
// NOTE: Skip if already readonly | ||
if (property.readonly) continue; | ||
|
||
context.report({ | ||
node: property, | ||
messageId: "noMutablePropsInterface", | ||
data: { | ||
propertyName: property.key.name, | ||
}, | ||
fix: (fixer) => { | ||
const propertyText = sourceCode.getText(property); | ||
return fixer.replaceText(property, `readonly ${propertyText}`); | ||
}, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |