Skip to content

feat(warn): warn when an existing property starting with $ is not pro… #8214

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

Merged
merged 4 commits into from
Oct 24, 2018

Conversation

posva
Copy link
Member

@posva posva commented May 21, 2018

…perly accessed

Closes #8213

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:

target
)
}

Copy link
Member

Choose a reason for hiding this comment

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

Should we do the same thing when we come across properties prefixed with _?

Copy link
Member Author

Choose a reason for hiding this comment

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

that's true, I thought there is an eslint warning for it. Let me check again

Copy link
Member Author

Choose a reason for hiding this comment

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

I added that as well but I wonder if it is correct. I'm not sure what babel helpers are we talking about in 8e1478e

Choose a reason for hiding this comment

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

Why not proxying both? I changed all mixins in a big project to respect this rule on Style Guide (https://vuejs.org/v2/style-guide/#Private-property-names-essential) and now when I am trying to pass it to a component prop it gets there undefined because it is not being proxied

Copy link
Member Author

Choose a reason for hiding this comment

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

to prevent conflicts. We cannot check all existing properties (which would be ideal), for conflicts, so we test that instead.
Nowadays, if you want to create plugins that inject custom properties, using Symbols is probably the safest way

Copy link

@mcskeeled mcskeeled May 21, 2018

Choose a reason for hiding this comment

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

Yet the style guide recommends it, I think this issue should be given some thought, either change the rule for private property names or proxy everything, (maybe giving a warning in development process). Not proxying this is removing functionality and forcing developers that follow the recommended style guide find workarounds because of limited functionality to prevent others who may not read the documentation and override vue properties.

if (!has && !isAllowed) {
warnNonPresent(target, key)
if (key in target.$data) warnStartingWithDollar(target, key)
Copy link
Member

Choose a reason for hiding this comment

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

Does it have to be some property starting with preserved prefixes here? It feels a little weird to me that after these checks we concluded that the property starts with _ or $.

Copy link
Member Author

Choose a reason for hiding this comment

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

if the key is in $data it is not proxified, then it can only be starting with those 2 characters. That's what I was thinking

Choose a reason for hiding this comment

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

Proxying properties starting with $_ would be an option

@@ -24,6 +24,16 @@ if (process.env.NODE_ENV !== 'production') {
)
}

const warnStartingWithDollar = (target, key) => {
Copy link
Member

Choose a reason for hiding this comment

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

Something like warnPreservedPrefix?

template: `<div>{{ $data.$a }}</div>`
}).$mount()
expect(`Property or method "$a" is not defined`).not.toHaveBeenWarned()
expect(`Property or method "$a" is not defined`).not.toHaveBeenWarned()

Choose a reason for hiding this comment

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

These two expects are identical, is that accidental?

@yyx990803 yyx990803 merged commit 952ae33 into vuejs:dev Oct 24, 2018
@posva posva deleted the feat/warn-starting-dollar branch October 24, 2018 18:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Vue props throw incomprehensible error when passing variables starting with symbols
5 participants