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

Undefined valueDeclaration in props is crashing vls #2367

Closed
3 tasks done
javiertury opened this issue Oct 9, 2020 · 2 comments
Closed
3 tasks done

Undefined valueDeclaration in props is crashing vls #2367

javiertury opened this issue Oct 9, 2020 · 2 comments
Labels

Comments

@javiertury
Copy link
Contributor

javiertury commented Oct 9, 2020

  • I have searched through existing issues
  • I have read through docs
  • I have read FAQ

Info

  • Platform: Linux
  • Vetur version: vls 0.5.4
  • VS Code version: Coc-vetur 1.2.0

Problem

Vetur is continuously crashing because s.valueDeclaration can be undefined here:

const status = tsModule.isPropertyAssignment(s.valueDeclaration)
? getPropValidatorInfo(s.valueDeclaration.initializer)
: { hasObjectValidator: false, required: true };

<project-path>/node_modules/typescript/lib/typescript.js:26110
        return node.kind === 285 /* PropertyAssignment */;
                    ^

TypeError: Cannot read property 'kind' of undefined
    at Object.isPropertyAssignment (<project-path>/node_modules/typescript/lib/typescript.js:26110:21)
    at .../coc-vetur/node_modules/vls/dist/modes/script/componentInfo.js:246:41
    at Array.map (<anonymous>)
    at getObjectProps (.../coc-vetur/node_modules/vls/dist/modes/script/componentInfo.js:245:59)
    at getClassAndObjectInfo (.../coc-vetur/node_modules/vls/dist/modes/script/componentInfo.js:463:35)
    at getProps (.../coc-vetur/node_modules/vls/dist/modes/script/componentInfo.js:65:60)
    at analyzeDefaultExportExpr (.../coc-vetur/node_modules/vls/dist/modes/script/componentInfo.js:39:19)
    at .../coc-vetur/node_modules/vls/dist/modes/script/componentInfo.js:29:45
    at Array.forEach (<anonymous>)
    at Object.getComponentInfo (.../coc-vetur/node_modules/vls/dist/modes/script/componentInfo.js:24:33)

I propose to use instead

                const status = s.valueDeclaration !== undefined && tsModule.isPropertyAssignment(s.valueDeclaration)
                    ? getPropValidatorInfo(s.valueDeclaration.initializer)
                    : { hasObjectValidator: false, required: true };

Reproducible Case

Debugging the issue the problem is the line value: T | undefined (obtained from s.declarations[0].getText()). This line is part of the following fragment.

type Base = Record<string, unknown>;

export type Props <T extends Base = Base> = {
  value: T | undefined
};

Later this type is used to build properties:

// Helper
export type PropsOptions <Props> = {
  [K in keyof Props]: PropOptions<Props[K]>
};

// Properties options
const props: PropOptions<Props<Resource>> = {
  value: {
     type: Object as () => Resource | undefined,
     default: undefined
  }
};

// Component
export default defineComponent({
  props,
  setup(props, { root: { $router } }) {
     ...
  }
});

And also used in parents to create an object of properties that will be binded to the component.

// In parent component, script
const resourceProps: Props<Resource> = {
  value: resource
};

// In parent component, template
v-bind="resourceProps"

EDIT: fix code typo

javiertury added a commit to javiertury/vetur that referenced this issue Oct 9, 2020
javiertury added a commit to javiertury/vetur that referenced this issue Oct 9, 2020
@javiertury javiertury changed the title valueDeclaration can be undefined Undefined valueDeclaration in props is crashing vls Oct 9, 2020
@yoyo930021
Copy link
Member

I don't think it's legal to write in vue.js.

The right way:

import { PropType } from 'vue'

// Properties options
const props = {
  value: {
     type: Function as PropType<() => Reource | undefined>,
     required: false
  }
};

// Component
export default defineComponent({
  props,
  setup(props, { root: { $router } }) {
     ...
  }
});

But I agree we need to deal with this crash.

@yoyo930021 yoyo930021 added the bug label Oct 10, 2020
javiertury added a commit to javiertury/vetur that referenced this issue Oct 10, 2020
@javiertury
Copy link
Contributor Author

Before vetur gained support to check properties in templates, the scheme above was the only way to reliably check property types in both parent and child components.

But now I agree, the scheme I used above should not be used.

@octref octref closed this as completed in 3c9a3b3 Oct 29, 2020
octref added a commit that referenced this issue Oct 29, 2020
Fix undefined valueDeclaration in props crashing vls (#2367)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants