Skip to content

Commit

Permalink
fix(runtime-core): should not warn unused attrs when accessed via set…
Browse files Browse the repository at this point in the history
…up context

close #625
  • Loading branch information
yyx990803 committed Jan 16, 2020
1 parent c35fea3 commit 751d838
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
24 changes: 24 additions & 0 deletions packages/runtime-core/__tests__/rendererAttrsFallthrough.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,4 +326,28 @@ describe('attribute fallthrough', () => {
`<!----><div></div><div class="parent"></div><!---->`
)
})

it('should not warn when context.attrs is used during render', () => {
const Parent = {
render() {
return h(Child, { foo: 1, class: 'parent' })
}
}

const Child = defineComponent({
props: ['foo'],
setup(_props, { attrs }) {
return () => [h('div'), h('div', attrs)]
}
})

const root = document.createElement('div')
document.body.appendChild(root)
render(h(Parent), root)

expect(`Extraneous non-props attributes`).not.toHaveBeenWarned()
expect(root.innerHTML).toBe(
`<!----><div></div><div class="parent"></div><!---->`
)
})
})
12 changes: 10 additions & 2 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ import {
} from '@vue/shared'
import { SuspenseBoundary } from './components/Suspense'
import { CompilerOptions } from '@vue/compiler-core'
import { currentRenderingInstance } from './componentRenderUtils'
import {
currentRenderingInstance,
markAttrsAccessed
} from './componentRenderUtils'

export type Data = { [key: string]: unknown }

Expand Down Expand Up @@ -428,7 +431,12 @@ export const SetupProxySymbol = Symbol()
const SetupProxyHandlers: { [key: string]: ProxyHandler<any> } = {}
;['attrs', 'slots'].forEach((type: string) => {
SetupProxyHandlers[type] = {
get: (instance, key) => instance[type][key],
get: (instance, key) => {
if (__DEV__) {
markAttrsAccessed()
}
return instance[type][key]
},
has: (instance, key) => key === SetupProxySymbol || key in instance[type],
ownKeys: instance => Reflect.ownKeys(instance[type]),
// this is necessary for ownKeys to work properly
Expand Down

0 comments on commit 751d838

Please sign in to comment.