Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Apply rules only to classes that extends Construct #60

Merged
merged 7 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion docs/ja/rules/no-construct-stack-suffix.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ titleTemplate: ":title"
を使用した場合、このルールが有効になります。
</div>

このルールは、コンストラクト ID およびスタック ID で `Construct` または `Stack` サフィックスの使用を禁止するものです。
このルールは、コンストラクト ID およびスタック ID で `Construct` または `Stack` サフィックスの使用を禁止するものです。
(このルールは `Construct` または `Stack` を継承したクラスにのみ適用されます)

コンストラクト ID に `Construct` が含まれていると、CDK の世界で止めるべき問題が CloudFormation テンプレートおよび AWS の世界に漏れてしまうため、好ましくありません。(スタック ID についても同様です)

#### ✅ 正しい例

```ts
import { Construct } from "constructs";
import { Bucket } from "aws-cdk-lib/aws-s3";

export class MyConstruct extends Construct {
constructor(scope: Construct, id: string) {
super(scope, id);
Expand All @@ -28,6 +32,9 @@ export class MyConstruct extends Construct {
#### ❌ 不正な例

```ts
import { Construct } from "constructs";
import { Bucket } from "aws-cdk-lib/aws-s3";

export class MyConstruct extends Construct {
constructor(scope: Construct, id: string) {
super(scope, id);
Expand Down
4 changes: 4 additions & 0 deletions docs/ja/rules/no-mutable-props-interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Props で変更可能なパブリック変数を指定すると、意図しな
#### ✅ 正しい例

```ts
import { IBucket } from "aws-cdk-lib/aws-s3";

interface MyConstructProps {
readonly bucket: IBucket;
}
Expand All @@ -33,6 +35,8 @@ interface MyConstructProps {
#### ❌ 誤った例

```ts
import { IBucket } from "aws-cdk-lib/aws-s3";

interface MyConstructProps {
bucket: IBucket;
}
Expand Down
10 changes: 8 additions & 2 deletions docs/ja/rules/no-mutable-public-fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ titleTemplate: ":title"
で自動修正できます。
</div>

このルールは、クラスのパブリック変数を変更可能にすることを禁止するものです
(`readonly`でないパブリック変数の定義を禁止します)
このルールは、クラスのパブリック変数を変更可能にすること(`readonly`でないパブリック変数の定義)を禁止するものです
(このルールは `Construct` または `Stack` を継承したクラスにのみ適用されます)

パブリック変数が変更可能である場合、意図しない副作用が発生する可能性があるため、推奨されません。

#### ✅ 正しい例

```ts
import { Construct } from "constructs";
import { IBucket } from "aws-cdk-lib/aws-s3";

export class MyConstruct extends Construct {
public readonly bucket: IBucket;
}
Expand All @@ -33,6 +36,9 @@ export class MyConstruct extends Construct {
#### ❌ 不正な例

```ts
import { Construct } from "constructs";
import { IBucket } from "aws-cdk-lib/aws-s3";

export class MyConstruct extends Construct {
public bucket: IBucket;
}
Expand Down
6 changes: 6 additions & 0 deletions docs/ja/rules/no-parent-name-construct-id-match.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ titleTemplate: ":title"
#### ✅ 正しい例

```ts
import { Construct } from "constructs";
import { Bucket } from "aws-cdk-lib/aws-s3";

export class MyConstruct extends Construct {
constructor(scope: Construct, id: string) {
super(scope, id);
Expand All @@ -28,6 +31,9 @@ export class MyConstruct extends Construct {
#### ❌ 不正な例

```ts
import { Construct } from "constructs";
import { Bucket } from "aws-cdk-lib/aws-s3";

export class MyConstruct extends Construct {
constructor(scope: Construct, id: string) {
super(scope, id);
Expand Down
7 changes: 5 additions & 2 deletions docs/ja/rules/no-public-class-fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ titleTemplate: ":title"
を使用した場合、このルールが有効になります。
</div>

このルールは、クラスの`public`変数にクラスを使用することを禁止します。
このルールは、クラスの`public`変数にクラスを使用することを禁止します。
(このルールは `Construct` または `Stack` を継承したクラスにのみ適用されます)

`public`変数でクラス型を使用すると、密結合が作成され、可変状態が公開されるため、推奨されません。

#### ✅ 正しい例

```ts
import { IBucket } from "aws-cdk-lib/aws-s3";
import { Construct } from "constructs";
import { IBucket, Bucket } from "aws-cdk-lib/aws-s3";

class MyConstruct extends Construct {
public readonly bucket: IBucket;
Expand All @@ -31,6 +33,7 @@ class MyConstruct extends Construct {
#### ❌ 不正な例

```ts
import { Construct } from "constructs";
import { Bucket } from "aws-cdk-lib/aws-s3";

class MyConstruct extends Construct {
Expand Down
7 changes: 6 additions & 1 deletion docs/ja/rules/pascal-case-construct-id.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@ titleTemplate: ":title"
で自動修正できます。
</div>

このルールは、コンストラクト ID に PascalCase を強制します。
このルールは、コンストラクト ID に PascalCase を強制します。
(このルールは `Construct` または `Stack` を継承したクラスにのみ適用されます)

#### ✅ 正しい例

```ts
import { Bucket } from "aws-cdk-lib/aws-s3";

const bucket = new Bucket(this, "MyBucket");
```

#### ❌ 不正な例

```ts
import { Bucket } from "aws-cdk-lib/aws-s3";

const bucket = new Bucket(this, "myBucket");
```
9 changes: 8 additions & 1 deletion docs/rules/no-construct-stack-suffix.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ titleTemplate: ":title"
in an ESLint configuration enables this rule.
</div>

This rule is to disallow using the `Construct` or `Stack` suffix in construct IDs and stack IDs.
This rule is to disallow using the `Construct` or `Stack` suffix in construct IDs and stack IDs.
(This rule applies only to classes that extends from `Construct` or `Stack`.)

If the Construct ID includes "Construct," the issues that should be stopped in the CDK world will leak into the CloudFormation template and the AWS world, so not good.(the same for Stack ID )

#### ✅ Correct Example

```ts
import { Construct } from "constructs";
import { Bucket } from "aws-cdk-lib/aws-s3";

export class MyConstruct extends Construct {
constructor(scope: Construct, id: string) {
super(scope, id);
Expand All @@ -29,6 +33,9 @@ export class MyConstruct extends Construct {
#### ❌ Incorrect Example

```ts
import { Construct } from "constructs";
import { Bucket } from "aws-cdk-lib/aws-s3";

export class MyConstruct extends Construct {
constructor(scope: Construct, id: string) {
super(scope, id);
Expand Down
4 changes: 4 additions & 0 deletions docs/rules/no-mutable-props-interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ It is not a good to specify mutable public properties in props, as this can lead
#### ✅ Correct Example

```ts
import { IBucket } from "aws-cdk-lib/aws-s3";

interface MyConstructProps {
readonly bucket: IBucket;
}
Expand All @@ -32,6 +34,8 @@ interface MyConstructProps {
#### ❌ Incorrect Example

```ts
import { IBucket } from "aws-cdk-lib/aws-s3";

interface MyConstructProps {
bucket: IBucket;
}
Expand Down
9 changes: 8 additions & 1 deletion docs/rules/no-mutable-public-fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ titleTemplate: ":title"
</a>
</div>

This rule disallow making public variables of a class mutable.
This rule disallow making public variables of a class mutable.
(This rule applies only to classes that extends from `Construct` or `Stack`.)

It's not good to have mutable public variables, because it can lead to unintended side effects.

#### ✅ Correct Example

```ts
import { Construct } from "constructs";
import { IBucket } from "aws-cdk-lib/aws-s3";

export class MyConstruct extends Construct {
public readonly bucket: IBucket;
}
Expand All @@ -32,6 +36,9 @@ export class MyConstruct extends Construct {
#### ❌ Incorrect Example

```ts
import { Construct } from "constructs";
import { IBucket } from "aws-cdk-lib/aws-s3";

export class MyConstruct extends Construct {
public bucket: IBucket;
}
Expand Down
6 changes: 6 additions & 0 deletions docs/rules/no-parent-name-construct-id-match.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ It is not good to specify a string that matches the parent class name for constr
#### ✅ Correct Example

```ts
import { Construct } from "constructs";
import { Bucket } from "aws-cdk-lib/aws-s3";

export class MyConstruct extends Construct {
constructor(scope: Construct, id: string) {
super(scope, id);
Expand All @@ -29,6 +32,9 @@ export class MyConstruct extends Construct {
#### ❌ Incorrect Example

```ts
import { Construct } from "constructs";
import { Bucket } from "aws-cdk-lib/aws-s3";

export class MyConstruct extends Construct {
constructor(scope: Construct, id: string) {
super(scope, id);
Expand Down
7 changes: 5 additions & 2 deletions docs/rules/no-public-class-fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ titleTemplate: ":title"
in an ESLint configuration enables this rule.
</div>

This rule disallows using class types for public class fields.
This rule disallows using class types for public class fields.
(This rule applies only to classes that extends from `Construct` or `Stack`.)

When class types are used in public fields, it creates tight coupling and exposes mutable state, so not good.

#### ✅ Correct Examples

```ts
import { IBucket } from "aws-cdk-lib/aws-s3";
import { Construct } from "constructs";
import { IBucket, Bucket } from "aws-cdk-lib/aws-s3";

class MyConstruct extends Construct {
public readonly bucket: IBucket;
Expand All @@ -32,6 +34,7 @@ class MyConstruct extends Construct {
#### ❌ Incorrect Examples

```ts
import { Construct } from "constructs";
import { Bucket } from "aws-cdk-lib/aws-s3";

class MyConstruct extends Construct {
Expand Down
7 changes: 6 additions & 1 deletion docs/rules/pascal-case-construct-id.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@ titleTemplate: ":title"
</a>
</div>

This rule enforces PascalCase for construct IDs.
This rule enforces PascalCase for construct IDs.
(This rule applies only to classes that extends from `Construct` or `Stack`.)

#### ✅ Correct Example

```ts
import { Bucket } from "aws-cdk-lib/aws-s3";

const bucket = new Bucket(this, "MyBucket");
```

#### ❌ Incorrect Example

```ts
import { Bucket } from "aws-cdk-lib/aws-s3";

const bucket = new Bucket(this, "myBucket");
```
Loading