Skip to content

Commit 698da0d

Browse files
committed
polyfill Object.hasOwn() #209
1 parent 52a588b commit 698da0d

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/processScript/transform.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ export function transform(
234234
}
235235

236236
let needGetPrototypeOf = false
237+
let needHasOwn = false
237238

238239
if (program.scope.hasGlobal(`Object`)) {
239240
for (const referencePath of getReferencePathsToGlobal(`Object`, program)) {
@@ -243,6 +244,9 @@ export function transform(
243244
if (referencePath.parent.property.name == `getPrototypeOf`) {
244245
referencePath.parentPath.replaceWith(t.identifier(`_${uniqueId}_GET_PROTOTYPE_OF_`))
245246
needGetPrototypeOf = true
247+
} else if (referencePath.parent.property.name == `hasOwn`) {
248+
referencePath.parentPath.replaceWith(t.identifier(`_${uniqueId}_HAS_OWN_`))
249+
needHasOwn = true
246250
}
247251
}
248252
}
@@ -568,6 +572,31 @@ export function transform(
568572
]))
569573
}
570574

575+
if (needHasOwn) {
576+
mainFunction.body.body.unshift(t.variableDeclaration(`let`, [
577+
t.variableDeclarator(
578+
t.identifier(`_${uniqueId}_HAS_OWN_`),
579+
t.callExpression(
580+
t.memberExpression(
581+
t.memberExpression(
582+
t.identifier(
583+
globalFunctionsUnder7Characters.find(name => !program.scope.hasOwnBinding(name))!
584+
),
585+
t.identifier(`call`)
586+
),
587+
t.identifier(`bind`)
588+
),
589+
[
590+
t.memberExpression(
591+
t.memberExpression(t.identifier(`Object`), t.identifier(`prototype`)),
592+
t.identifier(`hasOwnProperty`)
593+
)
594+
]
595+
)
596+
)
597+
]))
598+
}
599+
571600
if (consoleMethodsReferenced.size) {
572601
mainFunction.body.body.unshift(t.variableDeclaration(
573602
`let`,

0 commit comments

Comments
 (0)