require-passing-this
✅ Using recommended in an ESLint configuration enables this rule.
🔧 Some problems reported by this rule are automatically fixable by the --fix ESLint command line option
This rule enforces passing this
in a Construct
.
(This rule applies only to classes that extends Construct
.)
When creating AWS CDK resources, passing this
to the Construct
is crucial for maintaining the correct resource hierarchy.
Using other values like scope
can lead to:
- Incorrect resource hierarchy in the generated CloudFormation template
- Unexpected resource naming
✅ Correct Example
ts
import { Bucket } from "aws-cdk-lib/aws-s3";
import { Construct } from "constructs";
export class MyConstruct extends Construct {
constructor(scope: Construct, id: string) {
super(scope, id);
// ✅ Can use this
new Bucket(this, "SampleBucket");
}
}
❌ Incorrect Example
ts
import { Bucket } from "aws-cdk-lib/aws-s3";
import { Construct } from "constructs";
export class MyConstruct extends Construct {
constructor(scope: Construct, id: string) {
super(scope, id);
// ❌ Shouldn't use scope
new Bucket(scope, "SampleBucket");
}
}