Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Destructuring with function call in default value triggers error #1211

Open
adrianhelvik opened this issue Jun 4, 2020 · 0 comments
Open

Comments

@adrianhelvik
Copy link
Contributor

adrianhelvik commented Jun 4, 2020

Repro

const myFunc = ({
  foo = {
    something: [{ rand: Math.random() }],
  },
  bar: { text = 'Hello world' },
}) => {}

Recognised syntax

const myFunc = ({ /* <hi<jsObjectBraces> trans<jsObjectBraces> lo<Noise> */
  foo /* hi<jsObjectShorthandProp> trans<jsObjectShorthandProp> lo<jsObjectKey> */ = {
    something: [{ rand: Math.random() }],
  },
  bar: { text = 'Hello world' /* hi<jsFlowObject> trans<jsFlowObject> lo<PreProc> */ },
} /* hi<jsParensError> trans<jsParensError> lo<Error> */) => {}

Were the values shown when using this:

nnoremap <leader>s :echo "hi<" . synIDattr(synID(line("."),col("."),1),"name") . '> trans<'
\ . synIDattr(synID(line("."),col("."),0),"name") . "> lo<"
\ . synIDattr(synIDtrans(synID(line("."),col("."),1)),"name") . ">"<CR>

The issue still exists after disabling jsx. I am using Neovim. But looking at the recognised syntax it seems that the issue is that this is interpreted as an object and not destructuring. A workaround could be to allow <ident> = <expression> in objects. The best solution would of course be to correctly identify it as jsDestructuringBlock.

I tried to boil things down further.

This reproduces the issue

// It is triggered by newlines before
const myFunc = ({
  foo /* jsObjectShorthandProp */ }) => {}

// and after
const myFunc = ({ foo /* jsObjectShorthandProp */
}) => {}

// It is also triggered by function calls
const myFunc = ({ foo /* jsObjectShorthandProp */ = Math.random() }) => {}

While this works

const myFunc = ({ foo /* jsDestructuringBlock */ }) => {}

const myFunc = ({ foo /* jsDestructuringBlock */ = [{ foo: bar, 'bar': baz }] }) => {}

Note: Remove the comments to reproduce.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant