Skip to content

Warn when component option should be an object, but is not (#5605)#5642

Merged
yyx990803 merged 8 commits intovuejs:devfrom
maciej-ka:warn-option-non-object
May 12, 2017
Merged

Warn when component option should be an object, but is not (#5605)#5642
yyx990803 merged 8 commits intovuejs:devfrom
maciej-ka:warn-option-non-object

Conversation

@maciej-ka
Copy link
Copy Markdown
Contributor

#5605

What kind of change does this PR introduce? (check at least one)

  • Bugfix
  • Feature
  • Code style update
  • Refactor
  • Build-related changes
  • Other, please describe:

Does this PR introduce a breaking change? (check one)

  • Yes
  • No

If yes, please describe the impact and migration path for existing applications:

The PR fulfills these requirements:

If adding a new feature, the PR's description includes:

  • A convincing reason for adding this feature (to avoid wasting your time, it's best to open a suggestion issue first and wait for approval before working on it)

Other information:

Comment thread src/core/instance/state.js Outdated
const computedWatcherOptions = { lazy: true }

function initComputed (vm: Component, computed: Object) {
if (!isPlainObject(computed)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should refactor this block into a function and add the check there too so it is stripped when bundling:

process.env.NODE_ENV !== 'production' && checkOptionType(vm, 'computed')

expect(`method "hello" has an undefined value in the component definition`).toHaveBeenWarned()
})

it('should warn non object', () => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests can also be refactored into a function. It should also test it doesn't print the warning in the right scenario

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should refactored function be a separate file in test/helpers/?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, after all you need to include it in different test files 🙂

Comment thread src/core/instance/state.js Outdated
slot: 1
}

function parseObjectOption (vm: Component, option: Object, name: string) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for option arg because you have name

Comment thread src/core/instance/state.js Outdated
vm
)
}
computed = parseObjectOption(vm, computed, 'computed')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This must be called only in non-production env (NODE_ENV !== 'production')

Comment thread src/core/instance/state.js Outdated

function parseObjectOption (vm: Component, option: Object, name: string) {
if (!isPlainObject(option)) {
process.env.NODE_ENV !== 'production' && warn(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this function is only called in dev mode, this check is unecessary

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it may be good to provide a fallback (empty object) if a wrong type is used in prod mode

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, because it will never reach production code

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check must be removed in production

Comment thread src/core/instance/state.js Outdated
slot: 1
}

function parseObjectOption (vm: Component, option: Object, name: string) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better call it checkOptionType

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the result is used in assignment, to call it a "check" may be misleading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assignment prevents runtime errors but the function is meant to do a check

Comment thread src/core/instance/state.js Outdated
const option = vm.$options[name]
if (!isPlainObject(option)) {
warn(
`${name} should be an object.`,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The message could be a bit more explicit: component option "${name}" should be an object

@yyx990803 yyx990803 merged commit d6bd667 into vuejs:dev May 12, 2017
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

Successfully merging this pull request may close these issues.

3 participants