Skip to content

fix(helpers): Use hasOwnProperty.call - #39537

Merged
Ron Buckton (rbuckton) merged 3 commits into
masterfrom
fix/helpers/use-hasownproperty-call
Jul 10, 2020
Merged

fix(helpers): Use hasOwnProperty.call#39537
Ron Buckton (rbuckton) merged 3 commits into
masterfrom
fix/helpers/use-hasownproperty-call

Conversation

@rbuckton

@rbuckton Ron Buckton (rbuckton) commented Jul 9, 2020

Copy link
Copy Markdown
Contributor

This replaces #37013 as I was unable to push up changes to that PR. Thanks ExE Boss (@ExE-Boss)!

From ExE Boss (@ExE-Boss):

Currently, the following code:

exports.hasOwnProperty(…) override

// func-util.ts
export const { apply, call } = (({ apply }) => ({
	apply: <T, A extends any[], R>(
		target: (this: T, ...args: A) => R,
		thisArg: T,
		args: A = [] as A
	): R => apply(target, thisArg, args),
	call: <T, A extends any[], R>(
		target: (this: T, ...args: A) => R,
		thisArg: T,
		...args: A
	): R => apply(target, thisArg, args),
}))(Reflect);
// helpers.ts
import { assertArgs, assertObject } from "./types.js";
import { call } from "./func-util.js";

export * from "./func-util.js";
export const {
	hasOwnProperty,
	isPrototypeOf,
} = (({ hasOwnProperty, isPrototypeOf }) => {
	return {
		hasOwnProperty(target: object, property: PropertyKey) {
			assertArgs(arguments.length, 2);
			assertObject(target);
			return call(hasOwnProperty, target, property);
		},
		isPrototypeOf(proto: object, target: any) {
			assertArgs(arguments.length, 2);
			assertObject(proto);
			return call(isPrototypeOf, proto, target);
		},
	};
})(Object.prototype);

results in an error at runtime when transpiled to non‑ESM, because exports.hasOwnProperty(…) is called with only a single non‑object argument.


This also won't work in case exports has its prototype set to null


TSLib PR: microsoft/tslib#92

Replaces #37013
Fixes #37016
Fixes #3197

ExE Boss (ExE-Boss) and others added 3 commits February 25, 2020 14:13
# Conflicts:
#	tests/baselines/reference/tsbuild/sample1/incremental-declaration-changes/when-esModuleInterop-option-changes.js
#	tests/baselines/reference/tsc/declarationEmit/initial-build/when-pkg-references-sibling-package-through-indirect-symlink.js

@weswigham Wesley Wigham (weswigham) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was always confused why some helpers did use the prototype method and some did not.

@rbuckton
Ron Buckton (rbuckton) merged commit 87fd182 into master Jul 10, 2020
@rbuckton

Copy link
Copy Markdown
Contributor Author

The tslib side of this change has been merged as well.

@rbuckton
Ron Buckton (rbuckton) deleted the fix/helpers/use-hasownproperty-call branch July 11, 2020 00:13
Ricky Munroe (rimunroe) added a commit to rimunroe/vitest-commonjs-exports-prototype-issue that referenced this pull request Jan 26, 2023
Ricky Munroe (rimunroe) added a commit to rimunroe/vitest-commonjs-exports-prototype-issue that referenced this pull request Jan 27, 2023
@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid calling hasOwnProperty in helpers Re-export issue with hasOwnProperty.

4 participants