Repository: rollup/rollup
File: src/ast/nodes/ImportExpression.ts (TypeScript)

Code before:
			}

			// Side-effect only: import('bar').then(() => {})
			if (thenCallback.params.length === 0) {
				return EMPTY_ARRAY;
			}

			const declaration = thenCallback.params[0];
			if (thenCallback.params.length === 1 && declaration instanceof ObjectPattern) {
				return getDeterministicObjectDestructure(declaration);
			}

			return this.hasUnknownAccessedKey ? undefined : [...this.accessedPropKey];
		}
	}

	hasEffects(): boolean {
		return true;
	}

	include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
		if (!this.included) this.includeNode();
		this.source.include(context, includeChildrenRecursively);
		if (this.options && this.shouldBindAttributes) {
			this.options.include(context, includeChildrenRecursively);
			this.options.includePath(UNKNOWN_PATH, context);
		}
	}

	includeNode() {
		this.included = true;
		this.scope.context.includeDynamicImport(this);
		this.scope.addAccessedDynamicImport(this);
	}

	includePath(path: ObjectPath): void {
		if (!this.included) this.includeNode();
		// Technically, this is not correct as dynamic imports return a Promise.
		if (this.hasUnknownAccessedKey) return;
		if (path[0] === UnknownKey) {
			this.hasUnknownAccessedKey = true;
		} else if (typeof path[0] === 'string') {
			this.accessedPropKey.add(path[0]);
		}
		// Update included paths
		this.scope.context.includeDynamicImport(this);
	}

	initialise(): void {
		super.initialise();
		this.scope.context.addDynamicImport(this);

Review comment (performance):
`includePath` only needs to happen on first inclusion. Therefore, this should be done in `includeNode` to improve performance.

Code after:
			// Side-effect only: import('bar').then(() => {})
			if (thenCallback.params.length === 0) {
				return EMPTY_ARRAY;
			}

			const declaration = thenCallback.params[0];
			if (thenCallback.params.length === 1 && declaration instanceof ObjectPattern) {
				return getDeterministicObjectDestructure(declaration);
			}

			return this.hasUnknownAccessedKey ? undefined : [...this.accessedPropKey];
		}
	}

	hasEffects(): boolean {
		return true;
	}

	include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
		if (!this.included) this.includeNode(context);
		this.source.include(context, includeChildrenRecursively);
		if (this.shouldIncludeDynamicAttributes)
			this.options?.include(context, includeChildrenRecursively);
	}

	includeNode(context: InclusionContext) {
		this.included = true;
		if (this.shouldIncludeDynamicAttributes) this.options?.includePath(UNKNOWN_PATH, context);
		this.scope.context.includeDynamicImport(this);
		this.scope.addAccessedDynamicImport(this);
	}

	includePath(path: ObjectPath, context: InclusionContext): void {
		if (!this.included) this.includeNode(context);
		// Technically, this is not correct as dynamic imports return a Promise.
		if (this.hasUnknownAccessedKey) return;
		if (path[0] === UnknownKey) {
			this.hasUnknownAccessedKey = true;
		} else if (typeof path[0] === 'string') {
			this.accessedPropKey.add(path[0]);
		}
		// Update included paths
		this.scope.context.includeDynamicImport(this);
	}

	initialise(): void {
		super.initialise();
		this.scope.context.addDynamicImport(this);
		let parent = this.parent;
		let withinAwaitExpression = false;
		let withinTopLevelAwait = false;