Skip to content

Commit

Permalink
refactor: updates prop module updater with a more predictable fn sign…
Browse files Browse the repository at this point in the history
…ature; adds test case to check if module props were unchanged
  • Loading branch information
geotrev committed Jan 25, 2022
1 parent 677727e commit dcc61a7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
12 changes: 12 additions & 0 deletions src/__tests__/transform.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,16 @@ describe("transform", () => {
const vnode = transform(<div className="test" />)
expect(vnode.data.className).toBe(undefined)
})

it("ignores module props", () => {
const vnode = transform(
<button tab-index={0} on={{ click: noop }} hook={{ insert: noop }}>
Test
</button>
)
expect(vnode.data.on.click).toEqual(noop)
expect(vnode.data.hook.insert).toEqual(noop)
expect(vnode.data.props.tabIndex).toEqual(0)
expect(vnode.data["tab-index"]).toBe(undefined)
})
})
50 changes: 32 additions & 18 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import {
HYPHEN_CHAR,
} from "./constants"

function setPropToModule(vnode, deletions, module, key, value, dataKey) {
function setPropToModule(vnode, deletions, module, propKey, moduleKey, value) {
if (vnode.data[module]) {
vnode.data[module][key] = value
vnode.data[module][moduleKey] = value
} else {
vnode.data[module] = { [key]: value }
vnode.data[module] = { [moduleKey]: value }
}

deletions.push(dataKey || key)
deletions.push(propKey)
}

/**
Expand All @@ -26,20 +26,27 @@ function setPropToModule(vnode, deletions, module, key, value, dataKey) {
*/
export function transform(vnode) {
if (vnode.data) {
const propKeys = Object.keys(vnode.data)
const moduleKeys = [...Object.values(MODULE_PROPS), KEY]
// remove all keys that match a module key
const propKeys = Object.keys(vnode.data).filter(
(key) => moduleKeys.indexOf(key) === -1
)
const deletions = []

for (let i = 0; i < propKeys.length; i++) {
const propKey = propKeys[i]
const propValue = vnode.data[propKey]

// Don't scan snabbdom modules
if (moduleKeys.indexOf(propKey) > -1) continue

const pkey = PROP_PROPS[propKey]
if (pkey) {
setPropToModule(vnode, deletions, MODULE_PROPS.props, pkey, propValue)
setPropToModule(
vnode,
deletions,
MODULE_PROPS.props,
propKey,
pkey,
propValue
)
continue
}

Expand All @@ -56,18 +63,18 @@ export function transform(vnode) {
vnode,
deletions,
DATASET,
propKey,
kebabToCamel(postfix),
propValue,
propKey
propValue
)
} else {
setPropToModule(
vnode,
deletions,
modKey,
propKey,
postfix,
vnode.data[propKey],
propKey
vnode.data[propKey]
)
}

Expand All @@ -79,9 +86,9 @@ export function transform(vnode) {
vnode,
deletions,
MODULE_PROPS.attrs,
propKey,
propKey.slice(hyphenIdx + 1),
propValue,
propKey
propValue
)
continue
}
Expand All @@ -91,16 +98,23 @@ export function transform(vnode) {
vnode,
deletions,
MODULE_PROPS.props,
propKey,
propKey.slice(hyphenIdx + 1),
propValue,
propKey
propValue
)
continue
}
}

// As a fallback, we'll move everything else into `attrs`.
setPropToModule(vnode, deletions, MODULE_PROPS.attrs, propKey, propValue)
setPropToModule(
vnode,
deletions,
MODULE_PROPS.attrs,
propKey,
propKey,
propValue
)
}

forEach(deletions, (key) => delete vnode.data[key])
Expand Down

0 comments on commit dcc61a7

Please sign in to comment.