Skip to content

Commit 777f180

Browse files
committed
fix(babel-plugin-rn-stylename-to-style): more strict check for compilers used in the file
1 parent 353563e commit 777f180

2 files changed

Lines changed: 12 additions & 11 deletions

File tree

  • packages
    • babel-plugin-rn-stylename-inline
    • babel-plugin-rn-stylename-to-style

packages/babel-plugin-rn-stylename-inline/index.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ const getVisitor = ({ $program, usedCompilers }) => ({
2828
if (!shouldProcess($this, usedCompilers)) return
2929

3030
// I. validate template
31-
validateTemplate($this, usedCompilers)
31+
validateTemplate($this)
3232

33-
const compiler = usedCompilers[$this.node.tag.name]
33+
const compiler = usedCompilers.get($this.node.tag.name)
3434

3535
// II. compile template
3636
const source = $this.node.quasi.quasis[0]?.value?.raw || ''
@@ -90,13 +90,13 @@ function insertAfterImports ($program, expressionStatement) {
9090
}
9191
}
9292

93-
function shouldProcess ($template, usedCompilers = {}) {
93+
function shouldProcess ($template, usedCompilers) {
9494
if (!$template.get('tag').isIdentifier()) return
95-
if (!usedCompilers[$template.node.tag.name]) return
95+
if (!usedCompilers.has($template.node.tag.name)) return
9696
return true
9797
}
9898

99-
function validateTemplate ($template, usedCompilers = {}) {
99+
function validateTemplate ($template) {
100100
const { node: { quasi } } = $template
101101

102102
if (quasi.expressions.length > 0) {
@@ -107,16 +107,17 @@ function validateTemplate ($template, usedCompilers = {}) {
107107
}
108108

109109
function getUsedCompilers ($program, state) {
110-
const res = {}
110+
const res = new Map()
111111
const magicImports = state.opts.magicImports || DEFAULT_MAGIC_IMPORTS
112112
for (const $import of $program.get('body')) {
113113
if (!$import.isImportDeclaration()) continue
114114
if (!magicImports.includes($import.node.source.value)) continue
115115
for (const $specifier of $import.get('specifiers')) {
116116
if (!$specifier.isImportSpecifier()) continue
117117
const { local, imported } = $specifier.node
118-
if (compilers[imported.name]) {
119-
res[local.name] = compilers[imported.name]
118+
// it's important to use hasOwnProperty here to avoid prototype pollution issues, like 'toString'
119+
if (Object.prototype.hasOwnProperty.call(compilers, imported.name)) {
120+
res.set(local.name, compilers[imported.name])
120121
}
121122
}
122123
}

packages/babel-plugin-rn-stylename-to-style/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ module.exports = function (babel) {
362362
CallExpression ($this, state) {
363363
const $callee = $this.get('callee')
364364
if (!$callee.isIdentifier()) return
365-
if (!usedCompilers?.[$callee.node.name]) return
365+
if (!usedCompilers.has($callee.node.name)) return
366366
// Create a `process` function call
367367
state.hasTransformedClassName = true
368368
const processCall = t.callExpression(
@@ -537,7 +537,7 @@ function findReactFnComponent ($jsxAttribute) {
537537

538538
// Get compilers from the magic import
539539
function getUsedCompilers ($program, state) {
540-
const res = {}
540+
const res = new Map()
541541
const magicImports = state.opts.magicImports || DEFAULT_MAGIC_IMPORTS
542542
for (const $import of $program.get('body')) {
543543
if (!$import.isImportDeclaration()) continue
@@ -546,7 +546,7 @@ function getUsedCompilers ($program, state) {
546546
if (!$specifier.isImportSpecifier()) continue
547547
const { local, imported } = $specifier.node
548548
if (COMPILERS.includes(imported.name)) {
549-
res[local.name] = true
549+
res.set(local.name, true)
550550
$specifier.remove()
551551
}
552552
}

0 commit comments

Comments
 (0)