Skip to content

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 as the scope when creating new Construct instances within a Construct.

When creating AWS CDK resources, passing this as the scope to child Constructs is crucial for maintaining the correct resource hierarchy.
Passing other values as the scope (especially the scope variable received by the parent's constructor) can lead to:

  • Incorrect resource hierarchy in the generated CloudFormation template
  • Unexpected resource naming

(This rule applies only to classes that extend Construct.)


πŸ”§ How to use ​

js
// eslint.config.mjs
export default defineConfig([
  {
    // ... some configs
    rules: {
      "cdk/require-passing-this": "error",
    },
  },
]);

βœ… 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);

    const sample = new SampleConstruct(this, "Sample");

    // βœ… Using `this` as scope is always allowed.
    new Bucket(this, "SampleBucket");

    // βœ… `sample` (an instance of a Construct) is allowed as scope.
    new OtherConstruct(sample, "Child");
  }
}

❌ 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);

    // ❌ Shouldn't use the parent's `scope` variable,
    new Bucket(scope, "SampleBucket");
  }
}

Options ​

ts
type Options = {
  allowNonThisAndDisallowScope: boolean;
};

const defaultOptions: Options = {
  allowNonThisAndDisallowScope: true,
};

allowNonThisAndDisallowScope ​

Determines whether to allow constructs other than this as the scope (first argument) when instantiating a new Construct.

  • false: Only this is allowed as the scope (first argument) when instantiating a new Construct.
  • true: Allows passing Construct instances other than this as the scope (first argument).
    • However, directly passing the scope variable received by the parent's constructor is still disallowed.
    • This setting is useful for creating nested construct hierarchies.

With: { allowNonThisAndDisallowScope: false }

βœ… 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);

    // βœ… Using `this` as scope is always allowed.
    new Bucket(this, "SampleBucket");
  }
}

❌ 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);

    const sample = new SampleConstruct(this, "Sample");

    // ❌ Shouldn't use the parent's `scope` variable,
    new Bucket(scope, "SampleBucket");

    // ❌ Shouldn't use other Construct instances as scope.
    new OtherConstruct(sample, "Child");
  }
}