fix(compiler-sfc): properly analyze destructured bindings with dynamic keys
fix #4540
This commit is contained in:
		
							parent
							
								
									781d2d4d58
								
							
						
					
					
						commit
						a6e5f82d8e
					
				@ -96,6 +96,19 @@ export default /*#__PURE__*/ Object.assign(__default__, {
 | 
				
			|||||||
})"
 | 
					})"
 | 
				
			||||||
`;
 | 
					`;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					exports[`SFC compile <script setup> binding analysis for destructur 1`] = `
 | 
				
			||||||
 | 
					"export default {
 | 
				
			||||||
 | 
					  setup(__props, { expose }) {
 | 
				
			||||||
 | 
					  expose()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      const { foo, b: bar, ['x' + 'y']: baz, x: { y, zz: { z }}} = {}
 | 
				
			||||||
 | 
					      
 | 
				
			||||||
 | 
					return { foo, bar, baz, y, z }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}"
 | 
				
			||||||
 | 
					`;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
exports[`SFC compile <script setup> defineEmits() 1`] = `
 | 
					exports[`SFC compile <script setup> defineEmits() 1`] = `
 | 
				
			||||||
"export default {
 | 
					"export default {
 | 
				
			||||||
  emits: ['foo', 'bar'],
 | 
					  emits: ['foo', 'bar'],
 | 
				
			||||||
 | 
				
			|||||||
@ -36,6 +36,23 @@ describe('SFC compile <script setup>', () => {
 | 
				
			|||||||
    assertCode(content)
 | 
					    assertCode(content)
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  test('binding analysis for destructur', () => {
 | 
				
			||||||
 | 
					    const { content, bindings } = compile(`
 | 
				
			||||||
 | 
					      <script setup>
 | 
				
			||||||
 | 
					      const { foo, b: bar, ['x' + 'y']: baz, x: { y, zz: { z }}} = {}
 | 
				
			||||||
 | 
					      </script>
 | 
				
			||||||
 | 
					      `)
 | 
				
			||||||
 | 
					    expect(content).toMatch('return { foo, bar, baz, y, z }')
 | 
				
			||||||
 | 
					    expect(bindings).toStrictEqual({
 | 
				
			||||||
 | 
					      foo: BindingTypes.SETUP_MAYBE_REF,
 | 
				
			||||||
 | 
					      bar: BindingTypes.SETUP_MAYBE_REF,
 | 
				
			||||||
 | 
					      baz: BindingTypes.SETUP_MAYBE_REF,
 | 
				
			||||||
 | 
					      y: BindingTypes.SETUP_MAYBE_REF,
 | 
				
			||||||
 | 
					      z: BindingTypes.SETUP_MAYBE_REF
 | 
				
			||||||
 | 
					    })
 | 
				
			||||||
 | 
					    assertCode(content)
 | 
				
			||||||
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  test('defineProps()', () => {
 | 
					  test('defineProps()', () => {
 | 
				
			||||||
    const { content, bindings } = compile(`
 | 
					    const { content, bindings } = compile(`
 | 
				
			||||||
<script setup>
 | 
					<script setup>
 | 
				
			||||||
 | 
				
			|||||||
@ -1347,19 +1347,16 @@ function walkObjectPattern(
 | 
				
			|||||||
) {
 | 
					) {
 | 
				
			||||||
  for (const p of node.properties) {
 | 
					  for (const p of node.properties) {
 | 
				
			||||||
    if (p.type === 'ObjectProperty') {
 | 
					    if (p.type === 'ObjectProperty') {
 | 
				
			||||||
      // key can only be Identifier in ObjectPattern
 | 
					      if (p.key.type === 'Identifier' && p.key === p.value) {
 | 
				
			||||||
      if (p.key.type === 'Identifier') {
 | 
					        // shorthand: const { x } = ...
 | 
				
			||||||
        if (p.key === p.value) {
 | 
					        const type = isDefineCall
 | 
				
			||||||
          // const { x } = ...
 | 
					          ? BindingTypes.SETUP_CONST
 | 
				
			||||||
          const type = isDefineCall
 | 
					          : isConst
 | 
				
			||||||
            ? BindingTypes.SETUP_CONST
 | 
					          ? BindingTypes.SETUP_MAYBE_REF
 | 
				
			||||||
            : isConst
 | 
					          : BindingTypes.SETUP_LET
 | 
				
			||||||
            ? BindingTypes.SETUP_MAYBE_REF
 | 
					        registerBinding(bindings, p.key, type)
 | 
				
			||||||
            : BindingTypes.SETUP_LET
 | 
					      } else {
 | 
				
			||||||
          registerBinding(bindings, p.key, type)
 | 
					        walkPattern(p.value, bindings, isConst, isDefineCall)
 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
          walkPattern(p.value, bindings, isConst, isDefineCall)
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      // ...rest
 | 
					      // ...rest
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user