-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[helpers TS conversion] object (#16505)
* Migrate `objectWithoutProperties` * Migrate `extends` * Migrate `defaults` * Migrate `objectSpread` * Fix: `objectSpread2` ownKeys behavior * Fix: make `extends` smaller * Fix: `defaults` reduce changes * Apply code review suggestions from @nicolo-ribaudo and @liuxingbaoyu * Updates to `objectSpread2` * Updates to `extends.ts` * Update `objectSpread` * Regenerate helpers * Regenerate runtime --------- Co-authored-by: Nicolò Ribaudo <hello@nicr.dev>
- Loading branch information
1 parent
d3aabe4
commit 740b7ce
Showing
15 changed files
with
186 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* @minVersion 7.0.0-beta.0 */ | ||
|
||
export default function _defaults<T extends object, S extends object>( | ||
obj: T, | ||
defaults: S, | ||
): NonNullable<T & S> { | ||
for ( | ||
var keys: string[] = Object.getOwnPropertyNames(defaults), i = 0; | ||
i < keys.length; | ||
i++ | ||
) { | ||
var key: string = keys[i], | ||
value: PropertyDescriptor | undefined = Object.getOwnPropertyDescriptor( | ||
defaults, | ||
key, | ||
); | ||
if (value && value.configurable && obj[key as keyof T] === undefined) { | ||
Object.defineProperty(obj, key, value); | ||
} | ||
} | ||
return obj as NonNullable<T & S>; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* @minVersion 7.0.0-beta.0 */ | ||
|
||
type Intersection<R extends any[]> = R extends [infer H, ...infer S] | ||
? H & Intersection<S> | ||
: unknown; | ||
|
||
export default function _extends<T extends object, U extends unknown[]>( | ||
target: T, | ||
...sources: U | ||
): T & Intersection<U>; | ||
export default function _extends() { | ||
// @ts-expect-error explicitly assign to function | ||
_extends = Object.assign | ||
? // need a bind because https://github.com/babel/babel/issues/14527 | ||
// @ts-expect-error -- intentionally omitting the argument | ||
Object.assign.bind(/* undefined */) | ||
: function (target: any) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
|
||
return _extends.apply( | ||
null, | ||
arguments as any as [source: object, ...target: any[]], | ||
); | ||
} |
5 changes: 0 additions & 5 deletions
5
packages/babel-helpers/src/helpers/objectDestructuringEmpty.js
This file was deleted.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
packages/babel-helpers/src/helpers/objectDestructuringEmpty.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* @minVersion 7.0.0-beta.0 */ | ||
|
||
export default function _objectDestructuringEmpty<T>( | ||
obj: T | null | undefined, | ||
): asserts obj is T { | ||
if (obj == null) throw new TypeError("Cannot destructure " + obj); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* @minVersion 7.0.0-beta.0 */ | ||
/* @onlyBabel7 */ | ||
|
||
// @ts-expect-error Migrate in another PR | ||
import defineProperty from "./defineProperty.ts"; | ||
|
||
type Intersection<R extends any[]> = R extends [infer H, ...infer S] | ||
? H & Intersection<S> | ||
: unknown; | ||
|
||
export default function _objectSpread<T extends object, U extends unknown[]>( | ||
target: T, | ||
...sources: U | ||
): T & Intersection<U>; | ||
export default function _objectSpread(target: object) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source: object = arguments[i] != null ? Object(arguments[i]) : {}; | ||
var ownKeys: PropertyKey[] = Object.keys(source); | ||
if (typeof Object.getOwnPropertySymbols === "function") { | ||
ownKeys.push.apply( | ||
ownKeys, | ||
Object.getOwnPropertySymbols(source).filter(function (sym) { | ||
// sym already comes from `Object.getOwnPropertySymbols`, so getOwnPropertyDescriptor should always be defined | ||
return Object.getOwnPropertyDescriptor(source, sym)!.enumerable; | ||
}), | ||
); | ||
} | ||
ownKeys.forEach(function (key) { | ||
defineProperty(target, key, source[key as keyof typeof source]); | ||
}); | ||
} | ||
return target; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 0 additions & 22 deletions
22
packages/babel-helpers/src/helpers/objectWithoutProperties.js
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
packages/babel-helpers/src/helpers/objectWithoutProperties.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* @minVersion 7.0.0-beta.0 */ | ||
|
||
import objectWithoutPropertiesLoose from "./objectWithoutPropertiesLoose.ts"; | ||
|
||
export default function _objectWithoutProperties( | ||
source: null | undefined, | ||
excluded: PropertyKey[], | ||
): Record<string, never>; | ||
export default function _objectWithoutProperties< | ||
T extends object, | ||
K extends PropertyKey[], | ||
>( | ||
source: T | null | undefined, | ||
excluded: K, | ||
): Pick<T, Exclude<keyof T, K[number]>>; | ||
export default function _objectWithoutProperties< | ||
T extends object, | ||
K extends PropertyKey[], | ||
>( | ||
source: T | null | undefined, | ||
excluded: K, | ||
): Pick<T, Exclude<keyof T, K[number]>> | Record<string, never> { | ||
if (source == null) return {}; | ||
|
||
var target = objectWithoutPropertiesLoose(source, excluded); | ||
var key, i; | ||
|
||
if (Object.getOwnPropertySymbols) { | ||
var sourceSymbolKeys = Object.getOwnPropertySymbols(source); | ||
for (i = 0; i < sourceSymbolKeys.length; i++) { | ||
key = sourceSymbolKeys[i] as keyof typeof source & keyof typeof target; | ||
if (excluded.indexOf(key) >= 0) continue; | ||
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; | ||
target[key] = source[key]; | ||
} | ||
} | ||
|
||
return target; | ||
} |
16 changes: 0 additions & 16 deletions
16
packages/babel-helpers/src/helpers/objectWithoutPropertiesLoose.js
This file was deleted.
Oops, something went wrong.
30 changes: 30 additions & 0 deletions
30
packages/babel-helpers/src/helpers/objectWithoutPropertiesLoose.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* @minVersion 7.0.0-beta.0 */ | ||
|
||
export default function _objectWithoutPropertiesLoose< | ||
T extends object, | ||
K extends PropertyKey[], | ||
>( | ||
source: T | null | undefined, | ||
excluded: K, | ||
): Pick<T, Exclude<keyof T, K[number]>>; | ||
export default function _objectWithoutPropertiesLoose< | ||
T extends object, | ||
K extends Array<keyof T>, | ||
>(source: T | null | undefined, excluded: K): Omit<T, K[number]>; | ||
export default function _objectWithoutPropertiesLoose<T extends object>( | ||
source: T | null | undefined, | ||
excluded: PropertyKey[], | ||
): Partial<T> { | ||
if (source == null) return {}; | ||
|
||
var target: Partial<T> = {}; | ||
|
||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
if (excluded.indexOf(key) >= 0) continue; | ||
target[key] = source[key]; | ||
} | ||
} | ||
|
||
return target; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters