---
url: /rules/require-passing-this.md
---

# require-passing-this

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

> **Note:** This rule only applies inside classes that extend `Construct` or `Stack`. It does not flag cases where `this` is not available, such as functions outside Construct classes.

***

#### 🔧 How to use

::: code-group

```ts [Oxlint]
// oxlint.config.ts
export default defineConfig({
  // ... some configs
  rules: {
    "awscdk/require-passing-this": "error",
  },
});
```

```js [ESLint]
// eslint.config.mjs
export default defineConfig({
  // ... some configs
  rules: {
    "awscdk/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");
  }
}

// ✅ Functions outside Construct classes are not flagged.
function createBucket(scope: Construct, id: string) {
  new Bucket(scope, id);
}
```

#### ❌ 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");
  }
}
```
